Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Support comparison of GeoJSON vector cube assets in `assert_job_results_allclose`. ([#862](https://github.com/Open-EO/openeo-python-client/issues/862))

### Changed

### Removed
Expand Down
55 changes: 55 additions & 0 deletions openeo/testing/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import List, Optional, Union

import numpy
import pandas
import xarray
import xarray.testing
from xarray import DataArray
Expand Down Expand Up @@ -95,6 +96,55 @@ def _as_xarray_dataarray(data: Union[str, Path, xarray.DataArray]) -> xarray.Dat
return data


def _load_geodataframe(path: Union[str, Path]):
"""Load a vector data file as a GeoPandas GeoDataFrame."""
try:
import geopandas
except ImportError as e:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this? geopandas is a base requirement, not an extra,
no need for this guard

raise ImportError("Comparing vector data requires the 'geopandas' dependency.") from e
return geopandas.read_file(path)


def _compare_geodataframes(
actual: Union[str, Path],
expected: Union[str, Path],
*,
rtol: float = _DEFAULT_RTOL,
atol: float = _DEFAULT_ATOL,
) -> List[str]:
"""Compare vector geometries and their attribute data."""
actual_gdf = _load_geodataframe(actual)
expected_gdf = _load_geodataframe(expected)
import geopandas.testing

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid local import when not necessary


issues = []

try:
pandas.testing.assert_frame_equal(
actual_gdf.drop(columns=actual_gdf.geometry.name),
expected_gdf.drop(columns=expected_gdf.geometry.name),
check_dtype=False,
check_like=True,
rtol=rtol,
atol=atol,
)
except AssertionError as e:
issues.append(f"Attribute data mismatch:\n{str(e).strip()}")

try:
geopandas.testing.assert_geoseries_equal(
actual_gdf.geometry,
expected_gdf.geometry,
check_dtype=False,
check_geom_type=True,
check_crs=True,
)
except AssertionError as e:
issues.append(f"Geometry data mismatch:\n{str(e).strip()}")

return issues


def _ascii_art(
diff_data: DataArray,
*,
Expand Down Expand Up @@ -459,6 +509,11 @@ def _compare_job_results(
if issues:
all_issues.append(f"Issues for file {filename!r}:")
all_issues.extend(issues)
elif expected_path.suffix.lower() == ".geojson":

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure having ".geojson" is enough as an indicator for being loadable as geodataframe.
e.g. will a GeoJSON file with just a Polygon load here?
I think only Features and FeatureCollection objects can be loaded

issues = _compare_geodataframes(actual=actual_path, expected=expected_path, rtol=rtol, atol=atol)
if issues:
all_issues.append(f"Issues for file {filename!r}:")
all_issues.extend(issues)
else:
_log.warning(f"Unhandled job result asset {filename!r}")

Expand Down
37 changes: 37 additions & 0 deletions tests/testing/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,43 @@ def test_allclose_minimal_success(self, tmp_path, actual_dir, expected_dir):
ds.to_netcdf(actual_dir / "data.nc")
assert_job_results_allclose(actual=actual_dir, expected=expected_dir, tmp_path=tmp_path)

@staticmethod
def _write_geojson(path: Path, *, value: float, coordinates):
path.write_text(
json.dumps(
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"name": "sample", "value": value},
"geometry": {"type": "Point", "coordinates": coordinates},
}
],
}
)
)

def test_allclose_geojson_success(self, tmp_path, actual_dir, expected_dir):
self._write_geojson(expected_dir / "vectorcube.geojson", value=10, coordinates=[3, 51])
self._write_geojson(actual_dir / "vectorcube.geojson", value=10.000005, coordinates=[3, 51])

assert_job_results_allclose(actual=actual_dir, expected=expected_dir, tmp_path=tmp_path)

def test_allclose_geojson_attribute_mismatch(self, tmp_path, actual_dir, expected_dir):
self._write_geojson(expected_dir / "vectorcube.geojson", value=10, coordinates=[3, 51])
self._write_geojson(actual_dir / "vectorcube.geojson", value=11, coordinates=[3, 51])

with raises_assertion_error_or_not(r"Issues for file 'vectorcube.geojson'.*Attribute data mismatch"):
assert_job_results_allclose(actual=actual_dir, expected=expected_dir, tmp_path=tmp_path)

def test_allclose_geojson_geometry_mismatch(self, tmp_path, actual_dir, expected_dir):
self._write_geojson(expected_dir / "vectorcube.geojson", value=10, coordinates=[3, 51])
self._write_geojson(actual_dir / "vectorcube.geojson", value=10, coordinates=[4, 52])

with raises_assertion_error_or_not(r"Issues for file 'vectorcube.geojson'.*Geometry data mismatch"):
assert_job_results_allclose(actual=actual_dir, expected=expected_dir, tmp_path=tmp_path)

def test_allclose_xy_success(self, tmp_path, actual_dir, expected_dir):
expected_ds = xarray.Dataset(
{
Expand Down