The basic script in order to iterate recursively over sub-folders and merge all shapefiles into single one is:
#!/bin/bash
consolidated_file="./consolidated.shp"
for i in $(find . -name '*.shp'); do
if [ ! -f "$file""$consolidated_file" ]; then
# first file - create the consolidated output file
ogr2ogr -f "ESRI Shapefile" $consolidated_file $i
else
# update the output file with new file content
ogr2ogr -f "ESRI Shapefile" -update -append $consolidated_file $i
fi
done
Hoverer in vertaully all examples around the web I noticed that for the case where I update the output file, -nln
tag is added, for example:
ogr2ogr -f "ESRI Shapefile" -update -append $consolidated_file $i -nln merged
According to the documentation it says:
Assign an alternate name to the new layer
And I noticed it creates a temporary shapefile called "merged", and in the end of the loop the file is identical to the last shapefile I merged.
I don't understand why I need this? Because I succeeded to merge successfully without this tag.