本文整理汇总了Python中networkx.read_shp函数的典型用法代码示例。如果您正苦于以下问题:Python read_shp函数的具体用法?Python read_shp怎么用?Python read_shp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read_shp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_shp
def load_shp(shp_path):
""" loads a shapefile into a networkx based GeoGraph object
Args:
shp_path: string path to a line or point shapefile
Returns:
geograph: GeoGraph
"""
# NOTE: if shp_path is unicode io doesn't work for some reason
shp_path = shp_path.encode('ascii', 'ignore')
g = nx.read_shp(shp_path)
coords = dict(enumerate(g.nodes()))
driver = ogr.GetDriverByName('ESRI Shapefile')
shp = driver.Open(shp_path)
layer = shp.GetLayer()
spatial_ref = layer.GetSpatialRef()
proj4 = None
if not spatial_ref:
if gm.is_in_lon_lat(coords):
proj4 = gm.PROJ4_LATLONG
else:
warnings.warn("Spatial Reference could not be set for {}".
format(shp_path))
else:
proj4 = spatial_ref.ExportToProj4()
g = nx.convert_node_labels_to_integers(g)
return GeoGraph(srs=proj4, coords=coords, data=g)
示例2: buildNetwork
def buildNetwork(self):
'''Method to create NetworkX nodes and edges shapefiles.'''
try:
layer = str(self.filelist[self.ui.comboBoxInput.currentIndex()])
if layer == "Available layers:":
raise IOError, "Please specify input shapefile layer."
DG1 = nx.read_shp(layer)
if str(self.ui.lineEditSave.text()) == '':
raise IOError, "No output directory specified."
WriteNetworkShapefiles(DG1, self.fd, overwrite=True)
#self.writeNetworkShapefiles(DG1)
if self.ui.checkBoxAdd.isChecked():
# Get created files
nodes = self.fd+"/nodes.shp"
edges = self.fd+"/edges.shp"
# Add to QGIS instance
qgis.utils.iface.addVectorLayer(edges, "Network Edges", "ogr")
qgis.utils.iface.addVectorLayer(nodes, "Network Nodes", "ogr")
self.close()
except (AttributeError, IOError) as e:
QtGui.QMessageBox.warning( self, "NetworkX Plugin Error",
"%s" % e)
示例3: from_files
def from_files(cls, shp, csv, **kwargs):
"""
Parameters
----------
shp : file or string (File, directory, or filename to read).
csv : string or file handle / StringIO.
Example
----------
NetworkPlan.from_files('networks-proposed.shp',
'metrics-local.csv')
"""
# Only supports longlat format for now
# with fiona.open(shp) as shapefile:
# # Pass along the projection
# if 'proj' in shapefile.crs:
# kwargs['proj'] = shapefile.crs['proj']
# Ignore the PROJ.4 header if there
skip_rows = 0
with open(csv) as csv_stream:
if csv_stream.readline().startswith('PROJ.4'):
skip_rows = 1
# networkx read_shp fails on unicode paths, so try ascii
if isinstance(shp, unicode):
shp = shp.encode("ascii")
return cls(nx.read_shp(shp), pd.read_csv(csv, skiprows=skip_rows), **kwargs)
示例4: test_geometryexport
def test_geometryexport(self):
def testgeom(lyr, expected):
feature = lyr.GetNextFeature()
actualwkt = []
while feature:
actualwkt.append(feature.GetGeometryRef().ExportToWkt())
feature = lyr.GetNextFeature()
assert_equal(sorted(expected), sorted(actualwkt))
expectedpoints = (
"POINT (1 1)",
"POINT (2 2)",
"POINT (3 3)",
"POINT (0.9 0.9)",
"POINT (4 2)"
)
expectedlines = (
"LINESTRING (1 1,2 2)",
"LINESTRING (2 2,3 3)",
"LINESTRING (0.9 0.9,4 2)"
)
tpath = os.path.join(tempfile.gettempdir(),'shpdir')
G = nx.read_shp(self.shppath)
nx.write_shp(G, tpath)
shpdir = ogr.Open(tpath)
testgeom(shpdir.GetLayerByName("nodes"), expectedpoints)
testgeom(shpdir.GetLayerByName("edges"), expectedlines)
示例5: testload
def testload(self):
expected = nx.DiGraph()
for p in self.paths:
expected.add_path(p)
actual = nx.read_shp(self.shppath)
assert_equal(sorted(expected.node), sorted(actual.node))
assert_equal(sorted(expected.edges()), sorted(actual.edges()))
示例6: testload
def testload(self):
expected = nx.DiGraph()
map(expected.add_path, self.paths)
G = nx.read_shp(self.shppath)
assert_equal(sorted(expected.node), sorted(G.node))
assert_equal(sorted(expected.edges()), sorted(G.edges()))
names = [G.get_edge_data(s,e)['Name'] for s,e in G.edges()]
assert_equal(self.names, sorted(names))
示例7: test_geometryexport
def test_geometryexport(self):
expectedpoints_simple = (
"POINT (1 1)",
"POINT (2 2)",
"POINT (3 3)",
"POINT (0.9 0.9)",
"POINT (4 2)"
)
expectedlines_simple = (
"LINESTRING (1 1,2 2)",
"LINESTRING (2 2,3 3)",
"LINESTRING (0.9 0.9,4.0 0.9,4 2)"
)
expectedpoints = (
"POINT (1 1)",
"POINT (2 2)",
"POINT (3 3)",
"POINT (0.9 0.9)",
"POINT (4.0 0.9)",
"POINT (4 2)"
)
expectedlines = (
"LINESTRING (1 1,2 2)",
"LINESTRING (2 2,3 3)",
"LINESTRING (0.9 0.9,4.0 0.9)",
"LINESTRING (4.0 0.9,4 2)"
)
tpath = os.path.join(tempfile.gettempdir(), 'shpdir')
G = nx.read_shp(self.shppath)
nx.write_shp(G, tpath)
shpdir = ogr.Open(tpath)
self.checkgeom(shpdir.GetLayerByName("nodes"), expectedpoints_simple)
self.checkgeom(shpdir.GetLayerByName("edges"), expectedlines_simple)
# Test unsimplified
# Nodes should have additional point,
# edges should be 'flattened'
G = nx.read_shp(self.shppath, simplify=False)
nx.write_shp(G, tpath)
shpdir = ogr.Open(tpath)
self.checkgeom(shpdir.GetLayerByName("nodes"), expectedpoints)
self.checkgeom(shpdir.GetLayerByName("edges"), expectedlines)
示例8: testload
def testload(self):
def compare_graph_paths_names(g, paths, names):
expected = nx.DiGraph()
for p in paths:
expected.add_path(p)
assert_equal(sorted(expected.node), sorted(g.node))
assert_equal(sorted(expected.edges()), sorted(g.edges()))
g_names = [g.get_edge_data(s, e)['Name'] for s, e in g.edges()]
assert_equal(names, sorted(g_names))
# simplified
G = nx.read_shp(self.shppath)
compare_graph_paths_names(G, self.simplified_paths, \
self.simplified_names)
# unsimplified
G = nx.read_shp(self.shppath, simplify=False)
compare_graph_paths_names(G, self.paths, self.names)
示例9: shp_to_graph
def shp_to_graph(shp_path):
graph_shp = nx.read_shp(str(shp_path))
shp = QgsVectorLayer(shp_path, "network", "ogr")
# parallel edges are excluded of the graph because read_shp does not return a multi-graph, self-loops are included
#self_loops = [[feat.id(), feat.geometry().asPolyline()[0],feat.attributes()]for feat in shp.getFeatures() if feat.geometry().asPolyline()[0] == feat.geometry().asPolyline()[-1] ]
# parallel_edges =
graph = graph_shp.to_undirected(reciprocal=False)
#column_names = [i.name() for i in shp.pendingFields()]
#for i in self_loops:
# graph.add_edge(i[1],i[1],dict(zip(column_names,i[2])))
return graph
示例10: __init__
def __init__(self, shapefile, edge_weighted_by_distance=True):
g = nx.read_shp(shapefile)
mg = max(nx.connected_component_subgraphs(g.to_undirected()), key=len)
if edge_weighted_by_distance:
for n0, n1 in mg.edges_iter():
path = np.array(json.loads(mg[n0][n1]['Json'])['coordinates'])
distance = np.sum(
greate_circle_distance(path[1:,0],path[1:,1], path[:-1,0], path[:-1,1])
)
mg.edge[n0][n1]['distance'] = distance
self.graph = mg
self._cache = {}
self._cache_nn = {}
示例11: test_attributeexport
def test_attributeexport(self):
def testattributes(lyr, expected):
feature = lyr.GetNextFeature()
actualvalues = []
while feature:
actualvalues.append(feature.GetGeometryRef().ExportToWkt())
feature = lyr.GetNextFeature()
assert_equal(sorted(expected), sorted(actualvalues))
tpath = os.path.join(tempfile.gettempdir(),'shpdir')
G = nx.read_shp(self.shppath)
nx.write_shp(G, tpath)
shpdir = ogr.Open(tpath)
nodes = shpdir.GetLayerByName("nodes")
示例12: mappingOneDay
def mappingOneDay():
for i in range(13340):
try:
g = nx.read_shp('./Graph/edges.shp')
G = nx.Graph()
G.add_edges_from(g.edges())
filename = './data/' + str(i) + '.txt'
Graph = MapReader().mapGraph
mappingOneTaxi(filename, Graph, G)
del g
del G
except Exception,e:
fp = open('log_taxi.txt','a')
fp.write(str(e)+'\n'+str(i)+'.txt'+'\n')
fp.close()
示例13: read_shp_to_graph
def read_shp_to_graph(shp_path):
graph_shp = nx.read_shp(str(shp_path), simplify=True)
shp = QgsVectorLayer(shp_path, "network", "ogr")
graph = nx.MultiGraph(graph_shp.to_undirected(reciprocal=False))
# parallel edges are excluded of the graph because read_shp does not return a multi-graph, self-loops are included
all_ids = [i.id() for i in shp.getFeatures()]
ids_incl = [i[2]['feat_id'] for i in graph.edges(data=True)]
ids_excl = set(all_ids) - set(ids_incl)
request = QgsFeatureRequest().setFilterFids(list(ids_excl))
excl_features = [feat for feat in shp.getFeatures(request)]
ids_excl_attr = [[i.geometry().asPolyline()[0], i.geometry().asPolyline()[-1], i.attributes()] for i in excl_features]
column_names = [i.name() for i in shp.dataProvider().fields()]
for i in ids_excl_attr:
graph.add_edge(i[0], i[1], attr_dict=dict(zip(column_names,i[2])))
return graph
示例14: test_missing_attributes
def test_missing_attributes(self):
G = nx.DiGraph()
A = (0, 0)
B = (1, 1)
C = (2, 2)
G.add_edge(A, B, foo=100)
G.add_edge(A, C)
nx.write_shp(G, self.path)
H = nx.read_shp(self.path)
for u, v, d in H.edges(data=True):
if u == A and v == B:
assert_equal(d['foo'], 100)
if u == A and v == C:
assert_equal(d['foo'], None)
示例15: test_attributeexport
def test_attributeexport(self):
def testattributes(lyr, graph):
feature = lyr.GetNextFeature()
while feature:
coords = []
ref = feature.GetGeometryRef()
last = ref.GetPointCount() - 1
edge_nodes = (ref.GetPoint_2D(0), ref.GetPoint_2D(last))
name = feature.GetFieldAsString('Name')
assert_equal(graph.get_edge_data(*edge_nodes)['Name'], name)
feature = lyr.GetNextFeature()
tpath = os.path.join(tempfile.gettempdir(), 'shpdir')
G = nx.read_shp(self.shppath)
nx.write_shp(G, tpath)
shpdir = ogr.Open(tpath)
edges = shpdir.GetLayerByName("edges")
testattributes(edges, G)