I'm merging some shapefiles and I had some problems doing so inside QGIS, so i'm using ogr2ogr directly. I'm doing this (in a batch):
ogr2ogr -overwrite %destination% %n1%
ogr2ogr -update -append %destination% %n2% -nln all_new
ogr2ogr -update -append %destination% %n3% -nln all_new
ogr2ogr -update -append %destination% %n4% -nln all_new
It works fine, but now I need to have in the resulting shapefile, a field with the names of the original shapefiles I merged. Doesn't sound very difficult, but I'm not managing to do it.
Can anyone help? Thank you!
Answer
With small scripting it would be doable. With something like following you should be able to add column to a shapefile in all shapefiles in a folder, and merge them to merged.shp file
for %f in (*.shp) do (
ogrinfo %f -sql "ALTER TABLE %f ADD COLUMN filename character(15)"
ogrinfo %f -sql "UPDATE TABLE %f filename = '%f'"
ogr2ogr -update -append merged.shp %f -f “esri shapefile” -nln merge
)
edit: The same as a Bash script, with some changes to make it working:
for f in *.shp
do
base=${f%.shp}
ogrinfo $f -sql "ALTER TABLE $base ADD COLUMN filename character(15)"
ogrinfo $f -dialect SQLite -sql "UPDATE $base SET filename = '$base'"
ogr2ogr -update -append merged.shp $f
done
No comments:
Post a Comment