forked from GISGIT/GEE-Python-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathee_types.py
More file actions
executable file
·129 lines (94 loc) · 2.95 KB
/
Copy pathee_types.py
File metadata and controls
executable file
·129 lines (94 loc) · 2.95 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
#!/usr/bin/env python
"""A set of utilities to work with EE types."""
# Using lowercase function naming to match the JavaScript names.
# pylint: disable=g-bad-name
# pylint: disable=g-bad-import-order
import datetime
import numbers
import six
from . import computedobject
# A dictionary of the classes in the ee module. Set by registerClasses.
_registered_classes = {}
def _registerClasses(classes):
"""Registers the known classes.
Args:
classes: A dictionary of the classes available in the ee module.
"""
global _registered_classes
_registered_classes = classes
def classToName(klass):
"""Converts a class to the API-friendly type name.
Args:
klass: The class.
Returns:
The name of the class, or "Object" if not recognized.
"""
if issubclass(klass, computedobject.ComputedObject):
return klass.name()
elif issubclass(klass, numbers.Number):
return 'Number'
elif issubclass(klass, six.string_types):
return 'String'
elif issubclass(klass, (list, tuple)):
return 'Array'
elif issubclass(klass, datetime.datetime):
return 'Date'
else:
return 'Object'
def nameToClass(name):
"""Converts a class name to a class. Returns None if not an ee class.
Args:
name: The class name.
Returns:
The named class.
"""
return _registered_classes.get(name)
def isSubtype(firstType, secondType):
"""Checks whether a type is a subtype of another.
Args:
firstType: The first type name.
secondType: The second type name.
Returns:
Whether secondType is a subtype of firstType.
"""
if secondType == firstType:
return True
if firstType == 'Element':
return secondType in ('Element', 'Image', 'Feature',
'Collection', 'ImageCollection', 'FeatureCollection')
elif firstType in ('FeatureCollection', 'Collection'):
return secondType in ('Collection', 'ImageCollection', 'FeatureCollection')
elif firstType == object:
return True
else:
return False
def isNumber(obj):
"""Returns true if this object is a number or number variable.
Args:
obj: The object to check.
Returns:
Whether the object is a number or number variable.
"""
return (isinstance(obj, numbers.Number) or
(isinstance(obj, computedobject.ComputedObject) and
obj.name() == 'Number'))
def isString(obj):
"""Returns true if this object is a string or string variable.
Args:
obj: The object to check.
Returns:
Whether the object is a string or string variable.
"""
return (isinstance(obj, six.string_types) or
(isinstance(obj, computedobject.ComputedObject) and
obj.name() == 'String'))
def isArray(obj):
"""Returns true if this object is an array or array variable.
Args:
obj: The object to check.
Returns:
Whether the object is an array or array variable.
"""
return (isinstance(obj, (list, tuple)) or
(isinstance(obj, computedobject.ComputedObject) and
obj.name() == 'List'))