Skip to content

Geo #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Geo #57

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ venv.bak/

# mypy
.mypy_cache/

.idea/
67 changes: 67 additions & 0 deletions address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from typing import Dict, Any
from geopy.geocoders import Nominatim, Photon
from shapely import Point
import pickle

UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
ISRAEL = 'ישראל'
DATA_FILENAME = 'data/hoods_haifa.pkl'


class Address:
def __init__(self, building_num: int, street: str, city: str, country: str = ISRAEL):
self.building_num = building_num
self.street = street
self.city = city
self.country = country
self.geometry: Point = None
self.area_id: int = None
self.area_name: str = None
self.area_data = None

def _get_address_point(self) -> Point:
"""
takes an Israeli address and queries the free Nominatim geocoding API to get it's location.
spelling should be precise.
"""
coder = Photon(user_agent="abcd")

addr = self.street + ' ' + str(self.building_num)
full_addr = addr + ', ' + self.city + ', ' + self.country

res = coder.geocode(full_addr)
if res:
p = Point(res.longitude, res.latitude)
self.geometry = p
return p

else:
raise ValueError('Address geocoding failed')

def _point_to_area_id(self) -> int:
with open(DATA_FILENAME, 'rb') as f:
gdf = pickle.load(f)

area = gdf[gdf.contains(self.geometry)]
if not area.empty:
self.area_data = {k: list(v.values())[0] for k, v in area.to_dict().items()}
self.area_id = self.area_data['ID']
self.area_name = self.area_data['SchName']

## if no addr found
else:
self.area_id = -1

return self.area_id


def get_area_id(self) -> int:
self._get_address_point()
return self._point_to_area_id()


# if __name__ == "__main__":
# addr = Address(60, 'אינטרנציונל', 'חיפה')
# addr.get_area_id()
# print(addr.area_id)
# print(addr.area_name)
4 changes: 4 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from flask import Flask
from flask_cors import CORS

app = Flask(__name__)
CORS(app)


@app.route('/')
def hello_world():
Expand Down
Binary file added data/geodata_pickle
Binary file not shown.
1 change: 1 addition & 0 deletions data/hoods_haifa.geojson

Large diffs are not rendered by default.

Binary file added data/hoods_haifa.pkl
Binary file not shown.
Binary file modified requirements.txt
Binary file not shown.