Jul-26-2019, 06:35 AM
I have two point layers, one with bus stops and train stations, and another with playground centrepoints.
From QGIS I created a cleaned up network containing roads and footpaths, which I have converted to a multidigraph in Python. This so I can loop through each bus/train station and find the network to the playground point, if there is one.
The problem is trying to get the closest edge node to the playground spits out an error.
The error I receive is
I snapped all linework to itself to keep it as simple as possible. When I test the type it says it's a multidigraph.
Yes you can use QGIS shortest path tools but only if you have one end point or one start point. This has many start and end points so I will be looping through each.
From QGIS I created a cleaned up network containing roads and footpaths, which I have converted to a multidigraph in Python. This so I can loop through each bus/train station and find the network to the playground point, if there is one.
The problem is trying to get the closest edge node to the playground spits out an error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
path = "C:\\........\\Footpath_Roads_Extended_Python.shp" def read_multi_shp(path): #create multidigraph """ copied from read_shp, but allowing MultiDiGraph instead. """ try : from osgeo import ogr except ImportError: net = nx.MultiDiGraph() def getfieldinfo(lyr, feature, flds): f = feature return [f.GetField(f.GetFieldIndex(x)) for x in flds] def addlyr(lyr, fields): for findex in range (lyr.GetFeatureCount()): f = lyr.GetFeature(findex) flddata = getfieldinfo(lyr, f, fields) g = f.geometry() attributes = dict ( zip (fields, flddata)) attributes[ "ShpName" ] = lyr.GetName() if g.GetGeometryType() = = 1 : # point net.add_node((g.GetPoint_2D( 0 )), attributes) if g.GetGeometryType() = = 2 : # linestring attributes[ "Wkb" ] = g.ExportToWkb() attributes[ "Wkt" ] = g.ExportToWkt() attributes[ "Json" ] = g.ExportToJson() last = g.GetPointCount() - 1 net.add_edge(g.GetPoint_2D( 0 ), g.GetPoint_2D(last), attr_dict = attributes) if isinstance (path, str ): shp = ogr. Open (path) lyrcount = shp.GetLayerCount() for lyrindex in range (lyrcount): lyr = shp.GetLayerByIndex(lyrindex) flds = [x.GetName() for x in lyr.schema] addlyr(lyr, flds) return net H = read_multi_shp(path) PTV_Playground_Shortest_Path_300_400 = [] while i < len (PTV_Playground_Joined_initial): #loop through playgrounds and find nearest path node orig_xy = (df.loc[i, "PTV_N" ], "-" + df.loc[i, "PTV_E" ]) target_xy = (df.loc[i, "Playgroun1" ], df.loc[i, "Playground" ]) orig_node = ox.get_nearest_node(H, orig_xy, method = 'euclidean' ) #H seems to cause the error................. |
1 2 3 4 5 6 7 8 |
Traceback (most recent call last): File "C:\Users\geogjo\Desktop\PythonTraining\Network_Analysis_Test.py" , line 100 , in <module> orig_node = ox.get_nearest_node(H, orig_xy, method = 'euclidean' ) File "C:\Users\geogjo\AppData\Local\Continuum\anaconda3\lib\site-packages\osmnx\utils.py" , line 461 , in get_nearest_node coords = [[node, data[ 'x' ], data[ 'y' ]] for node, data in G.nodes(data = True )] File "C:\Users\geogjo\AppData\Local\Continuum\anaconda3\lib\site-packages\osmnx\utils.py" , line 461 , in <listcomp> coords = [[node, data[ 'x' ], data[ 'y' ]] for node, data in G.nodes(data = True )] KeyError: 'x' |
Yes you can use QGIS shortest path tools but only if you have one end point or one start point. This has many start and end points so I will be looping through each.