Skip to content

Commit 9e93d30

Browse files
committed
feat: region as swisstopoclient attribute
1 parent 24c7594 commit 9e93d30

File tree

5 files changed

+37
-25
lines changed

5 files changed

+37
-25
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ Easily filter swisstopo STAC collections based on geospatial extents, dates, fil
1919
import swisstopopy
2020

2121
region = "Lausanne, Switzerland"
22-
client = swisstopopy.SwissTopoClient()
22+
client = swisstopopy.SwissTopoClient(region)
2323

2424
alti3d_gdf = client.gdf_from_collection(
2525
swisstopopy.SWISSALTI3D_COLLECTION_ID,
26-
region=region,
2726
)
2827
alti3d_gdf
2928
```

swisstopopy/buildings.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,11 @@ def get_bldg_gdf(
117117
)
118118

119119
# use the STAC API to get building heights from swissSURFACE3D and swissALTI3D
120-
client = stac.SwissTopoClient()
120+
client = stac.SwissTopoClient(region=region_gser)
121121

122122
# surface3d-raster (raster dsm)
123123
surface3d_gdf = client.gdf_from_collection(
124124
stac.SWISSSURFACE3D_RASTER_COLLECTION_ID,
125-
region=region_gser,
126125
datetime=surface3d_datetime,
127126
)
128127
# filter to get tiff images only
@@ -134,7 +133,6 @@ def get_bldg_gdf(
134133
# alti3d (raster dem)
135134
alti3d_gdf = client.gdf_from_collection(
136135
stac.SWISSALTI3D_COLLECTION_ID,
137-
region=region_gser,
138136
datetime=alti3d_datetime,
139137
)
140138
# filter to get tiff images only

swisstopopy/dem.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,9 @@ def get_dem_raster(
4848
`gdal.Warp`.
4949
"""
5050
# use the STAC API to get the DEM from swissALTI3D
51-
# TODO: dry with `tree_canopy.get_tree_canopy_raster`?
52-
# note that we need to reproject the data to the STAC client CRS
53-
client = stac.SwissTopoClient()
51+
client = stac.SwissTopoClient(region=region, region_crs=region_crs)
5452
alti3d_gdf = client.gdf_from_collection(
5553
stac.SWISSALTI3D_COLLECTION_ID,
56-
region=region,
5754
datetime=alti3d_datetime,
5855
)
5956

swisstopopy/stac.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,26 @@ def get_latest(
122122

123123

124124
class SwissTopoClient:
125-
"""swisstopo client."""
125+
"""swisstopo client.
126126
127-
def __init__(self):
127+
Parameters
128+
----------
129+
region : region-like, optional
130+
Region to get the data for. Can any argument accepted by the pyregeon library.
131+
If None, all the collection data will be returned.
132+
region_crs : crs-like, optional
133+
Coordinate reference system (CRS) of the region. Required if `region` is a naive
134+
geometry or a list of bounding box coordinates. Ignored if `region` already has
135+
a CRS.
136+
137+
"""
138+
139+
def __init__(
140+
self,
141+
*,
142+
region: RegionType | None = None,
143+
region_crs: CRSType = None,
144+
):
128145
"""Initialize a swisstopo client."""
129146
with warnings.catch_warnings():
130147
warnings.simplefilter("ignore")
@@ -133,12 +150,23 @@ def __init__(self):
133150
client.add_conforms_to("COLLECTIONS")
134151
self._client = client
135152

153+
if region is not None:
154+
# rather than inheriting from `RegionMixin`, we just use the
155+
# `_process_region_arg` static method
156+
self.region = (
157+
RegionMixin._process_region_arg(region, region_crs=region_crs)
158+
.to_crs(CLIENT_CRS)
159+
.iloc[0]
160+
)
161+
else:
162+
# set it to so that it passes the default `None` value to the `intersects`
163+
# keyword argument in `pystac_client.client.Search`.
164+
self.region = None
165+
136166
def gdf_from_collection(
137167
self,
138168
collection_id: str,
139169
*,
140-
region: RegionType | None = None,
141-
region_crs: CRSType = None,
142170
datetime: DatetimeLike | None = None,
143171
collection_extents_crs: CRSType | None = None,
144172
) -> gpd.GeoDataFrame:
@@ -147,16 +175,8 @@ def gdf_from_collection(
147175
collection_extents_crs = self._client.get_collection(
148176
collection_id
149177
).extra_fields["crs"][0]
150-
if region is not None:
151-
# TODO: make the client bound to a region (i.e., inherit from `RegionMixin`)
152-
# so that we only need to do this once?
153-
region = (
154-
RegionMixin._process_region_arg(region, region_crs=region_crs)
155-
.to_crs(CLIENT_CRS)
156-
.iloc[0]
157-
)
158178
search = self._client.search(
159-
collections=[collection_id], intersects=region, datetime=datetime
179+
collections=[collection_id], intersects=self.region, datetime=datetime
160180
)
161181
return gpd.GeoDataFrame(
162182
_postprocess_items_gdf(_items_to_gdf(search.items_as_dicts()))

swisstopopy/tree_canopy.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,9 @@ def get_tree_canopy_raster(
105105
# note that we need to pass the STAC client's CRS to both `_process_region_arg` and
106106
# `to_crs`, because `region` may have another CRS and we need the extend in the
107107
# client's CRS
108-
client = stac.SwissTopoClient()
108+
client = stac.SwissTopoClient(region=region, region_crs=region_crs)
109109
surface3d_gdf = client.gdf_from_collection(
110110
stac.SWISSSURFACE3D_COLLECTION_ID,
111-
region=region,
112-
region_crs=region_crs,
113111
datetime=surface3d_datetime,
114112
)
115113
# filter to get zip assets (LiDAR) only

0 commit comments

Comments
 (0)