Created
October 4, 2017 23:33
-
-
Save debboutr/e6324ee271505c1050b9f6034a3a6084 to your computer and use it in GitHub Desktop.
trim NoData cells surrounding raster with GeoRaster package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import georasters as gr | |
def trim_nodata(ras): | |
ras.raster.unshare_mask() | |
bbox = np.where(ras.raster.mask == False) | |
g = ras.geot | |
ymin = bbox[0].min() | |
ymax = bbox[0].max() | |
xmin = bbox[1].min() | |
xmax = bbox[1].max() | |
geo_transform = ((g[0] + (xmin * g[1])), g[1], g[2], (g[3] + (ymin * g[5])), g[4], g[5]) | |
return gr.GeoRaster(ras.raster.data[ymin:ymax+1,xmin:xmax+1], | |
geo_transform, | |
nodata_value=ras.nodata_value, | |
projection=ras.projection, | |
datatype=ras.datatype) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment