|
1 | 1 | import textwrap
|
2 | 2 |
|
| 3 | +import geopandas |
3 | 4 | import geopandas as gpd
|
4 | 5 | import numpy.testing
|
5 | 6 | import pyproj
|
@@ -157,6 +158,89 @@ def test_with_cube_to_geojson(self, gdf):
|
157 | 158 | }
|
158 | 159 | )
|
159 | 160 |
|
| 161 | + def test_from_geodataframe_default(self, gdf): |
| 162 | + vc = DriverVectorCube.from_geodataframe(gdf) |
| 163 | + assert vc.to_geojson() == DictSubSet( |
| 164 | + { |
| 165 | + "type": "FeatureCollection", |
| 166 | + "features": [ |
| 167 | + DictSubSet( |
| 168 | + { |
| 169 | + "type": "Feature", |
| 170 | + "properties": {"id": "first", "pop": 1234}, |
| 171 | + "geometry": { |
| 172 | + "coordinates": (((1.0, 1.0), (3.0, 1.0), (2.0, 3.0), (1.0, 1.0)),), |
| 173 | + "type": "Polygon", |
| 174 | + }, |
| 175 | + } |
| 176 | + ), |
| 177 | + DictSubSet( |
| 178 | + { |
| 179 | + "type": "Feature", |
| 180 | + "properties": {"id": "second", "pop": 5678}, |
| 181 | + "geometry": { |
| 182 | + "coordinates": (((4.0, 2.0), (5.0, 4.0), (3.0, 4.0), (4.0, 2.0)),), |
| 183 | + "type": "Polygon", |
| 184 | + }, |
| 185 | + } |
| 186 | + ), |
| 187 | + ], |
| 188 | + } |
| 189 | + ) |
| 190 | + cube = vc.get_cube() |
| 191 | + assert cube.dims == ("geometries", "bands") |
| 192 | + assert cube.shape == (2, 1) |
| 193 | + assert {k: list(v.values) for k, v in cube.coords.items()} == {"geometries": [0, 1], "bands": ["pop"]} |
| 194 | + |
| 195 | + @pytest.mark.parametrize( |
| 196 | + ["columns_for_cube", "expected"], |
| 197 | + [ |
| 198 | + ("numerical", {"shape": (2, 1), "coords": {"geometries": [0, 1], "bands": ["pop"]}}), |
| 199 | + ("all", {"shape": (2, 2), "coords": {"geometries": [0, 1], "bands": ["id", "pop"]}}), |
| 200 | + ([], None), |
| 201 | + (["id"], {"shape": (2, 1), "coords": {"geometries": [0, 1], "bands": ["id"]}}), |
| 202 | + (["pop", "id"], {"shape": (2, 2), "coords": {"geometries": [0, 1], "bands": ["pop", "id"]}}), |
| 203 | + # TODO: test specifying non-existent column (to be filled with no-data): |
| 204 | + # (["pop", "nopenope"], {"shape": (2, 2), "coords": {"geometries": [0, 1], "bands": ["pop", "nopenope"]}}), |
| 205 | + ], |
| 206 | + ) |
| 207 | + def test_from_geodataframe_columns_for_cube(self, gdf, columns_for_cube, expected): |
| 208 | + vc = DriverVectorCube.from_geodataframe(gdf, columns_for_cube=columns_for_cube) |
| 209 | + assert vc.to_geojson() == DictSubSet( |
| 210 | + { |
| 211 | + "type": "FeatureCollection", |
| 212 | + "features": [ |
| 213 | + DictSubSet( |
| 214 | + { |
| 215 | + "type": "Feature", |
| 216 | + "properties": {"id": "first", "pop": 1234}, |
| 217 | + "geometry": { |
| 218 | + "coordinates": (((1.0, 1.0), (3.0, 1.0), (2.0, 3.0), (1.0, 1.0)),), |
| 219 | + "type": "Polygon", |
| 220 | + }, |
| 221 | + } |
| 222 | + ), |
| 223 | + DictSubSet( |
| 224 | + { |
| 225 | + "type": "Feature", |
| 226 | + "properties": {"id": "second", "pop": 5678}, |
| 227 | + "geometry": { |
| 228 | + "coordinates": (((4.0, 2.0), (5.0, 4.0), (3.0, 4.0), (4.0, 2.0)),), |
| 229 | + "type": "Polygon", |
| 230 | + }, |
| 231 | + } |
| 232 | + ), |
| 233 | + ], |
| 234 | + } |
| 235 | + ) |
| 236 | + cube = vc.get_cube() |
| 237 | + if expected is None: |
| 238 | + assert cube is None |
| 239 | + else: |
| 240 | + assert cube.dims == ("geometries", "bands") |
| 241 | + assert cube.shape == expected["shape"] |
| 242 | + assert {k: list(v.values) for k, v in cube.coords.items()} == expected["coords"] |
| 243 | + |
160 | 244 | @pytest.mark.parametrize(["geojson", "expected"], [
|
161 | 245 | (
|
162 | 246 | {"type": "Polygon", "coordinates": [[(1, 1), (3, 1), (2, 3), (1, 1)]]},
|
|
0 commit comments