Skip to content

Commit

Permalink
Support multiple tile providers, remove Stamen tiles
Browse files Browse the repository at this point in the history
Copied the same functionality from ckanext-spatial to leverage
leaflet-providers:

ckan/ckanext-spatial#317

https://docs.ckan.org/projects/ckanext-spatial/en/latest/map-widgets.html
  • Loading branch information
amercader committed Nov 17, 2023
1 parent 1eea3f2 commit 5bc1ccd
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ Common base layers for Map Widgets

The geospatial view plugins support the same base map configurations than the ckanext-spatial `widgets`_.

Check the following page to learn how to choose a different base map layer (Stamen, MapBox or custom):
Check the following page to learn how to choose a different base map layer:

http://docs.ckan.org/projects/ckanext-spatial/en/latest/map-widgets.html

Expand Down
10 changes: 10 additions & 0 deletions ckanext/geoview/public/css/geojson_preview.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ html, body {
height: 300px;
overflow: auto;
}

.leaflet-control-no-provider {
background-color: white;
margin-right: 10px;
padding: 10px;
border: 1px solid #b1b1b1;
}
.leaflet-control-attribution {
font-size: 11px;
}
10 changes: 10 additions & 0 deletions ckanext/geoview/public/css/shp_preview.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ html, body {
overflow: auto;
max-height: 200px;
}

.leaflet-control-no-provider {
background-color: white;
margin-right: 10px;
padding: 10px;
border: 1px solid #b1b1b1;
}
.leaflet-control-attribution {
font-size: 11px;
}
10 changes: 10 additions & 0 deletions ckanext/geoview/public/css/wmts_preview.css
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ html, body {
.ui-opacity .handle:hover {
background: #303030;
}

.leaflet-control-no-provider {
background-color: white;
margin-right: 10px;
padding: 10px;
border: 1px solid #b1b1b1;
}
.leaflet-control-attribution {
font-size: 11px;
}
66 changes: 52 additions & 14 deletions ckanext/geoview/public/js/common_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
maxZoom: 18
});

var baseLayer;

map = new L.Map(container, leafletMapOptions);

if (mapConfig.type == 'mapbox') {
Expand All @@ -44,28 +46,64 @@
'See http://www.mapbox.com/developers/api-overview/ for details';
}

baseLayerUrl = '//{s}.tiles.mapbox.com/v4/' + mapConfig['mapbox.map_id'] + '/{z}/{x}/{y}.png?access_token=' + mapConfig['mapbox.access_token'];
leafletBaseLayerOptions.handle = mapConfig['mapbox.map_id'];
leafletBaseLayerOptions.subdomains = mapConfig.subdomains || 'abcd';
leafletBaseLayerOptions.attribution = mapConfig.attribution || 'Data: <a href="http://osm.org/copyright" target="_blank">OpenStreetMap</a>, Design: <a href="http://mapbox.com/about/maps" target="_blank">MapBox</a>';
baseLayer = L.tileLayer.provider('MapBox', {
id: mapConfig['mapbox.map_id'],
accessToken: mapConfig['mapbox.access_token']
});

} else if (mapConfig.type == 'custom') {
// Custom XYZ layer
baseLayerUrl = mapConfig['custom.url'];
if (!baseLayerUrl)
throw '[CKAN Map Widgets] Custom URL must be set when using Custom Map type';

baseLayerUrl = mapConfig['custom_url'] || mapConfig['custom.url'];
if (mapConfig.subdomains) leafletBaseLayerOptions.subdomains = mapConfig.subdomains;
if (mapConfig.tms) leafletBaseLayerOptions.tms = mapConfig.tms;
leafletBaseLayerOptions.attribution = mapConfig.attribution;

baseLayer = new L.TileLayer(baseLayerUrl, leafletBaseLayerOptions);

} else if (mapConfig.type == 'wms') {

baseLayerUrl = mapConfig['wms.url'];
wmsOptions = {}
wmsOptions['layers'] = mapConfig['wms.layers'];
wmsOptions['styles'] = mapConfig['wms.styles'] || '';
wmsOptions['format'] = mapConfig['wms.format'] || 'image/png';
if(mapConfig['wms.srs'] || mapConfig['wms.crs']) {
wmsOptions['crs'] = mapConfig['wms.srs'] || mapConfig['wms.crs'];
}
wmsOptions['version'] = mapConfig['wms.version'] || '1.1.1';

baseLayer = new L.TileLayer.WMS(baseLayerUrl, wmsOptions);


} else if (mapConfig.type) {

baseLayer = L.tileLayer.provider(mapConfig.type, mapConfig)

} else {
// Default to Stamen base map
baseLayerUrl = 'https://stamen-tiles-{s}.a.ssl.fastly.net/terrain/{z}/{x}/{y}.png';
leafletBaseLayerOptions.subdomains = mapConfig.subdomains || 'abcd';
leafletBaseLayerOptions.attribution = mapConfig.attribution || 'Map tiles by <a href="http://stamen.com">Stamen Design</a> (<a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>). Data by <a href="http://openstreetmap.org">OpenStreetMap</a> (<a href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>)';
let c = L.Control.extend({

onAdd: (map) => {
let element = document.createElement("div");
element.className = "leaflet-control-no-provider";
element.innerHTML = 'No map provider set. Please check the <a href="https://docs.ckan.org/projects/ckanext-spatial/en/latest/map-widgets.html">documentation</a>';
return element;
},
onRemove: (map) => {}
})
map.addControl(new c({position: "bottomleft"}))

}

var baseLayer = new L.TileLayer(baseLayerUrl, leafletBaseLayerOptions);
map.addLayer(baseLayer);
if (baseLayer) {
let attribution = L.control.attribution({"prefix": false});
attribution.addTo(map)

map.addLayer(baseLayer);

if (mapConfig.attribution) {
attribution.addAttribution(mapConfig.attribution);
}
}

return map;

Expand Down
7 changes: 2 additions & 5 deletions ckanext/geoview/public/js/ol_preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,8 @@
} else if (mapConfig.type == 'custom') {
mapConfig.type = 'XYZ'
} else if (!mapConfig.type || mapConfig.type.toLowerCase() == 'osm') {
// default to Stamen base map
mapConfig.type = 'Stamen';
mapConfig.url = 'https://stamen-tiles-{s}.a.ssl.fastly.net/terrain/{z}/{x}/{y}.png';
mapConfig.subdomains = mapConfig.subdomains || 'abcd';
mapConfig.attribution = mapConfig.attribution || 'Map tiles by <a href="http://stamen.com">Stamen Design</a> (<a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>). Data by <a href="http://openstreetmap.org">OpenStreetMap</a> (<a href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>)';

mapConfig.type = 'OSM'
}

return OL_HELPERS.createLayerFromConfig(mapConfig, true, callback);
Expand Down

0 comments on commit 5bc1ccd

Please sign in to comment.