Skip to content

Commit

Permalink
Issue-61: Let's travel around the globe (#62)
Browse files Browse the repository at this point in the history
GEOJSON and Leaflet + SBF. All working together and flying in tourist class
  • Loading branch information
DiegoPino authored Feb 28, 2020
1 parent 7520388 commit 2a70b52
Show file tree
Hide file tree
Showing 4 changed files with 1,016 additions and 1 deletion.
66 changes: 65 additions & 1 deletion config/schema/format_strawberryfield.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ field.formatter.settings.strawberry_pdf_formatter:
label: 'First Page to display per PDF'
field.formatter.settings.strawberry_mirador_formatter:
type: mapping
label: 'Specific Config for strawberry_pdf_formatter'
label: 'Specific Config for strawberry_mirador_formatter'
mapping:
iiif_base_url:
type: string
Expand Down Expand Up @@ -343,6 +343,70 @@ format_strawberryfield.viewmodemapping_settings.mapping:
type: integer
label: 'Order in which this is evaluated'

field.formatter.settings.strawberry_map_formatter:
type: mapping
label: 'Specific Config for strawberry_map_formatter'
mapping:
json_key_source:
type: string
label: 'JSON or JMESPATH that conditions rendering. If empty will always render'
iiif_base_url:
type: string
label: 'Custom Public IIIF Server URL'
iiif_base_url_internal:
type: string
label: 'Custom internal IIIF Server URL'
metadataexposeentity:
type: string
label: 'Machine name of the exposed Metadata Config Entity endpoint'
mediasource:
type: mapping
label: 'Sources for GeoJSON URL'
mapping:
geojsonnodelist:
type: string
label: 'If geojsonnodelist is being used'
metadataexposeentity:
type: string
label: 'If metadataexposeentity is being used'
geojsonurl:
type: string
label: 'If geojsonurl is being used'
main_mediasource:
type: string
label: 'Primary GeoJSON Source used'
metadataexposeentity_source:
type: string
label: 'metadataexpose_entity machine name'
geojsonnodelist_json_key_source:
type: string
label: 'Strawberryfield/JSON key containing NODE ids or UUIDs from which to generate GeoJSON URLs'
geojsonurl_json_key_source:
type: string
label: 'Strawberryfield/JSON key containing GeoJSON URLs to display'
max_width:
type: string
label: 'Max with for the Map. 0 to force 100% width'
max_height:
type: string
initial_zoom:
type: integer
label: 'Initial Zoom for the Map'
max_zoom:
type: integer
label: 'Max Zoom for the Map'
min_zoom:
type: integer
label: 'Min Zoom for the Map'
tilemap_url:
type: string
label: 'Tile Map URL'
tilemap_attribution:
type: string
label: 'Tile Map Attribution String'
use_iiif_globals:
type: string
label: 'Whether to use global IIIF settings or not.'
# Given any DS field plugin formatter key a type
ds.field_plugin.*:
type: mapping
Expand Down
33 changes: 33 additions & 0 deletions format_strawberryfield.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,36 @@ mirador_strawberry:
- core/drupalSettings
- format_strawberryfield/mirador_projectmirador
- format_strawberryfield/mirador_font

leaflet_core:
version: 1.6.0
license:
name: BSD-2-Clause
url: https://cdn.jsdelivr.net/npm/[email protected]/LICENSE
gpl-compatible: true
js:
https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet-src.min.js: { external: true, minified: true, preprocess: false}
css:
component:
https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.css: { external: true}

leaflet_ajax:
version: 2.1.0
license:
name: MIT
url: https://cdn.jsdelivr.net/npm/[email protected]/license.md
gpl-compatible: true
js:
https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.ajax.min.js: { external: true, minified: true, preprocess: false}
dependencies:
- format_strawberryfield/leaflet_core

leaflet_strawberry:
version: 1.0
js:
js/leaflet_strawberry.js: {minified: false}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
- format_strawberryfield/leaflet_ajax
115 changes: 115 additions & 0 deletions js/leaflet_strawberry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
(function ($, Drupal, drupalSettings, L) {

'use strict';

Drupal.behaviors.format_strawberryfield_leaflet_initiate = {
attach: function(context, settings) {
$('.strawberry-leaflet-item[data-iiif-infojson]').once('attache_leaflet')
.each(function (index, value) {

var $featurecount = 0;

function popUpFeature(feature, layer){
var popupText = feature.properties.name +"<br>";
layer.bindPopup(popupText);
}
var markerArray = [];

function onEachFeature(feature, layer) {
popUpFeature(feature, layer);
}
// Get the node uuid for this element
var element_id = $(this).attr("id");
// Check if we got some data passed via Drupal settings.
if (typeof(drupalSettings.format_strawberryfield.leaflet[element_id]) != 'undefined') {

$(this).height(drupalSettings.format_strawberryfield.leaflet[element_id]['height']);
$(this).width(drupalSettings.format_strawberryfield.leaflet[element_id]['width']);
// Defines our basic options for leaflet GEOJSON

var $initialzoom = 5;

if (drupalSettings.format_strawberryfield.leaflet[element_id]['initialzoom'] || drupalSettings.format_strawberryfield.leaflet[element_id]['initialzoom'] === 0) {
$initialzoom = drupalSettings.format_strawberryfield.leaflet[element_id]['initialzoom'];
}
// initialize the map
var map = L.map(element_id).setView([40.1, -100], $initialzoom);
// Use current's user lat/long
// Does not work without HTTPS
// map.locate({setView: true, maxZoom: 8});

var geojsonLayer = L.geoJson.ajax(drupalSettings.format_strawberryfield.leaflet[element_id]['geojsonurl'],{
onEachFeature: onEachFeature,
pointToLayer: function (feature, latlng) {
markerArray.push(L.marker (latlng));
return L.marker (latlng);
},
});
// The tilemap url in /{z}/{x}/{y}.png format. Can have a key after a ? if provided by the user.
// Defaults, should never be needed, in case wants to get around of restricted forms?
// See https://operations.osmfoundation.org/policies/tiles/ and consider contributing if you
// are reading this.

var $tilemap = {
url:'https://a.tile.openstreetmap.org/{z}/{x}/{y}.png',
attribution: '&copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}
var $minzoom = 0;
var $maxzoom = 10;

if (drupalSettings.format_strawberryfield.leaflet[element_id]['tilemap_url']) {
$tilemap.url = drupalSettings.format_strawberryfield.leaflet[element_id]['tilemap_url'];
$tilemap.attribution = drupalSettings.format_strawberryfield.leaflet[element_id]['tilemap_attribution'];
}

if (drupalSettings.format_strawberryfield.leaflet[element_id]['minzoom'] || drupalSettings.format_strawberryfield.leaflet[element_id]['minzoom'] === 0) {
$minzoom = drupalSettings.format_strawberryfield.leaflet[element_id]['minzoom'];
}
if (drupalSettings.format_strawberryfield.leaflet[element_id]['maxzoom'] || drupalSettings.format_strawberryfield.leaflet[element_id]['maxzoom'] === 0) {
$maxzoom = drupalSettings.format_strawberryfield.leaflet[element_id]['maxzoom'];
}

// load a tile layer
L.tileLayer($tilemap.url,
{
attribution: $tilemap.attribution,
maxZoom: $maxzoom,
minZoom: $minzoom
}).addTo(map);

map.on('layeradd', function (e) {
if (markerArray.length > 0) {
var geojsongroup = new L.featureGroup(markerArray);
if (markerArray.length == 1) {
map.setView(geojsongroup.getBounds().getCenter(), $initialzoom);
}
else {
map.fitBounds(geojsongroup.getBounds());
}

}
});

var $firstgeojson = [drupalSettings.format_strawberryfield.leaflet[element_id]['geojsonurl']];
var $allgeojsons = $firstgeojson.concat(drupalSettings.format_strawberryfield.leaflet[element_id]['geojsonother']);
var $secondgeojson = drupalSettings.format_strawberryfield.leaflet[element_id]['geojsonother'].find(x=>x!==undefined);

if (Array.isArray($allgeojsons) && $allgeojsons.length && typeof($secondgeojson) != 'undefined') {

$allgeojsons.forEach(geojsonURL => {
// TODO Provider, rights, etc should be passed by metadata at
// \Drupal\format_strawberryfield\Plugin\Field\FieldFormatter\StrawberryMapFormatter
// Deal with this for Beta3
// Not a big issue if GeoJSON has that data. We can iterate over all Feature keys
// And print them on the overlay?
geojsonLayer.addUrl("geojsonURL");//we now have 2 layers
})
}
//@TODO add an extra geojsons key with every other one so people can select the others.
// load a tile layer
geojsonLayer.addTo(map);

console.log('initializing leaflet 1.6.0')
}
})}}
})(jQuery, Drupal, drupalSettings, window.L);
Loading

0 comments on commit 2a70b52

Please sign in to comment.