0

I am trying to clip a raster data with GDAL warp. I want data values of raster within the buffer.

import rasterio as rio
import shapely as shp
import geopandas as gp
import numpy as np

raster=rio.open('C:/Users/schol/montreal_500m.tif')
band=raster.read()

y=45.508888
x=-73.561668

poly=shp.geometry.Point(x,y).buffer(0.1)
crs='EPSG:4326'
my_poly=[poly]
mydat=gp.GeoDataFrame(crs=crs,geometry=my_poly)
mydat.to_file("C:/Users/schol/mypo.shp")

import gdal

ras_in='C:/Users/schol/montreal_500m.tif'
shp_in="C:/Users/schol/mypo.shp"
ras_out='C:/Users/schol/montreal_clip.tif'

result=gdal.Warp(ras_out,ras_in,cutlineDSName=shp_in,dstNodata=np.nan) 

When I get the minimum and maximum value - minimum says -41.46 and the maximum says nan. When seeing the clipped raster array it has a maximum value of 200. Also the input raster and the clipped raster both have same dimension. How is this happening and where am I doing it wrong?

Also, when I use this line below, I am not getting the clipped raster as output at all. And I need to get the clipped output like this with gdal.

result=gdal.Warp(ras_out,ras_in,cutlineDSName=shp_in,cropToCutline=True,dstNodata=np.nan)

1 Answer 1

1

Have a look at the gdalwarp documentation https://gdal.org/programs/gdalwarp.html. It is not so clearly expressed but the cutline makes only a mask

cutline

Enable use of a blend cutline from the name OGR support datasource.

It seems that you want to crop the image by the same and for doing that you must use also another option

-crop_to_cutline

Crop the extent of the target dataset to the extent of the cutline.

For the reference how it goes with gdalwarp from the command line. I created and saved the buffer geometry into GeoJSON. As WKT it is.

POLYGON (( -73.461668 45.508888, -73.46358947195968 45.48937896779839, -73.46928004674886 45.47061965676349, -73.47852103876974 45.45333097669804, -73.49095732188134 45.43817732188134, -73.50611097669804 45.42574103876974, -73.52339965676349 45.41650004674887, -73.54215896779839 45.41080947195967, -73.561668 45.408888, -73.58117703220161 45.41080947195967, -73.59993634323651 45.41650004674887, -73.61722502330196 45.42574103876974, -73.63237867811866 45.43817732188134, -73.64481496123025 45.45333097669804, -73.65405595325113 45.47061965676349, -73.65974652804032 45.48937896779839, -73.66166799999999 45.508888, -73.65974652804032 45.52839703220161, -73.65405595325113 45.54715634323651, -73.64481496123025 45.56444502330196, -73.63237867811866 45.57959867811866, -73.61722502330196 45.592034961230254, -73.59993634323651 45.601275953251125, -73.58117703220161 45.606966528040324, -73.561668 45.608888, -73.54215896779839 45.606966528040324, -73.52339965676349 45.601275953251125, -73.50611097669804 45.592034961230254, -73.49095732188134 45.57959867811866, -73.47852103876974 45.56444502330196, -73.46928004674886 45.54715634323651, -73.46358947195968 45.52839703220161, -73.461668 45.508888 ))

Command 1:

gdalwarp -cutline -crop_to_cutline Montreal_buffer.json montreal_500m.tif cropped.tif

Command 2:

gdalwarp -cutline Montreal_buffer.json -crop_to_cutline montreal_500m.tif cropped.tif

Result 1:

enter image description here

Result 2:

enter image description here

5
  • I suggest to learn with the gdalwarp binary first and convert the workflow into Python once you have had success with the binary.
    – user30184
    Dec 13, 2022 at 13:41
  • Thank you for your answer. I have used the crop to cutline option too, but when using this, I am not getting the output at all. The output clipped raster is not getting created.
    – SVpk
    Dec 13, 2022 at 13:41
  • I added gdalwarp examples. For some reason the cropping is somewhat inaccurate and it cuts some data from the left but the output is created anyway.
    – user30184
    Dec 13, 2022 at 14:17
  • 1
    Thank you so much for your effort in doing this. But since I am completely new to programming, I am finding it difficult to understand this. Anyways, thanks again.
    – SVpk
    Dec 13, 2022 at 14:33
  • I was trying to clip an unprojected TIFF file. I projected it then used the shapefile to clip the image with crop to cutline option and it worked! Thanks to @user30184
    – SVpk
    Dec 14, 2022 at 16:14

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.