Jonathan Marokhovsky commited on
Commit
fc12f93
1 Parent(s): 4f36249

Added type hints

Browse files
Files changed (1) hide show
  1. toy_app/src/map_utils.py +20 -16
toy_app/src/map_utils.py CHANGED
@@ -2,44 +2,48 @@
2
  These functions are meant to manipulate and act on a given leafmap instance in solara.
3
  """
4
 
 
5
  import logging
6
  import typing
7
- import leafmap
8
  import geojson
9
- from area import area as area
10
- import rioxarray
11
- import json
12
  import geopandas as gpd
 
13
 
 
14
 
15
- log = logging.getLogger(__name__)
16
 
 
17
 
18
- def calculate_area(poly: geojson.GeoJSON) -> float:
19
- output: float = 0.0
20
- try:
21
- if poly["geometry"] and poly["geometry"]["type"] == "Polgon":
22
- output = area(poly["geometry"])
23
- except KeyError:
24
- raise TypeError("The given GeoJSON did not contain a Polygon as was expected")
25
 
26
- return output
 
 
 
 
 
 
 
 
27
 
28
 
29
- def extract_geom(gdf, cog):
 
 
30
  x = rioxarray.open_rasterio("/vsicurl/" + cog, masked=True).rio.clip(
31
  gdf.geometry.values, gdf.crs, from_disk=True
32
  )
33
  return x
34
 
35
 
36
- def read_polygon(polygon):
37
  geojson_str = json.dumps(polygon)
38
  gdf = gpd.read_file(geojson_str, driver="GeoJSON")
39
  gdf.set_crs("epsg:4326")
40
  return gdf
41
 
42
 
43
- def area_hectares(gdf):
44
  area = gdf.to_crs("EPSG:9822").area / 10000.0
45
  return area
 
2
  These functions are meant to manipulate and act on a given leafmap instance in solara.
3
  """
4
 
5
+ import json
6
  import logging
7
  import typing
8
+
9
  import geojson
 
 
 
10
  import geopandas as gpd
11
+ import rioxarray
12
 
13
+ # from ipyleaflet import GeoJSON
14
 
15
+ # from area import area as area
16
 
17
+ log = logging.getLogger(__name__)
18
 
 
 
 
 
 
 
 
19
 
20
+ # def calculate_area(poly: geojson.GeoJSON) -> float:
21
+ # output: float = 0.0
22
+ # try:
23
+ # if poly["geometry"] and poly["geometry"]["type"] == "Polgon":
24
+ # output = area(poly["geometry"])
25
+ # except KeyError:
26
+ # raise TypeError("The given GeoJSON did not contain a Polygon as was expected")
27
+ #
28
+ # return output
29
 
30
 
31
+ def extract_geom(
32
+ gdf: gpd.GeoDataFrame, cog: str
33
+ ) -> rioxarray.rioxarray.raster_array.RasterArray:
34
  x = rioxarray.open_rasterio("/vsicurl/" + cog, masked=True).rio.clip(
35
  gdf.geometry.values, gdf.crs, from_disk=True
36
  )
37
  return x
38
 
39
 
40
+ def read_polygon(polygon: geojson.GeoJSON) -> gpd.GeoDataFrame:
41
  geojson_str = json.dumps(polygon)
42
  gdf = gpd.read_file(geojson_str, driver="GeoJSON")
43
  gdf.set_crs("epsg:4326")
44
  return gdf
45
 
46
 
47
+ def area_hectares(gdf: gpd.GeoDataFrame):
48
  area = gdf.to_crs("EPSG:9822").area / 10000.0
49
  return area