[GIS] append to different tables using ogr2ogr

gdalgmlogr2ogrpostgispython

I have been through many related questions and links but I am still confused as to how/if this is achievable. So, I have defined tables in my postgis database and I would like my ogr2ogr commnand to append to these existing tables rather than create its own – using GML files that I have.

I have tried using -nln and -sql but I had no success. The -nln option seems useful when importing a shapefile which usually contains one type of geometry thus needing to specify one table only. But what happens when need to specify 11 tables?

Finally, I found some links to use python with ogr library. I have not found good documentation with examples though…

Any suggestions?

Best Answer

From what I understood you want to append or update your postgis tables using a gml file

ogr2ogr provides such functionality using the --update flag like this:

ogr2ogr -update -f "PostgreSQL" PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" 
myfile.GML  -nln <myschema>.<mytable>

you can also try the -append flag.

Now if you are on unix you can bash you way through multiple files with few lines of script:

for f in $(ls *.gml); do
    ogr2ogr -update -f "PostgreSQL" PG:"<pg-details-here>" "$f" -nln <myschema>.`basename $f .gml`;
done

in windows machines you can use instead of an bash interpreter, the MSYS program, which is installed with OSGeo4W utility.

Related Question