Пример #1
0
    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'])
Пример #2
0
    def testSortAndLimit(self):
        """Verifies the behavior of the sort() and limit() methods."""
        collection = ee.Collection(ee.Function(), {})

        limited = collection.limit(10)
        self.assertEquals(ee.ApiFunction.lookup('Collection.limit'),
                          limited.func)
        self.assertEquals({
            'collection': collection,
            'limit': 10
        }, limited.args)

        sorted_collection = collection.sort('bar', True)
        self.assertEquals(ee.ApiFunction.lookup('Collection.limit'),
                          sorted_collection.func)
        self.assertEquals(
            {
                'collection': collection,
                'key': ee.String('bar'),
                'ascending': True
            }, sorted_collection.args)

        reverse_sorted_collection = collection.sort('bar', False)
        self.assertEquals(ee.ApiFunction.lookup('Collection.limit'),
                          reverse_sorted_collection.func)
        self.assertEquals(
            {
                'collection': collection,
                'key': ee.String('bar'),
                'ascending': False
            }, reverse_sorted_collection.args)
Пример #3
0
    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'])
Пример #4
0
  def testFilter(self):
    """Verifies the behavior of filter() method."""
    collection = ee.Collection(ee.Function(), {})

    # We don't allow empty filters.
    self.assertRaises(Exception, collection.filter)

    filtered = collection.filter(ee.Filter.eq('foo', 1))
    self.assertEqual(ee.ApiFunction.lookup('Collection.filter'), filtered.func)
    self.assertEqual({
        'collection': collection,
        'filter': ee.Filter.eq('foo', 1)
    }, filtered.args)
    self.assertIsInstance(filtered, ee.Collection)
Пример #5
0
    def testFilterShortcuts(self):
        """Verifies the behavior of the various filtering shortcut methods."""
        collection = ee.Collection(ee.Function(), {})
        geom = {'type': 'Polygon', 'coordinates': [[[1, 2], [3, 4]]]}
        d1 = datetime.datetime.strptime('1/1/2000', '%m/%d/%Y')
        d2 = datetime.datetime.strptime('1/1/2001', '%m/%d/%Y')

        self.assertEquals(collection.filter(ee.Filter.geometry(geom)),
                          collection.filterBounds(geom))
        self.assertEquals(collection.filter(ee.Filter.date(d1)),
                          collection.filterDate(d1))
        self.assertEquals(collection.filter(ee.Filter.date(d1, d2)),
                          collection.filterDate(d1, d2))
        self.assertEquals(collection.filter(ee.Filter.eq('foo', 13)),
                          collection.filterMetadata('foo', 'equals', 13))
Пример #6
0
    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)
#!/usr/bin/env python
"""Tests for the ee.function module."""

import unittest

import ee

# A function to experiment on.
TEST_FUNC = ee.Function()
TEST_FUNC.getSignature = lambda: {  # pylint: disable-msg=g-long-lambda
    'description':
    'Method description.',
    'returns':
    'Image',
    'args': [{
        'type': 'Image',
        'name': 'a',
        'description': 'Arg A doc.'
    }, {
        'type': 'Image',
        'name': 'b',
        'description': 'Arg B doc.',
        'optional': True
    }]
}

EXPECTED_DOC = """Method description.

Args:
  a: Arg A doc.
  b: Arg B doc."""