Skip to content

Commit

Permalink
Merge pull request #133 from developmentseed/patch/hide-map-elelemt
Browse files Browse the repository at this point in the history
hide map element in HTML pages when collections/items do not have spatial component
  • Loading branch information
vincentsarago authored Oct 18, 2023
2 parents 9c5bbf0 + 48e0a51 commit 11b4411
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 11b4411

@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: 11b4411 Previous: e75fd75 Ratio
tests/benchmarks.py::test_benchmark_items[geojson-10] 88.81248473575877 iter/sec (stddev: 0.00132049696777434) 121.99002399103077 iter/sec (stddev: 0.0009263629026094873) 1.37
tests/benchmarks.py::test_benchmark_items[geojson-100] 42.03670323802795 iter/sec (stddev: 0.01110872178855118) 57.20867185895398 iter/sec (stddev: 0.006542016572128997) 1.36
tests/benchmarks.py::test_benchmark_items[geojson-200] 26.18532353558364 iter/sec (stddev: 0.01729365839408987) 35.79934196751819 iter/sec (stddev: 0.007428568660599457) 1.37
tests/benchmarks.py::test_benchmark_items[geojson-250] 21.546270857264798 iter/sec (stddev: 0.02338308830002849) 28.450108228330965 iter/sec (stddev: 0.012534437074346625) 1.32
tests/benchmarks.py::test_benchmark_items[csv-1] 91.12423236173998 iter/sec (stddev: 0.001067989287990261) 126.54939427588509 iter/sec (stddev: 0.0008488161897162848) 1.39
tests/benchmarks.py::test_benchmark_items[csv-50] 36.720544915185094 iter/sec (stddev: 0.005363783957623249) 51.880737339217475 iter/sec (stddev: 0.001577207100635283) 1.41
tests/benchmarks.py::test_benchmark_items[html-1] 92.33223796147803 iter/sec (stddev: 0.0018863512955781384) 126.59373452455834 iter/sec (stddev: 0.0005832197662749906) 1.37
tests/benchmarks.py::test_benchmark_item[geojson-NewfoundlandandLabrador] 1.4399636624133039 iter/sec (stddev: 0.06418297720040653) 2.062646403676705 iter/sec (stddev: 0.04013449166466239) 1.43
tests/benchmarks.py::test_benchmark_item[geojson-Saskatchewan] 33.80724152911537 iter/sec (stddev: 0.02690112325881413) 47.267283472342214 iter/sec (stddev: 0.01647908518837106) 1.40
tests/benchmarks.py::test_benchmark_tile[4/8/5-WGS1984Quad] 121.77490875020004 iter/sec (stddev: 0.002121986612389241) 160.4615561302861 iter/sec (stddev: 0.00068328238606103) 1.32

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

Please sign in to comment.