forked from GISGIT/GEE-Python-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputedobject.py
More file actions
executable file
·225 lines (184 loc) · 7.08 KB
/
computedobject.py
File metadata and controls
executable file
·225 lines (184 loc) · 7.08 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
"""A representation of an Earth Engine computed object."""
# Using lowercase function naming to match the JavaScript names.
# pylint: disable=g-bad-name
# pylint: disable=g-bad-import-order
import six
from . import data
from . import ee_exception
from . import encodable
from . import serializer
class ComputedObjectMetaclass(type):
"""A meta-class that makes type coercion idempotent.
If an instance of a ComputedObject subclass is instantiated by passing
another instance of that class as the sole argument, this short-circuits
and returns that argument.
"""
def __call__(cls, *args, **kwargs):
"""Creates a computed object, catching self-casts."""
if len(args) == 1 and not kwargs and isinstance(args[0], cls):
# Self-casting returns the argument unchanged.
return args[0]
else:
return type.__call__(cls, *args, **kwargs)
class ComputedObject(six.with_metaclass(
ComputedObjectMetaclass, encodable.Encodable)):
"""A representation of an Earth Engine computed object.
This is a base class for most API objects.
The class itself is not abstract as it is used to wrap the return values of
algorithms that produce unrecognized types with the minimal functionality
necessary to interact well with the rest of the API.
ComputedObjects come in two flavors:
1. If func != null and args != null, the ComputedObject is encoded as an
invocation of func with args.
2. If func == null and args == null, the ComputedObject is a variable
reference. The variable name is stored in its varName member. Note that
in this case, varName may still be null; this allows the name to be
deterministically generated at a later time. This is used to generate
deterministic variable names for mapped functions, ensuring that nested
mapping calls do not use the same variable name.
"""
def __init__(self, func, args, opt_varName=None):
"""Creates a computed object.
Args:
func: The ee.Function called to compute this object, either as an
Algorithm name or an ee.Function object.
args: A dictionary of arguments to pass to the specified function.
Note that the caller is responsible for promoting the arguments
to the correct types.
opt_varName: A variable name. If not None, the object will be encoded
as a reference to a CustomFunction variable of this name, and both
'func' and 'args' must be None. If all arguments are None, the
object is considered an unnamed variable, and a name will be
generated when it is included in an ee.CustomFunction.
"""
if opt_varName and (func or args):
raise ee_exception.EEException(
'When "opt_varName" is specified, "func" and "args" must be null.')
self.func = func
self.args = args
self.varName = opt_varName
def __eq__(self, other):
# pylint: disable=unidiomatic-typecheck
return (type(self) == type(other) and
self.__dict__ == other.__dict__)
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash(ComputedObject.freeze(self.__dict__))
def getInfo(self):
"""Fetch and return information about this object.
Returns:
The object can evaluate to anything.
"""
return data.computeValue(self)
def encode(self, encoder):
"""Encodes the object in a format compatible with Serializer."""
if self.isVariable():
return {
'type': 'ArgumentRef',
'value': self.varName
}
else:
# Encode the function that we're calling.
func = encoder(self.func)
# Built-in functions are encoded as strings under a different key.
key = 'functionName' if isinstance(func, six.string_types) else 'function'
# Encode all arguments recursively.
encoded_args = {}
for name, value in self.args.items():
if value is not None:
encoded_args[name] = encoder(value)
return {
'type': 'Invocation',
'arguments': encoded_args,
key: func
}
def encode_cloud_value(self, encoder):
if self.isVariable():
return {'argumentReference': self.varName}
else:
if isinstance(self.func, six.string_types):
invocation = {'functionName': self.func}
else:
invocation = self.func.encode_cloud_invocation(encoder)
# Encode all arguments recursively.
encoded_args = {}
for name in sorted(self.args):
value = self.args[name]
if value is not None:
encoded_args[name] = {'valueReference': encoder(value)}
invocation['arguments'] = encoded_args
return {'functionInvocationValue': invocation}
def serialize(
self,
opt_pretty=False,
for_cloud_api=False
):
"""Serialize this object into a JSON string.
Args:
opt_pretty: A flag indicating whether to pretty-print the JSON.
for_cloud_api: Whether the encoding should be done for the Cloud API
or the legacy API.
Returns:
The serialized representation of this object.
"""
return serializer.toJSON(
self,
opt_pretty,
for_cloud_api=for_cloud_api
)
def __str__(self):
"""Writes out the object in a human-readable form."""
return 'ee.%s(%s)' % (self.name(), serializer.toReadableJSON(self))
def isVariable(self):
"""Returns whether this computed object is a variable reference."""
# We can't just check for varName != null, since we allow that
# to remain null until for CustomFunction.resolveNamelessArgs_().
return self.func is None and self.args is None
def aside(self, func, *var_args):
"""Calls a function passing this object as the first argument.
Returns the object itself for chaining. Convenient e.g. when debugging:
c = (ee.ImageCollection('foo').aside(logging.info)
.filterDate('2001-01-01', '2002-01-01').aside(logging.info)
.filterBounds(geom).aside(logging.info)
.aside(addToMap, {'min': 0, 'max': 142})
.select('a', 'b'))
Args:
func: The function to call.
*var_args: Any extra arguments to pass to the function.
Returns:
The same object, for chaining.
"""
func(self, *var_args)
return self
@classmethod
def name(cls):
"""Returns the name of the object, used in __str__()."""
return 'ComputedObject'
@classmethod
def _cast(cls, obj):
"""Cast a ComputedObject to a new instance of the same class as this.
Args:
obj: The object to cast.
Returns:
The cast object, and instance of the class on which this method is called.
"""
if isinstance(obj, cls):
return obj
else:
result = cls.__new__(cls)
result.func = obj.func
result.args = obj.args
result.varName = obj.varName
return result
@staticmethod
def freeze(obj):
"""Freeze a list or dict so it can be hashed."""
if isinstance(obj, dict):
return frozenset(
(key, ComputedObject.freeze(val)) for key, val in obj.items())
elif isinstance(obj, list):
return tuple(map(ComputedObject.freeze, obj))
else:
return obj