Skip to content

Commit

Permalink
Get bbox query parameter interactively (#1339)
Browse files Browse the repository at this point in the history
* Update script to allow to get bbox query parameter interactively

* Remove unnecessary line

* Remove '?' prefix for bbox

* Switch back to leaflet version 1.3.1
  • Loading branch information
MTachon authored Aug 29, 2023
1 parent fbd9209 commit aa42cc6
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pygeoapi/templates/collections/collection.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,70 @@ <h3>{% trans %}Storage CRS{% endtrans %}</h3>
map.addLayer(bbox_layer);
map.fitBounds(bbox_layer.getBounds(), {maxZoom: 10});
// Allow to get bbox query parameter of a rectangular area specified by
// dragging the mouse while pressing the Ctrl key
var firstCorner;
var secondCorner;
var currentBoundingBox;
var boundingBox;
var drawingBoundingBox = false;
map.on('mousedown', setFirstCorner);
map.on('mouseup', setSecondCorner);
map.on('mousemove', drawCurrentBoundingBox);
map.on('click', boundingBoxInfo);

function boundingBoxInfo(e) {
if (map.hasLayer(boundingBox)) {
var bboxCoords = boundingBox.getBounds().toBBoxString();
L.popup({maxWidth:"auto"})
.setLatLng(boundingBox.getBounds().getCenter())
.setContent(`bbox=${bboxCoords}`)
.addTo(map);
}
}

function setFirstCorner(e) {
if (e.originalEvent.ctrlKey) {
if (map.hasLayer(boundingBox)) {
map.removeLayer(boundingBox);
}
map.dragging.disable();
firstCorner = e.latlng;
drawingBoundingBox = true;
} else if (map.hasLayer(boundingBox) && !boundingBox.getBounds().contains(e.latlng)) {
map.removeLayer(boundingBox);
}
}

function setSecondCorner(e) {
if (map.hasLayer(currentBoundingBox)) {
map.removeLayer(currentBoundingBox);
}
if (e.originalEvent.ctrlKey) {
if (drawingBoundingBox) {
drawingBoundingBox = false;
secondCorner = e.latlng;
var bounds = [firstCorner, secondCorner];
boundingBox = L.rectangle(bounds, {color:"#ff7800", weight:1});
boundingBox.addTo(map);
}
}
drawingBoundingBox = false;
map.dragging.enable();
}

function drawCurrentBoundingBox(e) {
if (drawingBoundingBox) {
if (map.hasLayer(currentBoundingBox)) {
map.removeLayer(currentBoundingBox);
}
secondCorner = e.latlng;
var bounds = [firstCorner, secondCorner];
currentBoundingBox = L.rectangle(bounds, {dashArray:"4 1", weight:0.4});
currentBoundingBox.addTo(map);
}
}
</script>
{% endblock %}

0 comments on commit aa42cc6

Please sign in to comment.