Skip to content

Instantly share code, notes, and snippets.

@sgillies
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sgillies/9676184 to your computer and use it in GitHub Desktop.
Save sgillies/9676184 to your computer and use it in GitHub Desktop.
Stats on rasters subsampled by rasterio
import rasterio
import numpy as np
src = rasterio.open('/Users/sean/code/decloud/envs/1_16/output/0_q.tif')
# Fraction of elements with value > 100 at full resolution.
q = src.read_band(1)
ones = np.ones(q.shape)
print "Full, %d pixels" % (q.shape[0]*q.shape[1])
print sum(ones[q>100])/(q.shape[0]*q.shape[1])
# Fraction of elements with value > 100, sampling only 1 in 16.
shape = (q.shape[0]/4, q.shape[1]/4)
q = np.zeros(shape, dtype=np.uint8)
q = src.read_band(1, out=q)
ones = np.ones(q.shape)
print "Subsampled, %d pixels" % (q.shape[0]*q.shape[1])
print sum(ones[q>100])/(q.shape[0]*q.shape[1])
# Output
Full, 6806881 pixels
1683126000.0 m^2
Subsampled, 425104 pixels
1681646400.0 m^2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment