Skip to content

Commit 99916d6

Browse files
Fix for SRTM tile overlap in new xarray versions
1 parent b89fa2b commit 99916d6

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

  • insardev_toolkit/insardev_toolkit

insardev_toolkit/insardev_toolkit/Tiles.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,30 @@ def download(self, base_url, path_id, tile_id, archive, filetype,
218218
'All tiles must have the same shape to be combined correctly. '
219219
'This is a known issue with the Copernicus DEM — consider using the SRTM DEM instead.'
220220
)
221+
# Check if tiles have overlapping edge coordinates (e.g., SRTM DEM)
222+
all_lons = np.concatenate([t.lon.values for t in tile_xarrays])
223+
all_lats = np.concatenate([t.lat.values for t in tile_xarrays])
224+
lon_overlap = len(all_lons) != len(np.unique(all_lons))
225+
lat_overlap = len(all_lats) != len(np.unique(all_lats))
226+
227+
if lon_overlap != lat_overlap:
228+
raise ValueError(
229+
f'Inconsistent tile overlaps: lon_overlap={lon_overlap}, lat_overlap={lat_overlap}. '
230+
'Expected both or neither to have overlapping edge coordinates.'
231+
)
232+
233+
if lon_overlap: # implies lat_overlap too
234+
# Drop last row/col from interior tiles to remove overlapping edges
235+
max_lon = max(t.lon.values[-1] for t in tile_xarrays)
236+
max_lat = max(t.lat.values[-1] for t in tile_xarrays)
237+
tile_xarrays = [
238+
t.isel(
239+
lon=slice(None, -1) if t.lon.values[-1] < max_lon else slice(None),
240+
lat=slice(None, -1) if t.lat.values[-1] < max_lat else slice(None)
241+
)
242+
for t in tile_xarrays
243+
]
221244
da = xr.combine_by_coords(tile_xarrays)
222-
# drop duplicate indices for SRTM DEM when neighboring tiles share the same edge coordinates
223-
da = da.sel(lat=~da.indexes['lat'].duplicated(), lon=~da.indexes['lon'].duplicated())
224245

225246
if isinstance(da, xr.Dataset):
226247
da = da[list(da.data_vars)[0]]

0 commit comments

Comments
 (0)