File size: 1,245 Bytes
80e070e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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()
|