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 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 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) from_numeric_id = ee.FeatureCollection(123456) self.assertEquals(ee.ApiFunction.lookup('Collection.loadTable'), from_numeric_id.func) self.assertEquals({'tableId': 123456}, from_numeric_id.args) from_numeric_id_and_geom_column = ee.FeatureCollection(123456, 'xyz') self.assertEquals(ee.ApiFunction('Collection.loadTable'), from_numeric_id_and_geom_column.func) self.assertEquals({ 'tableId': 123456, 'geometryColumn': 'xyz' }, from_numeric_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 testDynamicClasses(self): """Verifies dynamic class initialization.""" # Use a custom set of known functions. def MockAlgorithms(): return { 'Array': { 'type': 'Algorithm', 'args': [ { 'name': 'values', 'type': 'Serializable', 'description': '' } ], 'description': '', 'returns': 'Array' }, 'Array.cos': { 'type': 'Algorithm', 'args': [ { 'type': 'Array', 'description': '', 'name': 'input' } ], 'description': '', 'returns': 'Array' }, 'Kernel.circle': { 'returns': 'Kernel', 'args': [ { 'type': 'float', 'description': '', 'name': 'radius', }, { 'default': 1.0, 'type': 'float', 'optional': True, 'description': '', 'name': 'scale' }, { 'default': True, 'type': 'boolean', 'optional': True, 'description': '', 'name': 'normalize' } ], 'type': 'Algorithm', 'description': '' }, 'Reducer.mean': { 'returns': 'Reducer', 'args': [] }, 'fakeFunction': { 'returns': 'Array', 'args': [ { 'type': 'Reducer', 'description': '', 'name': 'kernel', } ] } } ee.data.getAlgorithms = MockAlgorithms ee.Initialize(None) # Verify that the expected classes got generated. self.assertTrue(hasattr(ee, 'Array')) self.assertTrue(hasattr(ee, 'Kernel')) self.assertTrue(hasattr(ee.Array, 'cos')) self.assertTrue(hasattr(ee.Kernel, 'circle')) # Try out the constructors. kernel = ee.ApiFunction('Kernel.circle').call(1, 2) self.assertEqual(kernel, ee.Kernel.circle(1, 2)) array = ee.ApiFunction('Array').call([1, 2]) self.assertEqual(array, ee.Array([1, 2])) self.assertEqual(array, ee.Array(ee.Array([1, 2]))) # Try out the member function. self.assertEqual( ee.ApiFunction('Array.cos').call(array), ee.Array([1, 2]).cos()) # Test argument promotion. f1 = ee.ApiFunction('Array.cos').call([1, 2]) f2 = ee.ApiFunction('Array.cos').call(ee.Array([1, 2])) self.assertEqual(f1, f2) self.assertTrue(isinstance(f1, ee.Array)) f3 = ee.call('fakeFunction', 'mean') f4 = ee.call('fakeFunction', ee.Reducer.mean()) self.assertEqual(f3, f4) try: ee.call('fakeFunction', 'moo') self.fail() except ee.EEException as e: self.assertTrue('Unknown algorithm: Reducer.moo' in str(e))