pythonlistgdalshapefileosgeo

How to check in a list of shapefiles whether a point falls inside AND it is the same of a given FIELD?


I am drawing address points and checking if they are inside a large shapefile. However, now I also want to check if they fall inside type URBAN. This information comes in terms of a list of features in which one of the columns is TYPE. I pass if the point I want is urban or not (urban=True)

My code for the cointaining point:

def get_random_point_in_polygon(region, urban=None):
    while True:
        address = ogr.Geometry(ogr.wkbPoint)    
        address.AddPoint(random.uniform(region.get_region().geometry().GetEnvelope()[0],
                                    region.get_region().geometry().GetEnvelope()[1]),
                     random.uniform(region.get_region().geometry().GetEnvelope()[2],
                                    region.get_region().geometry().GetEnvelope()[3]))
        if region.get_region().geometry().Contains(address) and XXXXXXXXXX:
            return address

Region is a large shapefile Now I also have a list of other 52 features that are all inside region. They have a FIELD that contains the information URBAN or RURAL.

I want to fulfill my XXXXXX with a code that says: 'if address is inside any features in the list that FIELD = URBAN'

Any ideas? Something like:

any(x in a for x in b)

but for shapefiles...


Solution

  • I think I found a solution, although rather cumbersome. ShapesInput.urban is a list of features

    But, note that this code is extremely cost. And given that urban areas are much smaller than rural areas, it does take a long time.

    Well, it works...

    def get_random_point_in_polygon(region, urban=True):
        while True:
            address = ogr.Geometry(ogr.wkbPoint)
            address.AddPoint(random.uniform(region.get_region().geometry().GetEnvelope()[0],
                                        region.get_region().geometry().GetEnvelope()[1]),
                         random.uniform(region.get_region().geometry().GetEnvelope()[2],
                                        region.get_region().geometry().GetEnvelope()[3]))
            if region.get_region().geometry().Contains(address):
                if urban is True:
                    for item in ShapesInput.urban:
                        if item.geometry().Contains(address):
                            return address
                else:
                    return address