[gdal-dev] Appending a column on shapefile import to postgis via ogr2ogr

Aaron Racicot aaronr at z-pulley.com
Fri May 23 13:09:00 EDT 2008


Probably a silly SQL type question but...

So I am trying to run ogr2ogr from a php script with the intent of auto
loading some shapefile data into a pre-existing postgis table.  Since the
shapefile I am loading could be any arbitrary shapefile (It does not have
any unique identifier I have access to at the time of loading) I need a way
to append an attribute to it upon loading it into postgis.

Normal loading of the shapefile works fine, but when I try to append an
attribute when loading the data via ogr2ogr things don't seem to work. (I am
probably showing my lack of true SQL foo here...)

Inside of postgres I can easily do something like the following to get a new
column 'author' appended to the results of a query: 

select 'aaron' as author, * from mytable;

In the database something like appending a static string in the select works
fine, but what I would like to do is execute an sql like that inside the
ogr2ogr call with the -sql flag.  My initial attempts at this don't seem to
work.

Is something like that possible?

Basically in the end I would like something like:
ogr2ogr -update -append -f PostgreSQL PG:'host=localhost user=racicot
dbname=xxx' -sql "select 'aaronr' as author, * from test" test.shp -nln
user_shapes

My hope was that I could add the author field to the -sql statement and have
it populate the 'author' column in my postgis table.  If something like this
from the command line would work then I could just swap in an arbitrary
username for 'aaronr' in the above example on the data import via my script
and have a unique way to keep track of the data once it is imported.

Any advice appreciated...

Aaron





More information about the gdal-dev mailing list