4

I have a big raster file that I want to clip to 6 numpy arrays based on the polygons or shapefile. I have the shapefile and also the polygons as geopandas dataframe. Can anyone please help me how to do it using python (no arcpy)

4
  • did you see the py-gdal cookbook?
    – Val
    Apr 26, 2018 at 7:33
  • yes. i have 6 different polygons in my shapefile and i want to split my raster to 6 different numpy array each array corresponding to a polygon in the shapefile. How do I go it? Apr 26, 2018 at 7:38
  • Assuming your polygons are independent features, you could iterate over features and then clip the file using each feature in turn.
    – Val
    Apr 26, 2018 at 7:44
  • thanks. the polygons are independent features. what i dont know is how to iterate over features to clip the the big raster file. Would you be able to elaborate more on how to clip please Apr 26, 2018 at 7:54

1 Answer 1

5

I've created a little generator which should do what you need. I've opted for the generator instead of iterating over the features directly, because it's more convenient if you'd like to inspect the arrays. If you want, you can still iterate over the generator.

import gdal
import ogr, osr

# converts coordinates to index

def bbox2ix(bbox,gt):
    xo = int(round((bbox[0] - gt[0])/gt[1]))
    yo = int(round((gt[3] - bbox[3])/gt[1]))
    xd = int(round((bbox[1] - bbox[0])/gt[1]))
    yd = int(round((bbox[3] - bbox[2])/gt[1]))
    return(xo,yo,xd,yd)

def rasclip(ras,shp):
    ds = gdal.Open(ras)
    gt = ds.GetGeoTransform()

    driver = ogr.GetDriverByName("ESRI Shapefile")
    dataSource = driver.Open(shp, 0)
    layer = dataSource.GetLayer()

    for feature in layer:

        xo,yo,xd,yd = bbox2ix(feature.GetGeometryRef().GetEnvelope(),gt)
        arr = ds.ReadAsArray(xo,yo,xd,yd)
        yield arr

    layer.ResetReading()
    ds = None
    dataSource = None

Assuming your shapefile is called shapefile.shp and your raster big_raster.tif you can use it like so:

gen = rasclip('big_raster.tif','shapefile.shp')

# manually with next

clip = next(gen)

## some processing or inspection here

# clip with next feature

clip = next(gen)

# or with iteration

for clip in gen:

    ## apply stuff to clip
    pass # remove
5
  • Glad it helped. Kindly consider accepting the solution if it answered your question.
    – Val
    Apr 26, 2018 at 17:28
  • I did. Thanks once again. One last thing. Is it possible to exclude the no data pixels while creating the individual arrays? Apr 26, 2018 at 18:39
  • How exclude? The line arr = ds.ReadAsArray(xo,yo,xd,yd) will read the array for the intersection between feature and raster. You can add any modification to arr before the yield
    – Val
    Apr 26, 2018 at 19:39
  • how to I get the coordinates of all pixel values in the image. gis.stackexchange.com/questions/260304/… I am using the link as reference Jul 29, 2018 at 18:51
  • This is supposing a shapefile of rectangular polygons, what if they are irregular? Sep 24, 2021 at 14:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.