Skip to content

Instantly share code, notes, and snippets.

@oeon
Created November 29, 2012 08:13
Show Gist options
  • Save oeon/4167521 to your computer and use it in GitHub Desktop.
Save oeon/4167521 to your computer and use it in GitHub Desktop.
source filename in shapefile
#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;
@oeon
Copy link
Author

oeon commented Nov 29, 2012

i don't think shapefiles can handle columns that big - but i have tried this with (11) too. Always get ERROR 1: SQL Expression Parsing Error: syntax error

@geohacker
Copy link

Hmm, I tried the first one and this is what I've: ERROR 1: Unsupported ALTER TABLE command : ALTER TABLE ADD COLUMN filename character(21)

@oeon
Copy link
Author

oeon commented Nov 29, 2012

shapefiles cannot handle field names longer than 10 characters

@geohacker
Copy link

The alter command works.

@oeon
Copy link
Author

oeon commented Nov 29, 2012

maybe the error is coming when i pass the variable into the -sql ? Also, I must be sleepy ;) fieldnames cannot be longer than 10 - but contents can :P

@geohacker
Copy link

Yes. That's what I suspected too. I hard coded the column name but it still throws a "syntax error":
ogrinfo file.shp -sql "UPDATE file SET filename_4='blah'"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment