PostGIS GML Conversion – Using ogr2ogr to Convert Multiple GML Files to a PostGIS Table

gdalgmlogr2ogrpostgispostgresql

I have a folder with 150 .gml (addressbase) files I need to upload to my postGIS/postgres database. After the first gml is uploaded, the rest will be appended to it to form a single table. However I don't know how to make ogr2ogr iterate through each file… currently I'm having to upload/append each cml induvidually.

My code to convert the first gml:

ogr2ogr -update -append -f "PostgreSQL" PG:"host=localhost port=5432 dbname=testdb user=admin password=password" -lco SCHEMA=test_schema "D:\path to folder\File1.gml" -progress -lco OVERWRITE=YES

and my code to append each subsequent gml to the first:

ogr2ogr -update -append -f "PostgreSQL" PG:"host=localhost port=5432 dbname=testdb user=admin password=password active_schema=test_schema" "D:\path to folder\File2.gml" -progress

This works fine but I don't want to do this another 148 times, changing File2 to File3 each time etc…

The GMLs contain point features. For use with postgis and qgis.

Best Answer

Looking at your examples, I assume you are working on a Windows machine. Try the following:

  1. Copy/Move/Put the remaining 149 GML files in a directory, say D:\myGMLs.
  2. Paste the codes below into a text file and save it with a .bat or .cmd extension, say "D:\Work\do_149.cmd".
    set dirgml=D:\myGMLs

    set app=ogr2ogr
    set opt=-update -append -progress
    set dst=-f "PostgreSQL" PG:"host=localhost port=5432 dbname=testdb user=admin password=password active_schema=test_schema"

    for %%G in ("%dirgml%\*.gml") do %app% %opt% %dst% "%%G"
  1. Open up a Command Prompt (cmd.exe) and run the bat/cmd (eg "D:\Work\do_149").


The double quotes around "%dirgml%\*.gml" and "%%G" let you work with directory name and filename that have spaces in them e.g., "D:\Project Frosty\My Snowmobile.gml".

Related Question