Spaces:
Runtime error
Runtime error
""" | |
These functions are meant to manipulate and act on a given leafmap instance in solara. | |
""" | |
import json | |
import logging | |
import typing | |
import geojson | |
import geopandas as gpd | |
import rioxarray | |
# from ipyleaflet import GeoJSON | |
# from area import area as area | |
log = logging.getLogger(__name__) | |
# def calculate_area(poly: geojson.GeoJSON) -> float: | |
# output: float = 0.0 | |
# try: | |
# if poly["geometry"] and poly["geometry"]["type"] == "Polgon": | |
# output = area(poly["geometry"]) | |
# except KeyError: | |
# raise TypeError("The given GeoJSON did not contain a Polygon as was expected") | |
# | |
# return output | |
def extract_geom( | |
gdf: gpd.GeoDataFrame, cog: str | |
) -> rioxarray.rioxarray.raster_array.RasterArray: | |
x = rioxarray.open_rasterio("/vsicurl/" + cog, masked=True).rio.clip( | |
gdf.geometry.values, gdf.crs, from_disk=True | |
) | |
return x | |
def read_polygon(polygon: geojson.GeoJSON) -> gpd.GeoDataFrame: | |
geojson_str = json.dumps(polygon) | |
gdf = gpd.read_file(geojson_str, driver="GeoJSON") | |
gdf.set_crs("epsg:4326") | |
return gdf | |
def area_hectares(gdf: gpd.GeoDataFrame): | |
area = gdf.to_crs("EPSG:9822").area / 10000.0 | |
return area | |