Skip to content

Commit

Permalink
get_profile add crs & add bbox for xy row_col
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanchengyan committed Nov 19, 2023
1 parent 4a69ba0 commit 4b6217f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions faninsar/datasets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def get_profile(
else:
bounds = self.roi
elif isinstance(bbox, BoundingBox):
bounds = bbox
bounds = self._check_roi(bbox)
else:
raise ValueError(
"bbox must be one of ['bounds', 'roi'] or a "
Expand Down Expand Up @@ -826,6 +826,7 @@ def row_col(
self,
xy: Iterable,
crs: Optional[Union[CRS, str]] = None,
bbox: Union[BoundingBox, Literal["roi", "bounds"]] = "roi",
) -> np.ndarray:
"""Convert x, y coordinates to row, col in the dataset.
Expand All @@ -836,6 +837,9 @@ def row_col(
crs: CRS or str, optional
The CRS of the points. If None, the CRS of the dataset will be used.
allowed CRS formats are the same as those supported by rasterio.
bbox : str, one of ['bounds', 'roi'], optional
the bounding box used to calculate the ``width``, ``height``
and ``transform`` of the dataset for the profile. Default is 'roi'.
Returns
-------
Expand All @@ -855,7 +859,7 @@ def row_col(
xs, ys = warp_transform(crs, self.crs, xy[:, 0], xy[:, 1])
xy = np.column_stack((xs, ys))

profile = self.get_profile("bounds")
profile = self.get_profile(bbox)

rows, cols = rowcol(profile["transform"], xy[:, 0], xy[:, 1])
row_col = np.column_stack((rows, cols))
Expand All @@ -865,6 +869,7 @@ def xy(
self,
row_col: Iterable,
crs: Optional[Union[CRS, str]] = None,
bbox: Union[BoundingBox, Literal["roi", "bounds"]] = "roi",
) -> np.ndarray:
"""Convert row, col in the dataset to x, y coordinates.
Expand All @@ -875,7 +880,10 @@ def xy(
crs: CRS or str, optional
The CRS of the points. If None, the CRS of the dataset will be used.
allowed CRS formats are the same as those supported by rasterio.
bbox : str, one of ['bounds', 'roi'], optional
the bounding box used to calculate the ``width``, ``height``
and ``transform`` of the dataset for the profile. Default is 'roi'.
Returns
-------
xy: np.ndarray
Expand All @@ -889,7 +897,7 @@ def xy(
f"Expected row_col to be an array of shape (n, 2), got {row_col.shape}"
)

profile = self.get_profile("bounds")
profile = self.get_profile(bbox)

xs, ys = xy(profile["transform"], row_col[:, 0], row_col[:, 1])

Expand Down

0 comments on commit 4b6217f

Please sign in to comment.