section on each PHP page using Leaflet maps (../pages/geo_search.php), the Leaflet Providers section in ../include/config.default.php, and the appropriate providers group section in ../languages/en.php.
// Define available Leaflet basemaps groups and layers using leaflet.providers.js, L.TileLayer.PouchDBCached.js, and styledLayerControl.js.
use Gettext\Languages\Exporter\Php;
/**
* Generates OpenStreetMap basemaps for use in a Leaflet map.
*
* This function defines various tile layers from OpenStreetMap and related providers,
* setting properties such as caching, retina display support, maximum zoom levels,
* and attribution for each layer.
*
* @return string JavaScript code that initializes the OSM basemaps for Leaflet.
*/
function leaflet_osm_basemaps()
{
global $map_default_cache, $map_retina;
return "
var osm_attribute = 'Map data © OpenStreetMap contributors';
var osm_mapnik = L.tileLayer.provider('OpenStreetMap.Mapnik', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 19,
attribution: osm_attribute
});
var osm_de = L.tileLayer.provider('OpenStreetMap.DE', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 18,
attribution: osm_attribute
});
var osm_fr_attribute = '© Openstreetmap France | © OpenStreetMap';
var osm_fr = L.tileLayer.provider('OpenStreetMap.France', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 20,
attribution: osm_fr_attribute
});
var osm_ch = L.tileLayer.provider('OpenStreetMap.CH', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 18,
attribution: osm_attribute
});
var osm_bzh = L.tileLayer.provider('OpenStreetMap.BZH', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 19,
attribution: osm_attribute
});
var osm_hot = L.tileLayer.provider('OpenStreetMap.HOT', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 19,
attribution: osm_attribute
});
var osm_hikebike = L.tileLayer.provider('HikeBike.HikeBike', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 19,
attribution: osm_attribute
});
var osm_mtb = L.tileLayer.provider('MtbMap', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
attribution: osm_attribute
});
var osm_otm_attribute = 'Map data: © OpenStreetMap, SRTM | Map style: © OpenTopoMap (CC-BY-SA)';
var osm_otm = L.tileLayer.provider('OpenTopoMap', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 17,
attribution: osm_otm_attribute
}); ";
}
/**
* Generates ESRI basemaps for use in a Leaflet map.
*
* This function defines various tile layers from ESRI, setting properties such as caching,
* retina display support, maximum zoom levels, and attribution for each layer.
*
* @return string JavaScript code that initializes the ESRI basemaps for Leaflet.
*/
function leaflet_esri_basemaps() // ESRI basemaps.
{
global $map_default_cache, $map_retina;
return "
var esri_street_attribute = 'Tiles © Esri — Source: Esri, DeLorme, NAVTEQ, USGS, Intermap, iPC, NRCAN, Esri Japan, METI, Esri China (Hong Kong), Esri (Thailand), TomTom, 2012';
var esri_street = L.tileLayer.provider('Esri.WorldStreetMap', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
attribution: esri_street_attribute
});
var esri_delorme_attribute = 'Tiles © Esri — Copyright: ©2012 DeLorme';
var esri_delorme = L.tileLayer.provider('Esri.DeLorme', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
minZoom: 1,
maxZoom: 11,
attribution: esri_delorme_attribute
});
var esri_topo_attribute = 'Tiles © Esri — Esri, DeLorme, NAVTEQ, TomTom, Intermap, iPC, USGS, FAO, NPS, NRCAN, GeoBase, Kadaster NL, Ordnance Survey, Esri Japan, METI, Esri China (Hong Kong), and the GIS User Community';
var esri_topo = L.tileLayer.provider('Esri.WorldTopoMap', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
attribution: esri_topo_attribute
});
var esri_imagery_attribute = 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community';
var esri_imagery = L.tileLayer.provider('Esri.WorldImagery', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
attribution: esri_imagery_attribute
});
var esri_terrain_attribute = 'Tiles © Esri — Source: USGS, Esri, TANA, DeLorme, and NPS';
var esri_terrain = L.tileLayer.provider('Esri.WorldTerrain', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 13,
attribution: esri_terrain_attribute
});
var esri_relief_attribute = 'Tiles © Esri — Source: Esri';
var esri_relief = L.tileLayer.provider('Esri.WorldShadedRelief', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 13,
attribution: esri_relief_attribute
});
var esri_physical_attribute = 'Tiles © Esri — Source: US National Park Service';
var esri_physical = L.tileLayer.provider('Esri.WorldPhysical', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 8,
attribution: esri_physical_attribute
});
var esri_ocean_attribute = 'Tiles © Esri — Sources: GEBCO, NOAA, CHS, OSU, UNH, CSUMB, National Geographic, DeLorme, NAVTEQ, and Esri';
var esri_ocean = L.tileLayer.provider('Esri.OceanBasemap', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 13,
attribution: esri_ocean_attribute
});
var esri_natgeo_attribute = 'Tiles © Esri — National Geographic, Esri, DeLorme, NAVTEQ, UNEP-WCMC, USGS, NASA, ESA, METI, NRCAN, GEBCO, NOAA, iPC';
var esri_natgeo = L.tileLayer.provider('Esri.NatGeoWorldMap', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 16,
attribution: esri_natgeo_attribute
});
var esri_gray_attribute = 'Tiles © Esri — Esri, DeLorme, NAVTEQ';
var esri_gray = L.tileLayer.provider('Esri.WorldGrayCanvas', {
useCache: '" . ( $map_default_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 16,
attribution: esri_gray_attribute
}); ";
}
function leaflet_stamen_basemaps() // Stamen basemaps.
{
global $map_layer_cache, $map_retina;
return "
var stamen_attribute = 'Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap';
var stamen_toner = L.tileLayer.provider('Stamen.Toner', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
minZoom: 0,
maxZoom: 20,
ext: 'png',
attribution: stamen_attribute
});
var stamen_tonerlt = L.tileLayer.provider('Stamen.TonerLite', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
minZoom: 0,
maxZoom: 20,
ext: 'png',
attribution: stamen_attribute
});
var stamen_tonerback = L.tileLayer.provider('Stamen.TonerBackground', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
minZoom: 0,
maxZoom: 20,
ext: 'png',
attribution: stamen_attribute
});
var stamen_terrain = L.tileLayer.provider('Stamen.Terrain', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
minZoom: 0,
maxZoom: 18,
ext: 'png',
attribution: stamen_attribute
});
var stamen_terrainback = L.tileLayer.provider('Stamen.TerrainBackground', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
minZoom: 0,
maxZoom: 18,
ext: 'png',
attribution: stamen_attribute
});
var stamen_relief = L.tileLayer.provider('Stamen.TopOSMRelief', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
minZoom: 0,
maxZoom: 20,
ext: 'jpg',
attribution: stamen_attribute
});
var stamen_watercolor = L.tileLayer.provider('Stamen.Watercolor', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
minZoom: 1,
maxZoom: 16,
ext: 'jpg',
attribution: stamen_attribute
}); ";
}
/**
* Generates Hydda basemaps for use in a Leaflet map.
*
* @return string JavaScript code that initializes the Hydda basemaps for Leaflet.
*/
function leaflet_hydda_basemaps() // Hydda basemaps.
{
global $map_layer_cache, $map_retina;
return "
var hydda_attribute = 'Tiles courtesy of OpenStreetMap Sweden — Map data © OpenStreetMap';
var hydda_full = L.tileLayer.provider('Hydda.Full', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 18,
attribution: hydda_attribute
});
var hydda_base = L.tileLayer.provider('Hydda.Base', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 18,
attribution: hydda_attribute
}); ";
}
/**
* Generates NASA basemaps for use in a Leaflet map.
*
* @return string JavaScript code that initializes the NASA basemaps for Leaflet.
*/
function leaflet_nasa_basemaps() // NASA basemaps.
{
global $map_layer_cache, $map_retina;
return "
var nasa_attribute = 'Imagery provided by services from the Global Imagery Browse Services (GIBS), operated by the NASA/GSFC/Earth Science Data and Information System (ESDIS) with funding provided by NASA/HQ.';
var nasa_gibscolor = L.tileLayer.provider('NASAGIBS.ModisTerraTrueColorCR', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
minZoom: 1,
maxZoom: 9,
format: 'jpg',
attribution: nasa_attribute
});
var nasa_gibsfalsecolor = L.tileLayer.provider('NASAGIBS.ModisTerraBands367CR', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
minZoom: 1,
maxZoom: 9,
format: 'jpg',
attribution: nasa_attribute
});
var nasa_gibsnight = L.tileLayer.provider('NASAGIBS.ViirsEarthAtNight2012', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
minZoom: 1,
maxZoom: 8,
format: 'jpg',
attribution: nasa_attribute
}); ";
}
/**
* Generates USGS basemaps for use in a Leaflet map.
*
* @return string JavaScript code that initializes the USGS basemaps for Leaflet.
*/
function leaflet_usgs_basemaps() // U.S. Geological Survey The National Map basemaps.
{
global $map_layer_cache, $map_retina;
return "
var usgstnm_attribute = 'Map data U.S. Department of the Interior | U.S. Geological Survey';
var usgs_topo = L.tileLayer.provider('USGSTNM.USTopo', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
attribution: usgstnm_attribute
});
var usgs_imagery = L.tileLayer.provider('USGSTNM.USImagery', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
attribution: usgstnm_attribute
});
var usgs_imagerytopo = L.tileLayer.provider('USGSTNM.USImageryTopo', {
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
attribution: usgstnm_attribute
}); ";
}
/**
* Generates Thunderforest basemaps for use in a Leaflet map.
*
* @return string JavaScript code that initializes the Thunderforest basemaps for Leaflet.
*/
function leaflet_thunderforest_basemaps() // Thunderforest basemaps.
{
global $map_layer_cache, $map_retina, $map_tfapi;
return "
var tf_attribute = '© Thunderforest, © OpenStreetMap';
var tf_ocm = L.tileLayer.provider('Thunderforest.OpenCycleMap', {
apikey: '',
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 22,
attribution: tf_attribute
});
var tf_transport = L.tileLayer.provider('Thunderforest.Transport', {
apikey: '',
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 22,
attribution: tf_attribute
});
var tf_transportdark = L.tileLayer.provider('Thunderforest.TransportDark', {
apikey: '',
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 22,
attribution: tf_attribute
});
var tf_landscape = L.tileLayer.provider('Thunderforest.Landscape', {
apikey: '',
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 22,
attribution: tf_attribute
});
var tf_outdoors = L.tileLayer.provider('Thunderforest.Outdoors', {
apikey: '',
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 22,
attribution: tf_attribute
});
var tf_pioneer = L.tileLayer.provider('Thunderforest.Pioneer', {
apikey: '',
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 22,
attribution: tf_attribute
});
var tf_mobileatlas = L.tileLayer.provider('Thunderforest.MobileAtlas', {
apikey: '',
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 22,
attribution: tf_attribute
});
var tf_neighbourhood = L.tileLayer.provider('Thunderforest.Neighbourhood', {
apikey: '',
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
maxZoom: 22,
attribution: tf_attribute
}); ";
}
function leaflet_mapbox_basemaps() // Mapbox basemaps.
{
global $map_layer_cache, $map_retina, $map_mapboxid, $map_mapboxtoken, $map_mapboxattribution;
return "
var mapbox = L.tileLayer.provider('MapBox', {
id: '',
accessToken: '',
useCache: '" . ( $map_layer_cache ? "true" : "false" ) . "',
detectRetina: '" . ( $map_retina ? "true" : "false" ) . "',
attribution: ''
}); ";
}
/**
* Generates Mapbox basemaps for use in a Leaflet map.
*
* This function initializes a Mapbox tile layer, requiring an API ID and access token.
* It also allows configuration for caching, retina display support, and attribution.
*
* @return string JavaScript code that initializes the Mapbox basemap for Leaflet.
*/
function leaflet_map_zoom($map_zoom)
{
global $resource, $geolocation_default_bounds;
// If no zoom level is set or is non-numeric, define as 0 to enable automatic zoom assignment below.
$zoom = trim((string) $map_zoom);
if (!is_int_loose($zoom)) {
$zoom = 2;
$arr_bounds = explode(",", $geolocation_default_bounds);
if (count($arr_bounds) == 3) {
$zoom = end($arr_bounds);
}
}
if (!($zoom >= 2 && $zoom <= 21)) {
$zoom = 16;
$siglon = round(100000 * abs($resource['geo_long'])) % 100000;
$siglat = round(100000 * abs($resource['geo_lat'])) % 100000;
if ($siglon % 100000 == 0 && $siglat % 100000 == 0) {
$zoom = 3;
} elseif ($siglon % 10000 == 0 && $siglat % 10000 == 0) {
$zoom = 6;
} elseif ($siglon % 1000 == 0 && $siglat % 1000 == 0) {
$zoom = 10;
} elseif ($siglon % 100 == 0 && $siglat % 100 == 0) {
$zoom = 15;
}
}
return $zoom;
}
/**
* Parses the resource polygon string to extract latitude and longitude bounds and formats the polygon string.
*
* @param array $fields The resource fields array containing polygon data.
* @param bool $minmax Flag to determine if minimum and maximum latitude and longitude values should be calculated.
* @return array An associative array containing the minimum and maximum latitude and longitude values,
* as well as the formatted polygon string for Leaflet display.
*/
function leaflet_polygon_parsing($fields, $minmax = true)
{
global $map_polygon_field;
// Search resource $fields array for the $map_polygon_field.
$key1 = array_search($map_polygon_field, array_column($fields, 'ref'));
if ($minmax) {
// Strip coordinate pair parathenses from polygon array.
$values = str_replace(')', '', str_replace('(', '', explode(',', $fields[$key1]['value'])));
// Determine minimum and maximum latitude values.
$lat_values = array($values[0], $values[2], $values[4], $values[6]);
$polygon['lat_min'] = min($lat_values);
$polygon['lat_max'] = max($lat_values);
// Determine minimum and maximum longitude values.
$long_values = array($values[1], $values[3], $values[5], $values[7]);
$polygon['long_min'] = min($long_values);
$polygon['long_max'] = max($long_values);
}
// Format polygon string for Leaflet footprint display below.
$polygon1 = str_replace('(', '[', $fields[$key1]['value']);
$polygon1 = str_replace(')', ']', $polygon1);
$polygon['values'] = '[' . $polygon1 . ']';
return $polygon;
}
/**
* Validates geolocation coordinates to ensure they are numeric and within acceptable bounds.
*
* @param mixed $coordinate The coordinate value to check.
* @param string $type The type of coordinate ('latitude' or 'longitude').
* @return bool True if the coordinate is valid; otherwise, false.
*/
function leaflet_coordinate_check($coordinate, $type)
{
$check = false;
if (!is_numeric($coordinate)) {
return false;
}
if ($type == 'latitude' && $coordinate >= -20037508.34 && $coordinate <= 20037508.34) {
$check = true;
}
if ($type == 'longitude' && $coordinate >= -20037508.34 && $coordinate <= 20037508.34) {
$check = true;
}
return $check;
}
/**
* Creates a map color markers legend for Leaflet maps.
*
* This function generates HTML for a legend that displays markers with colors corresponding to resource types
* or custom metadata field values. If a custom metadata field is defined, the legend reflects that; otherwise,
* it shows the default resource types and their associated colors.
*
* @return void Outputs the HTML for the legend.
*/
function leaflet_markers_legend()
{
global $lang, $marker_metadata_field, $marker_metadata_array, $MARKER_COLORS;
if (!isset($marker_metadata_field) || $lang['custom_metadata_markers'] == '') { ?>
0) ? (int)$restype["colour"] : ($restype['ref'] % count($MARKER_COLORS));
echo "
" . $restype["name"] . " ";
}
} else // Custom metadata field color markers legend.
{ ?>