Let's say there's a single shapefile that contains multiple features. Most of which are polygons.
How can one convert those features into separate independent GeoJSON files?
Can this be done with ogr2ogr
?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communitybash /path/to/shp2geojson.sh /path/to/input.shp /path/to/output-dir
shp2geojson.sh
#!/bin/bash
clear;
layername=$(ogrinfo -nogeomtype "$1" | grep "1:" | awk '{print $2}')
count=$(ogrinfo -so -ro "$1" "$layername" | grep "Feature Count:" | awk '{print $3}')
if [ -d $2 ]; then
echo "The directory $2 is now ready."
else
mkdir "$2"
echo "The directory $2 has just been created and now ready."
fi
for (( i=0; i<$count; i++ )); do
echo "Processing feature #$i..."
ogr2ogr -f GeoJSON -t_srs EPSG:4326 -fid "$i" "$2/$i.geojson" "$1"
done
This bash script works for Mac and Linux machines. I do not know how to do it in batch script for Windows.
If one wants to use a certain column from the attribute table (instead of the feature index) for the naming of the GeoJSON files, I have made a derivative solution similar to the above script.
-sql
or-fid
can be used for selecting.ogrinfo
to list the features and then loop them? Worth a shot though.0
and the last one iscount - 1
. I did exactly what you said, which is to select features one-by-one and in a loop.