Skip to content

Instantly share code, notes, and snippets.

@mojodna
Created June 30, 2016 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojodna/1455c6483ca36427a98748301a47957a to your computer and use it in GitHub Desktop.
Save mojodna/1455c6483ca36427a98748301a47957a to your computer and use it in GitHub Desktop.
Polygonize landcover block-by-block.
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)
@mojodna
Copy link
Author

mojodna commented Jun 30, 2016

NOTE: this doesn't actually work, as the resulting Shapefile exceeds 4GB before completing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment