Longtime GIS user, but a very new Networkx user. I want to find all routes between points within a polygon, but I do not want routes between points that go outside the polygon. Let's say I define the nodes as such:
import networkx as nxfrom matplotlib import pyplot as plotfrom itertools import combinationsG = nx.Graph()locations = [(1,1),(3,4),(3.1,-2),(6,-1),(7,4),(8,15),(8,29),(6,28),(5,25),(7,24),(5,21),(4,15),(3,13),(2,5)]pos = {} # build a dictionary that contains coordinates of each node in the graphfor i in xrange(len(locations)): pos = locations G.add_path(pos.keys()) G.add_path([min(G.nodes()),max(G.nodes())]) #this closes the last edge in the shapefileOkay, so there is an example of a polygon of interest. If I use nx.draw(G,pos) and view it with plot.show(), I get what I expect (the outline of this irregular polygon). So far so good.
Now, I can create all possible edges between points like so:
combos = combinations(pos.keys(),2)G.add_edges_from(combos)But when I do this, it produces all of the intra-polygon connections I want, but also the exterior ones I do not want. Is there a simple way in networkx to either a) avoid creating those or b) delete those. I will eventually need to be automating this process with thousand of polygons (after importing all their node coordinates)
أكثر...
import networkx as nxfrom matplotlib import pyplot as plotfrom itertools import combinationsG = nx.Graph()locations = [(1,1),(3,4),(3.1,-2),(6,-1),(7,4),(8,15),(8,29),(6,28),(5,25),(7,24),(5,21),(4,15),(3,13),(2,5)]pos = {} # build a dictionary that contains coordinates of each node in the graphfor i in xrange(len(locations)): pos = locations G.add_path(pos.keys()) G.add_path([min(G.nodes()),max(G.nodes())]) #this closes the last edge in the shapefileOkay, so there is an example of a polygon of interest. If I use nx.draw(G,pos) and view it with plot.show(), I get what I expect (the outline of this irregular polygon). So far so good.
Now, I can create all possible edges between points like so:
combos = combinations(pos.keys(),2)G.add_edges_from(combos)But when I do this, it produces all of the intra-polygon connections I want, but also the exterior ones I do not want. Is there a simple way in networkx to either a) avoid creating those or b) delete those. I will eventually need to be automating this process with thousand of polygons (after importing all their node coordinates)
أكثر...