Skip to content

Commit

Permalink
Fix gis module (#213)
Browse files Browse the repository at this point in the history
<!-- Please ensure the PR fulfills the following requirements! -->
<!-- If this is your first PR, make sure to add your details to the
AUTHORS.rst! -->
### Pull Request Checklist:
- [ ] This PR addresses an already opened issue (for bug fixes /
features)
  - This PR fixes #xyz
- [X] (If applicable) Documentation has been added / updated (for bug
fixes / features).
- [X] (If applicable) Tests have been added.
- [ ] CHANGELOG.rst has been updated (with summary of main changes).
- [ ] Link to issue (:issue:`number`) and pull request (:pull:`number`)
has been added.

### What kind of change does this PR introduce?
GIS module's tests were failing because the Planetary Computer's doesn't
allow anonymous access to their blob storage anymore. However, they
provide with their python API a way to sign (SAS token) the blobs
allowing us to fetch the required files again.

### Does this PR introduce a breaking change?
No

### Other information:
  • Loading branch information
sebastienlanglois authored Oct 11, 2024
2 parents 0a98860 + 429119e commit 28d70e9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
14 changes: 14 additions & 0 deletions docs/notebooks/gis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,13 @@
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also extract the surface properties for the same `gpd.GeoDataFrame` : "
]
},
{
"cell_type": "code",
"execution_count": 10,
Expand Down Expand Up @@ -972,6 +979,13 @@
"xhgis.surface_properties(gdf)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Again, for convenience, we can output the results in `xarray.Dataset` format : "
]
},
{
"cell_type": "code",
"execution_count": 11,
Expand Down
7 changes: 5 additions & 2 deletions docs/notebooks/hydrological_modelling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"metadata": {},
"outputs": [],
"source": [
"import planetary_computer\n",
"\n",
"import xhydro as xh\n",
"import xhydro.modelling as xhm\n",
"\n",
Expand All @@ -52,7 +54,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "3",
"metadata": {
"editable": true,
Expand Down Expand Up @@ -166,7 +168,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "7",
"metadata": {},
"outputs": [],
Expand All @@ -179,6 +181,7 @@
"# Acquire the Copernicus DEM for the area covered by the watershed\n",
"catalog = pystac_client.Client.open(\n",
" \"https://planetarycomputer.microsoft.com/api/stac/v1\",\n",
" modifier=planetary_computer.sign_inplace,\n",
")\n",
"search = catalog.search(\n",
" collections=[\"cop-dem-glo-90\"],\n",
Expand Down
12 changes: 7 additions & 5 deletions src/xhydro/gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import planetary_computer
import pystac_client
import rasterio
import rasterio.features
Expand Down Expand Up @@ -270,11 +271,9 @@ def _recursive_upstream_lookup(


def _flatten(x, dim="time"):
# FIXME: assert statements should only be found in test code
assert isinstance(x, xr.DataArray) # noqa: S101
if len(x[dim].values) > len(set(x[dim].values)):
x = x.groupby(dim).map(stackstac.mosaic)

if isinstance(x, xr.DataArray):
if len(x[dim].values) > len(set(x[dim].values)):
x = x.groupby(dim).map(stackstac.mosaic)
return x


Expand Down Expand Up @@ -324,6 +323,7 @@ def surface_properties(

catalog = pystac_client.Client.open(
"https://planetarycomputer.microsoft.com/api/stac/v1",
modifier=planetary_computer.sign_inplace,
)

search = catalog.search(
Expand Down Expand Up @@ -489,6 +489,7 @@ def land_use_classification(
"""
catalog = pystac_client.Client.open(
"https://planetarycomputer.microsoft.com/api/stac/v1",
modifier=planetary_computer.sign_inplace,
)
collection = catalog.get_collection(collection)
ia = ItemAssetsExtension.ext(collection)
Expand Down Expand Up @@ -552,6 +553,7 @@ def land_use_plot(
"""
catalog = pystac_client.Client.open(
"https://planetarycomputer.microsoft.com/api/stac/v1",
modifier=planetary_computer.sign_inplace,
)

collection = catalog.get_collection(collection)
Expand Down

0 comments on commit 28d70e9

Please sign in to comment.