Exemple #1
0
    def test_add_bulk_and_update_with_id(self, vector_client):
        def create_features(product_id, attributes):
            return DotDict(data=[
                dict(id=attr['properties']['id'], attributes=attr)
                for attr in attributes
            ])

        vector_client.create_features.side_effect = create_features

        fc = FeatureCollection('foo', vector_client=vector_client)

        features = [
            Feature(geometry=POINT, properties=dict(id='bar')),
            Feature(geometry=POINT, properties=dict(id='bar2'))
        ]

        modified_features = fc.add(features)

        vector_client.create_features.assert_called_once_with(
            'foo', [
                dict(
                    geometry=getattr(features[0].geometry, '__geo_interface__',
                                     features[0].geometry),
                    properties=features[0].properties),
                dict(
                    geometry=getattr(features[1].geometry, '__geo_interface__',
                                     features[1].geometry),
                    properties=features[1].properties),
            ])

        for f in modified_features:
            # the side_effect above uses properties.id instead of uuid
            self.assertEqual(f.id, f.properties.id)
Exemple #2
0
    def test__create_from_jsonapi(self):
        r = DotDict(id="foo",
                    attributes=dict(geometry=POINT,
                                    properties=dict(foo="bar")))
        feature = Feature._create_from_jsonapi(r)

        assert feature.id is not None
        assert feature.geometry is not None
        assert feature.properties is not None
Exemple #3
0
    def test__create_from_jsonapi(self):
        r = DotDict(id='foo',
                    attributes=dict(geometry=POINT,
                                    properties=dict(foo='bar')))
        feature = Feature._create_from_jsonapi(r)

        self.assertIsNotNone(feature.id)
        self.assertIsNotNone(feature.geometry)
        self.assertIsNotNone(feature.properties)
Exemple #4
0
    def test_add_single(self, vector_client):
        vector_client = mock.MagicMock()
        fc = FeatureCollection('foo', vector_client=vector_client)

        feature = Feature(geometry=POINT, properties={})

        fc.add(feature)
        vector_client.create_features.assert_called_once_with(
            'foo', [
                dict(geometry=getattr(feature.geometry, '__geo_interface__',
                                      feature.geometry),
                     properties=feature.properties)
            ])
Exemple #5
0
    def test_add_bulk_and_update_with_id(self, vector_client):
        def create_features(product_id, attributes, fix_geometry="accept"):
            return DotDict(data=[
                dict(id=attr["properties"]["id"], attributes=attr)
                for attr in attributes
            ])

        vector_client.create_features.side_effect = create_features

        fc = FeatureCollection("foo", vector_client=vector_client)

        features = [
            Feature(geometry=POINT, properties=dict(id="bar")),
            Feature(geometry=POINT, properties=dict(id="bar2")),
        ]

        modified_features = fc.add(features)

        vector_client.create_features.assert_called_once_with(
            "foo",
            [
                dict(
                    geometry=getattr(features[0].geometry, "__geo_interface__",
                                     features[0].geometry),
                    properties=features[0].properties,
                ),
                dict(
                    geometry=getattr(features[1].geometry, "__geo_interface__",
                                     features[1].geometry),
                    properties=features[1].properties,
                ),
            ],
            fix_geometry="accept",
        )

        for f in modified_features:
            # the side_effect above uses properties.id instead of uuid
            assert f.id == f.properties.id
Exemple #6
0
    def test_geojson_geometries(self):
        geometries = [POLYGON, POINT]
        properties = {"temperature": 70.13, "size": "large"}

        for geometry in geometries:
            feature = Feature(geometry=geometry, properties=properties)

            assert json.dumps(feature.geojson, sort_keys=True) == json.dumps(
                {
                    "geometry": geometry,
                    "id": None,
                    "properties": properties,
                    "type": "Feature",
                },
                sort_keys=True,
            )
Exemple #7
0
    def test_geojson_geometries(self):
        geometries = [POLYGON, POINT]
        properties = {"temperature": 70.13, "size": "large"}

        for geometry in geometries:
            feature = Feature(geometry=geometry, properties=properties)

            self.assertEqual(
                json.dumps(feature.geojson, sort_keys=True),
                json.dumps(
                    {
                        'geometry': geometry,
                        'id': None,
                        'properties': properties
                    },
                    sort_keys=True))
Exemple #8
0
    def test_add_single(self, vector_client):
        vector_client = mock.MagicMock()
        fc = FeatureCollection("foo", vector_client=vector_client)

        feature = Feature(geometry=POINT, properties={})

        fc.add(feature)
        vector_client.create_features.assert_called_once_with(
            "foo",
            [
                dict(
                    geometry=getattr(feature.geometry, "__geo_interface__",
                                     feature.geometry),
                    properties=feature.properties,
                )
            ],
            fix_geometry="accept",
        )
Exemple #9
0
    def test___init__(self):
        feature = Feature(geometry=POINT, properties={})

        assert feature.id is None
        assert feature.geometry is not None
        assert feature.properties is not None
Exemple #10
0
    def test___init__(self):
        feature = Feature(geometry=POINT, properties={})

        self.assertIsNone(feature.id)
        self.assertIsNotNone(feature.geometry)
        self.assertIsNotNone(feature.properties)
        [-104.930419921875, 36.94550173495345],
        [-104.930419921875, 37.70120736474139],
        [-105.86975097656249, 37.70120736474139],
        [-105.86975097656249, 36.94550173495345],
    ]],
}

################################################
# Let's create a bunch of :class:`Feature <descarteslabs.vectors.feature.Feature>` for this product and add it using
# :meth:`FeatureCollection.add <descarteslabs.vectors.featurecollection.FeatureCollection.add>`.
# Each feature has a 'count' property with a value 'number n' where 'n' is
# in the range 0 through 19.  In addition there is feature with value
# 'number _' and  a feature with value 'number %'.

features = [
    Feature(geometry=polygon, properties={"count": "number %d" % count})
    for count in range(0, 20)
]

features.append(Feature(geometry=polygon, properties={"count": "number %"}))
features.append(Feature(geometry=polygon, properties={"count": "number _"}))

fc.add(features)

################################################
# Search for :class:`Feature <descarteslabs.vectors.feature.Feature>` in this
# :class:`FeatureCollection <descarteslabs.vectors.featurecollection.FeatureCollection>`
# where ``count=number 5``.
# This returns a single result.

features = fc.filter(properties=(p.count.like("number 5"))).features()
Exemple #12
0
fc = FeatureCollection(id_)
print(fc)

################################################
# Let's create a :class:`Feature <descarteslabs.vectors.feature.Feature>` for this product and add it using
# :meth:`FeatureCollection.add <descarteslabs.vectors.featurecollection.FeatureCollection.add>`.

feature = Feature(
    geometry={
        "type": "Polygon",
        "coordinates": [
            [
                [-105.86975097656249, 36.94550173495345],
                [-104.930419921875, 36.94550173495345],
                [-104.930419921875, 37.70120736474139],
                [-105.86975097656249, 37.70120736474139],
                [-105.86975097656249, 36.94550173495345],
            ]
        ],
    },
    properties={"foo": "bar"},
)

fc.add([feature])

################################################
# Search for features in this product intersecting an aoi using
# :meth:`FeatureCollection.filter <descarteslabs.vectors.featurecollection.FeatureCollection.filter>`
# and :meth:`FeatureCollection.features <descarteslabs.vectors.featurecollection.FeatureCollection.features>`.