pyrsgis.raster.clip¶
- pyrsgis.raster.clip(ds, data_arr, x_min, x_max, y_min, y_max)[source]¶
- dsdatasource object
A datasource object of the raster. Ideally, the datasource should be the same as one generated by the
pyrsgis.raster.read
function.- data_arrarray
A 2D or 3D array to clip.
- x_mininteger or float
The lower longitude value.
- x_maxinteger or float
The upper longitude value.
- y_mininteger or float
The lower latitude value.
- y_maxinteger or float
The upper latitude value.
- Returns
- dsdatasource object
A modified
ds
object.- clipped_arrayarray
A 2D or 3D array. This is the clipped version of the input array.
Examples
>>> from pyrsgis import raster >>> infile = r'E:/path_to_your_file/your_file.tif' >>> ds, data_arr = raster.read(infile) >>> print('Original bounding box:', ds.bbox) >>> print('Original shape of raster:', data_arr.shape) Original bounding box: ([752893.9818, 1405185.0], [814213.9818, 1466805.0]) Original shape of raster: (2054, 2044)
This is the original raster data. Now clip the raster using a bounding box of your choice.
>>> new_ds, clipped_arr = raster.clip(ds, data_arr, x_min=770000, x_max=790000, y_min=1420000, y_max=1440000) >>> print('Clipped bounding box:', ds.bbox) >>> print('Clipped shape of raster:', data_arr.shape) Clipped bounding box: ([770023.9818, 1420005.0], [789973.9818, 1439985.0]) Clipped shape of raster: (666, 665)
Note that the raster extent and the data array has now been clipped using the given bounding box. The raster can now easily be exported.
>>> raster.export(clipped_arr, new_ds, r'E:/path_to_your_file/clipped_file.tif')