2021-05-09 11:18:56 +01:00
|
|
|
"use strict";
|
2021-05-07 16:46:47 +01:00
|
|
|
|
|
|
|
var options = {};
|
|
|
|
if (start_lat || start_lng) {
|
2021-05-09 11:18:56 +01:00
|
|
|
start_lat = start_lat.toFixed(5);
|
|
|
|
start_lng = start_lng.toFixed(5);
|
2021-05-07 16:46:47 +01:00
|
|
|
options = {
|
|
|
|
center: [start_lat, start_lng],
|
|
|
|
zoom: zoom,
|
|
|
|
};
|
|
|
|
history.replaceState(null, null, `/map/${zoom}/${start_lat}/${start_lng}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
var map = L.map("map", options);
|
2021-05-09 07:18:20 +01:00
|
|
|
var items = {};
|
2021-05-07 16:46:47 +01:00
|
|
|
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");
|
2021-05-09 18:33:02 +01:00
|
|
|
var checkbox_list = document.getElementsByClassName("isa-checkbox");
|
2021-05-07 16:46:47 +01:00
|
|
|
var isa_labels = {};
|
2021-05-09 21:59:24 +01:00
|
|
|
|
2021-05-09 11:18:56 +01:00
|
|
|
map.zoomControl.setPosition("topright");
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-05-09 18:33:02 +01:00
|
|
|
map.on("moveend", function (e) {
|
|
|
|
var zoom = map.getZoom();
|
|
|
|
var c = map.getCenter();
|
|
|
|
var lat = c.lat.toFixed(5);
|
|
|
|
var lng = c.lng.toFixed(5);
|
|
|
|
history.replaceState(null, null, `/map/${zoom}/${lat}/${lng}`);
|
|
|
|
});
|
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
var blueMarker = L.ExtraMarkers.icon({
|
2021-05-09 11:18:56 +01:00
|
|
|
icon: "fa-wikidata",
|
|
|
|
markerColor: "blue",
|
|
|
|
shape: "circle",
|
|
|
|
prefix: "fa",
|
2021-05-07 16:46:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
var greenMarker = L.ExtraMarkers.icon({
|
2021-05-09 11:18:56 +01:00
|
|
|
icon: "fa-wikidata",
|
|
|
|
markerColor: "green",
|
|
|
|
shape: "circle",
|
|
|
|
prefix: "fa",
|
2021-05-07 16:46:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
var redMarker = L.ExtraMarkers.icon({
|
2021-05-09 11:18:56 +01:00
|
|
|
icon: "fa-wikidata",
|
|
|
|
markerColor: "red",
|
|
|
|
shape: "circle",
|
|
|
|
prefix: "fa",
|
2021-05-07 16:46:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
var osmYellowMarker = L.ExtraMarkers.icon({
|
2021-05-09 11:18:56 +01:00
|
|
|
icon: "fa-map",
|
|
|
|
markerColor: "yellow",
|
|
|
|
shape: "square",
|
|
|
|
prefix: "fa",
|
2021-05-07 16:46:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
var osmOrangeMarker = L.ExtraMarkers.icon({
|
2021-05-09 11:18:56 +01:00
|
|
|
icon: "fa-map",
|
|
|
|
markerColor: "orange",
|
|
|
|
shape: "square",
|
|
|
|
prefix: "fa",
|
2021-05-07 16:46:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!start_lat || !start_lng) {
|
2021-05-09 11:18:56 +01:00
|
|
|
map.fitBounds([
|
|
|
|
[49.85, -10.5],
|
|
|
|
[58.75, 1.9],
|
|
|
|
]);
|
2021-05-07 16:46:47 +01:00
|
|
|
}
|
|
|
|
|
2021-05-09 11:18:56 +01:00
|
|
|
var osm = L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
|
|
|
|
maxZoom: 19,
|
|
|
|
});
|
2021-05-07 16:46:47 +01:00
|
|
|
|
|
|
|
osm.addTo(map);
|
|
|
|
|
2021-05-09 18:33:02 +01:00
|
|
|
function load_complete() {
|
|
|
|
loading.classList.add("visually-hidden");
|
|
|
|
load_text.classList.remove("visually-hidden");
|
|
|
|
}
|
|
|
|
|
2021-05-09 21:59:24 +01:00
|
|
|
function add_to_feature_group(qid, thing) {
|
|
|
|
if (items[qid] === undefined) items[qid] = {};
|
|
|
|
if (items[qid].group === undefined) items[qid].group = L.featureGroup();
|
|
|
|
|
|
|
|
var group = items[qid].group;
|
|
|
|
thing.addTo(group);
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
function update_wikidata() {
|
2021-05-09 11:18:56 +01:00
|
|
|
if (
|
|
|
|
Object.keys(wikidata_items).length === 0 ||
|
|
|
|
Object.keys(osm_objects).length === 0
|
|
|
|
) {
|
2021-05-09 18:33:02 +01:00
|
|
|
if (wikidata_loaded && osm_loaded) load_complete();
|
2021-05-07 16:46:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const qid in osm_objects) {
|
2021-05-09 11:18:56 +01:00
|
|
|
var osm_list = osm_objects[qid];
|
|
|
|
|
|
|
|
var item = wikidata_items[qid];
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-05-09 22:41:39 +01:00
|
|
|
osm_list.forEach((osm) => {
|
|
|
|
osm.marker.setIcon(item ? osmYellowMarker : osmOrangeMarker);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!item) continue;
|
|
|
|
|
2021-05-09 11:18:56 +01:00
|
|
|
item.markers.forEach((marker_data) => {
|
|
|
|
marker_data.marker.setIcon(greenMarker);
|
|
|
|
osm_list.forEach((osm) => {
|
|
|
|
var path = [osm.centroid, marker_data];
|
2021-05-09 21:59:24 +01:00
|
|
|
var polyline = L.polyline(path, { color: "green" });
|
|
|
|
add_to_feature_group(qid, polyline);
|
2021-05-07 16:46:47 +01:00
|
|
|
});
|
2021-05-09 11:18:56 +01:00
|
|
|
});
|
2021-05-07 16:46:47 +01:00
|
|
|
}
|
|
|
|
for (const qid in wikidata_items) {
|
2021-05-09 11:18:56 +01:00
|
|
|
if (osm_objects[qid]) continue;
|
|
|
|
var item = wikidata_items[qid];
|
|
|
|
item.markers.forEach((marker_data) => {
|
|
|
|
marker_data.marker.setIcon(redMarker);
|
|
|
|
});
|
2021-05-07 16:46:47 +01:00
|
|
|
}
|
|
|
|
|
2021-05-09 18:33:02 +01:00
|
|
|
load_complete();
|
2021-05-07 16:46:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isa_only(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var this_id = e.target.parentNode.childNodes[0].id;
|
|
|
|
|
|
|
|
for (const checkbox of checkbox_list) {
|
|
|
|
checkbox.checked = checkbox.id == this_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkbox_change();
|
|
|
|
}
|
|
|
|
|
2021-05-08 09:45:14 +01:00
|
|
|
function show_all_isa(e) {
|
|
|
|
e.preventDefault();
|
2021-05-09 18:33:02 +01:00
|
|
|
|
2021-05-08 09:45:14 +01:00
|
|
|
for (const checkbox of checkbox_list) {
|
|
|
|
checkbox.checked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkbox_change();
|
|
|
|
}
|
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
function checkbox_change() {
|
|
|
|
var ticked = [];
|
|
|
|
for (const checkbox of checkbox_list) {
|
2021-05-09 18:33:02 +01:00
|
|
|
if (checkbox.checked) ticked.push(checkbox.id.substr(4));
|
2021-05-07 16:46:47 +01:00
|
|
|
}
|
|
|
|
|
2021-05-09 21:59:24 +01:00
|
|
|
for (const qid in items) {
|
|
|
|
var item = items[qid];
|
|
|
|
|
|
|
|
if (item.isa_list === undefined) continue;
|
|
|
|
const item_isa_list = item.isa_list;
|
2021-05-09 11:18:56 +01:00
|
|
|
const intersection = ticked.filter((isa_qid) =>
|
|
|
|
item_isa_list.includes(isa_qid)
|
|
|
|
);
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-05-09 21:59:24 +01:00
|
|
|
if (intersection.length) {
|
|
|
|
item.group.addTo(map);
|
|
|
|
} else {
|
|
|
|
item.group.removeFrom(map);
|
2021-05-09 07:18:20 +01:00
|
|
|
}
|
2021-05-07 16:46:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_isa_list(isa_count) {
|
|
|
|
isa_card.classList.remove("visually-hidden");
|
|
|
|
var isa_list = document.getElementById("isa-list");
|
2021-05-09 11:18:56 +01:00
|
|
|
isa_list.innerHTML = "";
|
|
|
|
isa_count.forEach((isa) => {
|
2021-05-07 16:46:47 +01:00
|
|
|
isa_labels[isa.qid] = isa.label;
|
|
|
|
var isa_id = `isa-${isa.qid}`;
|
2021-05-09 11:18:56 +01:00
|
|
|
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");
|
2021-05-07 16:46:47 +01:00
|
|
|
checkbox.onchange = checkbox_change;
|
|
|
|
e.appendChild(checkbox);
|
|
|
|
|
2021-05-09 11:18:56 +01:00
|
|
|
e.appendChild(document.createTextNode(" "));
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-05-09 11:18:56 +01:00
|
|
|
var label = document.createElement("label");
|
|
|
|
label.setAttribute("for", isa_id);
|
|
|
|
var label_text = document.createTextNode(
|
|
|
|
` ${isa.label} (${isa.qid}): ${isa.count} `
|
|
|
|
);
|
2021-05-07 16:46:47 +01:00
|
|
|
label.appendChild(label_text);
|
|
|
|
|
|
|
|
e.appendChild(label);
|
2021-05-09 11:18:56 +01:00
|
|
|
e.appendChild(document.createTextNode(" "));
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-05-09 11:18:56 +01:00
|
|
|
var only = document.createElement("a");
|
|
|
|
only.setAttribute("href", "#");
|
|
|
|
var only_text = document.createTextNode("only");
|
2021-05-07 16:46:47 +01:00
|
|
|
only.appendChild(only_text);
|
|
|
|
only.onclick = isa_only;
|
|
|
|
e.appendChild(only);
|
|
|
|
|
|
|
|
isa_list.appendChild(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-09 07:18:20 +01:00
|
|
|
function add_wikidata_marker(item, marker_data) {
|
2021-05-09 11:18:56 +01:00
|
|
|
var icon = blueMarker;
|
2021-05-09 21:59:24 +01:00
|
|
|
var qid = item.qid;
|
2021-05-09 11:18:56 +01:00
|
|
|
var label = `${item.label} (${item.qid})`;
|
|
|
|
var marker = L.marker(marker_data, { icon: icon });
|
|
|
|
// var tooltip = marker.bindTooltip(item.qid, {permanent: true, direction: 'bottom', opacity: 0.5});
|
|
|
|
var wd_url = "https://www.wikidata.org/wiki/" + item.qid;
|
|
|
|
var popup = "<p><strong>Wikidata item</strong><br>";
|
|
|
|
popup += `<a href="${wd_url}" target="_blank">${item.label}</a> (${item.qid})`;
|
|
|
|
if (item.description) {
|
|
|
|
popup += `<br>description: ${item.description}`;
|
|
|
|
}
|
|
|
|
if (item.isa_list && item.isa_list.length) {
|
|
|
|
popup += "<br><strong>item type</strong>";
|
|
|
|
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 += `<br><a href="${isa_url}" target="_blank">${isa_label}</a> (${isa_qid})`;
|
2021-05-09 07:18:20 +01:00
|
|
|
}
|
2021-05-09 11:18:56 +01:00
|
|
|
}
|
|
|
|
if (item.image_list && item.image_list.length) {
|
|
|
|
popup += `<br><img src="/commons/${item.image_list[0]}">`;
|
|
|
|
}
|
|
|
|
popup += "</p>";
|
|
|
|
marker.bindPopup(popup);
|
2021-05-09 21:59:24 +01:00
|
|
|
var group = add_to_feature_group(item.qid, marker);
|
|
|
|
group.addTo(map);
|
2021-05-09 11:18:56 +01:00
|
|
|
marker_data.marker = marker;
|
2021-05-09 07:18:20 +01:00
|
|
|
}
|
|
|
|
|
2021-05-07 16:46:47 +01:00
|
|
|
function load_wikidata_items() {
|
2021-05-09 11:18:56 +01:00
|
|
|
var checkbox_list = document.getElementsByClassName("isa-checkbox");
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-05-09 11:18:56 +01:00
|
|
|
for (const checkbox of checkbox_list) checkbox.checked = true;
|
2021-05-07 16:46:47 +01:00
|
|
|
|
|
|
|
checkbox_change();
|
|
|
|
|
|
|
|
loading.classList.remove("visually-hidden");
|
|
|
|
load_text.classList.add("visually-hidden");
|
|
|
|
|
|
|
|
var bounds = map.getBounds();
|
|
|
|
console.log("map moved", bounds.toBBoxString());
|
|
|
|
|
2021-05-09 11:18:56 +01:00
|
|
|
var params = { bounds: bounds.toBBoxString() };
|
2021-05-07 16:46:47 +01:00
|
|
|
var items_url = "/api/1/items";
|
|
|
|
|
2021-05-09 11:18:56 +01:00
|
|
|
axios.get(items_url, { params: params }).then((response) => {
|
2021-05-07 16:46:47 +01:00
|
|
|
set_isa_list(response.data.isa_count);
|
2021-05-09 21:59:24 +01:00
|
|
|
var load_items = response.data.items;
|
|
|
|
load_items.forEach((item) => {
|
|
|
|
var qid = item.qid;
|
2021-05-09 11:18:56 +01:00
|
|
|
if (item.qid in wikidata_items) return;
|
|
|
|
item.markers.forEach((marker_data) =>
|
|
|
|
add_wikidata_marker(item, marker_data)
|
|
|
|
);
|
|
|
|
wikidata_items[item.qid] = item;
|
2021-05-09 21:59:24 +01:00
|
|
|
|
|
|
|
if (items[qid] === undefined) items[qid] = {};
|
|
|
|
items[qid].isa_list = item.isa_list;
|
2021-05-07 16:46:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
wikidata_loaded = true;
|
|
|
|
isa_card.classList.remove("visually-hidden");
|
|
|
|
update_wikidata();
|
|
|
|
});
|
|
|
|
var osm_objects_url = "/api/1/osm";
|
2021-05-09 11:18:56 +01:00
|
|
|
axios.get(osm_objects_url, { params: params }).then((response) => {
|
2021-05-07 16:46:47 +01:00
|
|
|
console.log(`${response.data.duration} seconds`);
|
2021-05-09 11:18:56 +01:00
|
|
|
response.data.objects.forEach((osm) => {
|
2021-05-07 16:46:47 +01:00
|
|
|
var qid = osm.wikidata;
|
2021-05-09 11:18:56 +01:00
|
|
|
if (osm_objects[qid] === undefined) osm_objects[qid] = [];
|
2021-05-07 16:46:47 +01:00
|
|
|
osm_objects[qid].push(osm);
|
|
|
|
|
|
|
|
var icon = osmYellowMarker;
|
2021-05-09 11:18:56 +01:00
|
|
|
var marker = L.marker(osm.centroid, { title: osm.name, icon: icon });
|
2021-05-07 16:46:47 +01:00
|
|
|
osm.marker = marker;
|
2021-05-09 11:18:56 +01:00
|
|
|
var wd_url = "https://www.wikidata.org/wiki/" + qid;
|
2021-05-07 16:46:47 +01:00
|
|
|
var popup = `
|
|
|
|
<p>
|
|
|
|
${osm.name}:
|
|
|
|
<a href="${osm.url}" target="_blank">${osm.identifier}</a>
|
|
|
|
<br>
|
2021-05-08 09:45:43 +01:00
|
|
|
Wikidata tag: <a href="${wd_url}" target="_blank">${qid}</a>
|
2021-05-09 11:18:56 +01:00
|
|
|
</p>`;
|
2021-05-07 16:46:47 +01:00
|
|
|
marker.bindPopup(popup);
|
2021-05-09 21:59:24 +01:00
|
|
|
var group = add_to_feature_group(qid, marker);
|
|
|
|
group.addTo(map);
|
2021-05-08 12:14:30 +01:00
|
|
|
|
2021-05-09 11:18:56 +01:00
|
|
|
if (osm.type != "node" && osm.geojson) {
|
|
|
|
var mapStyle = { fillOpacity: 0 };
|
|
|
|
var geojson = L.geoJSON(null, { style: mapStyle });
|
2021-05-08 12:14:30 +01:00
|
|
|
geojson.addData(osm.geojson);
|
2021-05-09 22:41:39 +01:00
|
|
|
add_to_feature_group(qid, geojson);
|
2021-05-08 12:14:30 +01:00
|
|
|
}
|
2021-05-07 16:46:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
osm_loaded = true;
|
|
|
|
update_wikidata();
|
|
|
|
});
|
2021-05-09 11:18:56 +01:00
|
|
|
}
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-05-09 18:33:02 +01:00
|
|
|
document.getElementById("search-form").onsubmit = function (e) {
|
2021-05-07 16:46:47 +01:00
|
|
|
e.preventDefault();
|
2021-05-09 11:18:56 +01:00
|
|
|
var search_text = document.getElementById("search-text").value.trim();
|
|
|
|
if (!search_text) return;
|
|
|
|
var params = { q: search_text };
|
2021-05-07 16:46:47 +01:00
|
|
|
var search_url = "/api/1/search";
|
2021-05-09 11:18:56 +01:00
|
|
|
var search_results = document.getElementById("search-results");
|
|
|
|
axios.get(search_url, { params: params }).then((response) => {
|
|
|
|
search_results.innerHTML = "";
|
|
|
|
response.data.hits.forEach((hit) => {
|
|
|
|
var e = document.createElement("div");
|
|
|
|
var category = document.createTextNode(hit.category + " ");
|
2021-05-07 16:46:47 +01:00
|
|
|
e.appendChild(category);
|
2021-05-09 11:18:56 +01:00
|
|
|
var a = document.createElement("a");
|
2021-05-07 16:46:47 +01:00
|
|
|
var lat = parseFloat(hit.lat).toFixed(5);
|
|
|
|
var lon = parseFloat(hit.lon).toFixed(5);
|
2021-05-09 11:18:56 +01:00
|
|
|
a.setAttribute("href", `/map/15/${lat}/${lon}`);
|
2021-05-07 16:46:47 +01:00
|
|
|
var link_text = document.createTextNode(hit.name);
|
|
|
|
a.appendChild(link_text);
|
|
|
|
e.appendChild(a);
|
|
|
|
search_results.appendChild(e);
|
|
|
|
});
|
|
|
|
});
|
2021-05-09 11:18:56 +01:00
|
|
|
};
|
2021-05-07 16:46:47 +01:00
|
|
|
|
2021-05-09 18:33:02 +01:00
|
|
|
document.getElementById("load-btn").onclick = load_wikidata_items;
|
|
|
|
document.getElementById("show-all-isa").onclick = show_all_isa;
|