Skip to content

Commit

Permalink
Migrate to use of Arnhem python package on PyPi (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaasnicolaas authored Jul 1, 2023
1 parent aefed50 commit f88305b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ These are the cities currently supported:
| Germany | Hamburg | 812 (says 813) | |
| Netherlands | Amersfoort | 149 | every monday at 03:00 | `0 3 * * 1` |
| Netherlands | Amsterdam | 1328 | every second day of the month at 03:00 | `0 3 2 * *` |
| Netherlands | Arnhem | 121 | |
| Netherlands | Arnhem | 88 | |
| Netherlands | Den Haag | 234 (says 241) | every second day of the month at 02:30 | `30 2 2 * *` |
| Netherlands | Eindhoven | 180 | every second day of the month at 03:00 | `0 3 2 * *` |
| Netherlands | Groningen | 173 | |
Expand Down
70 changes: 33 additions & 37 deletions app/cities/netherlands/arnhem.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
"""Manage the location data of Arnhem."""
import datetime
import json
import os
from pathlib import Path

import pymysql
import pytz
import requests
from dotenv import load_dotenv
from arnhem import ODPArnhem

from app.cities import City
from app.database import connection, cursor

load_dotenv()
env_path = Path(".") / ".env"
load_dotenv(dotenv_path=env_path)
from app.helper import centroid


class Municipality(City):
Expand All @@ -29,43 +22,47 @@ def __init__(self) -> None:
province_id=6,
geo_code="NL-GE",
)
self.limit = 200
self.cbs_code = "0202"
self.local_file = "app/data/parking-arnhem.json"

def download(self) -> None:
"""Download the data as JSON file."""
# Create a variable and pass the url of file to be downloaded
remote_url = (
f'{os.getenv("ARCGIS_SOURCE")}/6f301547133a4acda9074ec3ca9b075b_0.geojson'
)
# Make http request for remote file data
data = requests.get(remote_url, timeout=10)
# Save file data to local copy
with Path(self.local_file).open("wb") as file:
file.write(data.content)
print(f"{self.name} - KLAAR met downloaden")
async def async_get_locations(self) -> list:
"""Get parking data from API.
Returns
-------
List of objects from all parking lots.
"""
async with ODPArnhem() as client:
locations = await client.locations(
limit=self.limit,
set_filter="RVV_SOORT='E6a'",
)
print(f"{self.name} - data has been retrieved")
return locations

def upload_json(self) -> None:
"""Upload the data from the JSON file to the database."""
with Path(self.local_file).open(encoding="UTF-8") as arnhem_data:
arnhem_obj = json.load(arnhem_data)
def upload_data(self, data_set: list) -> None:
"""Upload the data_set to the database.
Args:
----
data_set: The data set to upload.
"""
count: int = 0
try:
for item in arnhem_obj["features"]:
for item in data_set:
count += 1
location = item["properties"]
# Get the coordinates of the parking lot with centroid
latitude, longitude = centroid(item.coordinates)
# Generate unique id
location_id = f"{self.geo_code}-{self.cbs_code}-{location['OBJECTID']}"
location_id = f"{self.geo_code}-{self.cbs_code}-{item.spot_id}"

sql = """INSERT INTO `parking_cities` (id, country_id, province_id, municipality, street, orientation, number, longitude, latitude, visibility, created_at, updated_at)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY
sql = """INSERT INTO `parking_cities` (id, country_id, province_id, municipality, street, number, longitude, latitude, visibility, created_at, updated_at)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY
UPDATE id=values(id),
country_id=values(country_id),
province_id=values(province_id),
municipality=values(municipality),
street=values(street),
orientation=values(orientation),
number=values(number),
longitude=values(longitude),
latitude=values(latitude),
Expand All @@ -75,11 +72,10 @@ def upload_json(self) -> None:
int(self.country_id),
int(self.province_id),
str(self.name),
str(location["LOCATIE"]),
str(location["TYPE_VAK"]),
int(location["AANTAL"]),
float(location["LON"]),
float(location["LAT"]),
str(item.street),
int(1),
float(longitude),
float(latitude),
bool(True),
(datetime.datetime.now(tz=pytz.timezone("Europe/Amsterdam"))),
(datetime.datetime.now(tz=pytz.timezone("Europe/Amsterdam"))),
Expand Down
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pytz = "^2023.3"
requests = "^2.31.0"

[tool.poetry.group.cities.dependencies]
arnhem = "^0.1.0"
brussel = "^0.2.1"
dresden = "^0.2.1"
dusseldorf = "^1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def provide_city(self, city_name: str) -> object:
provided_city = cp.provide_city(selected_city)

# Get the data from the API
if selected_city in ["groningen", "arnhem", "zoetermeer"]:
if selected_city in ["groningen", "zoetermeer"]:
data_set = None # pylint: disable=C0103
provided_city.download()
else:
Expand Down

0 comments on commit f88305b

Please sign in to comment.