Python SimpleKML newpoint坐标替换

2024-07-08 11:19:45 发布

您现在位置:Python中文网/ 问答频道 /正文

使用pythonsimplekml库并在替换点(坐标)值时遇到问题。在

以下是来自网站的代码示例:

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")

我尝试下面的方法,但每次都失败。在

^{pr2}$

它抛出以下错误:

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无法正确呈现。我似乎不知道如何在不引起错误的情况下传递经度/纬度值。在

所以我想我可以把point变成一个字符串来修复它:

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

Tags: inpyselfformatpackageslinelibrarysite
1条回答
网友
1楼 · 发布于 2024-07-08 11:19:45

coords参数设为list。要执行此操作,请使用

pnt.coords = [point]

或者直接在newpoint构造函数中传递它

^{pr2}$

如果它需要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

相关问题 更多 >

    热门问题