Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/tests/fake_ee.py
2313 views
1
"""Fake ee module for use with testing."""
2
3
# pylint: disable=invalid-name
4
# pylint: disable=too-few-public-methods
5
6
import box
7
8
9
class Image:
10
def __init__(self, *_, **__):
11
pass
12
13
@staticmethod
14
def constant(self, *_, **__):
15
return Image()
16
17
def getMapId(self, *_, **__):
18
return box.Box({"tile_fetcher": {"url_format": "url-format"}})
19
20
def updateMask(self, *_, **__):
21
return self
22
23
def blend(self, *_, **__):
24
return self
25
26
def bandNames(self, *_, **__):
27
return List(["B1", "B2"])
28
29
def reduceRegion(self, *_, **__):
30
return Dictionary({"B1": 42, "B2": 3.14})
31
32
def getInfo(self):
33
return {
34
"type": "Image",
35
"bands": [
36
{
37
"id": "band-1",
38
"data_type": {
39
"type": "PixelType",
40
"precision": "int",
41
"min": -2,
42
"max": 2,
43
},
44
"dimensions": [4, 2],
45
"crs": "EPSG:4326",
46
"crs_transform": [1, 0, -180, 0, -1, 84],
47
},
48
],
49
"version": 42,
50
"id": "some/image/id",
51
"properties": {
52
"type_name": "Image",
53
"keywords": ["keyword-1", "keyword-2"],
54
"thumb": "https://some-thumbnail.png",
55
},
56
}
57
58
59
class List:
60
def __init__(self, items, *_, **__):
61
self.items = items
62
63
def getInfo(self, *_, **__):
64
return self.items
65
66
67
class Dictionary:
68
def __init__(self, data):
69
self.data = data
70
71
def getInfo(self):
72
return self.data
73
74
75
class ReduceRegionResult:
76
def getInfo(self):
77
return
78
79
80
class Geometry:
81
geometry = None
82
83
def __init__(self, *args, **kwargs):
84
if args:
85
self.geometry = args[0]
86
if kwargs.get("type"):
87
self.geom_type = kwargs.get("type")
88
89
@staticmethod
90
def Point(*_, **__):
91
return Geometry(type=String("Point"))
92
93
@staticmethod
94
def BBox(*_, **__):
95
return Geometry(type=String("BBox"))
96
97
@staticmethod
98
def Polygon(*_, **__):
99
return Geometry(type=String("Polygon"))
100
101
def transform(self, *_, **__):
102
return Geometry(type=self.geom_type)
103
104
def bounds(self, *_, **__):
105
return Geometry.Polygon()
106
107
def centroid(self, *_, **__):
108
return Geometry.Point()
109
110
def type(self, *_, **__):
111
return self.geom_type
112
113
def getInfo(self, *_, **__):
114
the_type = self.type()
115
assert the_type is not None
116
type_value = self.type().value
117
if type_value == "Polygon":
118
return {
119
"geodesic": False,
120
"type": "Polygon",
121
"coordinates": [
122
[[-178, -76], [179, -76], [179, 80], [-178, 80], [-178, -76]]
123
],
124
}
125
if type_value == "Point":
126
return {
127
"geodesic": False,
128
"type": "Point",
129
"coordinates": [120, -70],
130
}
131
if type_value == "BBox":
132
return {
133
"geodesic": False,
134
"type": "Polygon",
135
"coordinates": [[0, 1], [1, 2], [0, 1]],
136
}
137
raise ValueError("Unexpected geometry type in test: ", type_value)
138
139
def __eq__(self, other: object):
140
return self.geometry == getattr(other, "geometry")
141
142
143
class String:
144
value: str
145
146
def __init__(self, value):
147
self.value = value
148
149
def compareTo(self, other_str) -> bool:
150
return self.value == other_str.value
151
152
def getInfo(self, *_, **__):
153
return self.value
154
155
156
class FeatureCollection:
157
features = []
158
159
def __init__(self, *args, **_):
160
if args:
161
self.features = args[0]
162
163
def style(self, *_, **__):
164
return Image()
165
166
def first(self, *_, **__):
167
return Feature()
168
169
def filterBounds(self, *_, **__):
170
return FeatureCollection()
171
172
def geometry(self, *_, **__):
173
return Geometry.Polygon()
174
175
def aggregate_array(self, *_, **__):
176
return List(["aggregation-one", "aggregation-two"])
177
178
def __eq__(self, other: object):
179
return self.features == getattr(other, "features")
180
181
182
class Feature:
183
feature = None
184
properties = None
185
186
def __init__(self, *args, **_):
187
if len(args) > 0:
188
self.feature = args[0]
189
if len(args) >= 2:
190
self.properties = args[1]
191
192
def geometry(self, *_, **__):
193
return Geometry(type=String("Polygon"))
194
195
def getInfo(self, *_, **__):
196
return {
197
"type": "Feature",
198
"geometry": {
199
"type": "LineString",
200
"coordinates": [[-67.1, 46.2], [-67.3, 46.4], [-67.5, 46.6]],
201
},
202
"id": "00000000000000000001",
203
"properties": {
204
"fullname": "some-full-name",
205
"linearid": "110469267091",
206
"mtfcc": "S1400",
207
"rttyp": "some-rttyp",
208
},
209
}
210
211
def __eq__(self, other: object):
212
featuresEqual = self.feature == getattr(other, "feature")
213
propertiesEqual = self.properties == getattr(other, "properties")
214
return featuresEqual and propertiesEqual
215
216
def propertyNames(self, *_, **__):
217
return List(["prop-1", "prop-2"])
218
219
220
class ImageCollection:
221
def __init__(self, images: list[Image], *_, **__):
222
self.images = images
223
224
def mosaic(self, *_, **__):
225
return Image()
226
227
def getInfo(self):
228
return {
229
"type": "ImageCollection",
230
"bands": [],
231
"features": [f.getInfo() for f in self.images],
232
}
233
234
235
class Reducer:
236
@classmethod
237
def first(cls, *_, **__):
238
return Reducer()
239
240
241
class Algorithms:
242
@classmethod
243
def If(cls, *_, **__):
244
return Algorithms()
245
246