Skip to content

Commit

Permalink
hide map element in HTML pages when collections/items do not have spa…
Browse files Browse the repository at this point in the history
…tial component
  • Loading branch information
vincentsarago committed Oct 18, 2023
1 parent 9c5bbf0 commit 48e0a51
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 79 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

Note: Minor version `0.X.0` update might break the API, It's recommended to pin `tipg` to minor version: `tipg>=0.1,<0.2`

## [unreleased]

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

## [0.4.4] - 2023-10-03

### fixed
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

1 comment on commit 48e0a51

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'TiPg Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: 48e0a51 Previous: e75fd75 Ratio
tests/benchmarks.py::test_benchmark_queryables 301.02570798665727 iter/sec (stddev: 0.001797922296329769) 392.818909828259 iter/sec (stddev: 0.000774885510089034) 1.30
tests/benchmarks.py::test_benchmark_items[geojson-1] 100.24430707152555 iter/sec (stddev: 0.0012981074836520261) 132.92403592312527 iter/sec (stddev: 0.000816070781249228) 1.33
tests/benchmarks.py::test_benchmark_items[geojson-10] 85.83035570772084 iter/sec (stddev: 0.001843383631381456) 121.99002399103077 iter/sec (stddev: 0.0009263629026094873) 1.42
tests/benchmarks.py::test_benchmark_items[geojson-100] 41.222353042522954 iter/sec (stddev: 0.012519965891092232) 57.20867185895398 iter/sec (stddev: 0.006542016572128997) 1.39
tests/benchmarks.py::test_benchmark_items[geojson-200] 26.530693391122174 iter/sec (stddev: 0.015468916380076513) 35.79934196751819 iter/sec (stddev: 0.007428568660599457) 1.35
tests/benchmarks.py::test_benchmark_items[csv-1] 92.20415417251735 iter/sec (stddev: 0.0015489065349480347) 126.54939427588509 iter/sec (stddev: 0.0008488161897162848) 1.37
tests/benchmarks.py::test_benchmark_items[csv-10] 67.53183410139087 iter/sec (stddev: 0.0025324738222979423) 91.91998848417087 iter/sec (stddev: 0.0011282472183745795) 1.36
tests/benchmarks.py::test_benchmark_items[csv-50] 37.4411054573199 iter/sec (stddev: 0.0035447714330509262) 51.880737339217475 iter/sec (stddev: 0.001577207100635283) 1.39
tests/benchmarks.py::test_benchmark_items[csv-100] 21.814126353946698 iter/sec (stddev: 0.008041609814644121) 32.728019468667824 iter/sec (stddev: 0.0022010778222656666) 1.50
tests/benchmarks.py::test_benchmark_items[csv-200] 13.774778104141488 iter/sec (stddev: 0.005828997693463858) 18.40926388399906 iter/sec (stddev: 0.0030869897023960142) 1.34
tests/benchmarks.py::test_benchmark_items[html-1] 88.97716623767563 iter/sec (stddev: 0.0015832159765451364) 126.59373452455834 iter/sec (stddev: 0.0005832197662749906) 1.42
tests/benchmarks.py::test_benchmark_items[html-10] 76.06733268841901 iter/sec (stddev: 0.0022023562559729034) 99.72858206028634 iter/sec (stddev: 0.0012702637284808275) 1.31
tests/benchmarks.py::test_benchmark_items[html-100] 28.12254206785232 iter/sec (stddev: 0.021465557295555205) 37.093799042431556 iter/sec (stddev: 0.0018696312593765425) 1.32
tests/benchmarks.py::test_benchmark_item[geojson-Saskatchewan] 34.1193660904895 iter/sec (stddev: 0.025606232330712752) 47.267283472342214 iter/sec (stddev: 0.01647908518837106) 1.39
tests/benchmarks.py::test_benchmark_item[html-Saskatchewan] 12.853999251870771 iter/sec (stddev: 0.049646718299094096) 18.29548117433429 iter/sec (stddev: 0.03360805299530729) 1.42
tests/benchmarks.py::test_benchmark_tile[4/8/5-WGS1984Quad] 117.69412475744781 iter/sec (stddev: 0.0010350114827629656) 160.4615561302861 iter/sec (stddev: 0.00068328238606103) 1.36
tests/benchmarks.py::test_benchmark_tile[4/8/5-WebMercatorQuad] 78.11155403948928 iter/sec (stddev: 0.0016226360721828146) 103.89433998394951 iter/sec (stddev: 0.0008893832393445805) 1.33
tests/benchmarks.py::test_benchmark_tilematrixset_endpoints[/collections/public.landsat_wrs/tiles/WGS1984Quad] 145.8977341624986 iter/sec (stddev: 0.001085336956666662) 198.93270099366825 iter/sec (stddev: 0.0004945340594391225) 1.36
tests/benchmarks.py::test_benchmark_tilematrixset_endpoints[/collections/public.landsat_wrs/tiles/WebMercatorQuad] 143.30857790553125 iter/sec (stddev: 0.0012961193346084988) 196.4391672765982 iter/sec (stddev: 0.00039154133398389937) 1.37

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.