[gdal-dev] Creating new column with filename, scripting shapefiles
Joe Larson
joe at joelarson.com
Thu Nov 29 07:28:33 PST 2012
I have tried these three methods to add a filename column while scripting a folder of shapefiles, with a Bash script - which results in `Warning 6: Normalized/laundered field name` & `ERROR 1: SQL Expression Parsing Error: syntax error` messages and NULL values in the created column :
#1
for f in *.shp;
do
name=${f%.shp}
ogrinfo $f -sql "ALTER TABLE $name ADD COLUMN filename character(10)"
ogrinfo $f -sql "UPDATE TABLE $name filename = '$f'"
done;
#2
for f in *.shp;
do
name=`echo "$f"|sed 's/\.shp$//g'`
ogrinfo $f -sql "ALTER TABLE $name ADD COLUMN filename character(10)"
ogrinfo $f -sql "UPDATE TABLE $name filename = '$f'"
done;
#3
for f in *.shp;
do
name=`basename $f .shp`
ogrinfo $f -sql "ALTER TABLE $name ADD COLUMN filename character(10)"
ogrinfo $f -sql "UPDATE TABLE $name filename = '$f'"
done;
There's an example here http://trac.osgeo.org/gdal/wiki/FAQVector called "How do I include the source filename in a field when merging hundreds of shapefiles (Windows)?" I also cannot get this to work - getting "unrecognized fieldname" message.
Perhaps my variable does not work in the Bash SQL statement. I'm not sure what's going on in the Windows example.
Regards,
Joe Larson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20121129/cd497f34/attachment.html>
More information about the gdal-dev
mailing list