Last active
February 15, 2021 02:34
-
-
Save e5k/3ec0906dd83157e9109534c1c19c8956 to your computer and use it in GitHub Desktop.
contour_raster_rasterio
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
# From https://stackoverflow.com/questions/41487642/how-to-export-contours-created-in-scikit-image-find-contours-to-shapefile-or-geo | |
import rasterio as rio | |
flpth = '/Users/seb/Documents/Codes/VolcGIS/volcanoes/Agung/_hazard/Tephra/Agung_VEI3_P5.tif' | |
schema = {"geometry": "Polygon", "properties": {"value": "int"}} | |
maskVal = 1000 | |
with rio.open(flpth) as src: | |
image = src.read() | |
# use your function to generate mask | |
mask = image >= maskVal | |
# and convert to uint8 for rio.features.shapes | |
mask = mask.astype('uint8') | |
shapes = rio.features.shapes(mask, transform=src.transform) | |
# select the records from shapes where the value is 1, | |
# or where the mask was True | |
records = [{"geometry": geometry, "properties": {"value": value}} | |
for (geometry, value) in shapes if value == 1] | |
# Write to shapefile | |
# with fiona.open('test.shp', "w", "ESRI Shapefile", | |
# crs=src.crs.data, schema=schema) as out_file: | |
# out_file.writerecords(records) | |
# Create GeoDataFrame | |
clipMask = gpd.GeoDataFrame(records[0]['geometry']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment