Esempio n. 1
0
def test_groupby_agg_returns_expected_result():
    fc = FeatureCollection([
        GeoFeature(GeoVector(Point(3, 3)), {
            'prop1': 'a',
            'b': 1
        }),
        GeoFeature(GeoVector(Point(1, 1)), {
            'prop1': 'a',
            'b': 2
        }),
        GeoFeature(GeoVector(Point(2, 2)), {
            'prop1': 'b',
            'b': 3
        })
    ])

    def first(collection):
        return collection[0]

    expected_result = FeatureCollection([
        GeoFeature(GeoVector(Point(3, 3)), {'b': 1}),
        GeoFeature(GeoVector(Point(2, 2)), {'b': 3})
    ])

    assert list(fc.groupby('prop1')['b'].agg(first)) == expected_result
Esempio n. 2
0
def test_get_tile_merge_tiles(tile):
    raster1_path = './tests/data/raster/overlap1.tif'
    raster2_path = './tests/data/raster/overlap2.tif'
    raster1 = GeoRaster2.open(raster1_path)
    raster2 = GeoRaster2.open(raster2_path)

    features = [
        GeoFeature(raster1.footprint().reproject(new_crs=WGS84_CRS),
                   {'raster_url': raster1_path, 'created': datetime.now()}),
        GeoFeature(raster2.footprint().reproject(new_crs=WGS84_CRS),
                   {'raster_url': raster2_path, 'created': datetime.now()}),
    ]

    fc = FeatureCollection(features)
    bounds = mercantile.xy_bounds(*tile)
    eroi = GeoVector.from_bounds(xmin=bounds.left, xmax=bounds.right,
                                 ymin=bounds.bottom, ymax=bounds.top,
                                 crs=WEB_MERCATOR_CRS)
    expected_tile = merge_all([raster1.get_tile(*tile), raster2.get_tile(*tile)], roi=eroi)
    merged = fc.get_tile(*tile, sort_by='created')
    if merged is not None:
        assert merged == expected_tile
    else:
        assert expected_tile.image.mask.all()
        assert (expected_tile.image.data == 0).all()
Esempio n. 3
0
def test_rasterization_function():
    sq1 = GeoFeature(
        GeoVector.from_bounds(xmin=0, ymin=2, xmax=1, ymax=3, crs=WGS84_CRS),
        {'value': 1.0}
    )
    sq2 = GeoFeature(
        GeoVector.from_bounds(xmin=1, ymin=0, xmax=3, ymax=2, crs=WGS84_CRS),
        {'value': 2.0}
    )
    fc = FeatureCollection([sq1, sq2])

    def func(feat):
        return feat['value']

    expected_image = np.ma.masked_array(
        [[
            [1.0, 0.0, 0.0],
            [0.0, 2.0, 2.0],
            [0.0, 2.0, 2.0]
        ]],
        [
            [False, True, True],
            [True, False, False],
            [True, False, False],
        ]
    )

    result = fc.rasterize(1.0, fill_value=func, crs=WGS84_CRS, dtype=np.float32)

    assert_array_equal(result.image.mask, expected_image.mask)
    assert_array_equal(result.image.data, expected_image.data)
Esempio n. 4
0
def test_featurecollection_schema_for_property_types_with_none_values():
    fc = FeatureCollection([
        GeoFeature(GeoVector(Point(0, 0)), {
            'prop1': None,
            'prop2': 1.0,
            'prop3': 'A'
        }),
        GeoFeature(GeoVector(Point(0, 0)), {
            'prop1': 2,
            'prop2': None,
            'prop3': 'B'
        }),
        GeoFeature(GeoVector(Point(0, 0)), {
            'prop1': 3,
            'prop2': 3.0,
            'prop3': None
        })
    ])

    expected_schema = {
        'geometry': 'Point',
        'properties': {
            'prop1': 'int',
            'prop2': 'float',
            'prop3': 'str'
        }
    }

    assert fc.schema == expected_schema
Esempio n. 5
0
def test_featurecollection_property_names_includes_all():
    fc = FeatureCollection([
        GeoFeature(GeoVector(Point(0, 0)), {'prop1': 1}),
        GeoFeature(GeoVector(Point(0, 0)), {'prop2': 1})
    ])

    expected_property_names = ['prop1', 'prop2']

    assert sorted(fc.property_names) == expected_property_names
Esempio n. 6
0
def test_get_values():
    fc = FeatureCollection([
        GeoFeature(GeoVector(Point(0, 0)), {'prop1': 1}),
        GeoFeature(GeoVector(Point(0, 0)), {'prop1': 2}),
        GeoFeature(GeoVector(Point(0, 0)), {'prop1': 3}),
        GeoFeature(GeoVector(Point(0, 0)), {'prop2': 1}),
    ])

    assert list(fc.get_values('prop1')) == [1, 2, 3, None]
Esempio n. 7
0
def test_featurecollection_attribute_names_includes_all():
    fc = FeatureCollection([
        GeoFeature(GeoVector(Point(0, 0)), {'attr1': 1}),
        GeoFeature(GeoVector(Point(0, 0)), {'attr2': 1})
    ])

    expected_attribute_names = ['attr1', 'attr2']

    assert sorted(fc.attribute_names) == expected_attribute_names
Esempio n. 8
0
def test_groupby_has_proper_groups():
    gfa1 = GeoFeature(GeoVector(Point(3, 3)), {'prop1': 'a'})
    gfa2 = GeoFeature(GeoVector(Point(1, 1)), {'prop1': 'a'})
    gfb1 = GeoFeature(GeoVector(Point(2, 2)), {'prop1': 'b'})
    fc = FeatureCollection([gfa1, gfa2, gfb1])

    expected_groups = [('a', FeatureCollection([gfa1, gfa2])),
                       ('b', FeatureCollection([gfb1]))]

    assert list(fc.groupby('prop1')) == expected_groups
Esempio n. 9
0
def test_featurecollection_schema_raises_error_for_heterogeneous_property_types():
    fc = FeatureCollection([
        GeoFeature(GeoVector(Point(0, 0)), {'prop1': 1}),
        GeoFeature(GeoVector(Point(0, 0)), {'prop1': 1.0})
    ])

    with pytest.raises(FeatureCollectionIOError) as excinfo:
        fc.schema

    assert "Cannot generate a schema for a heterogeneous FeatureCollection. " in excinfo.exconly()
Esempio n. 10
0
def test_featurecollection_save_has_no_side_effects():
    fc = FeatureCollection([
        GeoFeature(GeoVector(Point(0, 0)), {'attr1': 1}),
        GeoFeature(GeoVector(Point(0, 0)), {'attr2': 1})
    ])

    with tempfile.NamedTemporaryFile(suffix=".json") as fp:
        fc.save(fp.name)

        assert fc[0].attributes == {'attr1': 1}
        assert fc[1].attributes == {'attr2': 1}
Esempio n. 11
0
def test_geofeature_equality_checks_geometry_and_properties(
        geometry, properties):
    ref_geometry = GeoVector(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]))
    ref_properties = {'property_1': 1}

    feature1 = GeoFeature(ref_geometry, ref_properties)
    feature2 = GeoFeature(geometry, properties)

    if geometry == ref_geometry and properties == ref_properties:
        assert feature1 == feature2

    else:
        assert feature1 != feature2
Esempio n. 12
0
def test_groupby_with_dissolve():
    fc = FeatureCollection([
        GeoFeature(GeoVector.from_bounds(xmin=0, ymin=0, xmax=2, ymax=1, crs=DEFAULT_CRS), {'prop1': 'a', 'b': 1}),
        GeoFeature(GeoVector.from_bounds(xmin=1, ymin=0, xmax=3, ymax=1, crs=DEFAULT_CRS), {'prop1': 'a', 'b': 2}),
        GeoFeature(GeoVector.from_bounds(xmin=0, ymin=0, xmax=2, ymax=1, crs=DEFAULT_CRS), {'prop1': 'b', 'b': 3}),
    ])

    expected_result = FeatureCollection([
        GeoFeature(GeoVector.from_bounds(xmin=0, ymin=0, xmax=3, ymax=1, crs=DEFAULT_CRS), {'b': 3}),
        GeoFeature(GeoVector.from_bounds(xmin=0, ymin=0, xmax=2, ymax=1, crs=DEFAULT_CRS), {'b': 3}),
    ])

    assert fc.dissolve('prop1', sum) == fc.groupby('prop1').agg(partial(dissolve, aggfunc=sum)) == expected_result
Esempio n. 13
0
def test_rasterization_function_raises_error_if_no_dtype_is_given():
    sq1 = GeoFeature(
        GeoVector.from_bounds(xmin=0, ymin=2, xmax=1, ymax=3, crs=WGS84_CRS),
        {'value': 1.0})
    sq2 = GeoFeature(
        GeoVector.from_bounds(xmin=1, ymin=0, xmax=3, ymax=2, crs=WGS84_CRS),
        {'value': 2.0})
    fc = FeatureCollection([sq1, sq2])

    with pytest.raises(ValueError) as excinfo:
        fc.rasterize(1.0, fill_value=lambda x: 1, crs=WGS84_CRS)

    assert "dtype must be specified for multivalue rasterization" in excinfo.exconly(
    )
Esempio n. 14
0
def test_featurecollection_schema_treat_unsupported_property_types_as_str():
    fc = FeatureCollection([
        GeoFeature(GeoVector(Point(0, 0)), {'prop1': bool(0), 'prop2': date(2018, 5, 19)}),
        GeoFeature(GeoVector(Point(0, 0)), {'prop1': bool(1), 'prop2': date(2018, 5, 20)})
    ])

    expected_schema = {
        'geometry': 'Point',
        'properties': {
            'prop1': 'str',
            'prop2': 'str'
        }
    }

    assert fc.schema == expected_schema
Esempio n. 15
0
def test_delegated_unary_predicates(predicate_name):
    feature = GeoFeature(
        GeoVector(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])),
        {}
    )

    assert getattr(feature, predicate_name) == getattr(feature.geometry, predicate_name)
Esempio n. 16
0
def test_geofeature_str():
    expected_geovector = GeoVector(Point(0.0, 0.0))
    expected_properties = {'property_1': 1}

    res = GeoFeature(expected_geovector, expected_properties)

    assert str(res) == "GeoFeature(Point, {'property_1': 1})"
Esempio n. 17
0
def test_geofeature_str():
    expected_geovector = GeoVector(Point(0.0, 0.0))
    expected_attributes = {'attribute_1': 1}

    res = GeoFeature(expected_geovector, expected_attributes)

    assert str(res) == "GeoFeature(Point, {'attribute_1': 1})"
Esempio n. 18
0
def test_featurecollection_apply_multiple_statements():
    fc = fc_generator(5)
    new_fc = fc.apply(prop1=3)
    new_fc_2 = new_fc.apply(prop2=lambda f: f['prop1'] + 2, prop3='aaa')

    assert new_fc_2 == FeatureCollection([GeoFeature(feat.geometry, {'prop1': 3, 'prop2': 5, 'prop3': 'aaa'})
                                          for feat in fc])
Esempio n. 19
0
def test_delegated_properties(property_name):
    feature = GeoFeature(
        GeoVector(Point(0, 10)),
        {}
    )

    assert getattr(feature, property_name) == getattr(feature.geometry, property_name)
Esempio n. 20
0
def test_feature_collection_with_dates_serializes_correctly():
    # "For Shapefiles, however, the only possible field type is 'date' as 'datetime' and 'time' are not available."
    # https://github.com/Toblerity/Fiona/pull/130
    # See also: https://github.com/Toblerity/Fiona/issues/572
    schema = {
        'geometry': 'Point',
        'properties': OrderedDict([
            ('prop_date', 'date'),
        ]),
    }
    expected_attributes = {
        'prop_date': date(2018, 4, 23),
    }
    feature = GeoFeature(GeoVector(Point(0, 0)), expected_attributes)
    with tempfile.TemporaryDirectory() as path:
        file_path = os.path.join(path, "test_dates.shp")
        with fiona.open(file_path,
                        mode='w',
                        driver="ESRI Shapefile",
                        schema=schema,
                        crs=feature.crs) as sink:
            sink.write(mapping(feature))

        fc = FileCollection.open(file_path)

        assert fc.schema == schema
        assert fc[0].geometry == feature.geometry
        assert fc[0].attributes == expected_attributes
Esempio n. 21
0
def test_rasterization_raise_error_for_too_big_image():
    shape = Polygon([(0, 0), (1, 0), (1, -1), (0, -1)])
    fcol = FeatureCollection([GeoFeature(GeoVector(shape), {})])

    with pytest.raises(ScaleError) as excinfo:
        fcol.rasterize(1e-50)
    assert "Scale is too fine, increase it for a smaller image" in excinfo.exconly()
Esempio n. 22
0
def test_delegated_properties(property_name):
    expected_properties = {'property_1': 1}
    feature = GeoFeature(GeoVector(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])),
                         expected_properties)

    assert getattr(feature,
                   property_name).geometry == getattr(feature.geometry,
                                                      property_name)
Esempio n. 23
0
def test_delegated_operations(operation_name):
    properties_1 = {'property_1': 1}
    properties_2 = {'property_2': 2}
    feature_1 = GeoFeature(
        GeoVector(Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])), properties_1)
    feature_2 = GeoFeature(
        GeoVector(Polygon([(0.5, 0), (1.5, 0), (1.5, 1), (0.5, 1)])),
        properties_2)
    expected_properties = {'property_1': 1, 'property_2': 2}

    assert (getattr(feature_1, operation_name)(feature_2).geometry == getattr(
        feature_1.geometry, operation_name)(feature_2.geometry))

    assert getattr(feature_1,
                   operation_name)(feature_2).properties == expected_properties
    assert getattr(feature_2,
                   operation_name)(feature_1).properties == expected_properties
Esempio n. 24
0
    def _adapt_feature_before_write(self, feature):
        new_properties = feature.properties.copy()
        for key in self.property_names:
            new_properties.setdefault(key, None)

        new_geometry = feature.geometry.reproject(self.crs)

        return GeoFeature(new_geometry, new_properties)
Esempio n. 25
0
    def __add__(self, other):
        if isinstance(other, GeoVector):
            other = GeoFeature(other, {})

        if isinstance(other, GeoFeature):
            other = [other]

        return FeatureCollection([feat for feat in chain(self, other)])
Esempio n. 26
0
def test_geofeature_dict():
    expected_geovector = GeoVector(Point(0.0, 0.0))
    expected_properties = {'property_1': 1}

    res = GeoFeature(expected_geovector, expected_properties)

    assert res['property_1'] == 1
    assert res.geometry == expected_geovector
Esempio n. 27
0
    def _adapt_feature_before_write(self, feature):
        new_attributes = feature.attributes
        for key in self.attribute_names:
            new_attributes.setdefault(key, None)

        new_geometry = feature.geometry.reproject(self.crs)

        return GeoFeature(new_geometry, new_attributes)
Esempio n. 28
0
def test_geofeature_initializer():
    expected_geovector = GeoVector(Point(0.0, 0.0))
    expected_attributes = {'attribute_1': 1}

    res = GeoFeature(expected_geovector, expected_attributes)

    assert res.geometry is expected_geovector
    assert dict(res) == expected_attributes
Esempio n. 29
0
def test_geofeature_dict():
    expected_geovector = GeoVector(Point(0.0, 0.0))
    expected_attributes = {'attribute_1': 1}

    res = GeoFeature(expected_geovector, expected_attributes)

    assert res['attribute_1'] == 1
    assert res.geometry == expected_geovector
Esempio n. 30
0
    def __getitem__(self, key):
        results = OrderedDict.fromkeys(self._groups)
        for name, group in self:
            results[name] = []
            for feature in group:
                new_feature = GeoFeature(feature.geometry, {key: feature[key]})
                results[name].append(new_feature)

        return self.__class__(results)