2

I'm trying to programmatically import some shapefiles to PostGIS, since I need to catch the exit code of the process I decided to use subprocess.Popen. I've managed to import the files using:

cmd = ["ogr2ogr","-f", "PostgreSQL", "PG:dbname=idegeo host=localhost port=5432      user=postgres password=postgres",
        "-nlt", "PROMOTE_TO_MULTI","-nln","tablename",
        "-unsetFieldWidth","shape.shp"]
process = subprocess.Popen(cmd,stdout=subprocess.PIPE)
output = process.communicate()[0]

Now, the only thing I'm having problems is in passing the argument --config PG_USE_COPY YES to increase performance (as suggested here). I've tried using [someargs,"--config", "PG_USE_COPY YES"] but Popen refuses to recognize the value passed to --config

plablo09
  • 513
  • 3
  • 10
  • Wouldn't you just put that in with your command (cmd) : cmd = ["ogr2ogr","--config PG_USE_COPY YES","-f",... – Michael Stimson Sep 12 '14 at 02:26
  • @MichaelMiles-Stimson well, that was my original thought but Popen expects arguments in pairs as in ["-f","PostgreSQL"] so if i pass the full string "--config PG_USE_COPY YES"I get Unknown option name '--config PG_USE_COPY YES' – plablo09 Sep 12 '14 at 17:16
  • if "--config PG_USE_COPY YES" doesn't work try "--config","PG_USE_COPY YES" – Michael Stimson Sep 14 '14 at 22:21
  • 1
    Well, it turns out that the correct option is ["ogr2ogr","--config" ,"PG_USE_COPY", "YES"..] – plablo09 Sep 14 '14 at 23:18
  • Can you put that in as an answer (yes, answer your own question) - answers are searchable, comments are not, and I am sure that someone will have the same or similar problem and your answer would benefit them. – Michael Stimson Sep 16 '14 at 21:43

1 Answers1

4

Well, I'm answering my own question in case someone runs into the same issue:

To pass the option --config PG_USE_COPY YES to Popen you have to pass it as three separate strings:

cmd = [...someArgs,"--config" ,"PG_USE_COPY", "YES", ...someMoreArgs]
process = subprocess.Popen(cmd,stdout=subprocess.PIPE)
output = process.communicate()[0]
plablo09
  • 513
  • 3
  • 10
  • Thank you for writing out that answer, the docs on the popen can be a little confusing; when to put in separate arguments and when joined arguments are acceptable can be a little hit and miss. – Michael Stimson Sep 17 '14 at 21:31
  • @MichaelMiles-Stimson No problem. As a rule of thumb, you should always separate arguments – plablo09 Sep 17 '14 at 22:31