2017-10-04 61 views
0

这很复杂,但长话短说:我已经使用了几个类似OSMN的库来绘制城市的几个景点之间的路线。现在我将它转换为一个shp文件。Shapely:不带逗号的元组与LineString

该路由是一个充满节点ID的列表。然后这些id用于提取每个节点的纬度和经度。我做了元组与来连接各个节点对(一个开始,一个到达)的坐标循环,这样的:在循环结束

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 

这里的打印(旅程)的结果:

[(6.15815, 48.6996136), (6.1629696, 48.7007431), (6.1629696, 48.7007431), [...], (6.1994411, 48.6768434), (6.1994411, 48.6768434), (6.1995322, 48.6767583)] 

每个元组都显示正确。但是,当我想旅途转换成一个身材匀称的线段形式...而它返回:

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 (most recent call last) in() 4 } 5 ----> 6 with fiona.open('test.shp', 'w', 'ESRI Shapefile', schema) as c: 7 c.write({ 8 'geometry': mapping(trace)

/usr/local/lib/python3.5/dist-packages/fiona/init.py in open(path, mode, driver, schema, crs, encoding, layer, vfs, enabled_drivers, crs_wkt) 173 c = Collection(path, mode, crs=crs, driver=driver, schema=this_schema, 174 encoding=encoding, layer=layer, vsi=vsi, archive=archive, --> 175 enabled_drivers=enabled_drivers, crs_wkt=crs_wkt) 176 else: 177 raise ValueError(

/usr/local/lib/python3.5/dist-packages/fiona/collection.py in init(self, path, mode, driver, schema, crs, encoding, layer, vsi, archive, enabled_drivers, crs_wkt, **kwargs) 154 elif self.mode in ('a', 'w'): 155 self.session = WritingSession() --> 156 self.session.start(self, **kwargs) 157 except IOError: 158 self.session = None

fiona/ogrext.pyx in fiona.ogrext.WritingSession.start (fiona/ogrext2.c:16207)()

TypeError: argument of type 'int' is not iterable

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) 

因此,我不能使用菲奥娜转换的轴马力我不明白为什么元组在经纬度之间没有逗号转换。此外,还有几个重复(第三行的第二个坐标是第四行的第一个坐标,等等),也许它可能是未来shp的错误来源。

在此先感谢!

+0

什么,当你做你所看到的'print(final_journey)'是你的行的[Well Known Text](https://en.wikipedia.org/wiki/Well-known_text)表示。这没什么错(例如'没有逗号的元组'),它只是在解释器中几何地显示几何图形。 – mgc

回答

1

我不认为获得节点的坐标,然后将它们连接在一起是您可以做的最好的事情。如果街道不直? OSMnx为您提供了街道的精确几何形状。为了提取节点的几何结构,更好的解决方案被解释为here。但你需要几何街道。由于两个节点之间可能存在多条边,因此并不总是很简单。我认为ox.get_route_edge_attributes()应该能够做到这一点,而且如果您要求其他属性(例如highway),但确实非常适用,但不能用于提取边缘的geometry。原因(我猜)是不是G中的所有边都有geometry,但是如果你得到了网络的gdf_edges,那么你总是有每条边的几何形状。以下是解决办法,我发现:

gdf_nodes, gdf_edges = ox.graph_to_gdfs(G) 
path = nx.shortest_path(G, G.nodes()[0], G.nodes()[1]) 

要在路由中得到GeoDataFrame节点:

output_points = gdf_nodes.loc[path] 
output_points.plot(color='red') 

enter image description here

,并获得边缘的几何形状第一组的元组u,v值作为gdf_edges的索引,然后loc来选择路径的GeoDataFrame:

gdf_edges.index = gdf_edges.apply(lambda row: (row.u, row.v), axis=1) 
output_lines = gdf_edges.loc[list(zip(path[:-1], path[1:]))] 
output_lines.plot(color='red') 

enter image description here

然后你可以将它保存到shape文件:

output_edges.to_file() 

一个重要的一句话:我已经说了,可以有两个节点之间的多个边缘。这意味着为了唯一标识边缘,uv(边缘的开始和结束)是不够的,您还需要key,它是由OSMnx自动添加的,您可以在图形和gdf_edges中为每个边缘找到它。所以如果你使用上面的代码,请注意它会给你all节点之间的边缘(如果有多个)。快速检查是:len(np.nonzero(output_edges['key'])[0])。如果没有平行边缘,这肯定是零。如果不是,则意味着存在平行边缘。

UPDATE

OSMnx有一个功能,可以节省shape文件从GeoDataFrame:

p = '/path/to/destination/' 
ox.save_gdf_shapefile(output_lines, 'output', p) 

添加架构to_file()似乎也工作:

sch = {'geometry': 'LineString', 
     'properties': {'key': 'int', 
         'u': 'int', 
         'v': 'int'}} 

test = gpd.GeoDataFrame(output_lines[['u', 'v', 'key']], 
         geometry=output_lines['geometry'], crs='+init=EPSG:3740') 

test.to_file('/path/to/destination/test.shp', schema=sch) 
+0

嗨Alireza!刚刚测试过你的解决方案,它的工作正常,直到output_edges.to_file()步骤(我不确定这是好变量,我认为这对output_lines更好)。我必须提交一个像我的问题那样的模式吗?提前致谢 ! – Raphadasilva

+0

@Raphadasilva检查更新。 –

+0

很好,我验证你的答案!再次感谢 – Raphadasilva

相关问题