feat: a code added to fix the issue with geojson

This commit is contained in:
Saeed Ranjbar 2024-08-06 16:43:12 -04:00
parent d5373a77a5
commit abd921fdc2
3 changed files with 32 additions and 2 deletions

28
fixing_geojson.py Normal file
View File

@ -0,0 +1,28 @@
import json
from shapely.geometry import shape, mapping, Polygon, MultiPolygon
from pathlib import Path
file_path = (Path(__file__).parent / 'input_files' / 'riga_case_study_84.geojson')
output_filepath = (Path(__file__).parent / 'input_files' / 'riga_case_study_84_rh.geojson')
def enforce_right_hand_rule(geojson):
for feature in geojson['features']:
geometry = shape(feature['geometry'])
if isinstance(geometry, Polygon):
if not geometry.exterior.is_ccw:
geometry = Polygon(geometry.exterior.coords[::-1], [interior.coords[::-1] for interior in geometry.interiors])
elif isinstance(geometry, MultiPolygon):
new_polygons = []
for polygon in geometry.geoms: # Use .geoms to iterate over the individual polygons
if not polygon.exterior.is_ccw:
polygon = Polygon(polygon.exterior.coords[::-1], [interior.coords[::-1] for interior in polygon.interiors])
new_polygons.append(polygon)
geometry = MultiPolygon(new_polygons)
feature['geometry'] = mapping(geometry)
return geojson
with open(file_path, 'r') as f:
geojson_data = json.load(f)
corrected_geojson = enforce_right_hand_rule(geojson_data)
with open(output_filepath, 'w') as f:
json.dump(geojson_data, f)
# Output the corrected GeoJSON

View File

@ -0,0 +1 @@
{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "MultiPolygon", "coordinates": [[[[24.1218395, 56.9645234], [24.1218831, 56.9645793], [24.1217329, 56.9646142], [24.1217704, 56.9646622], [24.1214121, 56.9647454], [24.1213666, 56.9647559], [24.1211979, 56.9645399], [24.1215644, 56.9644549], [24.1214785, 56.964399299999975], [24.1218065, 56.96424859999998], [24.1219036, 56.9643114], [24.1219191, 56.9643043], [24.1219417, 56.9643189], [24.1219924, 56.9643517], [24.1219769, 56.9643588], [24.1220837, 56.964428], [24.1218804, 56.96452139999998], [24.1218652, 56.9645116], [24.1218395, 56.9645234]]]]}, "id": "B1800", "properties": {"name": "B1800", "address": "", "function": "", "height": 57, "year_of_construction": ""}}]}

View File

@ -2,13 +2,14 @@ from pathlib import Path
from hub.imports.geometry_factory import GeometryFactory
from hub.helpers.dictionaries import Dictionaries
# Specify the GeoJSON file path
file_path = (Path(__file__).parent / 'input_files' / 'riga_case_study_84.geojson')
file_path = (Path(__file__).parent / 'input_files' / 'riga_case_study_84_rh.geojson')
# Specify the output path for the PDF file
output_path = (Path(__file__).parent / 'out_files').resolve()
# Create city object from GeoJSON file
city = GeometryFactory('geojson',
path=file_path,
height_field='height_ag',
height_field='height',
year_of_construction_field=None,
function_field=None,
function_to_hub=None).city
print('test')