【发布时间】:2017-10-04 08:51:06
【问题描述】:
这很复杂,但长话短说:我使用了几个库,如 OSMNx,在城市的几个地点之间绘制了一条路线。现在我将其转换为 shp 文件。
路由是一个包含节点 ID 的列表。然后这些id用于提取每个节点的纬度和经度。我制作了元组,将每个节点的坐标(一个开始,一个到达)与一个 for 循环连接起来,如下所示:
journey = []
# previous list will contain tuples with coordinates of each node
for node1, node2 in zip(route[:-1], route[1:]):
parcours.append(tuple((G.node[noeud1]['x'], G.node[noeud1]['y']))) # we create a tuple with coordinates of start's node
parcours.append(tuple((G.node[noeud2]['x'], G.node[noeud2]['y']))) # then we make the same for the arrival node
这是循环结束时 print(journey) 的结果:
[(6.15815, 48.6996136), (6.1629696, 48.7007431), (6.1629696, 48.7007431), [...], (6.1994411, 48.6768434), (6.1994411, 48.6768434), (6.1995322, 48.6767583)]
每个元组都正确显示。但是当我想在一个匀称的 LineString 中转换旅程时......它会返回这个:
from shapely.geometry import LineString
final_journey = LineString(journey)
print(final_journey)
LINESTRING (6.15815 48.6996136, 6.1629696 48.7007431, 6.1629696 48.7007431, 6.1630717 48.7002871, [...], 6.1991794 48.677085, 6.1994411 48.6768434, 6.1994411 48.6768434, 6.1995322 48.6767583)
因此,我无法使用 fiona 将其转换为 shp:
import fiona
schema = {
'geometry': 'Polygon',
"properties": {'id': 123}
}
with fiona.open('test.shp', 'w', 'ESRI Shapefile', schema) as c:
c.write({
'geometry': mapping(trace)
})
----------------------------------- ---------------------------- TypeError Traceback(最近一次调用 最后)在() 4 } 5 ----> 6 with fiona.open('test.shp', 'w', 'ESRI Shapefile', schema) as c: 7 c.写({ 8 '几何':映射(轨迹)
/usr/local/lib/python3.5/dist-packages/fiona/init.py in open(path, 模式、驱动程序、模式、crs、编码、层、vfs、启用驱动程序、 crs_wkt) 第173章 174 编码=编码,层=层,vsi=vsi,存档=存档, --> 175 启用驱动程序=启用驱动程序,crs_wkt=crs_wkt) 176 其他: 第177章
/usr/local/lib/python3.5/dist-packages/fiona/collection.py 在 init(自我、路径、模式、驱动程序、架构、crs、编码、层、vsi、存档、启用驱动程序、crs_wkt、**kwargs) 154 elif self.mode in ('a', 'w'): 第155章 --> 156 self.session.start(self, **kwargs) 157 除了 IOError: 158 self.session = 无
fiona/ogrext.pyx in fiona.ogrext.WritingSession.start (fiona/ogrex2.c:16207)()
TypeError: 'int' 类型的参数不可迭代
我不明白为什么在纬度和经度之间没有逗号的情况下转换元组。此外,还有几个重复(第三行的第二个坐标是第四行的第一个坐标等),可能会成为未来shp的错误来源。
提前致谢!
【问题讨论】:
-
当您执行
print(final_journey)时看到的是您的行的Well Known Text 表示。这没有什么问题(例如“不带逗号的元组”),它只是在解释器中显示几何图形的方式。