# Tag Info

12

I might have found a nice Python solution referring to the very shapefile in the question, so I am posting it for future reference. import networkx as nx import matplotlib.pyplot as plt G=nx.read_shp('C:\Users\MyName\MyFolder\TEST.shp') pos = {k: v for k,v in enumerate(G.nodes())} X=nx.Graph() #Empty graph X.add_nodes_from(pos.keys()) #Add nodes ...

6

The harder initial problem to solve is co-registering your point data with your network. These data may have come from different sources, and so some positional error is to be expected. In the absence of more complex rules governing how points should be located in the network, you can use the closest point on the network to each origin/destination as shown ...

5

This has nothing to do with your Python version or your os. The Python Networkx module (nx_spatial is very old, use directly NetworkX: read_shp) do not cut the lines at each intersection and simplify the line geometries into start and end coordinates. Generates a networkx.DiGraph from shapefiles. Point geometries are translated into nodes, lines into ...

5

If all you need is the shortest path analysis, and if you don't have much experience with GIS, then you can simply use the RoadGraph Plugin. With it, you can use distance or time to calculate the shortest path. You can find some instructions in the QGIS User manual. Make sure your project is in the same Coordinate System that your road data, and that the ...

5

Steiner tree connects some(!) of the network's nodes (terminals) shown as selected nodes: However don't get over excited about this feature of networkX, there is a good reason they called it "approximation.steinertree.steiner_tree", e.g. tree length in below picture is 380 m (4%) less than in the first one: Computation of Steiner tree is ginormous task, it ...

4

4

It is not complicated. nx.read_shp uses ogr to read a shapefile , look at nx_shp.py line 78-88 then the script use a dictionary to add nodes to the Graph (lines 87-88, net.add_node((g.GetPoint_2D(0)), attributes)) First part of the script, ogr only shp = ogr.Open("network_pts.shp") for lyr in shp: fields = [x.GetName() for x in lyr.schema] ...

4

That will pretty much depend on how your shapefile is. Is it segmented (the lines break, when they encounter intersections)? I haven't used read_shp for networkx, so I'm not sure. I guess that read_shp might do some work trying to figure everything out and that might be related to a precision issue. That should be configurable, but it's not. Looking at ...

4

In your script: Why do you work with nodes and not directly edges (= LineString and you want the predicate LineString.contains(LineString)) ? Why Numpy here, you can use the simple zip command (edge_set = zip(conflu, edge_node)) ? You forgot a for loop (for lines and for edges With the example of How to get lines and nodes around the confluence point in a ...

4

There are many other solutions proposed in GIS SE to convert a shapefile into a graph with Networkx.. If you use the Networkx solution (nx.read_shp()), the original geometry and the field values are still present in the edge data (see How to calculate edge length in Networkx) Open the shapefile with GeoPandas for example import geopandas as gpd graph = ...

4

You may ned to investigate what is often called a medial axis transform and sometimes a skeletonization. From wiki/Medial_axis: The medial axis of a simple polygon is a tree whose leaves are the vertices of the polygon, and whose edges are either straight segments or arcs of parabolas. The medial axis together with the associated radius function of ...

4

You first need to define what you mean by shortest path. If you don't weight your graph (G), shortest path is simply the path that connects the nodes that passes through the fewest number of other nodes. If you want to incorporate the actual length of the lines, you need to create a weighted graph: # Compute weights weights = lengths of each link in idict....

4

You can skip the python part and use the plugin QNEAT3 which is available for QGIS3 (see Distance Matrix with 2 point shapefiles and one street network. It also works in your case offers multiple processing algorithms that produce origin-destination matrices (OD-Matrix) as line layer, table or csv file out of the box. It also supports n:n relations which ...

4

NetworkX all_shortest_paths or single_source_dijkstra You need to calculate all the shortest paths from your source and then summarize edges weights fro every path. Also I'm absolutely sure that there is much simplier way to do this because Dejkstra algorithm calculates all the paths in you graph to return a single one. So you dont need to calculate it ...

3

You have a serious problem with your Anaconda installation: All the modules were installed with conda or pip and osgeo/gdal is well supported in 3.6. In your case, why numpy 1.8* -> python 2.6* -> ? GDAL

3

see Inaccurate output (missing features) while reading a shapefile into networkx) and nx_spatial does not read all shapefile features Networkx generate a networkx.DiGraph with nodes without duplicates. import networkx as nx G = nx.read_shp('edges_length_stac.shp' The original LineStrings and the resulting nodes of the graph And the calculated distance ...

3

It seem to me that you don't understand the principles of Nextwokx (see nx_spatial does not read all shapefile features) Networkx generate a networkx.DiGraph from the shapefile, with nodes and edges and only these nodes and edges are iterables, not the Graph import networkx as nx G = nx.read_shp('mylines.shp') print(G.nodes()) [(1.0, 2.0), (3.0, 2.0), (0.0,...

3

QGIS 1.8 has a built-in class called qgis.networkanalysis, it has functions to tie points to lines and calculate shortest path.

2

Given that the graph is loading successfully and no paths are being found there are two possibilities that might cause this: NetworkX does not split lines when loading into the system as it simplifies edges to their start and end points and only where those points overlap are edges created. To fix this you will need to explode your lines, using something ...

2

When you read a shapefile in networkx with read_shp networkx simplifies the line to a start and end, though it keeps all the attributes, and a WKT and WKB representation of the feature for when you export the data again. To get the subset of the graph g based on the shortest path you can simply get the subraph: result_graph = g.subgraph(nx.shortest_path(g, ...

2

The minimum spanning tree is computed on the input graph. Your input graph is the star network from the distribution point to the three premises - it doesn't contain edges from the premises to the other premises, so the output MST can't have those links in it. You want to create a full graph with all edges between all the pairs of locations - distribution ...

2

This didn;t actually work for me (maybe because of updates), but below is the code that worked for me. Updated from the answers above. I post this as a followup from How to load a weighed shapefile in networkX So I have created a network with QGIS and OSM (openstreetmaps), and exported it into two files: nodes and edges using of shapefiles. However, I ...

2

Now, I'm not sure if this will help, but here's how I got gdal for Python installed recently. Note, that I use Homebrew, which will further complicate your system, and really, I don't recommend it! For future reference, pick one install system and stick to it. In order to install gdal 2.1 with Homebrew, you need to add the osgeo source. You can do: brew ...

2

This same thing occurs for me in ArcMap 10.4.1 Python 2.7.10 32bit and ArcGIS Server 10.5.1. Python 2.7.13 32bit. It occurs for Networkx 2.1 but does not occur for Networkx 1.7 The issue can be reproduced by creating a simple tool for ArcMap: import arcpy import networkx as nx G = nx.krackhardt_kite_graph() arcpy.AddMessage(G.nodes()) Like the original ...

2

Based upon what @FelixIP said I would check the junctions marked here: See if they are genuinely snapped or its not a multi-part shape which breaks network topology.

2

I tried to replicate your setup as best as I could. I downloaded a juncture of two winding secondary highways from OpenStreetMap, loaded them into GeoPandas in Python. For every pair of points I computed the physical distance between the two points and used this as edge weight. (So I have a complete graph, a graph with an edge between any two nodes. I had ...

2

A graph is said to be connected if there is a path between every pair of vertex. Therefore create a new full connected Graph with itertools import itertools Road = nx.read_shp('stac_graph.shp') # control Road2 = Road.to_undirected() nx.is_connected(Road2) False # new graph with path between every pair of vertex G = nx.Graph() # add original nodes G....

1

I have confirmed that in networkx 1.11, the keys of the network dictionary are the start node coordinates, not the start and end node segments as i would suspect. Though significant changes have been made to the read_shp method through 2.1, I don't see a change to this behavior. It would seem we'd want the begin/end even though that might not be unique ...

1

You could "manually" write your import function, instead of using write_shp() like mentioned in this answer to Counting number of lines connected to point? at "Creation of a Graph".

1

Use Anaconda - https://www.anaconda.com Open up terminal: conda install gdal

Only top voted, non community-wiki answers of a minimum length are eligible