29 lines
1.2 KiB
Python
29 lines
1.2 KiB
Python
|
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
|