1

I've been trying to improve one of a couple of scripts I'm writing to be comparable to the other script in performance speed which uses a different approach to tackle the same FGDB problem. I'm simply trying to find buildings within perennial waters.

It's been brought to my attention even though I'm trying to do an R-Tree via a bounding box spatial analysis, that I may doing it wrong by not doing a bulk import to R-Tree via a generator.

Some concerns I have is it seems I need to manually find each feature's minimum bounding rectangle and also utilize a generator manually defined (which I've copied from the R-Tree website).

My code below which is currently getting me nowhere:

def getMBR(feature):

    global maxX
    global minX
    global maxY
    global minY

    for currFeature in feature[1]['coordinates']:
        if currFeature:
            for x,y,z in currFeature:
                if (maxX == maxY):

                    maxX = x
                    minX = x

                    maxY = y
                    minY = y

                else:

                    maxX = max(maxX, x)
                    minX = min(minX, x)

                    maxY = max(maxY, y)
                    minY = min(minY, y)
    return


def generator_function(someData):

    for i, obj in enumerate(someData):
        yield(i, (obj.xmin, obj.ymin, obj.xmax, obj.ymax), obj)
    return


def applyRTree(layer1, layer2):
    with fiona.open(sourceDatabaseFile, 'r', layer=layer2) as sourceFile2:
        for water in sourceFile2:
            if (('ZI024_HYP' in water['properties']) and (water['properties']['ZI024_HYP'] == 1)):
                waterFeatures.append((water['id'], water['geometry'], water['properties']))
        sourceFile2.close()

    #from rtree import index
    w = index.Property()
    idx = index.Index(properties=w)
    for i, coords in enumerate(waterFeatures):
        getMBR(coords)
        idx.add(i,(minX, minY, maxX, maxY)) 
        maxX = maxY = minX = minY = float(0.0)        

    b = index.Property()
    idx2 = index.Index(properties=b)
    for i, coords in enumerate(buildingFeatures):
        getMBR(coords)
        idx2.add(i,(minX, minY, maxX, maxY))
        maxX = maxY = minX = minY = float(0.0)

    hits = idx.intersection(idx2, objects=True)

try: 

        for bldg in buildingLayerList:
            with fiona.open(sourceDatabaseFile, 'r', layer=bldg) as sourceFile1:
                for building in sourceFile1:
                    buildingFeatures.append((building['id'], building['geometry']))
                sourceFile1.close()

            #Use current list of buildings and compare against all hydrography layers
            for hydro in hydrographyLayerList:
                applyRTree(bldg, hydro)
2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.