Skip to content

Commit

Permalink
fix jupyter stage 6 and json geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuli Massinen committed Feb 16, 2024
1 parent 09aab71 commit 9f7ebdf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
5 changes: 3 additions & 2 deletions 6-kaavatietomallin-implementointi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
Expand Down Expand Up @@ -63,12 +64,12 @@
"kunta_dissolve = kunta.dissolve(by='<insert kaavatunnus column here>', 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='<insert aineistolahde here>', # 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'<insert filepath here>.json', 'w', encoding='utf8') as outfile:\n",
" result = json.dumps(data_json, indent=4, ensure_ascii=False)\n",
" outfile.write(result)"
Expand Down
33 changes: 28 additions & 5 deletions lib/implement_kaavatietomalli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------
Expand All @@ -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])

Expand Down Expand Up @@ -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)

0 comments on commit 9f7ebdf

Please sign in to comment.