'use strict'; // Create a map var options = {}; if (start_lat || start_lng) { start_lat = start_lat.toFixed(5); start_lng = start_lng.toFixed(5); options = { center: [start_lat, start_lng], zoom: zoom, }; history.replaceState(null, null, `/map/${zoom}/${start_lat}/${start_lng}`); } var map = L.map("map", options); var group = L.featureGroup(); var wikidata_items = {}; var osm_objects = {}; var wikidata_loaded = false; var osm_loaded = false; var loading = document.getElementById("loading"); var load_text = document.getElementById("load-text"); var isa_card = document.getElementById("isa-card"); var isa_labels = {}; var connections = {}; map.addLayer(group); map.zoomControl.setPosition('topright'); var blueMarker = L.ExtraMarkers.icon({ icon: 'fa-wikidata', markerColor: 'blue', shape: 'circle', prefix: 'fa', }); var greenMarker = L.ExtraMarkers.icon({ icon: 'fa-wikidata', markerColor: 'green', shape: 'circle', prefix: 'fa', }); var redMarker = L.ExtraMarkers.icon({ icon: 'fa-wikidata', markerColor: 'red', shape: 'circle', prefix: 'fa', }); var osmYellowMarker = L.ExtraMarkers.icon({ icon: 'fa-map', markerColor: 'yellow', shape: 'square', prefix: 'fa', }); var osmOrangeMarker = L.ExtraMarkers.icon({ icon: 'fa-map', markerColor: 'orange', shape: 'square', prefix: 'fa', }); if (!start_lat || !start_lng) { map.fitBounds([[49.85,-10.5], [58.75, 1.9]]); } // Add OpenStreetMap layer var osm = L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", { maxZoom: 19 }); osm.addTo(map); function check_items(items) { if (items.length === 0) return; var item = items.shift(); var qid = item.qid; var markers_url = `/api/1/item/${qid}`; axios.get(markers_url).then(response => { // console.log(response.data, item); response.data.info.forEach(osm => { var icon = osmYellowMarker; var marker = L.marker(osm.centroid, {title: osm.name, icon: icon}); var popup = `
${osm.identifier}: ${osm.name}
` marker.bindPopup(popup); marker.addTo(group); }); item.markers.forEach(marker_data => { var marker = marker_data.marker; var icon = response.data.tagged ? greenMarker : redMarker; marker.setIcon(icon); response.data.info.forEach(osm => { var path = [osm.centroid, marker_data]; var polyline = L.polyline(path, {color: 'green'}).addTo(map); }); }); if (items.length) check_items(items); }); } function update_wikidata() { if (Object.keys(wikidata_items).length === 0 || Object.keys(osm_objects).length === 0) { if (wikidata_loaded && osm_loaded) { loading.classList.add("visually-hidden"); load_text.classList.remove("visually-hidden"); } return; } for (const qid in osm_objects) { var osm_list = osm_objects[qid]; var item = wikidata_items[qid]; if (!item) { osm_list.forEach(osm => { osm.marker.setIcon(osmOrangeMarker); }); continue; } if (item.lines === undefined) item.lines = []; item.markers.forEach(marker_data => { marker_data.marker.setIcon(greenMarker); osm_list.forEach(osm => { var path = [osm.centroid, marker_data]; var polyline = L.polyline(path, {color: 'green'}).addTo(group); item.lines.push(polyline); }); }); } for (const qid in wikidata_items) { if (osm_objects[qid]) continue; var item = wikidata_items[qid]; item.markers.forEach(marker_data => { marker_data.marker.setIcon(redMarker); }); } loading.classList.add("visually-hidden"); load_text.classList.remove("visually-hidden"); } function isa_only(e) { e.preventDefault(); var this_id = e.target.parentNode.childNodes[0].id; var checkbox_list = document.getElementsByClassName('isa-checkbox'); for (const checkbox of checkbox_list) { checkbox.checked = checkbox.id == this_id; } checkbox_change(); } function show_all_isa(e) { e.preventDefault(); var checkbox_list = document.getElementsByClassName('isa-checkbox'); for (const checkbox of checkbox_list) { checkbox.checked = true; } checkbox_change(); } function checkbox_change() { var checkbox_list = document.getElementsByClassName('isa-checkbox'); var ticked = []; for (const checkbox of checkbox_list) { if (checkbox.checked) { ticked.push(checkbox.id.substr(4)); } } for (const qid in wikidata_items) { var item = wikidata_items[qid]; const item_isa_list = wikidata_items[qid]['isa_list']; const intersection = ticked.filter(isa_qid => item_isa_list.includes(isa_qid)); if (item.lines) { item.lines.forEach(line => { if (intersection.length) { line.addTo(group); } else { line.removeFrom(group); } }); } item.markers.forEach(marker_data => { var marker = marker_data.marker; if (intersection.length) { marker.addTo(group); } else { marker.removeFrom(group); } }); if(osm_objects[qid]) { osm_objects[qid].forEach(osm => { if (intersection.length) { osm.marker.addTo(group); } else { osm.marker.removeFrom(group); } }); } } } function set_isa_list(isa_count) { isa_card.classList.remove("visually-hidden"); var isa_list = document.getElementById("isa-list"); isa_list.innerHTML = ''; isa_count.forEach(isa => { isa_labels[isa.qid] = isa.label; var isa_id = `isa-${isa.qid}`; var e = document.createElement('div'); e.setAttribute('class', 'isa-item'); var checkbox = document.createElement('input'); checkbox.setAttribute('type', 'checkbox'); checkbox.setAttribute('checked', 'checked'); checkbox.setAttribute('id', isa_id); checkbox.setAttribute('class', 'isa-checkbox'); checkbox.onchange = checkbox_change; e.appendChild(checkbox); e.appendChild(document.createTextNode(' ')); var label = document.createElement('label'); label.setAttribute('for', isa_id); var label_text = document.createTextNode(` ${isa.label} (${isa.qid}): ${isa.count} `); label.appendChild(label_text); e.appendChild(label); e.appendChild(document.createTextNode(' ')); var only = document.createElement('a'); only.setAttribute('href', '#'); var only_text = document.createTextNode('only'); only.appendChild(only_text); only.onclick = isa_only; e.appendChild(only); isa_list.appendChild(e); }); } function load_wikidata_items() { var checkbox_list = document.getElementsByClassName('isa-checkbox'); for (const checkbox of checkbox_list) checkbox.checked = true; checkbox_change(); loading.classList.remove("visually-hidden"); load_text.classList.add("visually-hidden"); var bounds = map.getBounds(); console.log("map moved", bounds.toBBoxString()); // var items_to_check = []; var params = {bounds: bounds.toBBoxString()}; var items_url = "/api/1/items"; axios.get(items_url, {params: params}).then(response => { set_isa_list(response.data.isa_count); var items = response.data.items; items.forEach(item => { if (item.qid in wikidata_items) return; item.markers.forEach(marker_data => { // var icon = marker.tagged ? greenMarker : blueMarker; var icon = blueMarker; var label = `${item.label} (${item.qid})` var marker = L.marker(marker_data, {title: label, icon: icon}); var wd_url = 'https://www.wikidata.org/wiki/' + item.qid; var popup = 'Wikidata item
'
popup += `${item.label} (${item.qid})`
if (item.description) {
popup += `
description: ${item.description}`
}
if (item.isa_list && item.isa_list.length) {
popup += '
item type'
for (const [index, isa_qid] of item.isa_list.entries()) {
var isa_url = 'https://www.wikidata.org/wiki/' + isa_qid;
var isa_label = isa_labels[isa_qid];
popup += `
${isa_label} (${isa_qid})`;
}
}
if (item.image_list && item.image_list.length) {
popup += `
`;
}
popup += '
${osm.name}:
${osm.identifier}
Wikidata tag: ${qid}