Skip to content

Commit

Permalink
Merge branch 'main' into patch/includes-excludes
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago authored Oct 30, 2023
2 parents 0739603 + 7859298 commit 77722da
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 83 deletions.
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin

## [unreleased]

### added

- add `py.typed` file

### fixed

- hide map element in HTML pages when collections/items do not have spatial component

### changed

- split endpoints registration for more customization

## [0.4.4] - 2023-10-03

### fixed
Expand Down
33 changes: 29 additions & 4 deletions tipg/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ def __post_init__(self):
"""Post Init: register route and configure specific options."""
self.register_routes()
if self.with_common:
self.register_common_routes()
self._conformance_route()
self._landing_route()

def url_for(self, request: Request, name: str, **path_params: Any) -> str:
"""Return full url (with prefix) for a specific handler."""
Expand Down Expand Up @@ -269,8 +270,8 @@ def links(self, request: Request) -> List[model.Link]:
"""Register factory Routes."""
...

def register_common_routes(self):
"""Register Landing (/) and Conformance (/conformance) routes."""
def _conformance_route(self):
"""Register Conformance (/conformance) route."""

@self.router.get(
"/conformance",
Expand Down Expand Up @@ -303,6 +304,9 @@ def conformance(

return data

def _landing_route(self):
"""Register Landing (/) and Conformance (/conformance) routes."""

@self.router.get(
"/",
response_model=model.Landing,
Expand Down Expand Up @@ -421,9 +425,15 @@ def links(self, request: Request) -> List[model.Link]:
),
]

def register_routes(self): # noqa: C901
def register_routes(self):
"""Register OGC Features endpoints."""
self._collections_route()
self._collection_route()
self._queryables_route()
self._items_route()
self._item_route()

def _collections_route(self): # noqa: C901
@self.router.get(
"/collections",
response_model=model.Collections,
Expand Down Expand Up @@ -603,6 +613,7 @@ def collections( # noqa: C901

return data

def _collection_route(self):
@self.router.get(
"/collections/{collectionId}",
response_model=model.Collection,
Expand Down Expand Up @@ -688,6 +699,7 @@ def collection(

return data

def _queryables_route(self):
@self.router.get(
"/collections/{collectionId}/queryables",
response_model=model.Queryables,
Expand Down Expand Up @@ -731,6 +743,7 @@ def queryables(

return data

def _items_route(self): # noqa: C901
@self.router.get(
"/collections/{collectionId}/items",
response_class=GeoJSONResponse,
Expand Down Expand Up @@ -1005,6 +1018,7 @@ async def items( # noqa: C901
# Default to GeoJSON Response
return GeoJSONResponse(data)

def _item_route(self):
@self.router.get(
"/collections/{collectionId}/items/{itemId}",
response_class=GeoJSONResponse,
Expand Down Expand Up @@ -1247,7 +1261,13 @@ def links(self, request: Request) -> List[model.Link]:

def register_routes(self): # noqa: C901
"""Register OGC Tiles endpoints."""
self._tilematrixsets_routes()
self._tilesets_routes()
self._tile_routes()
self._tilejson_routes()
self._stylejson_routes()

def _tilematrixsets_routes(self):
@self.router.get(
r"/tileMatrixSets",
response_model=model.TileMatrixSetList,
Expand Down Expand Up @@ -1344,6 +1364,7 @@ async def tilematrixset(

return data

def _tilesets_routes(self):
@self.router.get(
"/collections/{collectionId}/tiles",
response_model=model.TileSetList,
Expand Down Expand Up @@ -1547,6 +1568,7 @@ async def collection_tileset(

return data

def _tile_routes(self):
@self.router.get(
"/collections/{collectionId}/tiles/{tileMatrixSetId}/{z}/{x}/{y}",
response_class=Response,
Expand Down Expand Up @@ -1620,6 +1642,8 @@ async def collection_get_tile(

return Response(bytes(tile), media_type=MediaType.mvt.value)

def _tilejson_routes(self):

############################################################################
# ADDITIONAL ENDPOINTS NOT IN OGC Tiles API (tilejson, style.json, viewer) #
############################################################################
Expand Down Expand Up @@ -1721,6 +1745,7 @@ async def collection_tilejson(

return tile_json

def _stylejson_routes(self):
@self.router.get(
"/collections/{collectionId}/{tileMatrixSetId}/style.json",
response_model=model.StyleJSON,
Expand Down
38 changes: 22 additions & 16 deletions tipg/templates/collection.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,30 @@ <h2>Links</h2>

<script>
$(function() {
var map = L.map('map').setView([0, 0], 1);
map.addLayer(new L.TileLayer(
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}
));
var collection = {{ response|tojson }};
if (collection.extent && collection.extent.spatial){
var bbox = collection.extent.spatial.bbox[0]
var bbox_polygon = L.polygon([
[bbox[1], bbox[0]],
[bbox[1], bbox[2]],
[bbox[3], bbox[2]],
[bbox[3], bbox[0]]
]);

var bbox = {{ ('extent' in response and response.extent.spatial.bbox.0) or [-180,-90,180,90] }};
var bbox_polygon = L.polygon([
[bbox[1], bbox[0]],
[bbox[1], bbox[2]],
[bbox[3], bbox[2]],
[bbox[3], bbox[0]]
]);
var map = L.map('map').setView([0, 0], 1);
map.addLayer(new L.TileLayer(
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}
));

map.addLayer(bbox_polygon);
map.fitBounds(bbox_polygon.getBounds());
} else {
document.getElementById("map").style.display = "none";
}

map.addLayer(bbox_polygon);
map.fitBounds(bbox_polygon.getBounds());
});
</script>

Expand Down
93 changes: 49 additions & 44 deletions tipg/templates/item.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,60 +32,65 @@ <h2>Properties</h2>

<script>
var geojson = {{ response|tojson }};
var map = L.map('map').setView([0, 0], 1);
map.addLayer(new L.TileLayer(
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}
));

function displayValue(value) {
switch (typeof value) {
case 'string':
return value;
case 'number':
return value.toString();
case 'object':
if (value instanceof Array) {
return value.map(displayValue).join(', ');
} else {
return JSON.stringify(value);
}
default:
return '';
if (geojson.geometry) {
var map = L.map('map').setView([0, 0], 1);
map.addLayer(new L.TileLayer(
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}
));

function displayValue(value) {
switch (typeof value) {
case 'string':
return value;
case 'number':
return value.toString();
case 'object':
if (value instanceof Array) {
return value.map(displayValue).join(', ');
} else {
return JSON.stringify(value);
}
default:
return '';
}
}
}

function addPopup(feature, layer) {
if (feature.properties) {
var popupElm = document.createElement('div');
popupElm.style.overflowX = 'scroll';
function addPopup(feature, layer) {
if (feature.properties) {
var popupElm = document.createElement('div');
popupElm.style.overflowX = 'scroll';

Object.keys(geojson.properties).map(prop => {
var propElm = document.createElement('div');
Object.keys(geojson.properties).map(prop => {
var propElm = document.createElement('div');

var bElm = document.createElement('b');
bElm.innerText = prop;
propElm.appendChild(bElm);
var valueElm = document.createTextNode(` : ${displayValue(feature.properties[prop])}`);
propElm.appendChild(valueElm);
var bElm = document.createElement('b');
bElm.innerText = prop;
propElm.appendChild(bElm);
var valueElm = document.createTextNode(` : ${displayValue(feature.properties[prop])}`);
propElm.appendChild(valueElm);

var brElm = document.createElement('br');
propElm.appendChild(brElm);
var brElm = document.createElement('br');
propElm.appendChild(brElm);

popupElm.appendChild(propElm);
})
popupElm.appendChild(propElm);
})

layer.bindPopup(popupElm);
layer.bindPopup(popupElm);
}
}

var features = L.geoJSON(geojson, {
onEachFeature: addPopup
}).addTo(map);

map.fitBounds(features.getBounds());
} else {
document.getElementById("map").style.display = "none";
}

var features = L.geoJSON(geojson, {
onEachFeature: addPopup
}).addTo(map);

map.fitBounds(features.getBounds());
</script>

{% include "footer.html" %}
44 changes: 25 additions & 19 deletions tipg/templates/items.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,32 +84,38 @@ <h1>Collection Items: {{ response.title or response.id }}</h1>
window.location.href = url;
}
$(function() {

//
// mapping
//
var geojson = {{ response|tojson }};
var map = L.map('map').setView([0, 0], 1);
map.addLayer(new L.TileLayer(
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'

var features = (geojson.features) ? geojson.features : [];
var hasGeom = features.some(feat => feat.geometry);
if (hasGeom) {
var map = L.map('map').setView([0, 0], 1);
map.addLayer(new L.TileLayer(
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}
));

function addPopup(feature, layer) {
var aElm = document.createElement('a');
aElm.setAttribute('href', `${currentURL}/${feature.id}`);
aElm.setAttribute('target', '_blank');
aElm.innerText = feature.id;
layer.bindPopup(aElm);
}
));

function addPopup(feature, layer) {
var aElm = document.createElement('a');
aElm.setAttribute('href', `${currentURL}/${feature.id}`);
aElm.setAttribute('target', '_blank');
aElm.innerText = feature.id;
layer.bindPopup(aElm);
}

var features = L.geoJSON(geojson, {
onEachFeature: addPopup
}).addTo(map);
var features = L.geoJSON(geojson, {
onEachFeature: addPopup
}).addTo(map);

map.fitBounds(features.getBounds());
map.fitBounds(features.getBounds());
} else {
document.getElementById("map").style.display = "none";
}

//
// paging
Expand Down

0 comments on commit 77722da

Please sign in to comment.