hub/crs_conversion.py

30 lines
808 B
Python
Raw Normal View History

import geopandas as gpd
from shapely.geometry import Polygon, MultiPolygon
from shapely.geometry.polygon import orient
2024-12-03 10:40:50 -05:00
input_file = 'data/selected_buildings.geojson'
2024-12-03 10:40:50 -05:00
output_file = 'data/selected_buildings_corrected.geojson'
gdf = gpd.read_file(input_file)
if gdf.crs is None:
gdf.set_crs(epsg=3857, inplace=True)
gdf = gdf.to_crs(epsg=4326)
def reorient_geometry(geom):
if geom.is_empty:
return geom
elif geom.geom_type == 'Polygon':
return orient(geom, sign=1.0)
elif geom.geom_type == 'MultiPolygon':
reoriented_polygons = [orient(polygon, sign=1.0) for polygon in geom.geoms]
return MultiPolygon(reoriented_polygons)
else:
return geom
gdf['geometry'] = gdf['geometry'].apply(reorient_geometry)
gdf.to_file(output_file, driver='GeoJSON')