Skip to content

Commit

Permalink
Small changes in tiles.py (os-climate#78)
Browse files Browse the repository at this point in the history
* Small changes in tiles.py

Small changes in tiles.py to avoid failures when creating maps.

Signed-off-by: EglantineGiraud <eglantine.giraud@gmail.com>

* Chore: pre-commit autoupdate

---------

Signed-off-by: EglantineGiraud <eglantine.giraud@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
EglantineGiraud and pre-commit-ci[bot] authored Apr 28, 2024
1 parent e581301 commit a8bc5c2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ repos:
rev: "7.0.0"
hooks:
- id: flake8
args: ["--ignore=E203"]

# Check for misspells in documentation files:
# - repo: https://github.com/codespell-project/codespell
Expand Down
2 changes: 1 addition & 1 deletion dockerfiles/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ COPY src src

RUN python -m pip install -e .

CMD ["os_climate_hazard"]
CMD ["os_climate_hazard"]
13 changes: 7 additions & 6 deletions src/hazard/utilities/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ def _write_zoom_level(


def get_tile_bounds(left: float, bottom: float, right: float, top: float, zoom: int):
ul = mercantile.tile(left, top, zoom)
lr = mercantile.tile(right, bottom, zoom)
ul = mercantile.tile(left, min(top, 89.9999), zoom)
lr = mercantile.tile(right, max(bottom, -89.9999), zoom)
return ul.x, lr.x, ul.y, lr.y


Expand Down Expand Up @@ -390,15 +390,16 @@ def _coarsen(


def trim_array(da_index: xr.DataArray, crs, left, bottom, right, top):
y_dim, x_dim = da_index.dims
da_left, da_bottom, da_right, da_top = rasterio.warp.transform_bounds(
crs, da_index.rio.crs, left, bottom, right, top
)
# find pixels required in order to add a pixel buffer
x = np.where((da_index.x >= da_left) & (da_index.x <= da_right))[0]
y = np.where((da_index.y >= da_bottom) & (da_index.y <= da_top))[0]
x = np.where((da_index[x_dim] >= da_left) & (da_index[x_dim] <= da_right))[0]
y = np.where((da_index[y_dim] >= da_bottom) & (da_index[y_dim] <= da_top))[0]
if len(x) == 0 or len(y) == 0:
return None
# add a 2 pixel buffer
xmin, xmax = max(0, x[0] - 2), min(len(da_index.x), x[-1] + 2)
ymin, ymax = max(0, y[0] - 2), min(len(da_index.y), y[-1] + 2)
xmin, xmax = max(0, x[0] - 2), min(len(da_index[x_dim]), x[-1] + 2)
ymin, ymax = max(0, y[0] - 2), min(len(da_index[y_dim]), y[-1] + 2)
return da_index[ymin:ymax, xmin:xmax]

0 comments on commit a8bc5c2

Please sign in to comment.