From 59c86089ad065d690ea25bd1b74e7346298585ff Mon Sep 17 00:00:00 2001 From: justian Date: Wed, 15 May 2024 18:01:35 +0200 Subject: [PATCH 1/2] Feat: get data from PDOK api --- app/signals/load_areas.py | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/signals/load_areas.py diff --git a/app/signals/load_areas.py b/app/signals/load_areas.py new file mode 100644 index 000000000..85761eaad --- /dev/null +++ b/app/signals/load_areas.py @@ -0,0 +1,48 @@ +import requests +import sys + +from apps.signals.models import AreaType + +# Get municipality data from the PDOK WFS service +def get_municipality_data(municipality_name): + url = "https://service.pdok.nl/cbs/wijkenbuurten/2023/wfs/v1_0" + type_string = "gemeente" + + params = { + "service": "WFS", + "version": "1.0.0", + "request": "GetFeature", + "typename": "wijkenbuurten:gemeenten", + "outputFormat": "application/json", + "maxFeatures": 10, + "filter": f"gemeentenaam{municipality_name}" + } + + response = requests.get(url, params=params) + + if response.status_code == 200: + data = response.json() + + AreaType.objects.get_or_create( + code=type_string, + defaults={'name': type_string, 'description': f'{type_string} from CBS "Wijk- en buurtkaart" data.'} + ) + + print(data['features']) + else: + print("Failed to retrieve data. Status code:", response.status_code) + + +# When executing this script you have to give a Dutch municipality name as argument, if you did this we will execute +# get_municipality_data() +def load_cbs_areas(): + if len(sys.argv) != 2: + print("Usage: python3 file.py ") + return + + municipality_name = sys.argv[1] + get_municipality_data(municipality_name) + + +if __name__ == "__main__": + load_cbs_areas() From ac2c87ccb25bb8b39e49a9dbd104657d119162e0 Mon Sep 17 00:00:00 2001 From: justian Date: Wed, 22 May 2024 15:17:19 +0200 Subject: [PATCH 2/2] Feat: load areas with PDOK api --- .../management/commands/load_areas_v2.py | 71 +++++++++++++++++++ app/signals/load_areas.py | 48 ------------- 2 files changed, 71 insertions(+), 48 deletions(-) create mode 100644 app/signals/apps/dataset/management/commands/load_areas_v2.py delete mode 100644 app/signals/load_areas.py diff --git a/app/signals/apps/dataset/management/commands/load_areas_v2.py b/app/signals/apps/dataset/management/commands/load_areas_v2.py new file mode 100644 index 000000000..17192ed6e --- /dev/null +++ b/app/signals/apps/dataset/management/commands/load_areas_v2.py @@ -0,0 +1,71 @@ +import requests +import json +from django.core.management.base import BaseCommand +from django.contrib.gis.geos import GEOSGeometry +from signals.apps.signals.models import AreaType, Area + +class Command(BaseCommand): + help = "Load CBS areas into the database" + + def add_arguments(self, parser): + parser.add_argument("-n", "--name", required=True, help="Municipality name") + parser.add_argument("-t", "--type", required=True, help="Area type (wijken or buurten)") + parser.add_argument("-d", "--delete", action="store_true", required=False, help="If specified, delete former database entries in Area table") + + def handle(self, *args, **options): + municipality_name = options['name'] + area_type_name = options['type'] + delete = options['delete'] + + if delete: + Area.objects.filter(_type__name='district').delete() + + self.get_pdok_data(municipality_name, area_type_name) + + def get_pdok_data(self, municipality_name, area_type_name): + url = "https://service.pdok.nl/cbs/wijkenbuurten/2023/wfs/v1_0" + + params = { + "service": "WFS", + "version": "1.1.0", + "request": "GetFeature", + "typename": f"wijkenbuurten:{area_type_name}", + "outputFormat": "application/json", + "srsName": "EPSG:4326", + "filter": f"gemeentenaam{municipality_name}" + } + + response = requests.get(url, params=params) + + if response.status_code == 200: + data = response.json() + + area_type, _ = AreaType.objects.get_or_create( + code='district', + defaults={'name': 'district', 'description': 'data from CBS "Wijk- en buurtkaart" data.'} + ) + + for area in data['features']: + geojson_str = json.dumps(area['geometry']) + + if area_type_name == "wijken": + area_name = area['properties']['wijknaam'] + area_code = area['properties']['wijkcode'] + elif area_type_name == "buurten": + area_name = area['properties']['buurtnaam'] + area_code = area['properties']['buurtcode'] + else: + self.stdout.write(self.style.ERROR(f"Unsupported area type: {area_type_name}")) + return + + geometry = GEOSGeometry(geojson_str) + + Area.objects.create( + name=area_name, + code=area_code, + _type=area_type, + geometry=geometry + ) + + else: + self.stdout.write(self.style.ERROR(f"Failed to retrieve data. Status code: {response.status_code}")) \ No newline at end of file diff --git a/app/signals/load_areas.py b/app/signals/load_areas.py deleted file mode 100644 index 85761eaad..000000000 --- a/app/signals/load_areas.py +++ /dev/null @@ -1,48 +0,0 @@ -import requests -import sys - -from apps.signals.models import AreaType - -# Get municipality data from the PDOK WFS service -def get_municipality_data(municipality_name): - url = "https://service.pdok.nl/cbs/wijkenbuurten/2023/wfs/v1_0" - type_string = "gemeente" - - params = { - "service": "WFS", - "version": "1.0.0", - "request": "GetFeature", - "typename": "wijkenbuurten:gemeenten", - "outputFormat": "application/json", - "maxFeatures": 10, - "filter": f"gemeentenaam{municipality_name}" - } - - response = requests.get(url, params=params) - - if response.status_code == 200: - data = response.json() - - AreaType.objects.get_or_create( - code=type_string, - defaults={'name': type_string, 'description': f'{type_string} from CBS "Wijk- en buurtkaart" data.'} - ) - - print(data['features']) - else: - print("Failed to retrieve data. Status code:", response.status_code) - - -# When executing this script you have to give a Dutch municipality name as argument, if you did this we will execute -# get_municipality_data() -def load_cbs_areas(): - if len(sys.argv) != 2: - print("Usage: python3 file.py ") - return - - municipality_name = sys.argv[1] - get_municipality_data(municipality_name) - - -if __name__ == "__main__": - load_cbs_areas()