首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python子过程公式

Python子过程公式
EN

Stack Overflow用户
提问于 2016-10-20 05:18:14
回答 2查看 812关注 0票数 0

我一直在试验Python的子流程模块。我希望效仿以下成功的指挥:

命令行:

代码语言:javascript
复制
ogr2ogr -f GeoJSON -clipsrc cutting.json output.json input.json

Python实现:

代码语言:javascript
复制
import subprocess
subprocess.call(["ogr2ogr", "-f", "GeoJSON", "-clipsrc", "cutting.json", "output.json", "input.json"], shell=True)

但是,python脚本输出:FAILURE: no target datasource provided

从文档中,我可以看到语法是正确的,并且我以正确的顺序指定了源数据源和目标数据源。当然,由于它在命令行上工作,所以我倾向于认为数据方向是正确的。我是不是不正确地调用子进程?

作为参考,ogr2ogr的使用:

代码语言:javascript
复制
Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update]
               [-select field_list] [-where restricted_where]
               [-progress] [-sql <sql statement>] [-dialect dialect]
               [-preserve_fid] [-fid FID]
               [-spat xmin ymin xmax ymax] [-geomfield field]
               [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]
               [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...]
               dst_datasource_name src_datasource_name
               [-lco NAME=VALUE] [-nln name] [-nlt type] [-dim 2|3|layer_dim] [layer [layer ...]]

Advanced options :
               [-gt n]
               [-clipsrc [xmin ymin xmax ymax]|WKT|datasource|spat_extent]
               [-clipsrcsql sql_statement] [-clipsrclayer layer]
               [-clipsrcwhere expression]
               [-clipdst [xmin ymin xmax ymax]|WKT|datasource]
               [-clipdstsql sql_statement] [-clipdstlayer layer]
               [-clipdstwhere expression]
               [-wrapdateline][-datelineoffset val]
               [[-simplify tolerance] | [-segmentize max_dist]]
               [-addfields]
               [-relaxedFieldNameMatch]
               [-fieldTypeToString All|(type1[,type2]*)] [-unsetFieldWidth]
               [-fieldmap identity | index1[,index2]*]
               [-splitlistfields] [-maxsubfields val]
               [-explodecollections] [-zfield field_name]
               [-gcp pixel line easting northing [elevation]]* [-order n | -tps]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-20 05:51:11

就我个人而言,我在基于*nix的机器上使用如下所示的内容来完全发送python脚本中的调用。也许它对你有帮助(或者你可以根据你的需要量身定做)。

代码语言:javascript
复制
import os, subprocess, time
name = 'ogr2ogr_{}'.format(time.time())
call = ['nohup', 'ogr2ogr', '-f', 'GeoJSON', '-clipsrc', 
        'cutting.json', 'output.json', 'input.json']
o_std = open(os.path.join('/my/log/dir', '{}.log'.format(name)), 'w')
o_err = open(os.path.join('/my/log/dir', '{}.err'.format(name)), 'w')
r = subprocess.Popen(call, stdout=o_std, stderr=o_err, preexec_fn=os.setpgrp)

如果不想分派子进程,只需删除nohup列表项,不要将os.setpgrp设置为preexec_fn

票数 2
EN

Stack Overflow用户

发布于 2017-10-20 14:57:23

我通常使用子进程来调用命令行,对于ogr2ogr,我可以通过在调用中添加shell=True选项来工作:

代码语言:javascript
复制
ProcessToCall = ['ogr2ogr'+" "+str(OutShp)+" "+str(InShp)+" "+'-dialect sqlite -sql "SELECT ST_Union(geometry) AS geometry FROM'+" "+str(InShp).split(".")[0]+'"']

subprocess.call(ProcessToCall, shell=True)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40145997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档