2024-10-09 12:11:39 -04:00
|
|
|
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-10-09 12:11:39 -04:00
|
|
|
|
2024-12-03 10:40:50 -05:00
|
|
|
output_file = 'data/selected_buildings_corrected.geojson'
|
2024-10-09 12:11:39 -04:00
|
|
|
|
|
|
|
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')
|