writing encode fn
This commit is contained in:
parent
f0da44baf1
commit
e8cd8795f1
2 changed files with 39 additions and 2 deletions
38
engine.py
38
engine.py
|
|
@ -1,6 +1,11 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import overpy
|
import overpy
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
import geopy.distance as distance
|
||||||
|
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
# brandname : overpass query filters
|
# brandname : overpass query filters
|
||||||
BRANDS: dict[str, str] = {
|
BRANDS: dict[str, str] = {
|
||||||
|
|
@ -35,6 +40,35 @@ def fetch_data(brand: str) -> list[tuple[float | None, float | None]]:
|
||||||
def encode(location: tuple[float, float]) -> EncodedLocation:
|
def encode(location: tuple[float, float]) -> EncodedLocation:
|
||||||
"""Encode a location."""
|
"""Encode a location."""
|
||||||
|
|
||||||
|
print("fetching")
|
||||||
|
greggs = fetch_data("greggs")
|
||||||
|
print("fetched")
|
||||||
|
#greggs = [(55.85,-4.02),(52.443,-1.833),(51.28,-1.0)]
|
||||||
|
|
||||||
|
#relative distance matrix
|
||||||
|
dist_matrix = np.zeros((len(greggs),len(greggs)))
|
||||||
|
for i in tqdm(range(len(greggs))):
|
||||||
|
first=greggs[i]
|
||||||
|
for j in range(len(greggs)):
|
||||||
|
second=greggs[j]
|
||||||
|
#calculate the distance between i and j coordinates
|
||||||
|
#dist_matrix[i,j] = np.sqrt((first[0]-second[0])**2 + (first[1]-second[1])**2 )
|
||||||
|
dist_matrix[i,j] = distance.distance(distance.lonlat(*first), distance.lonlat(*second)).km*1000
|
||||||
|
print(dist_matrix)
|
||||||
|
|
||||||
|
#find closest greggs
|
||||||
|
distances = pd.Series(np.zeros(len(greggs)))
|
||||||
|
for i in range(len(greggs)):
|
||||||
|
current = greggs[i]
|
||||||
|
#distances[i] = np.sqrt((current[0]-location[0])**2 + (current[1]-location[1])**2
|
||||||
|
distances[i] = distance.distance(distance.lonlat(*current), distance.lonlat(*location)).km*1000
|
||||||
|
|
||||||
|
print(distances)
|
||||||
|
distances = distances.sort_values()
|
||||||
|
top3 = distances.head(3)
|
||||||
|
print(top3)
|
||||||
|
|
||||||
|
|
||||||
# Stub
|
# Stub
|
||||||
return [
|
return [
|
||||||
(5., [1., 2., 3.]),
|
(5., [1., 2., 3.]),
|
||||||
|
|
@ -67,8 +101,8 @@ def parse_location(location: str) -> EncodedLocation:
|
||||||
def main():
|
def main():
|
||||||
"""Testing."""
|
"""Testing."""
|
||||||
print("Running query...")
|
print("Running query...")
|
||||||
greggs = fetch_data("greggs")
|
#greggs = fetch_data("greggs")
|
||||||
print(f"Query done - got {len(greggs)} Greggs!")
|
#print(f"Query done - got {len(greggs)} Greggs!")
|
||||||
|
|
||||||
print(format_location(encode((0.091659, 52.210796))))
|
print(format_location(encode((0.091659, 52.210796))))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,5 @@
|
||||||
overpy
|
overpy
|
||||||
django
|
django
|
||||||
|
numpy
|
||||||
|
pandas
|
||||||
|
geopy
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue