def testConstructors(self): """Verifies that constructors understand valid parameters.""" point = ee.Geometry.Point(1, 2) from_geometry = ee.Feature(point) self.assertEquals(ee.ApiFunction('Feature'), from_geometry.func) self.assertEquals({ 'geometry': point, 'metadata': None }, from_geometry.args) from_null_geometry = ee.Feature(None, {'x': 2}) self.assertEquals(ee.ApiFunction('Feature'), from_null_geometry.func) self.assertEquals({ 'geometry': None, 'metadata': { 'x': 2 } }, from_null_geometry.args) computed_geometry = ee.Geometry( ee.ComputedObject(ee.Function(), {'a': 1})) computed_properties = ee.ComputedObject(ee.Function(), {'b': 2}) from_computed_one = ee.Feature(computed_geometry) from_computed_both = ee.Feature(computed_geometry, computed_properties) self.assertEquals(ee.ApiFunction('Feature'), from_computed_one.func) self.assertEquals({ 'geometry': computed_geometry, 'metadata': None }, from_computed_one.args) self.assertEquals(ee.ApiFunction('Feature'), from_computed_both.func) self.assertEquals( { 'geometry': computed_geometry, 'metadata': computed_properties }, from_computed_both.args) from_variable = ee.Feature(ee.CustomFunction.variable(None, 'foo')) self.assertTrue(isinstance(from_variable, ee.Feature)) self.assertEquals({ 'type': 'ArgumentRef', 'value': 'foo' }, from_variable.encode(None)) from_geo_json_feature = ee.Feature({ 'type': 'Feature', 'id': 'bar', 'geometry': point.toGeoJSON(), 'properties': { 'foo': 42 } }) self.assertEquals(ee.ApiFunction('Feature'), from_geo_json_feature.func) self.assertEquals(point, from_geo_json_feature.args['geometry']) self.assertEquals({ 'foo': 42, 'system:index': 'bar' }, from_geo_json_feature.args['metadata'])
def testCallAndApply(self): """Verifies library initialization.""" # Use a custom set of known functions. def MockSend(path, params, unused_method=None, unused_raw=None): if path == '/algorithms': return { 'fakeFunction': { 'type': 'Algorithm', 'args': [ {'name': 'image1', 'type': 'Image'}, {'name': 'image2', 'type': 'Image'} ], 'returns': 'Image' }, 'Image.constant': apitestcase.BUILTIN_FUNCTIONS['Image.constant'] } else: raise Exception('Unexpected API call to %s with %s' % (path, params)) ee.data.send_ = MockSend ee.Initialize(None) image1 = ee.Image(1) image2 = ee.Image(2) expected = ee.Image(ee.ComputedObject( ee.ApiFunction.lookup('fakeFunction'), {'image1': image1, 'image2': image2})) applied_with_images = ee.apply( 'fakeFunction', {'image1': image1, 'image2': image2}) self.assertEquals(expected, applied_with_images) applied_with_numbers = ee.apply('fakeFunction', {'image1': 1, 'image2': 2}) self.assertEquals(expected, applied_with_numbers) called_with_numbers = ee.call('fakeFunction', 1, 2) self.assertEquals(expected, called_with_numbers) # Test call and apply() with a custom function. sig = {'returns': 'Image', 'args': [{'name': 'foo', 'type': 'Image'}]} func = ee.CustomFunction(sig, lambda foo: ee.call('fakeFunction', 42, foo)) expected_custom_function_call = ee.Image( ee.ComputedObject(func, {'foo': ee.Image(13)})) self.assertEquals(expected_custom_function_call, ee.call(func, 13)) self.assertEquals(expected_custom_function_call, ee.apply(func, {'foo': 13})) # Test None promotion. called_with_null = ee.call('fakeFunction', None, 1) self.assertEquals(None, called_with_null.args['image1'])
def testImageCollectionConstructors(self): """Verifies that constructors understand valid parameters.""" from_id = ee.ImageCollection('abcd') self.assertEqual(ee.ApiFunction.lookup('ImageCollection.load'), from_id.func) self.assertEqual({'id': 'abcd'}, from_id.args) from_images = ee.ImageCollection([ee.Image(1), ee.Image(2)]) self.assertEqual(ee.ApiFunction.lookup('ImageCollection.fromImages'), from_images.func) self.assertEqual({'images': [ee.Image(1), ee.Image(2)]}, from_images.args) self.assertEqual(ee.ImageCollection([ee.Image(1)]), ee.ImageCollection(ee.Image(1))) original = ee.ImageCollection('foo') from_other_image_collection = ee.ImageCollection(original) self.assertEqual(from_other_image_collection, original) l = ee.List([ee.Image(1)]).slice(0) from_list = ee.ImageCollection(l) self.assertEqual({'images': l}, from_list.args) from_computed_object = ee.ImageCollection( ee.ComputedObject(None, {'x': 'y'})) self.assertEqual({'x': 'y'}, from_computed_object.args)
def testConstructors(self): """Verifies that constructors understand valid parameters.""" from_id = ee.FeatureCollection('abcd') self.assertEquals(ee.ApiFunction.lookup('Collection.loadTable'), from_id.func) self.assertEquals({'tableId': 'abcd'}, from_id.args) from_id_and_geom_column = ee.FeatureCollection('abcd', 'xyz') self.assertEquals(ee.ApiFunction.lookup('Collection.loadTable'), from_id_and_geom_column.func) self.assertEquals({'tableId': 'abcd', 'geometryColumn': 'xyz'}, from_id_and_geom_column.args) geometry = ee.Geometry.Point(1, 2) feature = ee.Feature(geometry) from_geometries = ee.FeatureCollection([geometry]) from_single_geometry = ee.FeatureCollection(geometry) from_features = ee.FeatureCollection([feature]) from_single_feature = ee.FeatureCollection(feature) self.assertEquals(from_geometries, from_single_geometry) self.assertEquals(from_geometries, from_features) self.assertEquals(from_geometries, from_single_feature) self.assertEquals(ee.ApiFunction.lookup('Collection'), from_geometries.func) self.assertEquals({'features': [feature]}, from_geometries.args) # Test a computed list object. l = ee.List([feature]).slice(0) from_list = ee.FeatureCollection(l) self.assertEquals({'features': l}, from_list.args) from_computed_object = ee.FeatureCollection( ee.ComputedObject(None, {'x': 'y'})) self.assertEquals({'x': 'y'}, from_computed_object.args)
def testSet(self): """Verifies Element.set() keyword argument interpretation.""" image = ee.Image(1) # Constant dictionary. def AssertProperties(expected, image): properties = {} while image.func == ee.ApiFunction.lookup('Element.set'): key = image.args['key'] if not isinstance(key, basestring): key = key.encode() properties[key] = image.args['value'] image = image.args['object'] self.assertEquals(ee.Image(1), image) self.assertEquals(expected, properties) AssertProperties({'foo': 'bar'}, image.set({'foo': 'bar'})) AssertProperties({'foo': 'bar'}, image.set({'properties': {'foo': 'bar'}})) AssertProperties({'properties': 5}, image.set({'properties': 5})) AssertProperties({'properties': {'foo': 'bar'}, 'baz': 'quux'}, image.set({'properties': {'foo': 'bar'}, 'baz': 'quux'})) AssertProperties({'foo': 'bar', 'baz': 'quux'}, image.set('foo', 'bar', 'baz', 'quux')) # Computed dictionary. computed_arg = ee.ComputedObject(None, None, 'foo') def CheckMultiProperties(result): self.assertEquals(ee.ApiFunction.lookup('Element.setMulti'), result.func) self.assertEquals( {'object': image, 'properties': ee.Dictionary(computed_arg)}, result.args) CheckMultiProperties(image.set(computed_arg)) CheckMultiProperties(image.set({'properties': computed_arg}))
def testConstructors(self): """Verifies that constructors understand valid parameters.""" point = ee.Geometry.Point(1, 2) from_geometry = ee.Feature(point) self.assertEquals(ee.ApiFunction('Feature'), from_geometry.func) self.assertEquals({ 'geometry': point, 'metadata': None }, from_geometry.args) from_null_geometry = ee.Feature(None, {'x': 2}) self.assertEquals(ee.ApiFunction('Feature'), from_null_geometry.func) self.assertEquals({ 'geometry': None, 'metadata': { 'x': 2 } }, from_null_geometry.args) computed_geometry = ee.Geometry( ee.ComputedObject(ee.Function(), {'a': 1})) computed_properties = ee.ComputedObject(ee.Function(), {'b': 2}) from_computed_one = ee.Feature(computed_geometry) from_computed_both = ee.Feature(computed_geometry, computed_properties) self.assertEquals(ee.ApiFunction('Feature'), from_computed_one.func) self.assertEquals({ 'geometry': computed_geometry, 'metadata': None }, from_computed_one.args) self.assertEquals(ee.ApiFunction('Feature'), from_computed_both.func) self.assertEquals( { 'geometry': computed_geometry, 'metadata': computed_properties }, from_computed_both.args) from_geo_json_feature = ee.Feature({ 'type': 'Feature', 'geometry': point.toGeoJSON(), 'properties': { 'foo': 42 } }) self.assertEquals(ee.ApiFunction('Feature'), from_geo_json_feature.func) self.assertEquals(point, from_geo_json_feature.args['geometry']) self.assertEquals({'foo': 42}, from_geo_json_feature.args['metadata'])
def testPromotion(self): """Verifies object promotion rules.""" self.InitializeApi() # Features and Images are both already Elements. self.assertTrue(isinstance(ee._Promote(ee.Feature(None), 'Element'), ee.Feature)) self.assertTrue(isinstance(ee._Promote(ee.Image(0), 'Element'), ee.Image)) # Promote an untyped object to an Element. untyped = ee.ComputedObject('foo', {}) self.assertTrue(isinstance(ee._Promote(untyped, 'Element'), ee.Element)) # Promote an untyped variable to an Element. untyped = ee.ComputedObject(None, None, 'foo') self.assertTrue(isinstance(ee._Promote(untyped, 'Element'), ee.Element)) self.assertEqual('foo', ee._Promote(untyped, 'Element').varName)
def testConstructors(self): """Verifies that constructors understand valid parameters.""" from_constant = ee.Image(1) self.assertEquals(ee.ApiFunction.lookup('Image.constant'), from_constant.func) self.assertEquals({'value': 1}, from_constant.args) array_constant = ee.Array([1, 2]) from_array_constant = ee.Image(array_constant) self.assertEquals(ee.ApiFunction.lookup('Image.constant'), from_array_constant.func) self.assertEquals({'value': array_constant}, from_array_constant.args) from_id = ee.Image('abcd') self.assertEquals(ee.ApiFunction.lookup('Image.load'), from_id.func) self.assertEquals({'id': 'abcd'}, from_id.args) from_array = ee.Image([1, 2]) self.assertEquals(ee.ApiFunction.lookup('Image.addBands'), from_array.func) self.assertEquals({ 'dstImg': ee.Image(1), 'srcImg': ee.Image(2) }, from_array.args) from_computed_object = ee.Image(ee.ComputedObject(None, {'x': 'y'})) self.assertEquals({'x': 'y'}, from_computed_object.args) original = ee.Image(1) from_other_image = ee.Image(original) self.assertEquals(from_other_image, original) from_nothing = ee.Image() self.assertEquals(ee.ApiFunction.lookup('Image.mask'), from_nothing.func) self.assertEquals({ 'image': ee.Image(0), 'mask': ee.Image(0) }, from_nothing.args) from_id_and_version = ee.Image('abcd', 123) self.assertEquals(ee.ApiFunction.lookup('Image.load'), from_id_and_version.func) self.assertEquals({ 'id': 'abcd', 'version': 123 }, from_id_and_version.args) from_variable = ee.Image(ee.CustomFunction.variable(None, 'foo')) self.assertTrue(isinstance(from_variable, ee.Image)) self.assertEquals({ 'type': 'ArgumentRef', 'value': 'foo' }, from_variable.encode(None))
def testSerialization(self): """Verifies a complex serialization case.""" class ByteString(ee.Encodable): """A custom Encodable class that does not use invocations. This one is actually supported by the EE API encoding. """ def __init__(self, value): """Creates a bytestring with a given string value.""" self._value = value def encode(self, unused_encoder): # pylint: disable-msg=g-bad-name return { 'type': 'Bytes', 'value': self._value } call = ee.ComputedObject('String.cat', {'string1': 'x', 'string2': 'y'}) body = lambda x, y: ee.CustomFunction.variable(None, 'y') sig = {'returns': 'Object', 'args': [ {'name': 'x', 'type': 'Object'}, {'name': 'y', 'type': 'Object'}]} custom_function = ee.CustomFunction(sig, body) to_encode = [ None, True, 5, 7, 3.4, 2.5, 'hello', ee.Date(1234567890000), ee.Geometry(ee.Geometry.LineString(1, 2, 3, 4), 'SR-ORG:6974'), ee.Geometry.Polygon([ [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], [[5, 6], [7, 6], [7, 8], [5, 8]], [[1, 1], [2, 1], [2, 2], [1, 2]] ]), ByteString('aGVsbG8='), { 'foo': 'bar', 'baz': call }, call, custom_function ] self.assertEquals(apitestcase.ENCODED_JSON_SAMPLE, json.loads(serializer.toJSON(to_encode)))
def testConstructor(self): """Check the behavior of the Geometry constructor. There are 5 options: 1) A geoJSON object. 2) A not-computed geometry. 3) A not-computed geometry with overrides. 4) A computed geometry. 5) something to cast to geometry. """ line = ee.Geometry.LineString(1, 2, 3, 4) # GeoJSON. from_json = ee.Geometry(line.toGeoJSON()) self.assertEquals(from_json.func, None) self.assertEquals(from_json._type, 'LineString') self.assertEquals(from_json._coordinates, [[1, 2], [3, 4]]) # GeoJSON with a CRS specified. json_with_crs = line.toGeoJSON() json_with_crs['crs'] = { 'type': 'name', 'properties': { 'name': 'SR-ORG:6974' } } from_json_with_crs = ee.Geometry(json_with_crs) self.assertEquals(from_json_with_crs.func, None) self.assertEquals(from_json_with_crs._type, 'LineString') self.assertEquals(from_json_with_crs._proj, 'SR-ORG:6974') # A not-computed geometry. self.assertEquals(ee.Geometry(line), line) # A not-computed geometry with an override. with_override = ee.Geometry(line, 'SR-ORG:6974') self.assertEquals(with_override._proj, 'SR-ORG:6974') # A computed geometry. self.assertEquals(ee.Geometry(line.bounds()), line.bounds()) # Something to cast to a geometry. computed = ee.ComputedObject(ee.Function(), {'a': 1}) geom = ee.Geometry(computed) self.assertEquals(computed.func, geom.func) self.assertEquals(computed.args, geom.args)
def testConstructors(self): """Verifies that constructors understand valid parameters.""" from_constant = ee.Image(1) self.assertEquals(ee.ApiFunction.lookup('Image.constant'), from_constant.func) self.assertEquals({'value': 1}, from_constant.args) from_id = ee.Image('abcd') self.assertEquals(ee.ApiFunction.lookup('Image.load'), from_id.func) self.assertEquals({'id': 'abcd'}, from_id.args) from_array = ee.Image([1, 2]) self.assertEquals(ee.ApiFunction.lookup('Image.addBands'), from_array.func) self.assertEquals({ 'dstImg': ee.Image(1), 'srcImg': ee.Image(2) }, from_array.args) from_computed_object = ee.Image(ee.ComputedObject(None, {'x': 'y'})) self.assertEquals({'x': 'y'}, from_computed_object.args) original = ee.Image(1) from_other_image = ee.Image(original) self.assertEquals(from_other_image, original) from_nothing = ee.Image() self.assertEquals(ee.ApiFunction.lookup('Image.mask'), from_nothing.func) self.assertEquals({ 'image': ee.Image(0), 'mask': ee.Image(0) }, from_nothing.args) from_id_and_version = ee.Image('abcd', 123) self.assertEquals(ee.ApiFunction.lookup('Image.load'), from_id_and_version.func) self.assertEquals({ 'id': 'abcd', 'version': 123 }, from_id_and_version.args)
def testSerialization(self): """Verifies a complex serialization case.""" class ByteString(ee.Encodable): """A custom Encodable class that does not use invocations. This one is actually supported by the EE API encoding. """ def __init__(self, value): """Creates a bytestring with a given string value.""" self._value = value def encode(self, unused_encoder): # pylint: disable-msg=g-bad-name return {'type': 'Bytes', 'value': self._value} def encode_cloud_value(self, unused_encoder): # Proto3 JSON embedding of "bytes" values uses base64 encoding, which # this already is. return {'bytesValue': self._value} call = ee.ComputedObject('String.cat', { 'string1': 'x', 'string2': 'y' }) body = lambda x, y: ee.CustomFunction.variable(None, 'y') sig = { 'returns': 'Object', 'args': [{ 'name': 'x', 'type': 'Object' }, { 'name': 'y', 'type': 'Object' }] } custom_function = ee.CustomFunction(sig, body) to_encode = [ None, True, 5, 7, 3.4, 112233445566778899, 'hello', ee.Date(1234567890000), ee.Geometry(ee.Geometry.LineString(1, 2, 3, 4), 'SR-ORG:6974'), ee.Geometry.Polygon([[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], [[5, 6], [7, 6], [7, 8], [5, 8]], [[1, 1], [2, 1], [2, 2], [1, 2]]]), ByteString('aGVsbG8='), { 'foo': 'bar', 'baz': call }, call, custom_function ] self.assertEqual( apitestcase.ENCODED_JSON_SAMPLE, json.loads(serializer.toJSON(to_encode, for_cloud_api=False))) encoded = serializer.encode(to_encode, for_cloud_api=True) self.assertEqual(apitestcase.ENCODED_CLOUD_API_JSON_SAMPLE, encoded) pretty_encoded = serializer.encode(to_encode, is_compound=False, for_cloud_api=True) self.assertEqual(apitestcase.ENCODED_CLOUD_API_JSON_SAMPLE_PRETTY, pretty_encoded) encoded_json = serializer.toJSON(to_encode, for_cloud_api=True) decoded_encoded_json = json.loads(encoded_json) self.assertEqual(encoded, decoded_encoded_json)