rockglacierNET / datacollection /separate_polygons.py
kabiriitb's picture
Upload folder using huggingface_hub
80e070e verified
Raw History Blame Contribute Delete
1.25 kB
import os
import yaml
import geopandas as gpd
def load_config():
config_path = os.path.join(os.path.dirname(__file__), 'config.yaml')
with open(config_path, 'r') as f:
return yaml.safe_load(f)
def main():
config = load_config()
feature_stack_shp = os.path.join(os.path.dirname(__file__), config['paths']['rock_glacier_boundary_shp'])
output_dir = os.path.join(os.path.dirname(__file__), config['paths']['output_dir'], 'individual_polygons')
os.makedirs(output_dir, exist_ok=True)
print(f"Loading feature stack shapefile: {feature_stack_shp}")
try:
gdf = gpd.read_file(feature_stack_shp)
except Exception as e:
print(f"Error loading {feature_stack_shp}: {e}")
print("Please ensure you have updated the 'feature_stack_shp' path in config.yaml.")
return
print(f"Separating {len(gdf)} feature stack polygons...")
for idx, row in gdf.iterrows():
single_gdf = gpd.GeoDataFrame([row], crs=gdf.crs)
out_json = os.path.join(output_dir, f"polygon_{idx:04d}.geojson")
single_gdf.to_file(out_json, driver="GeoJSON")
print(f"Done! Saved {len(gdf)} GeoJSON files to {output_dir}")
if __name__ == "__main__":
main()