Created
August 23, 2017 14:14
-
-
Save jachym/53a65d7388f8fe8599fb3701099c4fd3 to your computer and use it in GitHub Desktop.
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 rasterio as rio | |
from rasterio import features | |
from rasterio.rio.helpers import coords, write_features | |
import numpy as np | |
import json | |
from shapely.geometry import asShape | |
from shapely.geometry import mapping | |
def generate_jsons(): | |
features = [] | |
with rio.open("data/esa-cr.tiff", "r") as inputdataset: | |
data = inputdataset.read()[0] | |
msk = inputdataset.read_masks(1) | |
msk = inputdataset.read()[0] | |
transform = inputdataset.transform | |
msk[msk < 40] = 0 | |
msk[msk == 40] = 1 | |
msk[msk > 40] = 0 | |
kwargs = { | |
"transform": transform, | |
"mask": msk | |
} | |
for i, (g, val) in enumerate( | |
rio.features.shapes(data, **kwargs)): | |
xs, ys = zip(*coords(g)) | |
geom = asShape(g) | |
geom = geom.simplify(30) | |
g = mapping(geom) | |
features.append({ | |
'type': 'Feature', | |
'id': "{0}".format(i), | |
'properties': { | |
'val': val | |
}, | |
'bbox': [min(xs), min(ys), max(xs), max(ys)], | |
'geometry': g | |
}) | |
return features | |
print(json.dumps({ | |
"type": "FeatureCollection", | |
"features": generate_jsons() | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment