From 8fc7e456c5832d68f38600a3412491f644c1587e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles=20Kr=C3=BCger?= Date: Thu, 28 Mar 2024 10:31:10 +0100 Subject: [PATCH] load data from map data service rather than from file --- .../commands/import_constructions.py | 22 ++++++++++--------- pyproject.toml | 1 + ...on => sample-construction-sites-v2.geojson | 0 3 files changed, 13 insertions(+), 10 deletions(-) rename backend/sample-construction-sites-v2.geojson => sample-construction-sites-v2.geojson (100%) diff --git a/backend/construction/management/commands/import_constructions.py b/backend/construction/management/commands/import_constructions.py index 4678026..3bb937c 100644 --- a/backend/construction/management/commands/import_constructions.py +++ b/backend/construction/management/commands/import_constructions.py @@ -1,7 +1,5 @@ -import json - +import requests from construction.models import Construction -from django.conf import settings from django.contrib.gis.geos import Point from django.core.management.base import BaseCommand @@ -62,14 +60,18 @@ def handle(self, *args, **options): print("Importing construction data from priobike-map-data...") - # Import GeoJSON file from data folder. - with open( - str(settings.BASE_DIR) - + "/sample-construction-sites-v2.geojson" # TODO: use real data - ) as f: - data = json.load(f) + BASE_URL = "priobike.vkw.tu-dresden.de/production" + API = "https://" + BASE_URL + "/map-data/construction_sites_v2.geojson" + + try: + response = requests.get(API) + response.raise_for_status() + data = response.json() + except Exception as e: + print("Failed to fetch construction data: " + str(e)) + return - print(f"Found {len(data['features'])} construction sites.") + print(f"Loaded {len(data['features'])} construction sites.") try: Construction.objects.all().delete() diff --git a/pyproject.toml b/pyproject.toml index 4733180..0b11ab5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ gunicorn = "^21.2.0" scipy = "^1.12.0" numpy = "^1.26.4" shapely = "^2.0.3" +requests = "^2.31.0" [tool.poetry.dev-dependencies] diff --git a/backend/sample-construction-sites-v2.geojson b/sample-construction-sites-v2.geojson similarity index 100% rename from backend/sample-construction-sites-v2.geojson rename to sample-construction-sites-v2.geojson