Skip to content

Instantly share code, notes, and snippets.

@debboutr
Created October 4, 2017 23:33
Show Gist options
  • Save debboutr/e6324ee271505c1050b9f6034a3a6084 to your computer and use it in GitHub Desktop.
Save debboutr/e6324ee271505c1050b9f6034a3a6084 to your computer and use it in GitHub Desktop.
trim NoData cells surrounding raster with GeoRaster package
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