2016-10-20 78 views
0

我一直在試驗Python的子流程模塊。我想效仿以下,成功的命令:Python子流程公式

命令行:

ogr2ogr -f GeoJSON -clipsrc cutting.json output.json input.json 

Python實現:

import subprocess 
subprocess.call(["ogr2ogr", "-f", "GeoJSON", "-clipsrc", "cutting.json", "output.json", "input.json"], shell=True) 

然而,python腳本輸出: FAILURE: no target datasource provided

從文檔,我可以看到該語法是正確的,並且我正在指定源數據源和目標數據源 正確的順序。當然,由於它在命令行上工作,我傾向於認爲數據方向是正確的。我是否錯誤地調用子進程?

僅供參考,使用ogr2ogr

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] 
+1

是否有任何理由指定'shell = True'? – zondo

+0

不!隨意發佈作爲答案;) – LearningSlowly

+0

做類似'sub.call(['回聲',' - 幫助'],殼=真)'工程? – Rptk99

回答

2

我個人而言,使用類似的基於* NIX的機器下面完全派遣由python腳本調用。也許它可以幫助你(或者你可以量身定製它來滿足你的需求)。

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

0

我通常使用子過程調用命令行,對於ogr2ogr我有一些其中工程通過添加殼=真選項到呼叫:

ProcessToCall = [「ogr2ogr」 +」「+ STR(OutShp) +「」+ str(InShp)+「」+' - 方言sqlite -sql「選擇ST_Union(幾何體)AS幾何體FROM'+」「+ str(InShp).split(」。「)[0] +''' ] subprocess.call(ProcessToCall,shell = True)