解析单个kml文件中的坐标,其中包含大量多边形

时间:2013-04-01 16:44:01

标签: python-2.7 kml

我有这个kml,文件很长,我设法从一个多边形获取坐标。但我的循环似乎没有转移到下一个多边形。有没有办法“指向”下一个多边形,所以我可以从中获取坐标?感谢

P.S。我从stackoverflow中的一些问题的代码中获取了此代码的一部分

from pykml import parser
import os, zipfile, os.path
from zipfile import ZipFile as zip

mapCoords = [[]]

def extractAll(zipName):
    z = zip(zipName)
    for f in z.namelist():
        if f.endswith('/'):
             os.makedirs(f)
        else:
             z.extract(f)
    return f

doc = parser.parse(extractAll("temp_kmz")).getroot()

for Polygon in doc.Document.Folder.Placemark.MultiGeometry:
    a = 0
    mapCoords[a].append(doc.Document.Folder.Placemark.MultiGeometry.Polygon.outerBoundaryIs.LinearRing.coordinates)
    a+=1

print mapCoords

,这是kml文件的剪切

<MultiGeometry>
    <Polygon>
      <outerBoundaryIs><LinearRing><coordinates> 121.077285,14.626314 121.077006,14.626312 121.077009,14.626040 121.077287,14.626043 121.077285,14.626314</coordinates></LinearRing></outerBoundaryIs>
    </Polygon>
    <Polygon>
      <outerBoundaryIs><LinearRing><coordinates> 121.077285,14.626314 121.077563,14.626316 121.077561,14.626587 121.077282,14.626585 121.077285,14.626314</coordinates></LinearRing></outerBoundaryIs>
    </Polygon>
    <Polygon>
      <outerBoundaryIs><LinearRing><coordinates> 121.079236,14.626059 121.079234,14.626330 121.079512,14.626332 121.079510,14.626603 121.079231,14.626601 121.078953,14.626599 121.078955,14.626328 121.078958,14.626057 121.079236,14.626059</coordinates></LinearRing></outerBoundaryIs>
    </Polygon>
    <Polygon>
      <outerBoundaryIs><LinearRing><coordinates> 121.077561,14.626587 121.077839,14.626590 121.077837,14.626861 121.077834,14.627132 121.077556,14.627129 121.077558,14.626858 121.077561,14.626587</coordinates></LinearRing></outerBoundaryIs>
    </Polygon>
    <Polygon>
      <outerBoundaryIs><LinearRing><coordinates> 121.125000,14.627245 121.125000,14.627516 121.124887,14.627515 121.124890,14.627244 121.125000,14.627245</coordinates></LinearRing></outerBoundaryIs>
    </Polygon>

1 个答案:

答案 0 :(得分:0)

问题在于如何调用MultiGeometry的属性,你必须调用如下:

for Poly in doc.Document.Folder.Placemark.MultiGeometry.Polygon:

      mapCoords[a].append(Poly.outerBoundaryIs.LinearRing.coordinates)

with“Polygon”