Title / Description
Code import osgeo.ogr as ogr import networkx class Network(networkx.DiGraph): """Basic utility network object """ def load(self, path): """Load a shapefile into the network. """ def getfieldinfo(feature, flds): f = feature return [f.GetField(f.GetFieldIndex(x)) for x in flds] def addlyr(shp, fields): for findex in xrange(shp.GetFeatureCount()): f = shp.GetFeature(findex) fldinfo = getfieldinfo(f, fields) g = f.geometry() attributes = dict(zip(fields, fldinfo)) if g.GetGeometryType() == 1: #point self.add_node((g.GetPoint(0)), attributes) if g.GetGeometryType() == 2: #linestring last = g.GetPointCount() - 1 self.add_edge(g.GetPoint(0), g.GetPoint(last), attributes) if isinstance(path, str) and path.split(".")[-1] == "shp": shp = ogr.Open(path) lyrcount = shp.GetLayerCount() for lyrindex in xrange(lyrcount): lyr = shp.GetLayerByIndex(lyrcount - 1) flds = [x.GetName() for x in lyr.schema] addlyr(lyr, flds)
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code