ssb-sgis 1.0.13__py3-none-any.whl → 1.0.14__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,165 @@
1
+ import datetime
2
+ import json
3
+ import re
4
+ from collections.abc import Iterable
5
+ from dataclasses import dataclass
6
+ from pathlib import Path
7
+ from typing import Any
8
+ from urllib.request import urlopen
9
+
10
+ import folium
11
+ import shapely
12
+
13
+ from ..geopandas_tools.conversion import to_shapely
14
+
15
+ JSON_PATH = Path(__file__).parent / "norge_i_bilder.json"
16
+
17
+ DEFAULT_YEARS: tuple[str] = tuple(
18
+ str(year)
19
+ for year in range(
20
+ int(datetime.datetime.now().year) - 8,
21
+ int(datetime.datetime.now().year) + 1,
22
+ )
23
+ )
24
+
25
+
26
+ @dataclass
27
+ class NorgeIBilderWms:
28
+ """Loads Norge i bilder tiles as folium.WmsTiles."""
29
+
30
+ years: Iterable[int | str] = DEFAULT_YEARS
31
+ contains: str | Iterable[str] | None = None
32
+ not_contains: str | Iterable[str] | None = None
33
+
34
+ def load_tiles(self) -> None:
35
+ """Load all Norge i bilder tiles into self.tiles."""
36
+ url = "https://wms.geonorge.no/skwms1/wms.nib-prosjekter?SERVICE=WMS&REQUEST=GetCapabilities"
37
+
38
+ name_pattern = r"<Name>(.*?)</Name>"
39
+ bbox_pattern = (
40
+ r"<EX_GeographicBoundingBox>.*?"
41
+ r"<westBoundLongitude>(.*?)</westBoundLongitude>.*?"
42
+ r"<eastBoundLongitude>(.*?)</eastBoundLongitude>.*?"
43
+ r"<southBoundLatitude>(.*?)</southBoundLatitude>.*?"
44
+ r"<northBoundLatitude>(.*?)</northBoundLatitude>.*?</EX_GeographicBoundingBox>"
45
+ )
46
+
47
+ all_tiles: list[dict] = []
48
+ with urlopen(url) as file:
49
+ xml_data: str = file.read().decode("utf-8")
50
+
51
+ for text in xml_data.split('<Layer queryable="1">')[1:]:
52
+
53
+ # Extract bounding box values
54
+ bbox_match = re.search(bbox_pattern, text, re.DOTALL)
55
+ if bbox_match:
56
+ minx, maxx, miny, maxy = (
57
+ float(bbox_match.group(i)) for i in [1, 2, 3, 4]
58
+ )
59
+ this_bbox = shapely.box(minx, miny, maxx, maxy)
60
+ else:
61
+ this_bbox = None
62
+
63
+ name_match = re.search(name_pattern, text, re.DOTALL)
64
+ name = name_match.group(1) if name_match else None
65
+
66
+ if (
67
+ not name
68
+ or not any(year in name for year in self.years)
69
+ or (
70
+ self.contains
71
+ and not any(re.search(x, name.lower()) for x in self.contains)
72
+ )
73
+ or (
74
+ self.not_contains
75
+ and any(re.search(x, name.lower()) for x in self.not_contains)
76
+ )
77
+ ):
78
+ continue
79
+
80
+ this_tile = {}
81
+ this_tile["name"] = name
82
+ this_tile["bbox"] = this_bbox
83
+ year = name.split(" ")[-1]
84
+ if year.isnumeric() and len(year) == 4:
85
+ this_tile["year"] = year
86
+ else:
87
+ this_tile["year"] = "9999"
88
+ all_tiles.append(this_tile)
89
+
90
+ self.tiles = sorted(all_tiles, key=lambda x: x["year"])
91
+
92
+ def get_tiles(self, bbox: Any, max_zoom: int = 40) -> list[folium.WmsTileLayer]:
93
+ """Get all Norge i bilder tiles intersecting with a bbox."""
94
+ if self.tiles is None:
95
+ self.load_tiles()
96
+
97
+ all_tiles = {}
98
+
99
+ bbox = to_shapely(bbox)
100
+
101
+ for tile in self.tiles:
102
+ if not tile["bbox"] or not tile["bbox"].intersects(bbox):
103
+ continue
104
+
105
+ name = tile["name"]
106
+
107
+ if (
108
+ not name
109
+ or not any(year in name for year in self.years)
110
+ or (
111
+ self.contains
112
+ and not any(re.search(x, name.lower()) for x in self.contains)
113
+ )
114
+ or (
115
+ self.not_contains
116
+ and any(re.search(x, name.lower()) for x in self.not_contains)
117
+ )
118
+ ):
119
+ continue
120
+
121
+ all_tiles[name] = folium.WmsTileLayer(
122
+ url="https://wms.geonorge.no/skwms1/wms.nib-prosjekter",
123
+ name=name,
124
+ layers=name,
125
+ format="image/png", # Tile format
126
+ transparent=True, # Allow transparency
127
+ version="1.3.0", # WMS version
128
+ attr="&copy; <a href='https://www.geonorge.no/'>Geonorge</a>",
129
+ show=False,
130
+ max_zoom=max_zoom,
131
+ )
132
+
133
+ return all_tiles
134
+
135
+ def __post_init__(self) -> None:
136
+ """Fix typings."""
137
+ if self.contains and isinstance(self.contains, str):
138
+ self.contains = [self.contains.lower()]
139
+ elif self.contains:
140
+ self.contains = [x.lower() for x in self.contains]
141
+
142
+ if self.not_contains and isinstance(self.not_contains, str):
143
+ self.not_contains = [self.not_contains.lower()]
144
+ elif self.not_contains:
145
+ self.not_contains = [x.lower() for x in self.not_contains]
146
+
147
+ self.years = [str(int(year)) for year in self.years]
148
+
149
+ if all(year in DEFAULT_YEARS for year in self.years):
150
+ try:
151
+ with open(JSON_PATH, encoding="utf-8") as file:
152
+ self.tiles = json.load(file)
153
+ except FileNotFoundError:
154
+ self.tiles = None
155
+ return
156
+ self.tiles = [
157
+ {
158
+ key: value if key != "bbox" else shapely.wkt.loads(value)
159
+ for key, value in tile.items()
160
+ }
161
+ for tile in self.tiles
162
+ if any(str(year) in tile["name"] for year in self.years)
163
+ ]
164
+ else:
165
+ self.tiles = None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ssb-sgis
3
- Version: 1.0.13
3
+ Version: 1.0.14
4
4
  Summary: GIS functions used at Statistics Norway.
5
5
  Home-page: https://github.com/statisticsnorway/ssb-sgis
6
6
  License: MIT
@@ -1,4 +1,4 @@
1
- sgis/__init__.py,sha256=kqHf60QUo0_TaB1ikBQXkxCQK4hUjSM6u_GKfLJ3C-4,7351
1
+ sgis/__init__.py,sha256=h_5A40stVg0dKwoQRDmv_owy-x-n66pDpM7XtcHMSwE,7404
2
2
  sgis/debug_config.py,sha256=Tfr19kU46hSkkspsIJcrUWvlhaL4U3-f8xEPkujSCAQ,593
3
3
  sgis/exceptions.py,sha256=WNaEBPNNx0rmz-YDzlFX4vIE7ocJQruUTqS2RNAu2zU,660
4
4
  sgis/geopandas_tools/__init__.py,sha256=bo8lFMcltOz7TtWAi52_ekR2gd3mjfBfKeMDV5zuqFY,28
@@ -24,11 +24,13 @@ sgis/io/opener.py,sha256=HWO3G1NB6bpXKM94JadCD513vjat1o1TFjWGWzyVasg,898
24
24
  sgis/io/read_parquet.py,sha256=FvZYv1rLkUlrSaUY6QW6E1yntmntTeQuZ9ZRgCDO4IM,3776
25
25
  sgis/maps/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
26
26
  sgis/maps/examine.py,sha256=Pb0dH8JazU5E2svfQrzHO1Bi-sjy5SeyY6zoeMO34jE,9369
27
- sgis/maps/explore.py,sha256=dHwQmD_8U9dLaBJHs2VKJvqK-yNUhH6yZoInF0iFrAE,46237
27
+ sgis/maps/explore.py,sha256=LO5ESH4e5M_fXIZzvnd-lkGyYmWcARcJYhpcZX6fLsg,47301
28
28
  sgis/maps/httpserver.py,sha256=7Od9JMCtntcIQKk_TchetojMHzFHT9sPw7GANahI97c,1982
29
29
  sgis/maps/legend.py,sha256=lVRVCkhPmJRjGK23obFJZAO3qp6du1LYnobkkN7DPkc,26279
30
30
  sgis/maps/map.py,sha256=smaf9i53EoRZWmZjn9UuqlhzUvVs1XKo2ItIpHxyuik,29592
31
- sgis/maps/maps.py,sha256=9uidfWhbdJzO6lzDUjn4EWJMpCtc4st3uy4dRbNRVtQ,22430
31
+ sgis/maps/maps.py,sha256=vOB09wiquW7-wGEqHJMotFOBX8tFfnD4gcvvpYf5Wfo,23599
32
+ sgis/maps/norge_i_bilder.json,sha256=W_mFfte3DxugWbEudZ5fadZ2JeFYb0hyab2Quf4oJME,481311
33
+ sgis/maps/norge_i_bilder_wms.py,sha256=Pb1puQFCZfEO_ng915_aOkB17wpZLbRMnUEBAiLPzjQ,5698
32
34
  sgis/maps/thematicmap.py,sha256=yAE1xEfubJcDmBlOJf-Q3SVae1ZHIEMP-YB95Wy8cRw,21691
33
35
  sgis/maps/tilesources.py,sha256=F4mFHxPwkiPJdVKzNkScTX6xbJAMIUtlTq4mQ83oguw,1746
34
36
  sgis/networkanalysis/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -54,7 +56,7 @@ sgis/raster/indices.py,sha256=-J1HYmnT240iozvgagvyis6K0_GHZHRuUrPOgyoeIrY,223
54
56
  sgis/raster/regex.py,sha256=kYhVpRYzoXutx1dSYmqMoselWXww7MMEsTPmLZwHjbM,3759
55
57
  sgis/raster/sentinel_config.py,sha256=nySDqn2R8M6W8jguoBeSAK_zzbAsqmaI59i32446FwY,1268
56
58
  sgis/raster/zonal.py,sha256=D4Gyptw-yOLTCO41peIuYbY-DANsJCG19xXDlf1QAz4,2299
57
- ssb_sgis-1.0.13.dist-info/LICENSE,sha256=np3IfD5m0ZUofn_kVzDZqliozuiO6wrktw3LRPjyEiI,1073
58
- ssb_sgis-1.0.13.dist-info/METADATA,sha256=H0AhyHQ3_Va2WmbAkd5IgycphRpYVw0GyctqeIAqQt0,11741
59
- ssb_sgis-1.0.13.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
60
- ssb_sgis-1.0.13.dist-info/RECORD,,
59
+ ssb_sgis-1.0.14.dist-info/LICENSE,sha256=np3IfD5m0ZUofn_kVzDZqliozuiO6wrktw3LRPjyEiI,1073
60
+ ssb_sgis-1.0.14.dist-info/METADATA,sha256=fey5-NS1CXLcspl94MR5TNZeGIXjwbQmefO3UCTYkJI,11741
61
+ ssb_sgis-1.0.14.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
62
+ ssb_sgis-1.0.14.dist-info/RECORD,,