Created
March 8, 2017 17:17
-
-
Save debboutr/2de659dad43b7f597b7950a48d750b75 to your computer and use it in GitHub Desktop.
Handle conversion for very large rasters to prevent Overflow Error!
This file contains 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 itertools | |
import rasterio as rs | |
import geopandas as gpd | |
from rasterio.crs import CRS | |
from rasterio import features | |
def makeAffine(tf, lbl, w, h): | |
if lbl == 'A': | |
return tf | |
if lbl == 'B': | |
return tf * tf.translation((w/2),0) | |
if lbl == 'C': | |
return tf * tf.translation(0,(h/2)) | |
if lbl == 'D': | |
return tf * tf.translation((w/2),(h/2)) | |
lakes = gpd.read_file('./ohSevenA.shp') | |
src = rs.open('./fdr') | |
if src.crs != lakes.crs: | |
lakes.to_crs({'init': u'epsg:5070'}, inplace=True) | |
meta = src.meta.copy() | |
w, h = src.width, src.height | |
meta.update(compress='lzw', | |
nodata=0, | |
dtype=rs.uint32, | |
driver='GTiff', | |
height = h/2, | |
width = w/2, | |
crs=CRS({'init': u'epsg:5070'})) | |
col_chunks = [(0, w/2), (w/2, w)] | |
row_chunks = [(0, h/2), (h/2, h)] | |
for win, lbl in zip(itertools.product(row_chunks, col_chunks),['A','B','C','D']): | |
nuAff = makeAffine(src.transform, lbl, w, h) | |
meta.update(transform=nuAff) | |
lksRas = rs.open("./lakes_%s.tif" % lbl, 'w', **meta) | |
arr = lksRas.read(1) | |
shapes = ((g,v) for g,v in zip(lakes.geometry,lakes.COMID)) | |
burned = features.rasterize(shapes=shapes, fill=0, | |
out=arr, | |
out_shape=arr.shape, | |
transform=nuAff) | |
lksRas.write(burned,indexes=1) | |
lksRas = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment