File size: 1,207 Bytes
c140c33
 
 
 
fc12f93
c140c33
 
fc12f93
c140c33
4f36249
fc12f93
c140c33
fc12f93
c140c33
fc12f93
c140c33
fc12f93
c140c33
 
fc12f93
 
 
 
 
 
 
 
 
4f36249
 
fc12f93
 
 
4f36249
 
 
 
 
 
fc12f93
4f36249
 
 
 
 
 
fc12f93
4f36249
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
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