【问题标题】:Google KML file to python谷歌 KML 文件到 python
【发布时间】:2021-05-09 04:40:33
【问题描述】:

我有以下代码可以在 python 中访问 kml 文件的坐标

from pykml import parser
with open('test.kml', 'r') as kml_file:
root = parser.parse(kml_file).getroot()
for i in root.findall('{http://www.opengis.net/kml/2.2}Document/{http://www.opengis.net/kml/2.2}Placemark/{http://www.opengis.net/kml/2.2}Point'):
print(i.coordinates)

这个找到了我在 kml 文件中的所有单个点,我在其中标记了某些兴趣点。但我也有一些我在谷歌地球中创建的点多边形,这个算法不会返回它们。我怎样才能得到多边形?

如果您有任何问题,请告诉我。

【问题讨论】:

    标签: kml google-earth


    【解决方案1】:

    如果 KML 源文件包含带有地标的文档,则以下 Python 代码将使用多边形几何体遍历每个地标并转储坐标。

    from pykml import parser
    with open('test.kml', 'r') as f:
      root = parser.parse(f).getroot()
    namespace = {"kml": 'http://www.opengis.net/kml/2.2'}
    pms = root.xpath(".//kml:Placemark[.//kml:Polygon]", namespaces=namespace)
    for p in pms:
      print(p.Polygon.outerBoundaryIs.LinearRing.coordinates)
    

    如果 KML 使用带有一个或多个多边形的 MultiGeometry,则需要对 for 循环内的检查进行少量更改。

    for p in pms:
      if getattr(p, 'MultiGeometry'):
        for poly in p.MultiGeometry.Polygon:
          print(poly.outerBoundaryIs.LinearRing.coordinates)
      else:
        print(p.Polygon.outerBoundaryIs.LinearRing.coordinates)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 2013-05-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      相关资源
      最近更新 更多