[ create a new paste ] login | about

Link: http://codepad.org/Cm2BV5mp    [ raw code | fork ]

Python, pasted on Jan 25:
class Network(networkx.Graph):
    """Basic utility network object   
    """
    polygons=[]#store the polygons found 
    pointsWithin = []#store the points found within the polygons
    def loadrooms(self, path, ogr):
        
        def getfieldinfo(lyr, feature, flds):
                f = feature
                return [f.GetField(f.GetFieldIndex(x)) for x in flds]

        def addlyr(lyr, fields):
            for findex in xrange(lyr.GetFeatureCount()):
                f = lyr.GetFeature(findex)
                flddata = getfieldinfo(lyr, f, fields)
                g = f.geometry()
                if g.GetGeometryType() == 3 : #polygon
                    Network.polygons.append(f)#store all the polygon geometry features in a list
           
        if isinstance(path, str):
            shp = ogr.Open(path)
            lyrcount = shp.GetLayerCount()
            for lyrindex in xrange(lyrcount):
                lyr = shp.GetLayerByIndex(lyrindex)
                flds = [x.GetName() for x in lyr.schema]
                addlyr(lyr, flds)

    def loadlines(self, path, ogr):
        """Load a shapefile into the network.
        """
        def getfieldinfo(lyr, feature, flds):
                f = feature
                return [f.GetField(f.GetFieldIndex(x)) for x in flds]
                
        def addlyr(lyr, fields):
            for findex in xrange(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() == 2 : #linestring

                    for j in xrange(g.GetPointCount()-1):#iterate through all the points of the CURRENT  linestring
                        attributes["weight"] = distance(g.GetPoint(j), g.GetPoint(j+1))
                        self.add_edge(g.GetPoint(j), g.GetPoint(j+1), attributes)
                        if j == 0 or j == g.GetPointCount(): #add a reference to the points list of the first and the last
                            for x in xrange(len(Network.polygons)):#check that point (first or last of the linestring) against all the polygon objects saved in polygons list.
                                if f.GetGeometryRef().Within(Network.polygons[x].GetGeometryRef()): 
                                    g = f.geometry()
                                    Network.pointsWithin.append(g.GetPoint())
                                    
                    
        if isinstance(path, str):
            shp = ogr.Open(path)
            lyrcount = shp.GetLayerCount()
            for lyrindex in xrange(lyrcount):
                lyr = shp.GetLayerByIndex(lyrindex)
                flds = [x.GetName() for x in lyr.schema]
                addlyr(lyr, flds)
            


Create a new paste based on this one


Comments: