【问题标题】:Python SimpleKML newpoint coordinates substitutionPython SimpleKML 新点坐标替换
【发布时间】:2015-07-13 07:50:08
【问题描述】:

使用 python SimpleKML 库并在替换我的点(坐标)值时遇到问题。

这是来自网站的代码示例:

import simplekml
kml = simplekml.Kml()
pnt = kml.newpoint(name='A Point')
pnt.coords = [(1.0, 2.0)]
pnt.style.labelstyle.color = simplekml.Color.red  # Make the text red
pnt.style.labelstyle.scale = 2  # Make the text twice as big
pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png'
kml.save("Point Styling.kml")

我尝试了以下方法,但每次都失败。

import simplekml
kml = simplekml.Kml()

a = range(10)
b = a

test = zip(a, b)

for point in test:
    pnt = kml.newpoint(name='Bogusname')
    pnt.coords = point

它会抛出以下错误:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/Library/Python/2.7/site-packages/simplekml/featgeom.py", line 1079, in coords
    self._kml['coordinates'].addcoordinates(coords)
  File "/Library/Python/2.7/site-packages/simplekml/coordinates.py", line 30, in addcoordinates
    if len(coord) == 2:
TypeError: object of type 'int' has no len()

我相信这归结为某种替代误解。如果我将这两个值串成一个以满足 1 参数要求,它会添加单引号,导致 kml 无法正确呈现。我似乎无法弄清楚如何在不导致错误的情况下传递经度/纬度值。

所以我想我可以通过将点变成一个字符串来解决它:

for i in test:
    pnt = kml.newpoint(name='Bogusname')
    pnt.coords = str(i)

但收到以下错误:

>>> kml.save("Point Shared Style.kml")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/simplekml/kml.py", line 285, in save
    out = self._genkml(format)
  File "/Library/Python/2.7/site-packages/simplekml/kml.py", line 198, in _genkml
    kml_str = self._feature.__str__()
  File "/Library/Python/2.7/site-packages/simplekml/featgeom.py", line 418, in __str__
    buf.append(feat.__str__())
  File "/Library/Python/2.7/site-packages/simplekml/featgeom.py", line 414, in __str__
    buf.append(super(Feature, self).__str__())
  File "/Library/Python/2.7/site-packages/simplekml/base.py", line 46, in __str__
    buf.append(u"{0}".format(val))  # Use the variable's __str__ as is
  File "/Library/Python/2.7/site-packages/simplekml/featgeom.py", line 1250, in __str__
    return '<Point id="{0}">{1}</Point>'.format(self._id, super(Point, self).__str__())
  File "/Library/Python/2.7/site-packages/simplekml/base.py", line 54, in __str__
    buf.append(u("<{0}>{1}</{0}>").format(var, val))  # Enclose the variable's __str__ with its name
  File "/Library/Python/2.7/site-packages/simplekml/coordinates.py", line 40, in __str__
    buf.append("{0},{1},{2}".format(cd[0], cd[1], cd[2]))
IndexError: string index out of range

【问题讨论】:

  • 如果你使用pnt.coords = [point]呢?我不确定结果应该是什么,但似乎coords(复数)期待某种可迭代或集合,而不是单一坐标。
  • 这也是我最初的想法,但失败并出现同样的错误。谷歌代码站点声明它需要一个浮点元组列表。我对如何确保发生这种情况很模糊。 [链接]code.google.com/p/simplekml/issues/…
  • &gt;&gt;&gt;type(test[0]) &lt;type 'tuple'&gt; &gt;&gt;&gt;
  • zip 负责创建tuples。我的回答显示了如何调整问题代码中的声明 range(10) 以获得浮点数列表。

标签: python variables substitution simplekml


【解决方案1】:

coords 参数设为list。为此,请使用

pnt.coords = [point]

或者只是在newpoint构造函数中传递它

kml.newpoint(name="Bogusname", coords=[point])

如果需要floats,您可以按如下方式创建示例浮点数据

a = [float(x) for x in range(10)]

完整示例

from simplekml import Kml

a = range(10)
test = zip(a, a)
kml = Kml(name='KmlUsage')

for coord in test:
    kml.newpoint(name='Bogusname', coords=[coord])  # A simple Point
print kml.kml()  # Printing out the kml to screen

【讨论】:

  • 这就是现在正在发生的事情...&gt;&gt;&gt; type(test) &lt;type 'list'&gt;
  • 但您将point 分配给pnt.coordspoint 是一个元组。如果你摆脱了循环并将test直接分配给pnt.coords应该没问题。
  • 做到了,哇...这个简单的答案让我整个人都没有找到答案。感谢您让我看到了显而易见的事实!
猜你喜欢
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 2016-01-14
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多