Skip to content

Instantly share code, notes, and snippets.

@brunosan
Forked from sgillies/subsample.py
Created March 21, 2014 00:46
Show Gist options
  • Save brunosan/9677242 to your computer and use it in GitHub Desktop.
Save brunosan/9677242 to your computer and use it in GitHub Desktop.
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