ee-client 0.0.0__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.
- ee_client-0.0.0.dist-info/LICENSE +21 -0
- ee_client-0.0.0.dist-info/METADATA +142 -0
- ee_client-0.0.0.dist-info/RECORD +9 -0
- ee_client-0.0.0.dist-info/WHEEL +5 -0
- ee_client-0.0.0.dist-info/top_level.txt +1 -0
- eeclient/__init__.py +8 -0
- eeclient/client.py +67 -0
- eeclient/data.py +92 -0
- eeclient/exceptions.py +10 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Daniel Guerrero
|
|
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.
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: ee-client
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: extends the capabilities of the earthengine-api by providing custom session management and client interactions
|
|
5
|
+
Author-email: Daniel Guerrero <dfgm2006@gmail.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/dfguerrerom/ee-client
|
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Requires-Python: >=3.9
|
|
15
|
+
Description-Content-Type: text/x-rst
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: earthengine-api
|
|
18
|
+
Requires-Dist: httpx
|
|
19
|
+
|
|
20
|
+
Earth Engine Session Client
|
|
21
|
+
===========================
|
|
22
|
+
|
|
23
|
+
ee-client extends the capabilities of the earthengine-api by providing custom session management and client interactions
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
ee-client is a Python package designed to extend the capabilities of the Google Earth Engine (GEE) API by providing custom session management and client interactions. This package allows users to make authenticated requests to the Earth Engine REST API using their own credentials, facilitating seamless integration and customization of GEE functionalities.
|
|
27
|
+
|
|
28
|
+
Key Features
|
|
29
|
+
------------
|
|
30
|
+
|
|
31
|
+
- Custom Authentication:
|
|
32
|
+
- Session Management: Handle sessions efficiently with reusable session objects that store credentials and project information.
|
|
33
|
+
- Enhanced API Calls: Replace and extend existing GEE API calls with custom methods to suit specific project requirements.
|
|
34
|
+
- Integration with Existing GEE Objects: Seamlessly integrate custom methods into existing Earth Engine objects, allowing for intuitive and familiar usage.
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
Installation
|
|
38
|
+
------------
|
|
39
|
+
|
|
40
|
+
To install the package, simply use pip:
|
|
41
|
+
|
|
42
|
+
.. code-block:: bash
|
|
43
|
+
|
|
44
|
+
pip install ee-client # Not yet developed
|
|
45
|
+
|
|
46
|
+
Usage
|
|
47
|
+
-----
|
|
48
|
+
|
|
49
|
+
Here are a few examples of how to use ee-client in your projects:
|
|
50
|
+
|
|
51
|
+
Initialization and Authentication
|
|
52
|
+
+++++++++++++++++++++++++++++++++
|
|
53
|
+
|
|
54
|
+
.. code-block:: python
|
|
55
|
+
|
|
56
|
+
from eeclient import Session, get_info, get_asset
|
|
57
|
+
|
|
58
|
+
# Define your credentials and project ID
|
|
59
|
+
credentials = {
|
|
60
|
+
"client_id": "your_client_id",
|
|
61
|
+
"client_secret": "your_client_secret",
|
|
62
|
+
"refresh_token": "your_refresh_token",
|
|
63
|
+
}
|
|
64
|
+
project = "your_project"
|
|
65
|
+
|
|
66
|
+
# Create a session object
|
|
67
|
+
session = Session(credentials, project)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
Making API Calls
|
|
71
|
+
++++++++++++++++
|
|
72
|
+
|
|
73
|
+
.. code-block:: python
|
|
74
|
+
|
|
75
|
+
import ee
|
|
76
|
+
|
|
77
|
+
# Initialize the Earth Engine library and authenticate
|
|
78
|
+
ee.Initialize()
|
|
79
|
+
|
|
80
|
+
# Example: Get information about an Earth Engine object
|
|
81
|
+
result = get_info(session, ee.Number(5))
|
|
82
|
+
print(result)
|
|
83
|
+
|
|
84
|
+
# Example: Get asset information
|
|
85
|
+
asset_info = get_asset(session, "users/your_username/your_asset")
|
|
86
|
+
print(asset_info)
|
|
87
|
+
|
|
88
|
+
Fetching Map Tiles:
|
|
89
|
+
+++++++++++++++++++
|
|
90
|
+
|
|
91
|
+
.. code-block:: python
|
|
92
|
+
|
|
93
|
+
from eeclient import get_map_id, get_map_tile
|
|
94
|
+
|
|
95
|
+
# Example: Get map ID for an Earth Engine image
|
|
96
|
+
image = ee.Image('COPERNICUS/S2/20190726T104031_20190726T104035_T31TGL')
|
|
97
|
+
map_id = get_map_id(session, image)
|
|
98
|
+
|
|
99
|
+
# Example: Get map tile layer
|
|
100
|
+
tile_layer = get_map_tile(map_id)
|
|
101
|
+
print(tile_layer)
|
|
102
|
+
|
|
103
|
+
Integration with Existing GEE Objects
|
|
104
|
+
+++++++++++++++++++++++++++++++++++++
|
|
105
|
+
|
|
106
|
+
WIP
|
|
107
|
+
|
|
108
|
+
.. code-block:: python
|
|
109
|
+
|
|
110
|
+
import ee
|
|
111
|
+
import eeclient
|
|
112
|
+
|
|
113
|
+
# Custom method to get information about an Earth Engine Number object
|
|
114
|
+
def custom_get_info(self, session):
|
|
115
|
+
return get_info(session, self)
|
|
116
|
+
|
|
117
|
+
# Extend the Earth Engine Number class with the custom method
|
|
118
|
+
ee.Number.custom_get_info = custom_get_info
|
|
119
|
+
|
|
120
|
+
# Usage
|
|
121
|
+
number = ee.Number(5)
|
|
122
|
+
result = number.eeclient.get_info(session)
|
|
123
|
+
print(result)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
Contributing
|
|
127
|
+
------------
|
|
128
|
+
|
|
129
|
+
We welcome contributions from the community. Please feel free to submit issues and pull requests to help improve this package.
|
|
130
|
+
|
|
131
|
+
Fork the repository
|
|
132
|
+
+++++++++++++++++++
|
|
133
|
+
|
|
134
|
+
Create a new branch (git checkout -b feature-branch).
|
|
135
|
+
Commit your changes (git commit -am 'Add new feature').
|
|
136
|
+
Push to the branch (git push origin feature-branch).
|
|
137
|
+
Create a new Pull Request.
|
|
138
|
+
|
|
139
|
+
License
|
|
140
|
+
-------
|
|
141
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
|
142
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
eeclient/__init__.py,sha256=fi46D4hRBsgbrqxGR7Jlk7npL_x-1Yji5OUSsQPRN64,180
|
|
2
|
+
eeclient/client.py,sha256=H0dnc850BMVgqJ2nAQvYW-UJASm15Cs2dw-1RQL7M3U,1960
|
|
3
|
+
eeclient/data.py,sha256=Pw0siBdo-pYNnrNsgYlC9GSQAMm9--xTCbedHE9GT-U,2302
|
|
4
|
+
eeclient/exceptions.py,sha256=MvQj9X7urvCzflzCVsYR6czCePyqO1Ow0ukmVooAIT0,363
|
|
5
|
+
ee_client-0.0.0.dist-info/LICENSE,sha256=hYVVtthZP962KZ7cmqS1lYvEmn--vcf4t6i819r-QNM,1072
|
|
6
|
+
ee_client-0.0.0.dist-info/METADATA,sha256=KuO2nm2i4FvVF-ytFAZ46TGakDW9Oz84YuqEZPlNJyk,4243
|
|
7
|
+
ee_client-0.0.0.dist-info/WHEEL,sha256=R0nc6qTxuoLk7ShA2_Y-UWkN8ZdfDBG2B6Eqpz2WXbs,91
|
|
8
|
+
ee_client-0.0.0.dist-info/top_level.txt,sha256=VxCL1XOipzAHHkJWGuav31g_bR1idMa2EaB0MFL7J0g,9
|
|
9
|
+
ee_client-0.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
eeclient
|
eeclient/__init__.py
ADDED
eeclient/client.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from typing import Literal
|
|
2
|
+
import httpx
|
|
3
|
+
from eeclient.exceptions import EERestException
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Session:
|
|
7
|
+
def __init__(self, credentials, ee_project):
|
|
8
|
+
|
|
9
|
+
self.credentials = credentials
|
|
10
|
+
self.project = ee_project
|
|
11
|
+
|
|
12
|
+
self.headers = {
|
|
13
|
+
"x-goog-user-project": ee_project,
|
|
14
|
+
"Authorization": f"Bearer {self._get_access_token()}",
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
self.client = httpx.Client(headers=self.headers)
|
|
18
|
+
|
|
19
|
+
def _set_url_project(self, url):
|
|
20
|
+
"""Set the project in the url"""
|
|
21
|
+
|
|
22
|
+
return url.format(project=self.project)
|
|
23
|
+
|
|
24
|
+
def rest_call(
|
|
25
|
+
self,
|
|
26
|
+
method: Literal["GET", "POST"],
|
|
27
|
+
url: str,
|
|
28
|
+
data: dict = None,
|
|
29
|
+
):
|
|
30
|
+
"""Make a call to the Earth Engine REST API"""
|
|
31
|
+
|
|
32
|
+
url = self._set_url_project(url)
|
|
33
|
+
|
|
34
|
+
with httpx.Client(headers=self.headers) as client:
|
|
35
|
+
|
|
36
|
+
response = (
|
|
37
|
+
client.post(url, data=data) if method == "POST" else client.get(url)
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
if response.status_code >= 400:
|
|
41
|
+
if "application/json" in response.headers.get("Content-Type", ""):
|
|
42
|
+
raise EERestException(response.json().get("error", {}))
|
|
43
|
+
else:
|
|
44
|
+
raise EERestException(
|
|
45
|
+
{"code": response.status_code, "message": response.reason}
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
return response.json()
|
|
49
|
+
|
|
50
|
+
def _get_access_token(self):
|
|
51
|
+
"""Get the access token from the refresh token"""
|
|
52
|
+
|
|
53
|
+
url = "https://oauth2.googleapis.com/token"
|
|
54
|
+
|
|
55
|
+
with httpx.Client() as client:
|
|
56
|
+
|
|
57
|
+
response = client.post(
|
|
58
|
+
url,
|
|
59
|
+
data={
|
|
60
|
+
"client_id": self.credentials["client_id"],
|
|
61
|
+
"client_secret": self.credentials["client_secret"],
|
|
62
|
+
"refresh_token": self.credentials["refresh_token"],
|
|
63
|
+
"grant_type": "refresh_token",
|
|
64
|
+
},
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
return response.json().get("access_token")
|
eeclient/data.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from ee.ee_exception import EEException
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import ee
|
|
5
|
+
from ee import serializer
|
|
6
|
+
from ee import _cloud_api_utils
|
|
7
|
+
|
|
8
|
+
from ee.data import TileFetcher
|
|
9
|
+
from ipyleaflet import TileLayer
|
|
10
|
+
|
|
11
|
+
from eeclient.client import Session
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def get_map_id(session: Session, ee_image: ee.Image):
|
|
15
|
+
|
|
16
|
+
url = "https://earthengine.googleapis.com/v1alpha/projects/{project}/maps"
|
|
17
|
+
|
|
18
|
+
request_body = {
|
|
19
|
+
"expression": serializer.encode(ee_image, for_cloud_api=True),
|
|
20
|
+
"fileFormat": _cloud_api_utils.convert_to_image_file_format(None),
|
|
21
|
+
"bandIds": _cloud_api_utils.convert_to_band_list(None),
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
request_body = json.dumps(request_body)
|
|
25
|
+
|
|
26
|
+
response = session.rest_call("POST", url, data=request_body)
|
|
27
|
+
map_name = response["name"]
|
|
28
|
+
|
|
29
|
+
return map_name
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def get_map_tile(map_name: str):
|
|
33
|
+
|
|
34
|
+
_tile_base_url = "https://earthengine.googleapis.com"
|
|
35
|
+
version = "v1"
|
|
36
|
+
|
|
37
|
+
url_format = "%s/%s/%s/tiles/{z}/{x}/{y}" % (
|
|
38
|
+
_tile_base_url,
|
|
39
|
+
version,
|
|
40
|
+
map_name,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
map_id = {
|
|
44
|
+
"mapid": map_name,
|
|
45
|
+
"token": "",
|
|
46
|
+
"tile_fetcher": TileFetcher(url_format, map_name=map_name),
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return TileLayer(
|
|
50
|
+
url=map_id["tile_fetcher"].url_format,
|
|
51
|
+
attribution="Google Earth Engine",
|
|
52
|
+
name="name",
|
|
53
|
+
max_zoom=24,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def get_info(session: Session, ee_object, workloadTag=None):
|
|
58
|
+
"""Get the info of an Earth Engine object"""
|
|
59
|
+
|
|
60
|
+
data = {
|
|
61
|
+
"expression": serializer.encode(ee_object),
|
|
62
|
+
"workloadTag": workloadTag,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
url = "https://earthengine.googleapis.com/v1/projects/{project}/value:compute"
|
|
66
|
+
|
|
67
|
+
return session.rest_call("POST", url, data=data)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def get_asset(session: Session, ee_asset_id: str):
|
|
71
|
+
"""Get the asset info from the asset id"""
|
|
72
|
+
|
|
73
|
+
url = (
|
|
74
|
+
"https://earthengine.googleapis.com/v1alpha/projects/{project}/assets/"
|
|
75
|
+
+ ee_asset_id
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
return session.rest_call("GET", url)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class EERestException(EEException):
|
|
82
|
+
def __init__(self, error):
|
|
83
|
+
self.message = error.get("message", "EE responded with an error")
|
|
84
|
+
super().__init__(self.message)
|
|
85
|
+
self.code = error.get("code", -1)
|
|
86
|
+
self.status = error.get("status", "UNDEFINED")
|
|
87
|
+
self.details = error.get("details")
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
getInfo = get_info
|
|
91
|
+
getAsset = get_asset
|
|
92
|
+
getMapTile = get_map_tile
|
eeclient/exceptions.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from ee.ee_exception import EEException
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class EERestException(EEException):
|
|
5
|
+
def __init__(self, error):
|
|
6
|
+
self.message = error.get("message", "EE responded with an error")
|
|
7
|
+
super().__init__(self.message)
|
|
8
|
+
self.code = error.get("code", -1)
|
|
9
|
+
self.status = error.get("status", "UNDEFINED")
|
|
10
|
+
self.details = error.get("details")
|