From 9f7ebdfa1a01f6f221074ca96afdefdcc8060c37 Mon Sep 17 00:00:00 2001 From: Samuli Massinen Date: Fri, 16 Feb 2024 10:32:41 +0200 Subject: [PATCH] fix jupyter stage 6 and json geometries --- 6-kaavatietomallin-implementointi.ipynb | 5 ++-- lib/implement_kaavatietomalli.py | 33 +++++++++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/6-kaavatietomallin-implementointi.ipynb b/6-kaavatietomallin-implementointi.ipynb index 0a39a30..1797914 100644 --- a/6-kaavatietomallin-implementointi.ipynb +++ b/6-kaavatietomallin-implementointi.ipynb @@ -33,6 +33,7 @@ "source": [ "# Käytettävien Python moduulien ja kehitettyjen funktioiden sisäänluku\n", "import geopandas as gpd\n", + "import json\n", "from lib.implement_kaavatietomalli import dataToJSON" ] }, @@ -63,12 +64,12 @@ "kunta_dissolve = kunta.dissolve(by='', as_index=False) \n", "\n", "# Aineiston muuntaminen kaavatietomalliin ja JSON-dumpiksi\n", - "data_json = dataToGeoJSON(kaavadata=kunta,\n", + "data_json = dataToJSON(kaavadata=kunta,\n", " aineistolahde='', # joko \"kunta\" tai \"KTJ\"\n", " ktj_kaavatunnus='kaavatunnus_1', # vaihda, jos olet tenhyt vertailun kuntapohjaisesti!\n", " kunta_kaavatunnus='refe_kaavatunnus') # vaihda, jos olet tenhyt vertailun kuntapohjaisesti!\n", "\n", - "# GeoJSON tallennus\n", + "# JSON tallennus\n", "with open(r'.json', 'w', encoding='utf8') as outfile:\n", " result = json.dumps(data_json, indent=4, ensure_ascii=False)\n", " outfile.write(result)" diff --git a/lib/implement_kaavatietomalli.py b/lib/implement_kaavatietomalli.py index 022e08e..caa9f51 100644 --- a/lib/implement_kaavatietomalli.py +++ b/lib/implement_kaavatietomalli.py @@ -5,10 +5,10 @@ """ -def dataToGeoJSON(kaavadata, aineistolahde, ktj_kaavatunnus, kunta_kaavatunnus): +def dataToJSON(kaavadata, aineistolahde, ktj_kaavatunnus, kunta_kaavatunnus): """ - A function for creating a GeoJSON file (collections.OrderedDict) from Geopandas GeoDataFrame. + A function for creating a JSON file (collections.OrderedDict) from Geopandas GeoDataFrame. Parameters -------------------- @@ -35,7 +35,7 @@ def dataToGeoJSON(kaavadata, aineistolahde, ktj_kaavatunnus, kunta_kaavatunnus): import ast import geopandas as gpd - # Function to order GeoJSON keys properly + # Function to order JSON keys properly def ordered(d, desired_key_order): return OrderedDict([(key, d[key]) for key in desired_key_order]) @@ -740,14 +740,37 @@ def ordered(d, desired_key_order): # Drop unnecessary columns kopio = kopio.drop(col_list, axis=1) - # Pandas to GeoJSON + # Pandas to JSON geojson = kopio.to_json() json_data = json.loads(geojson) - # Extract "features" part from GeoJSON + # Extract "features" part from JSON features_only = json_data.get("features", []) # Extract only "properties" part from each feature properties_only = [feature.get("properties", {}) for feature in features_only] + # Remove unnecessary 'type' and 'properties' keys under plans --> geographicalArea + for item in properties_only: + for phase in item.get('planMatterPhases', []): + for decision in phase.get('planMatterDecisions', []): + for plan in decision.get('plans', []): + geo_area = plan.get('geographicalArea', {}) + # Use pop with a default value to avoid KeyError if the key doesn't exist + geo_area.pop('type', None) + geo_area.pop('properties', None) + + # Reorder keys, 'srid' first + if 'srid' in geo_area: + # Create a new dictionary with 'srid' as the first key + reordered_geo_area = {'srid': geo_area['srid']} + # Add the remaining items, excluding 'srid' since it's already added + for key, value in geo_area.items(): + if key != 'srid': + reordered_geo_area[key] = value + + plan['geographicalArea'] = reordered_geo_area + else: + plan['geographicalArea'] = geo_area + return(properties_only)