0

我坚信这个问题已经被问过了,但我找不到答案,所以我把它放在你面前。我在运行脚本以将 osm 文件转换为 shp 文件时遇到问题。该脚本正在读取所有 osm 文件,但只是在最后创建第一个 osm 文件的一个 shp 文件,而不是转换所有 osm 文件。我提供了我在下面使用的代码。所以请帮我解决这个问题。


from xml.dom import minidom
import os, sys
import xml.etree.ElementTree as ET

### ruta a gdal-data C:\Program Files (x86)\PostgreSQL\9.4\gdal-data

path = r"C:\Users\Administrator\Desktop\CHECKING\T2"

systemOutput = 'Shp'


print ("\n#### Execute python NY_osm2shapes")
print ("#### MONITORING CITIES")
print ("#### Conversor osm to shapes")
print ("#### OSM Path: " + path)

print "#### "
"""
Modify
Win: C:/Program Files/GDAL/gdal-data/osmconfig.ini
Linux: /usr/share/gdal/1.11/osmconfig.ini
report_all_ways=yes #activate lines without tag
attributes=landuse, plots #inside [lines]
attributes=landuse, plots #inside [multipolygons]
"""

### Check if path from argv
try:
    if len(sys.argv) >= 2:
       print("#### Path from argv: ", sys.argv[1])
       path = sys.argv[1]
    else:
       print "#### Path set to", path
       sys.exit()
except:
      pass


#### Ogr config
print "\n#### Process: osm to shapes"
ogrOutputType = ''      #-f "Esri Shapefile"'
ogrProjection = ''      # -t_srs EPSG:4326' #+ epsg
ogrProjectionA = ''     #-a_srs EPSG:3827'
ogrProjectionIn = ''    #-s_srs EPSG:3827' #-t_srs EPSG:4326

ogrConfigType = ' --config OSM_USE_CUSTOM_INDEXING NO'

ogr2ogr = 'ogr2ogr %s %s %s %s %s %s -overwrite %s %s %s  %s layer %s'

### Process
for l in os.walk(path):
    archivos = l[2]
    ruta = l[0]

for a in archivos:
    if a.endswith(".osm"):
        osmFile = os.path.join(ruta, a)
        folder = os.path.join(ruta, systemOutput)
        shapeFile = a[:-4]
        ogrFileOutput = " -nln " + shapeFile
        print "Archivo Shape: ", shapeFile,
        layerType = shapeFile[-1]


        if layerType=="0":
            print "\t TIPO 0: Circles"
            ogrSelectLayer = "lines"
            ogrLcoType = ' -lco SHPT=ARC'
            ogrSelect = ' -select ID_string'

        elif layerType == "1":
            print "\t TIPO 1: Blocks"
            ogrSelectLayer = "lines"
            ogrLcoType = ' -lco SHPT=ARC'
            ogrSelect = ' -select Land_use'

        elif layerType == "2":
            print "\t TIPO 2: Plots"
            ogrSelectLayer = "lines"
            ogrLcoType = ' -lco SHPT=ARC'
            ogrSelect = ' -select Plot'

        elif layerType == "3":
            print "\t TIPO 3: Medians"
            ogrSelectLayer = "lines"
            ogrLcoType = ' -lco SHPT=ARC'
            ogrSelect = ' -select ID_string'


        else:
            print "ELSE ERROR*"


        systemOutput = ogr2ogr % (ogrOutputType, folder, osmFile, ogrProjectionA, ogrProjectionIn, ogrProjection, ogrFileOutput, ogrLcoType, ogrConfigType, ogrSelect, ogrSelectLayer)

        #print ("Fichero: ", osmFile, shapeFile, layerType, ogrSelectLayer)

        os.system(systemOutput)
print "End process"
4

1 回答 1

0

您使用的方式os.walk返回遍历的最后一个树结构中的archivos所有osm文件。ruta这可能(至少部分)是您的问题,或者将来可能是这样。

你必须以os.walk不同的方式使用:

import os, re

ext_regx = '\.osm$'
archivos = []

for ruta, dirs, archs in os.walk( path ) :
    for arch in archs :
        if re.search( ext_regx, arch ) :
            archivos.append( os.path.join( ruta, arch ) )

for osmFile in archivos :
    print( osmFile )
    ...

现在,如果for循环内的代码没有按照您的意思执行,那是另一个问题。我建议你:

  1. 添加print( systemOutput )以检查执行的每个命令是否符合您的预期。
  2. 检查该命令中引用的文件和目录是否正确。

PS:中archivos的每个项目都已经包含 dir 部分,因此您必须split使用文件夹部分,而不是joining。

PS2:您可能需要对目录使用双反斜杠。另外,请记住os.sep

于 2015-07-08T08:23:46.530 回答