4

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.

Theo F
  • 1,817
  • 12
  • 34
  • ogr2ogr should happily read all supported files from a folder if given only the directory. – geozelot Nov 16 '18 at 17:01
  • I'm afraid it doesn't. If I remove File2.gml I get an 'unable to open datasource 'D:\path to folder' with the following drivers.' Same goes for 'D:\path to folder' (no backslash at the end). @ThingumaBob – Theo F Nov 19 '18 at 11:32
  • 1
    Ths question refers to a similar process with shp2pgsql, for which the accepted answer deals with ogr2ogr. – John Powell Nov 19 '18 at 12:59
  • There is the Loader tool from Astun which is free and worth a look if you need to do this regularly. – RoperMaps Nov 19 '18 at 13:32

1 Answers1

5

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".

Ralph Tee
  • 1,722
  • 12
  • 17
  • can you explain the last line of that .bat code? I need to know whether I need to ammend any of it to suit my folder directories/gml file names. – Theo F Nov 28 '18 at 16:59
  • @TheoF. If you had created say "abc.bat" - then you run "abc.bat". If you had created say "abc.cmd", then you run "abc.cmd". Both extensions are equivalent. – Ralph Tee Nov 28 '18 at 17:25
  • not quite what I meant, but anyway, your answer worked! thank you very much. – Theo F Nov 29 '18 at 10:57
  • @TheoF Oops, sorry. Please refer to this link https://ss64.com/nt/syntax-percent.html – Ralph Tee Dec 04 '18 at 07:26