|
| 1 | +import geopandas as gpd |
| 2 | +import pytest |
| 3 | +import xarray |
| 4 | +from shapely.geometry import Polygon, MultiPolygon |
| 5 | + |
| 6 | +from openeo_driver.datacube import DriverVectorCube |
| 7 | +from openeo_driver.testing import DictSubSet |
| 8 | +from .data import get_path |
| 9 | + |
| 10 | + |
| 11 | +class TestDriverVectorCube: |
| 12 | + |
| 13 | + @pytest.fixture |
| 14 | + def gdf(self) -> gpd.GeoDataFrame: |
| 15 | + """Fixture for a simple GeoPandas DataFrame from file""" |
| 16 | + path = str(get_path("geojson/FeatureCollection02.json")) |
| 17 | + df = gpd.read_file(path) |
| 18 | + return df |
| 19 | + |
| 20 | + def test_basic(self, gdf): |
| 21 | + vc = DriverVectorCube(gdf) |
| 22 | + assert vc.get_bounding_box() == (1, 1, 5, 4) |
| 23 | + |
| 24 | + def test_to_multipolygon(self, gdf): |
| 25 | + vc = DriverVectorCube(gdf) |
| 26 | + mp = vc.to_multipolygon() |
| 27 | + assert isinstance(mp, MultiPolygon) |
| 28 | + assert len(mp) == 2 |
| 29 | + assert mp.equals(MultiPolygon([ |
| 30 | + Polygon([(1, 1), (2, 3), (3, 1), (1, 1)]), |
| 31 | + Polygon([(4, 2), (5, 4), (3, 4), (4, 2)]), |
| 32 | + ])) |
| 33 | + |
| 34 | + def test_get_geometries(self, gdf): |
| 35 | + vc = DriverVectorCube(gdf) |
| 36 | + geometries = vc.get_geometries() |
| 37 | + assert len(geometries) == 2 |
| 38 | + expected_geometries = [ |
| 39 | + Polygon([(1, 1), (2, 3), (3, 1), (1, 1)]), |
| 40 | + Polygon([(4, 2), (5, 4), (3, 4), (4, 2)]), |
| 41 | + ] |
| 42 | + for geometry, expected in zip(geometries, expected_geometries): |
| 43 | + assert geometry.equals(expected) |
| 44 | + |
| 45 | + def test_to_geojson(self, gdf): |
| 46 | + vc = DriverVectorCube(gdf) |
| 47 | + assert vc.to_geojson() == DictSubSet({ |
| 48 | + "type": "FeatureCollection", |
| 49 | + "features": [ |
| 50 | + DictSubSet({ |
| 51 | + "type": "Feature", |
| 52 | + "geometry": {"type": "Polygon", "coordinates": (((1, 1), (3, 1), (2, 3), (1, 1)),)}, |
| 53 | + "properties": {"id": "first", "pop": 1234}, |
| 54 | + }), |
| 55 | + DictSubSet({ |
| 56 | + "type": "Feature", |
| 57 | + "geometry": {"type": "Polygon", "coordinates": (((4, 2), (5, 4), (3, 4), (4, 2)),)}, |
| 58 | + "properties": {"id": "second", "pop": 5678}, |
| 59 | + }), |
| 60 | + ] |
| 61 | + }) |
| 62 | + |
| 63 | + def test_with_cube_to_geojson(self, gdf): |
| 64 | + vc1 = DriverVectorCube(gdf) |
| 65 | + dims, coords = vc1.get_xarray_cube_basics() |
| 66 | + dims += ("bands",) |
| 67 | + coords["bands"] = ["red", "green"] |
| 68 | + cube = xarray.DataArray(data=[[1, 2], [3, 4]], dims=dims, coords=coords) |
| 69 | + vc2 = vc1.with_cube(cube, flatten_prefix="bandz") |
| 70 | + assert vc1.to_geojson() == DictSubSet({ |
| 71 | + "type": "FeatureCollection", |
| 72 | + "features": [ |
| 73 | + DictSubSet({ |
| 74 | + "type": "Feature", |
| 75 | + "geometry": {"type": "Polygon", "coordinates": (((1, 1), (3, 1), (2, 3), (1, 1)),)}, |
| 76 | + "properties": {"id": "first", "pop": 1234}, |
| 77 | + }), |
| 78 | + DictSubSet({ |
| 79 | + "type": "Feature", |
| 80 | + "geometry": {"type": "Polygon", "coordinates": (((4, 2), (5, 4), (3, 4), (4, 2)),)}, |
| 81 | + "properties": {"id": "second", "pop": 5678}, |
| 82 | + }), |
| 83 | + ] |
| 84 | + }) |
| 85 | + assert vc2.to_geojson() == DictSubSet({ |
| 86 | + "type": "FeatureCollection", |
| 87 | + "features": [ |
| 88 | + DictSubSet({ |
| 89 | + "type": "Feature", |
| 90 | + "geometry": {"type": "Polygon", "coordinates": (((1, 1), (3, 1), (2, 3), (1, 1)),)}, |
| 91 | + "properties": {"id": "first", "pop": 1234, "bandz~red": 1, "bandz~green": 2}, |
| 92 | + }), |
| 93 | + DictSubSet({ |
| 94 | + "type": "Feature", |
| 95 | + "geometry": {"type": "Polygon", "coordinates": (((4, 2), (5, 4), (3, 4), (4, 2)),)}, |
| 96 | + "properties": {"id": "second", "pop": 5678, "bandz~red": 3, "bandz~green": 4}, |
| 97 | + }), |
| 98 | + ] |
| 99 | + }) |
0 commit comments