かなり複雑ですが、長いストーリーは短いです。私はOSMNxのようないくつかのライブラリを使って、都市のいくつかのスポットの間のルートを描きました。今私はそれを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
ここで印刷(旅)の結果は、ループの終わりです:
私はこのように、forループで、各ノードのカップル(1つのスタート、1つの到着)の座標を連結するタプルを作りました[(6.15815, 48.6996136), (6.1629696, 48.7007431), (6.1629696, 48.7007431), [...], (6.1994411, 48.6768434), (6.1994411, 48.6768434), (6.1995322, 48.6767583)]
各タプルは正しく表示されます。しかし、私は...のすっきりラインストリングで旅を変換したいそして、それはこの返すとき:
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でそれを変換することはできません。
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
を私はタプルが緯度と経度の間にカンマなしで変換される理由を理解していません。さらに、いくつかの重複があります(3行目の2番目の座標は4行目の最初の座標などです)、将来のshpのエラーの原因になる可能性があります。
ありがとうございます!
'print(final_journey)'はあなたの行の[よく知られているテキスト](https://en.wikipedia.org/wiki/Well-known_text)表現です。これには何も問題はありません(カンマなしのタプルなど)。インタプリタ内のジオメトリをどのように整然と表示するかです。 – mgc