Skip to content

Commit 721c98b

Browse files
committed
- Make sure we only show the map preview, when the view is within bounds of the crs
1 parent b581b8c commit 721c98b

1 file changed

Lines changed: 43 additions & 23 deletions

File tree

pygeoapi/templates/collections/collection.html

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ <h3>{% trans %}Storage CRS{% endtrans %}</h3>
140140
{% block extrafoot %}
141141
<script>
142142
var map = L.map('collection-map').setView([{{ 0 }}, {{ 0 }}], 1);
143+
143144
map.addLayer(new L.TileLayer(
144145
'{{ config['server']['map']['url'] }}', {
145146
maxZoom: 18,
@@ -156,6 +157,7 @@ <h3>{% trans %}Storage CRS{% endtrans %}</h3>
156157

157158
var lbounds = bbox_layer.getBounds();
158159

160+
// Make sure that we pass valid coordinates to the imageOverlay
159161
function clampLat(lat) {
160162
return Math.max(-85.0511, Math.min(lat, 85.0511));
161163
}
@@ -166,34 +168,52 @@ <h3>{% trans %}Storage CRS{% endtrans %}</h3>
166168
var clampedNe = L.latLng(clampLat(ne.lat), ne.lng);
167169
var clampedBounds = L.latLngBounds(clampedSw, clampedNe);
168170

169-
// Set the bounds to the limits of WebMercator
170-
var crsBounds = L.latLngBounds(
171-
L.latLng(-85.0511, -180), // SouthWest
172-
L.latLng(85.0511, 180) // NorthEast
173-
);
174-
map.setMaxBounds(crsBounds);
171+
var ogcapi_layer = null;
175172

176-
{# if this collection has a map representation, add it to the map #}
177-
{% for link in data['links'] %}
178-
{% if link['rel'] == 'http://www.opengis.net/def/rel/ogc/1.0/map' and link['href'] %}
179-
L.imageOverlay.ogcapi("{{ data['base_url'] }}", {collection: "{{ data['id'] }}", "opacity": .7, "transparent": true, "bounds": clampedBounds }).addTo(map);
180-
bbox_layer.setStyle({
181-
fillOpacity: 0
182-
});
183-
{% endif %}
184-
{% endfor %}
173+
{# if this collection has a map representation, add it to the map #}
174+
{% for link in data['links'] %}
175+
{% if link['rel'] == 'http://www.opengis.net/def/rel/ogc/1.0/map' and link['href'] %}
176+
ogcapi_layer = L.imageOverlay.ogcapi("{{ data['base_url'] }}", {
177+
collection: "{{ data['id'] }}",
178+
"opacity": .7,
179+
"transparent": true,
180+
"bounds": clampedBounds
181+
});
182+
bbox_layer.setStyle({
183+
fillOpacity: 0
184+
});
185+
{% endif %}
186+
{% endfor %}
185187

186-
map.addLayer(bbox_layer);
187-
map.fitBounds(clampedBounds, {maxZoom: 10,});
188+
// Check bounds and toggle the visibility of the imageOverlay, accordingly
189+
function toggleOverlayVisibility() {
190+
191+
if (ogcapi_layer) {
192+
var currentBounds = map.getBounds();
193+
var west = currentBounds.getWest();
194+
var east = currentBounds.getEast();
195+
var centerLng = map.getCenter().lng;
196+
197+
var viewWidth = east - west;
188198

189-
// Make sure we stay within the bounds
190-
map.on('moveend', function() {
191-
if (!crsBounds.contains(map.getBounds())) {
192-
map.fitBounds(clampedBounds);
199+
var isWithinBounds = (viewWidth <= 360) && (centerLng >= -180 && centerLng <= 180);
200+
201+
if (isWithinBounds) {
202+
if (!map.hasLayer(ogcapi_layer)) map.addLayer(ogcapi_layer);
203+
} else {
204+
if (map.hasLayer(ogcapi_layer)) map.removeLayer(ogcapi_layer);
205+
}
193206
}
194-
});
207+
}
208+
209+
map.on('moveend', toggleOverlayVisibility);
210+
211+
map.addLayer(bbox_layer);
212+
213+
map.fitBounds(clampedBounds, {maxZoom: 10});
195214

196-
map.setMinZoom(2);
215+
// Run the initial visibility check
216+
toggleOverlayVisibility();
197217

198218
// Allow to get bbox query parameter of a rectangular area specified by
199219
// dragging the mouse while pressing the Ctrl key

0 commit comments

Comments
 (0)