简体   繁体   English

从python脚本运行imposm命令

[英]run imposm commands from a python script

I'm just getting started using imposm to help get openstreetmap data into a postgis database. 我刚刚开始使用imposm帮助将openstreetmap数据获取到postgis数据库中。 All the docs point to making all commands via Terminal. 所有文档都指向通过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. 从命令行可以正常工作,但是由于创建了osmXML.osm多次,所以我想以某种方式在创建时将其导入。

Putting the same thing in a python script as: 将相同的内容放入python脚本中,如下所示:

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. **编辑imposm的完整路径解决了第一个问题,但是在提示时为postgres用户输入密码。 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. 这可能是因为os.system()正在调用/bin/sh ,它使用的命令行环境与您在命令行上使用的命令行环境不同。

To work around this, in your script, get the full path to the imposm script and then use that in your command. 要解决此问题,请在脚本中获取imposm脚本的完整路径,然后在命令中使用该路径。 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... 或者您可以修复您的shell定义,以便/bin/sh定义了正确的PATH ,但这在很大程度上取决于您的设置...

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 在进一步研究的帮助下和@Eli Rose的评论中解决了(非常感谢):找出使用imposm的路径(或尝试执行的任何命令)

which <command>

Then include the path in the python shell command. 然后将路径包含在python shell命令中。 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. 意味着您可以在一行中执行该命令,而不必担心在以下命令中输入数据库用户密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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