Skip to content

Commit

Permalink
Update interp_points input with new geoutils release
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet committed Jun 6, 2024
1 parent 9739dee commit 3bd0c62
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion dev-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
- tqdm
- scikit-image=0.*
- scikit-gstat>=1.0,<1.1
- geoutils=0.1.5
- geoutils=0.1.6

# Development-specific, to mirror manually in setup.cfg [options.extras_require].
- pip
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
- tqdm
- scikit-image=0.*
- scikit-gstat>=1.0,<1.1
- geoutils=0.1.5
- geoutils=0.1.6
- pip

# To run CI against latest GeoUtils
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ scipy>=1.0,<1.13
tqdm
scikit-image==0.*
scikit-gstat>=1.0,<1.1
geoutils==0.1.5
geoutils==0.1.6
pip
10 changes: 5 additions & 5 deletions xdem/coreg/affine.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ def _fit_rst_pts(
for key, raster in [("nx", normal_east), ("ny", normal_north), ("nz", normal_up)]:
raster.tags["AREA_OR_POINT"] = "Area"
point_elev[key] = raster.interp_points(
point_elev[["E", "N"]].values,
(point_elev["E"].values, point_elev["N"].values),
shift_area_or_point=True,
)

Expand Down Expand Up @@ -906,8 +906,8 @@ def _fit_rst_pts(
x_coords -= resolution / 2
y_coords += resolution / 2

pts = np.array((x_coords, y_coords)).T
# This needs to be consistent, so it's cardcoded here
pts = (x_coords, y_coords)
# This needs to be consistent, so it's hardcoded here
area_or_point = "Area"
# Make a new DEM which will be modified inplace
aligned_dem = rst_elev.copy()
Expand All @@ -934,7 +934,7 @@ def _fit_rst_pts(
tba_pts = aligned_dem.interp_points(pts, shift_area_or_point=True)

# Treat new_pts as a window, every time we shift it a little bit to fit the correct view
new_pts = pts.copy()
new_pts = (pts[0].copy(), pts[1].copy())

elevation_difference = point_elev[z_name].values - tba_pts
vshift = float(np.nanmedian(elevation_difference))
Expand Down Expand Up @@ -965,7 +965,7 @@ def _fit_rst_pts(

# Assign offset to the coordinates of the pts
# Treat new_pts as a window, every time we shift it a little bit to fit the correct view
new_pts += [east_diff * resolution, north_diff * resolution]
new_pts = (new_pts[0] + east_diff * resolution, new_pts[1] + north_diff * resolution)

# Get new values
tba_pts = aligned_dem.interp_points(new_pts, shift_area_or_point=True)
Expand Down
15 changes: 10 additions & 5 deletions xdem/coreg/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ def _df_sampling_from_dem(
return df


def _mask_dataframe_by_dem(df: pd.DataFrame | NDArrayf, dem: RasterType) -> pd.DataFrame | NDArrayf:
def _mask_dataframe_by_dem(
df: pd.DataFrame | tuple[NDArrayf, NDArrayf], dem: RasterType
) -> pd.DataFrame | tuple[NDArrayf, NDArrayf]:
"""
Mask out the dataframe (has 'E','N' columns), or np.ndarray ([E,N]) by DEM's mask.
Expand All @@ -200,12 +202,15 @@ def _mask_dataframe_by_dem(df: pd.DataFrame | NDArrayf, dem: RasterType) -> pd.D
mask_raster = dem.copy(new_array=final_mask.astype(np.float32))

if isinstance(df, pd.DataFrame):
pts = np.array((df["E"].values, df["N"].values)).T
elif isinstance(df, np.ndarray):
pts = df
pts = (df["E"].values, df["N"].values)
elif isinstance(df, tuple):
pts = df # type: ignore

ref_inlier = mask_raster.interp_points(pts)
new_df = df[ref_inlier.astype(bool)].copy()
if isinstance(df, pd.DataFrame):
new_df = df[ref_inlier.astype(bool)].copy()
else:
new_df = (pts[0][ref_inlier.astype(bool)], pts[1][ref_inlier.astype(bool)])

return new_df, ref_inlier.astype(bool)

Expand Down
6 changes: 3 additions & 3 deletions xdem/coreg/biascorr.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def _fit_rst_pts( # type: ignore
pts_elev = ref_elev if isinstance(ref_elev, gpd.GeoDataFrame) else tba_elev
rst_elev = ref_elev if not isinstance(ref_elev, gpd.GeoDataFrame) else tba_elev

pts = np.array((pts_elev.geometry.x.values, pts_elev.geometry.y.values)).T
pts = (pts_elev.geometry.x.values, pts_elev.geometry.y.values)

# Get valid mask ahead of subsampling to have the exact number of requested subsamples by user
if bias_vars is not None:
Expand All @@ -397,10 +397,10 @@ def _fit_rst_pts( # type: ignore

# If there is a subsample, it needs to be done now on the point dataset to reduce later calculations
subsample_mask = self._get_subsample_on_valid_mask(valid_mask=valid_pts, verbose=verbose)
pts = pts[subsample_mask]
pts = (pts[0][subsample_mask], pts[1][subsample_mask])

# Now all points should be valid, we can pass an inlier mask completely true
inlier_pts_alltrue = np.ones(len(pts), dtype=bool)
inlier_pts_alltrue = np.ones(len(pts[0]), dtype=bool)

# Below, we derive 1D arrays for the rst_rst function to take over after interpolating to the point coordinates
# (as rst_rst works for 1D arrays as well as 2D arrays, as long as coordinates match)
Expand Down

0 comments on commit 3bd0c62

Please sign in to comment.