Skip to content

Commit

Permalink
Merge pull request #29 from PDOK/fix-bag-api-tiles
Browse files Browse the repository at this point in the history
Support v1_0 for ogc:api tiles with reading landing page
  • Loading branch information
kad-velsl authored Sep 28, 2023
2 parents ade2ae8 + 0b01f50 commit 638238a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 55 deletions.
18 changes: 15 additions & 3 deletions ngr_spider/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Style:

@dataclasses.dataclass
class VectorTileStyle:
id: str
name: str
url: str

Expand All @@ -77,13 +78,24 @@ class WmsLayer(Layer):
minscale: str = ""
maxscale: str = ""

@dataclasses.dataclass
class OatTileSet():
tileset_title: str
tileset_crs: str
tileset_data_type: str
tileset_min_scale: str = ""
tileset_max_scale: str = ""


@dataclasses.dataclass
class OatTiles():
title: str
abstract: str
tilesets: list[OatTileSet]

@dataclasses.dataclass
class OatLayer(Layer):
styles: list[VectorTileStyle]
crs: str
minscale: str = ""
maxscale: str = ""


@dataclasses.dataclass
Expand Down
85 changes: 35 additions & 50 deletions ngr_spider/ogc_api_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import urllib.request

from .models import OatLayer, VectorTileStyle
from .models import OatLayer, OatTiles, VectorTileStyle

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -76,7 +76,7 @@ class OGCApiTiles:

service_desc: ServiceDesc
data: Data
tiles: Tiles
tiles: OatTiles
tile_matrix_sets: TileMatrixSets

title: str
Expand All @@ -86,66 +86,52 @@ def __init__(self, url):
self.service_url = url
self.__load_landing_page(url)

# https://docs.mapbox.com/mapbox-gl-js/style-spec/sources/#vector
def get_layers(self):
service_layer_name: str
service_layer_title: str = ""
service_layer_abstract: str
service_layer_crs: str = ""
service_layer_min_scale: str = ""
service_layer_max_scale: str = ""
service_data_type: str

# process styles
# TODO style should be generated based on the type of the tiles; png, Vector etc.
vector_tile_styles: list[VectorTileStyle] = self.get_styles()
tiles_title: str
tiles_abstract: str
# tileset_titles: str = ""
# tileset_crs: str = ""
# tileset_min_scale: str = ""
# tileset_max_scale: str = ""
# tileset_data_type: str

# process layers
tiles_json = self.tiles.json

service_layer_name = tiles_json["title"]
service_layer_abstract = tiles_json["description"]

tile_sets = tiles_json["tilesets"]
for tile_set in tile_sets:
service_layer_crs = tile_set["crs"]

service_layer_title = tile_set["title"] if "title" in tile_set else ""
t_links = tile_set["links"]
for l in t_links:
if l["rel"] == "self":
with urllib.request.urlopen(l["href"]) as url:
tile = json.load(url)
service_layer_title = tile["title"]
self.service_type = tile["dataType"]

return [
OatLayer(
service_layer_name,
service_layer_title,
service_layer_abstract,
"",
vector_tile_styles,
service_layer_crs,
service_layer_min_scale,
service_layer_max_scale,
tiles_title = tiles_json["title"]
tiles_abstract = tiles_json["description"]
# tile_sets = tiles_json["tilesets"]
# for tile_set in tile_sets:
# t_links = tile_set["links"]
# for l in t_links:
# if l["rel"] == "self":
# with urllib.request.urlopen(l["href"]) as url:
# tile = json.load(url)
# tileset_title = tile["title"]
# tileset_crs = tile["crs"]
# tileset_data_type: tile["dataType"]

layer = OatLayer(
tiles_title,
tiles_title,
tiles_abstract, "",
self.get_styles()
)
]
return [layer]


def __load_landing_page(self, service_url: str):
with urllib.request.urlopen(service_url) as response:
response_body = response.read().decode("utf-8")
response_body_data = json.loads(response_body)

links = response_body_data["links"]
for link in links:
if link["rel"] == "service-desc":
self.service_desc = ServiceDesc(link["href"])
elif link["rel"] == "data":
elif link["rel"] == "data" or link["rel"].endswith('styles'):
self.data = Data(link["href"])
elif link["rel"] == "tiles":
elif link["rel"] == "tiles" or link["rel"].endswith('tilesets-vector'):
self.tiles = Tiles(link["href"])
elif link["rel"] == "tileMatrixSets":
elif link["rel"] == "tileMatrixSets" or link["rel"].endswith('tiling-schemes'):
self.tile_matrix_sets = TileMatrixSets(link["href"])
title = response_body_data["title"]
self.title = title if title else ""
Expand All @@ -158,21 +144,20 @@ def get_styles(self):
default_style_name: str = ""
if data["default"] is not None:
default_style_name = data["default"]

for style in data["styles"]:
style_stylesheet = ""
for link in style["links"]:
sr = link["rel"]
if sr == "stylesheet":
style_stylesheet = link["href"]
s = VectorTileStyle(style["title"], style_stylesheet)
if len(default_style_name) > 0:
s = VectorTileStyle(style["id"], style["title"], style_stylesheet)
if len(default_style_name) > 0 and style["title"] == default_style_name:
styles.insert(0, s) # insert as first element if it is default
else:
styles.append(s)

return styles


def get_tile_matrix_sets(self):
tile_matrix_sets = dict()
matrix_sets = self.tile_matrix_sets.json
Expand Down
6 changes: 4 additions & 2 deletions ngr_spider/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def get_output(
if jq_filter:
transformed_config_text = jq.compile(jq_filter).input(config).text()
config = json.loads(transformed_config_text)

if yaml_output:
content = yaml.dump(config, default_flow_style=False)
else:
Expand Down Expand Up @@ -322,15 +321,18 @@ def get_oat_service(
description = oat.description or oat.service_desc.get_info().description or ""

layers = oat.get_layers()

for layer in layers:
layer.dataset_metadata_id = service_record.dataset_metadata_id

service_url = oat.service_desc.get_tile_request_url()

return OatService(
# http://docs.ogc.org/DRAFTS/19-072.html#rc_landing-page-section
title=title,
abstract=description,
metadata_id=md_id,
url=oat.service_desc.get_tile_request_url(),
url=service_url,
layers=layers,
keywords=oat.service_desc.get_tags(),
dataset_metadata_id=service_record.dataset_metadata_id,
Expand Down

0 comments on commit 638238a

Please sign in to comment.