Skip to content
Merged
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
26 changes: 24 additions & 2 deletions podpac/core/coordinates/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ def merge_dims(coords_list, validate_crs=True):

# check crs
crs = coords_list[0].crs
if validate_crs and not all(coords.crs == crs for coords in coords_list):
if validate_crs and not all(_crs_equal(coords.crs, crs) for coords in coords_list):
raise ValueError("Cannot merge Coordinates, crs mismatch")

# merge
Expand Down Expand Up @@ -1728,7 +1728,7 @@ def concat(coords_list):

# check crs
crs = coords_list[0].crs
if not all(coords.crs == crs for coords in coords_list):
if not all(_crs_equal(coords.crs, crs) for coords in coords_list):
raise ValueError("Cannot concat Coordinates, crs mismatch")

# concatenate
Expand Down Expand Up @@ -1769,3 +1769,25 @@ def union(coords_list):
"""

return concat(coords_list).unique()


def _crs_equal(a: str | None, b: str | None) -> bool:
"""Check if two CRS strings define the same coordinate reference system.

Parameters
----------
a : str | None
The first CRS string or None.
b : str | None
The second CRS string or None.

Returns
-------
bool
True if the strings define the same CRS, otherwise False.
"""
if a == b:
return True
if a is None or b is None:
return False
return pyproj.CRS(a) == pyproj.CRS(b)
36 changes: 35 additions & 1 deletion podpac/core/coordinates/test/test_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from podpac.core.coordinates.uniform_coordinates1d import UniformCoordinates1d
from podpac.core.coordinates.cfunctions import crange, clinspace
from podpac.core.coordinates.coordinates import Coordinates
from podpac.core.coordinates.coordinates import concat, union, merge_dims
from podpac.core.coordinates.coordinates import concat, union, merge_dims, _crs_equal

_DIMENSION_MISMATCH = "Dimension mismatch"
_MERCATOR_CRS = "+proj=merc +lat_ts=56.5 +ellps=GRS80"
Expand Down Expand Up @@ -1822,6 +1822,40 @@ def test_concat_crs(self):
with pytest.raises(ValueError, match="Cannot concat Coordinates"):
concat([c1, c2])

def test_crs_equal(self):
"""Test CRS equality for WKT strings and shorthand codes."""
wkt = pyproj.CRS("EPSG:4326").to_wkt()
assert _crs_equal("EPSG:4326", "EPSG:4326")
assert _crs_equal("EPSG:4326", wkt)
assert _crs_equal(None, None)
assert not _crs_equal("EPSG:4326", None)
assert not _crs_equal(None, "EPSG:4326")
assert not _crs_equal("EPSG:4326", "EPSG:2193")

def test_merge_dims_equivalent_crs(self):
"""Test coordinate merging with equivalent WKT strings and shorthand codes."""
wkt = pyproj.CRS("EPSG:4326").to_wkt()
clat = Coordinates([[2, 4, 5]], dims=["lat"], crs=wkt)
clon = Coordinates([[3, -1, 5]], dims=["lon"], crs="EPSG:4326")

c = merge_dims([clat, clon])
assert c.dims == ("lat", "lon")
assert c.crs == wkt

def test_concat_and_union_equivalent_crs(self):
"""Test concatenation and union with equivalent WKT strings and shorthand codes."""
wkt = pyproj.CRS("EPSG:4326").to_wkt()
c1 = Coordinates([[0, 1]], dims=["lat"], crs=wkt)
c2 = Coordinates([[1, 2]], dims=["lat"], crs="EPSG:4326")

c = concat([c1, c2])
assert c.shape == (4,)
assert c.crs == wkt

c = union([c1, c2])
assert c.shape == (3,)
assert c.crs == wkt


class TestCoordinatesGeoTransform(object):
def test_uniform_working(self):
Expand Down
Loading