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
def test_groupby_can_extract_property(): 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 }) ]) expected_groups = [ ('a', FeatureCollection([ GeoFeature(GeoVector(Point(3, 3)), {'b': 1}), GeoFeature(GeoVector(Point(1, 1)), {'b': 2}), ])), ('b', FeatureCollection([GeoFeature(GeoVector(Point(2, 2)), {'b': 3})])) ] assert list(fc.groupby('prop1')['b']) == expected_groups
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
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
def test_filter_group_by(): fc = FeatureCollection([ GeoFeature(GeoVector(Point(3, 3)), { 'prop1': 'a', 'b': 1 }), GeoFeature(GeoVector(Point(1, 1)), { 'prop1': 'a', 'b': 2 }), GeoFeature(GeoVector(Point(3, 3)), { 'prop1': 'b', 'b': 3 }), GeoFeature(GeoVector(Point(1, 1)), { 'prop1': 'b', 'b': 1 }), GeoFeature(GeoVector(Point(2, 2)), { 'prop1': 'b', 'b': 2 }), ]) expected_groups = [('b', FeatureCollection([ GeoFeature(GeoVector(Point(3, 3)), {'b': 3}), GeoFeature(GeoVector(Point(1, 1)), {'b': 1}), GeoFeature(GeoVector(Point(2, 2)), {'b': 2}) ]))] groups = fc.groupby('prop1') def filter_func(fc): return sorted([b for b in fc.get_values('b')]) == [1, 2, 3] filtered_group = groups.filter(filter_func) assert list(filtered_group['b']) == expected_groups