Skip to content

Instantly share code, notes, and snippets.

@rasha-salim
Created October 2, 2022 21:39
Show Gist options
  • Save rasha-salim/32f6ac659874f19b6ecd8b846a3839c2 to your computer and use it in GitHub Desktop.
Save rasha-salim/32f6ac659874f19b6ecd8b846a3839c2 to your computer and use it in GitHub Desktop.
def getGeojson():
# Store the mask coordinates into a geojson file
bounds = getImgBounds()
dataset = rasterio.open(png_path)
bands = [1]
data = dataset.read(bands)
transform = rasterio.transform.from_bounds(bounds[0], bounds[1], bounds[2], bounds[3], data.shape[1], data.shape[2])
crs = rasterio.crs.CRS({"init": "epsg:4326"}) # 3857 Google Maps Projection 4326 World wide (3D)
with MemoryFile() as memfile:
meta = {"count": 1, "width": data.shape[1], "height": data.shape[2], "transform": transform, "nodata": 0, "crs": crs, "dtype":data.dtype}
with memfile.open(driver='GTiff', **meta) as dataset:
dataset.write(data)
band=dataset.read()
mask = band!= 0
shapes = features.shapes(band, mask=mask, transform=transform)
fc = ({"geometry": shape, "properties": {"value": value}} for shape, value in shapes)
gdf = gpd.GeoDataFrame.from_features(fc) #.to_file(os.path.join(self.output_path, f'm_{self.lat}_{self.lng}.geojson'), driver='GeoJSON')
gdf.crs = "epsg:4326"
return gdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment