Skip to content

Commit

Permalink
Merge pull request #508 from Krishna-Baldwa/stable-maps
Browse files Browse the repository at this point in the history
Stable maps
  • Loading branch information
PalMit2002 authored Oct 31, 2023
2 parents ea2b18e + 516845a commit 96d92d2
Show file tree
Hide file tree
Showing 10 changed files with 1,740 additions and 1,253 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
google_client_secret.json
new_locations.json
locations.json
chatbot_logs.json
.vscode
Expand Down
1 change: 0 additions & 1 deletion helpers/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def query_from_num(request, default_num, queryset):

return queryset[from_i: from_i + num]


def query_search( # pylint: disable=too-many-arguments
request, min_length, queryset, fields, collection, order_relevance=False
):
Expand Down
1,503 changes: 1,503 additions & 0 deletions locations/management/commands/adj_list.py

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions locations/management/commands/adj_updater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import os
import math as m


class UpdateAdjList:
def __init__(self):
self.adj_list = self.load_adj_list()
self.adj_list_path = f"{os.getcwd()}/locations/management/commands/adj_list.py"

def load_adj_list(self):
adj_list_path = f"{os.getcwd()}/locations/management/commands/adj_list.py"
adj_list = {}

with open(adj_list_path, "r") as f:
adj_list = dict(eval(f.read()))

return adj_list

@staticmethod
def get_location_name(location):
name = location.name

if "Node" in name:
return int(name.replace("Node", ""))
return name

@staticmethod
def calculate_distance(loc1, loc2):
x_loc1 = loc1.pixel_x if loc1.pixel_x else 0
y_loc1 = loc1.pixel_y if loc1.pixel_y else 0
x_loc2 = loc2.pixel_x if loc2.pixel_x else 0
y_loc2 = loc2.pixel_y if loc2.pixel_y else 0

return m.sqrt(0.001 * ((x_loc1 - x_loc2) ** 2 + (y_loc1 - y_loc2) ** 2))

"""
This function updates the adj_list with the new connections and distances betweem them.
"""

def add_conns(self, loc1, connections=[]):
new_data = self.adj_list.copy()
for loc2 in connections:
if loc2:
distance = UpdateAdjList.calculate_distance(loc1, loc2)
loc1_name = UpdateAdjList.get_location_name(loc1)
loc2_name = UpdateAdjList.get_location_name(loc2)
try:
new_data[loc1_name]
except KeyError:
new_data[loc1_name] = {}
try:
new_data[loc2_name]
except KeyError:
new_data[loc2_name] = {}
new_data[loc1_name][loc2_name] = distance
new_data[loc2_name][loc1_name] = distance

with open(self.adj_list_path, "w") as f:
f.write(str(new_data))

def delete_all_connections(self, location):
new_data = self.adj_list.copy()
loc_name = UpdateAdjList.get_location_name(location)

if loc_name in new_data:
new_data.pop(loc_name)
for key in new_data:
if loc_name in new_data[key]:
new_data[key].pop(loc_name)

with open(self.adj_list_path, "w") as f:
f.write(str(new_data))

def delete_connections(self, location, connections):
new_data = self.adj_list.copy()
loc1_name = self.get_location_name(location)

for loc2 in connections:
loc2_name = self.get_location_name(loc2)

if loc2_name in new_data:
if loc1_name in new_data[loc2_name]:
new_data[loc2_name].pop(loc1_name)
if loc1_name in new_data:
if loc2_name in new_data[loc1_name]:
new_data[loc1_name].pop(loc2_name)

with open(self.adj_list_path, "w") as f:
f.write(str(new_data))
Loading

0 comments on commit 96d92d2

Please sign in to comment.