重现“在 python 中创建具有点和线的 kml”问题邮政

发布于 2025-01-17 02:50:09 字数 1047 浏览 2 评论 0原文

我正在尝试复制我在下面的链接中遇到的帖子,但出现了一条错误消息,我有点不确定如何解决: 链接到之前的帖子我'我尝试复制

我在以下行中收到错误:

coord = (row[5], row[6]) # lon, lat order

错误消息显示IndexError:字符串索引超出范围。我正在调用该列有我的纬度和经度的数字,即 5 & 6. 知道这个错误消息指的是什么吗?

这是我此时的脚本:

import geopandas as gpd
import simplekml
kml = simplekml.Kml()


inputfile = gpd.read_file("C:/Users/CombineKMLs_AddLabels/Data/ScopePoles.shp") 
points = []
for row in inputfile:
    coord = (row[5], row[6]) # lon, lat order
    pnt = kml.newpoint(name=row[2], coords=[coord])
    points.append(coord)    
    pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_square.png'

ls = kml.newlinestring(name='A LineString')
ls.coords = np.array(points)
ls.altitudemode = simplekml.AltitudeMode.relativetoground
ls.extrude = 1

kml.save("C:/Users/CombineKMLs_AddLabels/Data/PolesandLines.shp")

I'm trying to replicate a post I came across in the link below and had an error message come up I'm a bit unsure how to resolve:
Link to Prior Post I'm trying to replicate

I'm getting an error on the following line:

coord = (row[5], row[6]) # lon, lat order

The error message reads IndexError: string index out of range. I'm calling the column numbers that have my lat and long, which is 5 & 6. Any idea what this error message is referring to?

Here's the script I have at this point:

import geopandas as gpd
import simplekml
kml = simplekml.Kml()


inputfile = gpd.read_file("C:/Users/CombineKMLs_AddLabels/Data/ScopePoles.shp") 
points = []
for row in inputfile:
    coord = (row[5], row[6]) # lon, lat order
    pnt = kml.newpoint(name=row[2], coords=[coord])
    points.append(coord)    
    pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_square.png'

ls = kml.newlinestring(name='A LineString')
ls.coords = np.array(points)
ls.altitudemode = simplekml.AltitudeMode.relativetoground
ls.extrude = 1

kml.save("C:/Users/CombineKMLs_AddLabels/Data/PolesandLines.shp")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木格 2025-01-24 02:50:09

使用 df.iterrows() 迭代数据框,使用 df.loc[index, 'geometry'] 访问点。

import geopandas as gpd
import simplekml

kml = simplekml.Kml()

df = gpd.read_file("C:/Users/CombineKMLs_AddLabels/Data/ScopePoles.shp")

points = []
for index, poi in df.iterrows():
  pt = df.loc[index, 'geometry']
  coord = (pt.x, pt.y)
  pnt = kml.newpoint(name=index, coords=[coord])
  points.append(coord)
  pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_square.png'

ls = kml.newlinestring(name='A LineString', coords=points)

kml.save('PolesandLines.kml')

Use df.iterrows() to iterate over the data frame and df.loc[index, 'geometry'] to access the point.

import geopandas as gpd
import simplekml

kml = simplekml.Kml()

df = gpd.read_file("C:/Users/CombineKMLs_AddLabels/Data/ScopePoles.shp")

points = []
for index, poi in df.iterrows():
  pt = df.loc[index, 'geometry']
  coord = (pt.x, pt.y)
  pnt = kml.newpoint(name=index, coords=[coord])
  points.append(coord)
  pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/placemark_square.png'

ls = kml.newlinestring(name='A LineString', coords=points)

kml.save('PolesandLines.kml')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文