openforis-whisp 2.0.0a5__py3-none-any.whl → 2.0.0a6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
openforis_whisp/utils.py CHANGED
@@ -1,154 +1,154 @@
1
- import base64
2
- import ee
3
- import math
4
- import os
5
- import pandas as pd
6
-
7
- import importlib.resources as pkg_resources
8
-
9
- from dotenv import load_dotenv
10
- from pathlib import Path
11
-
12
- from .logger import StdoutLogger
13
-
14
-
15
- logger = StdoutLogger(__name__)
16
-
17
-
18
- def get_example_data_path(filename):
19
- """
20
- Get the path to an example data file included in the package.
21
-
22
- Parameters:
23
- -----------
24
- filename : str
25
- The name of the example data file.
26
-
27
- Returns:
28
- --------
29
- str
30
- The path to the example data file.
31
- """
32
- return os.path.join("..", "tests", "fixtures", filename)
33
-
34
-
35
- def load_env_vars() -> None:
36
- """Loads the environment variables required for testing the codebase.
37
-
38
- Returns
39
- -------
40
- out : None
41
- """
42
-
43
- all_dotenv_paths = [Path(__file__).parents[2] / ".env", Path.cwd() / ".env"]
44
- dotenv_loaded = False
45
-
46
- for dotenv_path in all_dotenv_paths:
47
- logger.logger.debug(f"dotenv_path: {dotenv_path}")
48
- if dotenv_path.exists():
49
- dotenv_loaded = load_dotenv(dotenv_path)
50
- break
51
-
52
- if not dotenv_loaded:
53
- raise DotEnvNotFoundError
54
- logger.logger.info(f"Loaded evironment variables from '{dotenv_path}'")
55
-
56
-
57
- def init_ee() -> None:
58
- """Initialize earth engine according to the environment"""
59
-
60
- # only do the initialization if the credential are missing
61
- if not ee.data._credentials:
62
-
63
- # if in test env use the private key
64
- if "EE_PRIVATE_KEY" in os.environ:
65
-
66
- # key need to be decoded in a file
67
- content = base64.b64decode(os.environ["EE_PRIVATE_KEY"]).decode()
68
- with open("ee_private_key.json", "w") as f:
69
- f.write(content)
70
-
71
- # connection to the service account
72
- service_account = "test-sepal-ui@sepal-ui.iam.gserviceaccount.com"
73
- credentials = ee.ServiceAccountCredentials(
74
- service_account, "ee_private_key.json"
75
- )
76
- ee.Initialize(credentials)
77
- logger.logger.info(f"Used env var")
78
-
79
- # if in local env use the local user credential
80
- else:
81
- try:
82
- load_env_vars()
83
- logger.logger.info("Called 'ee.Initialize()'.")
84
- ee.Initialize(project=os.environ["PROJECT"])
85
- except ee.ee_exception.EEException:
86
- logger.logger.info("Called 'ee.Authenticate()'.")
87
- ee.Authenticate()
88
- ee.Initialize(project=os.environ["PROJECT"])
89
-
90
-
91
- def clear_ee_credentials():
92
-
93
- path_to_creds = Path().home() / ".config" / "earthengine" / "credentials"
94
- if not path_to_creds.exists():
95
- logger.logger.error(
96
- f"GEE credentials file '{path_to_creds}' not found, could not de-authenticate."
97
- )
98
- else:
99
- path_to_creds.unlink()
100
- logger.logger.warning(f"GEE credentials file deleted.")
101
-
102
-
103
- def remove_geometry_from_feature_collection(feature_collection):
104
- """Define the function to remove geometry from features in a feature collection"""
105
- # Function to remove geometry from features
106
- def remove_geometry(feature):
107
- # Remove the geometry property
108
- feature = feature.setGeometry(None)
109
- return feature
110
-
111
- # Apply the function to remove geometry to the feature collection
112
- feature_collection_no_geometry = feature_collection.map(remove_geometry)
113
- return feature_collection_no_geometry
114
-
115
-
116
- # Compute centroids of each polygon including the external_id_column
117
- def get_centroid(feature, external_id_column="external_id"):
118
- keepProperties = [external_id_column]
119
- # Get the centroid of the feature's geometry.
120
- centroid = feature.geometry().centroid(1)
121
- # Return a new Feature, copying properties from the old Feature.
122
- return ee.Feature(centroid).copyProperties(feature, keepProperties)
123
-
124
-
125
- def buffer_point_to_required_area(feature, area, area_unit):
126
- """buffers feature to get a given area (needs math library); area unit in 'ha' or 'km2' (the default)"""
127
- area = feature.get("REP_AREA")
128
-
129
- # buffer_size = get_radius_m_to_buffer_for_given_area(area,"km2")# should work but untested in this function
130
-
131
- buffer_size = (
132
- (ee.Number(feature.get("REP_AREA")).divide(math.pi)).sqrt().multiply(1000)
133
- ) # calculating radius in metres from REP_AREA in km2
134
-
135
- return ee.Feature(feature).buffer(buffer_size, 1)
136
- ### buffering (incl., max error parameter should be 0m. But put as 1m anyhow - doesn't seem to make too much of a difference for speed)
137
-
138
-
139
- def get_radius_m_to_buffer_to_required_area(area, area_unit="km2"):
140
- """gets radius in metres to buffer to get an area (needs math library); area unit ha or km2 (the default)"""
141
- if area_unit == "km2":
142
- unit_fix_factor = 1000
143
- elif area_unit == "ha":
144
- unit_fix_factor = 100
145
- radius = ee.Number(area).divide(math.pi).sqrt().multiply(unit_fix_factor)
146
- return radius
147
-
148
-
149
- class DotEnvNotFoundError(FileNotFoundError):
150
- def __init__(self) -> None:
151
- super().__init__(
152
- "Running tests requires setting an appropriate '.env' in the root directory or in your current working "
153
- "directory. You may copy and edit the '.env.template' file from the root directory or from the README.",
154
- )
1
+ import base64
2
+ import ee
3
+ import math
4
+ import os
5
+ import pandas as pd
6
+
7
+ import importlib.resources as pkg_resources
8
+
9
+ from dotenv import load_dotenv
10
+ from pathlib import Path
11
+
12
+ from .logger import StdoutLogger
13
+
14
+
15
+ logger = StdoutLogger(__name__)
16
+
17
+
18
+ def get_example_data_path(filename):
19
+ """
20
+ Get the path to an example data file included in the package.
21
+
22
+ Parameters:
23
+ -----------
24
+ filename : str
25
+ The name of the example data file.
26
+
27
+ Returns:
28
+ --------
29
+ str
30
+ The path to the example data file.
31
+ """
32
+ return os.path.join("..", "tests", "fixtures", filename)
33
+
34
+
35
+ def load_env_vars() -> None:
36
+ """Loads the environment variables required for testing the codebase.
37
+
38
+ Returns
39
+ -------
40
+ out : None
41
+ """
42
+
43
+ all_dotenv_paths = [Path(__file__).parents[2] / ".env", Path.cwd() / ".env"]
44
+ dotenv_loaded = False
45
+
46
+ for dotenv_path in all_dotenv_paths:
47
+ logger.logger.debug(f"dotenv_path: {dotenv_path}")
48
+ if dotenv_path.exists():
49
+ dotenv_loaded = load_dotenv(dotenv_path)
50
+ break
51
+
52
+ if not dotenv_loaded:
53
+ raise DotEnvNotFoundError
54
+ logger.logger.info(f"Loaded evironment variables from '{dotenv_path}'")
55
+
56
+
57
+ def init_ee() -> None:
58
+ """Initialize earth engine according to the environment"""
59
+
60
+ # only do the initialization if the credential are missing
61
+ if not ee.data._credentials:
62
+
63
+ # if in test env use the private key
64
+ if "EE_PRIVATE_KEY" in os.environ:
65
+
66
+ # key need to be decoded in a file
67
+ content = base64.b64decode(os.environ["EE_PRIVATE_KEY"]).decode()
68
+ with open("ee_private_key.json", "w") as f:
69
+ f.write(content)
70
+
71
+ # connection to the service account
72
+ service_account = "test-sepal-ui@sepal-ui.iam.gserviceaccount.com"
73
+ credentials = ee.ServiceAccountCredentials(
74
+ service_account, "ee_private_key.json"
75
+ )
76
+ ee.Initialize(credentials)
77
+ logger.logger.info(f"Used env var")
78
+
79
+ # if in local env use the local user credential
80
+ else:
81
+ try:
82
+ load_env_vars()
83
+ logger.logger.info("Called 'ee.Initialize()'.")
84
+ ee.Initialize(project=os.environ["PROJECT"])
85
+ except ee.ee_exception.EEException:
86
+ logger.logger.info("Called 'ee.Authenticate()'.")
87
+ ee.Authenticate()
88
+ ee.Initialize(project=os.environ["PROJECT"])
89
+
90
+
91
+ def clear_ee_credentials():
92
+
93
+ path_to_creds = Path().home() / ".config" / "earthengine" / "credentials"
94
+ if not path_to_creds.exists():
95
+ logger.logger.error(
96
+ f"GEE credentials file '{path_to_creds}' not found, could not de-authenticate."
97
+ )
98
+ else:
99
+ path_to_creds.unlink()
100
+ logger.logger.warning(f"GEE credentials file deleted.")
101
+
102
+
103
+ def remove_geometry_from_feature_collection(feature_collection):
104
+ """Define the function to remove geometry from features in a feature collection"""
105
+ # Function to remove geometry from features
106
+ def remove_geometry(feature):
107
+ # Remove the geometry property
108
+ feature = feature.setGeometry(None)
109
+ return feature
110
+
111
+ # Apply the function to remove geometry to the feature collection
112
+ feature_collection_no_geometry = feature_collection.map(remove_geometry)
113
+ return feature_collection_no_geometry
114
+
115
+
116
+ # Compute centroids of each polygon including the external_id_column
117
+ def get_centroid(feature, external_id_column="external_id"):
118
+ keepProperties = [external_id_column]
119
+ # Get the centroid of the feature's geometry.
120
+ centroid = feature.geometry().centroid(1)
121
+ # Return a new Feature, copying properties from the old Feature.
122
+ return ee.Feature(centroid).copyProperties(feature, keepProperties)
123
+
124
+
125
+ def buffer_point_to_required_area(feature, area, area_unit):
126
+ """buffers feature to get a given area (needs math library); area unit in 'ha' or 'km2' (the default)"""
127
+ area = feature.get("REP_AREA")
128
+
129
+ # buffer_size = get_radius_m_to_buffer_for_given_area(area,"km2")# should work but untested in this function
130
+
131
+ buffer_size = (
132
+ (ee.Number(feature.get("REP_AREA")).divide(math.pi)).sqrt().multiply(1000)
133
+ ) # calculating radius in metres from REP_AREA in km2
134
+
135
+ return ee.Feature(feature).buffer(buffer_size, 1)
136
+ ### buffering (incl., max error parameter should be 0m. But put as 1m anyhow - doesn't seem to make too much of a difference for speed)
137
+
138
+
139
+ def get_radius_m_to_buffer_to_required_area(area, area_unit="km2"):
140
+ """gets radius in metres to buffer to get an area (needs math library); area unit ha or km2 (the default)"""
141
+ if area_unit == "km2":
142
+ unit_fix_factor = 1000
143
+ elif area_unit == "ha":
144
+ unit_fix_factor = 100
145
+ radius = ee.Number(area).divide(math.pi).sqrt().multiply(unit_fix_factor)
146
+ return radius
147
+
148
+
149
+ class DotEnvNotFoundError(FileNotFoundError):
150
+ def __init__(self) -> None:
151
+ super().__init__(
152
+ "Running tests requires setting an appropriate '.env' in the root directory or in your current working "
153
+ "directory. You may copy and edit the '.env.template' file from the root directory or from the README.",
154
+ )
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 lecrabe
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2023 lecrabe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: openforis-whisp
3
- Version: 2.0.0a5
3
+ Version: 2.0.0a6
4
4
  Summary: Whisp (What is in that plot) is an open-source solution which helps to produce relevant forest monitoring information and support compliance with deforestation-related regulations.
5
5
  License: MIT
6
6
  Keywords: whisp,geospatial,data-processing
@@ -0,0 +1,17 @@
1
+ openforis_whisp/__init__.py,sha256=OOCc-TFeVPAdYb06jMNzHqpeLBhoaWKOvzhAe2QaeLE,2328
2
+ openforis_whisp/data_conversion.py,sha256=u4lJ39B22H2KBKDFEd-lgwH_IRW2MTldaOxFQjmDGA0,16416
3
+ openforis_whisp/datasets.py,sha256=aDwoPmO_MViiGjyqmn7Sp17YYxaz-_gDKEdslSCz0Zc,50629
4
+ openforis_whisp/logger.py,sha256=73Eppe-Rd8wtzUGswUNUY3jOaNrNHy0N7qJ1TxgLSKk,2233
5
+ openforis_whisp/parameters/__init__.py,sha256=1ThsS9HYwZJzBR_3_BWU2UkyVtzN9u4CgdnmuClt4ug,334
6
+ openforis_whisp/parameters/config_runtime.py,sha256=wm1dC9cuDkrNPBMMoncK2a4TKcwd8Jw47HY_zsUuk_0,1377
7
+ openforis_whisp/parameters/lookup_context_and_metadata.csv,sha256=_UQ8u7WOJH1ZHOGUVUNirfj-XdyALuLk_ele1ANSvZk,1285
8
+ openforis_whisp/parameters/lookup_gee_datasets.csv,sha256=5K1LQyuvwvG1vOdlyCknv_foDtRUKHPU3VvOU_zsoWQ,17626
9
+ openforis_whisp/pd_schemas.py,sha256=lTv7fbwwG9bGOi6M4-tsJ3xm_HpiWJW-fhrZ0ELUF2c,2604
10
+ openforis_whisp/reformat.py,sha256=ckSO8N_2GySe7X_Pot72N88wz2Y0RFIB774asWY69lc,17427
11
+ openforis_whisp/risk.py,sha256=DKfFN4RrqSkVDbDFcEEWjlFIiDfNO-XACSzxxI1MNNI,31182
12
+ openforis_whisp/stats.py,sha256=oNLr6yU5M-BY61L6NIcDUy93t1VwUTLEgnh8T6YyweA,39748
13
+ openforis_whisp/utils.py,sha256=jWfq12wiVxcnlPPAtfDy0scHwYwSNKANoY2rRsDYx88,5224
14
+ openforis_whisp-2.0.0a6.dist-info/LICENSE,sha256=RmwgaoqiMSysLXZaB9GP-f106r1F1OiC7dLKxjrfh20,1064
15
+ openforis_whisp-2.0.0a6.dist-info/METADATA,sha256=AbMJGBkxVId1lSEWTFRfRDoaieBqwLR_sR2sxTVPCVk,16681
16
+ openforis_whisp-2.0.0a6.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
17
+ openforis_whisp-2.0.0a6.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 2.1.1
2
+ Generator: poetry-core 2.1.3
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,17 +0,0 @@
1
- openforis_whisp/__init__.py,sha256=xfXNzskPfnlQkmf3QZHEydhte3U9_uLdoYM04eowNqw,2403
2
- openforis_whisp/data_conversion.py,sha256=_HSjYozNO1xAOAk-uGmzTVCTOc3W7x3GDlvEUgrnj_Q,16909
3
- openforis_whisp/datasets.py,sha256=9Ofxyy2ignnN6mSXfXDP9n6SsQ8QPQQWivuolS_i8LY,52013
4
- openforis_whisp/logger.py,sha256=n9k0EhAZYZKesnfskv8KyWnkGbjqRqk84ulx9-u_Jsc,2308
5
- openforis_whisp/parameters/__init__.py,sha256=KL7iORJVjSpZatYjoyWckcmQJnE89_DBC8R6_0_eR6o,349
6
- openforis_whisp/parameters/config_runtime.py,sha256=NOo39MAi60XCwEx5pwkS0EHKJBh0XY1q06y4j0HAABg,1421
7
- openforis_whisp/parameters/lookup_context_and_metadata.csv,sha256=KgK0ik_Gd4t_Nq5cUkGPT4ZFZVO93HWSG82jRrOukt4,1298
8
- openforis_whisp/parameters/lookup_gee_datasets.csv,sha256=5K1LQyuvwvG1vOdlyCknv_foDtRUKHPU3VvOU_zsoWQ,17626
9
- openforis_whisp/pd_schemas.py,sha256=W_ocS773LHfc05dJqvWRa-bRdX0wKFoNp0lMxgFx94Y,2681
10
- openforis_whisp/reformat.py,sha256=o3TpeuddR1UlP1C3uFeI957kIZYMQqEW1pXsjKbAtiY,17922
11
- openforis_whisp/risk.py,sha256=FNWH84xhSjVZW3yTnTWZF3MxiZtNA5jb154vu-C2kJ0,31951
12
- openforis_whisp/stats.py,sha256=_l2V8BWdbJ2GoK7N5Zswg0Gvs1I5RRT-JGgl9fyl2AY,40882
13
- openforis_whisp/utils.py,sha256=YqFYK1fH2WpuWolXa-gCeSGYiHdJ0_xQUIo15dQ9Sh8,5378
14
- openforis_whisp-2.0.0a5.dist-info/LICENSE,sha256=nqyqICO95iw_iwzP1t_IIAf7ZX3DPbL_M9WyQfh2q1k,1085
15
- openforis_whisp-2.0.0a5.dist-info/METADATA,sha256=4ii5-gyxRZZmWyAhorNo9phcbpQoLRcmhagxxCCKHeA,16681
16
- openforis_whisp-2.0.0a5.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
17
- openforis_whisp-2.0.0a5.dist-info/RECORD,,