Created
June 30, 2016 20:38
-
-
Save mojodna/1455c6483ca36427a98748301a47957a to your computer and use it in GitHub Desktop.
Polygonize landcover block-by-block.
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
from __future__ import print_function | |
import json | |
import sys | |
import fiona | |
from fiona.crs import from_epsg | |
import rasterio | |
from rasterio import features | |
with rasterio.open("LCType_4326.tif") as src: | |
schema = { | |
"geometry": "Polygon", | |
"properties": { | |
"VALUE": "int", | |
} | |
} | |
with fiona.open("landcover.shp", "w", | |
crs=from_epsg(4326), | |
driver="ESRI Shapefile", | |
schema=schema, | |
) as sink: | |
for ji, window in src.block_windows(1): | |
print(ji, file=sys.stderr) | |
data = src.read(1, window=window) | |
mask = 0 < data | |
shapes = features.shapes(data, mask=mask, transform=src.window_transform(window)) | |
for geometry, value in shapes: | |
feature = { | |
"geometry": geometry, | |
"properties": { | |
"VALUE": value | |
} | |
} | |
sink.write(feature) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: this doesn't actually work, as the resulting Shapefile exceeds 4GB before completing.