Search code examples
pythongdalogr

Find indices of raster cells that intersect with a polygon


I want to get a list of indices (row,col) for all raster cells that fall within or are intersected by a polygon feature. Looking for a solution in python, ideally with gdal/ogr modules.

Other posts have suggested rasterizing the polygon, but I would rather have direct access to the cell indices if possible.


Solution

  • Since you don't provide a working example, it's bit unclear what your starting point is. I made a dataset with 1 polygon, if you have a dataset with multiple but only want to target a specific polygon you can add SQLStatement or where to the gdal.Rasterize call.

    Sample polygon

    geojson = """{"type":"FeatureCollection",
    "name":"test",
    "crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},
    "features":[
    {"type":"Feature","properties":{},"geometry":{"type":"MultiPolygon","coordinates":[[[[-110.254,44.915],[-114.176,37.644],[-105.729,36.41],[-105.05,43.318],[-110.254,44.915]]]]}}
    ]}"""
    

    Rasterizing

    Rasterizing can be done with gdal.Rasterize. You need to specify the properties of the target grid. If there is no predefined grid these could be extracted from the polygon itself

    ds = gdal.Rasterize('/vsimem/tmpfile', geojson, xRes=1, yRes=-1, allTouched=True,
                        outputBounds=[-120, 30, -100, 50], burnValues=1, 
                        outputType=gdal.GDT_Byte)
    mask = ds.ReadAsArray()
    ds = None
    gdal.Unlink('/vsimem/tmpfile')
    

    Converting to indices

    Retrieving the indices from the rasterized polygon can be done with Numpy:

    y_ind, x_ind = np.where(mask==1)