Skip to content

Commit 168e40f

Browse files
authored
Merge pull request #262 from statisticsnorway/wms
More general wms class
2 parents 05fb86f + 49d48fe commit 168e40f

File tree

6 files changed

+50
-49
lines changed

6 files changed

+50
-49
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ssb-sgis"
3-
version = "1.0.14"
3+
version = "1.0.15"
44
description = "GIS functions used at Statistics Norway."
55
authors = ["Morten Letnes <morten.letnes@ssb.no>"]
66
license = "MIT"

src/sgis/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@
9696
from .maps.maps import explore_locals
9797
from .maps.maps import qtm
9898
from .maps.maps import samplemap
99-
from .maps.norge_i_bilder_wms import NorgeIBilderWms
10099
from .maps.thematicmap import ThematicMap
101100
from .maps.tilesources import kartverket as kartverket_tiles
102101
from .maps.tilesources import xyz as xyztiles
102+
from .maps.wms import NorgeIBilderWms
103103
from .networkanalysis.closing_network_holes import close_network_holes
104104
from .networkanalysis.closing_network_holes import close_network_holes_to_deadends
105105
from .networkanalysis.closing_network_holes import get_k_nearest_points_for_deadends

src/sgis/maps/explore.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
from ..geopandas_tools.general import make_all_singlepart
4545
from ..geopandas_tools.geometry_types import get_geom_type
4646
from ..geopandas_tools.geometry_types import to_single_geom_type
47-
from .norge_i_bilder_wms import NorgeIBilderWms
47+
from .wms import WmsLoader
4848

4949
try:
5050
from ..raster.image_collection import Band
@@ -280,7 +280,7 @@ def __init__(
280280
max_images: int = 10,
281281
max_nodata_percentage: int = 100,
282282
display: bool = True,
283-
norge_i_bilder: bool = False,
283+
wms: WmsLoader | None = None,
284284
**kwargs,
285285
) -> None:
286286
"""Initialiser.
@@ -310,9 +310,7 @@ def __init__(
310310
max_nodata_percentage: Maximum percentage nodata values (e.g. clouds) ro allow in
311311
image arrays.
312312
display: Whether to display the map interactively.
313-
norge_i_bilder: If True, all Norge i bilder images in bounds will be loaded
314-
into the map. Can optionally be set to an instance of NorgeIBilderWms to filter
315-
years and names.
313+
wms: A WmsLoader instance for loading image tiles as layers. E.g. NorgeIBilderWms.
316314
**kwargs: Additional keyword arguments. Can also be geometry-like objects
317315
where the key is the label.
318316
"""
@@ -329,7 +327,7 @@ def __init__(
329327
self.max_images = max_images
330328
self.max_nodata_percentage = max_nodata_percentage
331329
self.display = display
332-
self.norge_i_bilder = norge_i_bilder
330+
self.wms = [wms] if isinstance(wms, WmsLoader) else wms
333331
self.legend = None
334332

335333
self.browser = browser
@@ -768,13 +766,11 @@ def _add_tiles(
768766
for tile in tiles:
769767
to_tile(tile, max_zoom=self.max_zoom).add_to(mapobj)
770768

771-
def _add_norge_i_bilder(self, map_: folium.Map, bbox: Any) -> None:
772-
if not isinstance(self.norge_i_bilder, NorgeIBilderWms):
773-
self.norge_i_bilder = NorgeIBilderWms()
774-
775-
tiles = self.norge_i_bilder.get_tiles(bbox, max_zoom=self.max_zoom)
776-
for tile in tiles.values():
777-
map_.add_child(tile)
769+
def _add_wms(self, map_: folium.Map, bbox: Any) -> None:
770+
for wms in self.wms:
771+
tiles = wms.get_tiles(bbox, max_zoom=self.max_zoom)
772+
for tile in tiles.values():
773+
map_.add_child(tile)
778774

779775
def _create_continous_map(self):
780776
self._prepare_continous_map()
@@ -982,8 +978,8 @@ def _make_folium_map(
982978
m.get_root().add_child(style)
983979
# folium.LayerControl(collapsed=False).add_to(m)
984980

985-
if self.norge_i_bilder:
986-
self._add_norge_i_bilder(m, bounds)
981+
if self.wms:
982+
self._add_wms(m, bounds)
987983

988984
return m
989985

src/sgis/maps/maps.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
from ..geopandas_tools.geometry_types import get_geom_type
3232
from .explore import Explore
3333
from .map import Map
34-
from .norge_i_bilder_wms import NorgeIBilderWms
3534
from .thematicmap import ThematicMap
35+
from .wms import WmsLoader
3636

3737
try:
3838
from torchgeo.datasets.geo import RasterDataset
@@ -91,7 +91,7 @@ def explore(
9191
size: int | None = None,
9292
max_images: int = 10,
9393
max_nodata_percentage: int = 100,
94-
norge_i_bilder: bool | NorgeIBilderWms = False,
94+
wms: WmsLoader | None = None,
9595
**kwargs,
9696
) -> Explore:
9797
"""Interactive map of GeoDataFrames with layers that can be toggled on/off.
@@ -123,9 +123,7 @@ def explore(
123123
map. Defaults to 10.
124124
max_nodata_percentage: Maximum percentage nodata values (e.g. clouds) ro allow in
125125
image arrays.
126-
norge_i_bilder: If True, all Norge i bilder images in bounds will be loaded
127-
into the map. Can optionally be set to an instance of NorgeIBilderWms to filter
128-
years and names.
126+
wms: A WmsLoader instance for loading image tiles as layers. E.g. NorgeIBilderWms.
129127
**kwargs: Keyword arguments to pass to geopandas.GeoDataFrame.explore, for
130128
instance 'cmap' to change the colors, 'scheme' to change how the data
131129
is grouped. This defaults to 'fisherjenkssampled' for numeric data.
@@ -175,7 +173,7 @@ def explore(
175173
max_zoom=max_zoom,
176174
max_images=max_images,
177175
max_nodata_percentage=max_nodata_percentage,
178-
norge_i_bilder=norge_i_bilder,
176+
wms=wms,
179177
**kwargs,
180178
)
181179

@@ -233,7 +231,7 @@ def explore(
233231
max_zoom=max_zoom,
234232
max_images=max_images,
235233
max_nodata_percentage=max_nodata_percentage,
236-
norge_i_bilder=norge_i_bilder,
234+
wms=wms,
237235
**kwargs,
238236
)
239237

@@ -245,7 +243,7 @@ def explore(
245243
smooth_factor=smooth_factor,
246244
max_images=max_images,
247245
max_nodata_percentage=max_nodata_percentage,
248-
norge_i_bilder=norge_i_bilder,
246+
wms=wms,
249247
**kwargs,
250248
)
251249

@@ -271,7 +269,7 @@ def samplemap(
271269
browser: bool = False,
272270
max_images: int = 10,
273271
max_nodata_percentage: int = 100,
274-
norge_i_bilder: bool | NorgeIBilderWms = False,
272+
wms: WmsLoader | None = None,
275273
**kwargs,
276274
) -> Explore:
277275
"""Shows an interactive map of a random area of GeoDataFrames.
@@ -307,9 +305,7 @@ def samplemap(
307305
map. Defaults to 10.
308306
max_nodata_percentage: Maximum percentage nodata values (e.g. clouds) ro allow in
309307
image arrays.
310-
norge_i_bilder: If True, all Norge i bilder images in bounds will be loaded
311-
into the map. Can optionally be set to an instance of NorgeIBilderWms to filter
312-
years and names.
308+
wms: A WmsLoader instance for loading image tiles as layers. E.g. NorgeIBilderWms.
313309
**kwargs: Keyword arguments to pass to geopandas.GeoDataFrame.explore, for
314310
instance 'cmap' to change the colors, 'scheme' to change how the data
315311
is grouped. This defaults to 'fisherjenkssampled' for numeric data.
@@ -393,7 +389,7 @@ def samplemap(
393389
smooth_factor=smooth_factor,
394390
max_images=max_images,
395391
max_nodata_percentage=max_nodata_percentage,
396-
norge_i_bilder=norge_i_bilder,
392+
wms=wms,
397393
**kwargs,
398394
)
399395

@@ -408,7 +404,7 @@ def clipmap(
408404
browser: bool = False,
409405
max_images: int = 10,
410406
max_nodata_percentage: int = 100,
411-
norge_i_bilder: bool | NorgeIBilderWms = False,
407+
wms: WmsLoader | None = None,
412408
**kwargs,
413409
) -> Explore | Map:
414410
"""Shows an interactive map of a of GeoDataFrames clipped to the mask extent.
@@ -439,9 +435,7 @@ def clipmap(
439435
map. Defaults to 10.
440436
max_nodata_percentage: Maximum percentage nodata values (e.g. clouds) ro allow in
441437
image arrays.
442-
norge_i_bilder: If True, all Norge i bilder images in bounds will be loaded
443-
into the map. Can optionally be set to an instance of NorgeIBilderWms to filter
444-
years and names.
438+
wms: A WmsLoader instance for loading image tiles as layers. E.g. NorgeIBilderWms.
445439
**kwargs: Keyword arguments to pass to geopandas.GeoDataFrame.explore, for
446440
instance 'cmap' to change the colors, 'scheme' to change how the data
447441
is grouped. This defaults to 'fisherjenkssampled' for numeric data.
@@ -477,7 +471,7 @@ def clipmap(
477471
smooth_factor=smooth_factor,
478472
max_images=max_images,
479473
max_nodata_percentage=max_nodata_percentage,
480-
norge_i_bilder=norge_i_bilder,
474+
wms=wms,
481475
**kwargs,
482476
)
483477
m.mask = mask

src/sgis/maps/norge_i_bilder_wms.py renamed to src/sgis/maps/wms.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import abc
12
import datetime
23
import json
34
import re
@@ -24,7 +25,28 @@
2425

2526

2627
@dataclass
27-
class NorgeIBilderWms:
28+
class WmsLoader(abc.ABC):
29+
"""Abstract base class for wms loaders.
30+
31+
Child classes must implement the method 'get_tiles',
32+
which should return a list of folium.WmsTileLayer.
33+
"""
34+
35+
@abc.abstractmethod
36+
def get_tiles(self, bbox: Any, max_zoom: int = 40) -> list[folium.WmsTileLayer]:
37+
"""Get all tiles intersecting with a bbox."""
38+
39+
@abc.abstractmethod
40+
def load_tiles(self) -> None:
41+
"""Load all tiles into self.tiles.
42+
43+
Not needed in sgis.explore.
44+
"""
45+
pass
46+
47+
48+
@dataclass
49+
class NorgeIBilderWms(WmsLoader):
2850
"""Loads Norge i bilder tiles as folium.WmsTiles."""
2951

3052
years: Iterable[int | str] = DEFAULT_YEARS

tests/test_explore.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,9 @@ def test_explore(points_oslo, roads_oslo):
126126
"meters",
127127
r100,
128128
bygdoy=7000,
129-
norge_i_bilder=True,
130-
tiles=["openstreetmap"],
129+
wms=sg.NorgeIBilderWms(years=[2022, 2023, 2024], not_contains="sentinel"),
131130
)
132-
assert isinstance(e.norge_i_bilder, sg.NorgeIBilderWms)
133-
e = sg.explore(
134-
r300,
135-
"meters",
136-
r100,
137-
bygdoy=7000,
138-
norge_i_bilder=sg.NorgeIBilderWms(
139-
years=[2022, 2023, 2024], not_contains="sentinel"
140-
),
141-
)
142-
assert isinstance(e.norge_i_bilder, sg.NorgeIBilderWms)
131+
assert isinstance(next(iter(e.wms)), sg.NorgeIBilderWms)
143132

144133
inner_test_center(r300, r200, r100, p)
145134

0 commit comments

Comments
 (0)