简体   繁体   中英

run imposm commands from a python script

I'm just getting started using imposm to help get openstreetmap data into a postgis database. All the docs point to making all commands via Terminal. This is fine for one off imports but I plan to have many many imports of varying bounding boxes and would like to script the loading of the data in the database.

Currently I use:

imposm --overwrite-cache --read --write -d postgis_test --user postgres -p ""  /Users/Me/MapnikTest/osmXML.osm

Which works fine from the command line but as osmXML.osm is being created many times I would like somehow to import this at the point of creation.

Putting the same thing in a python script as:

os.system("imposm --overwrite-cache --read --write -d postgis_test --user postgres -p ""  /Users/Ali\ Mac\ Pro/Desktop/MapnikTest/osmXML.osm")

just returns:

/bin/sh: imposm: command not found

Solving this would be the final step to automate the acquisition of data to render small maps on demand but I'm falling at the final hurdle!

** Edit full path to imposm solved the first problem but imputing the password for the postgres user happens when prompted. Is there a way to send the password in the same single line command? (maybe this needs to be a new post?, happy if someone points me in the right direction)**

This is probably because os.system() is calling /bin/sh which uses a different shell environment from the one you use when working on the command line.

To work around this, in your script, get the full path to the imposm script and then use that in your command. Use can use some code like this to find the executable.

Or you can fix your shell definitions so that /bin/sh has the proper PATH defined, but that depends greatly on your setup...

Solved with the help of further research and the comments from @Eli Rose (many thanks): find out what path to imposm (or whichever command you are trying to make) with

which <command>

Then include the path in the python shell command. Using a module from subprocess you can even see the full terminal output.

import subprocess
from subprocess import *
print Popen("/usr/local/bin/imposm --overwrite-cache --read --write --connection postgis://<database user>:<password>@<host>/<database> /path/to/data.osm", stdout=PIPE, shell=True).stdout.read()

The

--connection postgis://<database user>:<password>@<host>/<database>

means you can make the command in a single line and not have to worry about entering the database user password in a following command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM