-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
208 lines (168 loc) · 11.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html>
<head>
<title>Museums on Wikidata</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.1/leaflet.css" />
<link rel="stylesheet" href="style.css" />
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.1/leaflet.js"></script>
<script src="//code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/js-url/2.3.1/url.min.js"></script>
</head>
<body>
<div id="map"></div>
<div id="info" class="info leaflet-container leaflet-control leaflet-top leaflet-right"><h1>Museums in <span id="country">…</span></h1><p>Change country (on enter):</p><div id="bloodhound"><input class="typeahead" type="text" placeholder="change country here"></div><ul><li>Museums in Wikidata: <strong><span id="totalCount"></span></strong></li><li class="mini">Museums with coordinates: <strong><span id="coordinateCount"</span></strong> (<span id="coordinatePercentage"></span> %)</li><li class="midi">Museums with web site: <strong><span id="websiteCount"></span></strong> (<span id="websitePercentage"></span> %)</li><li class="maxi">Museums with visitor count: <strong><span id="visitorsCount"></span></strong> (<span id="visitorsPercentage"></span> %)</li><li><a href="" target="_blank" id="withoutCoordinatesLink">Museums without coordinates: <strong><span id="withoutCoordinatesCount">0</span></strong> (<span id="withoutCoordinatesPercentage"></span> %)</a></ul><p><a href="https://github.com/Ambrosiani/museums-wikidata" target="_blank">Source (Github)</a></p><p>By <a href="https://www.twitter.com/aronambrosiani/" target="_blank">Aron Ambrosiani</a> (with help)</p></div>
<script>
var map = L.map('map'); //create Leaflet map centered on Sweden
var country = "Sweden"; //default value = Sweden. This variable is changed by the querystring and by the form.
if($.url('?country')) {
country = $.url('?country');
}
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiYXJvbmFtYnJvc2lhbmkiLCJhIjoiWFNxbC1KUSJ9.tXE2c-tVR3TAKiFogBBOtQ', {
maxZoom: 18,
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
id: 'mapbox.streets'
}).addTo(map);
var markers = {};
markers[country] = L.layerGroup(); //markers[country] is later populated with markers representing museums in a specific country
var museumStats = { "totalCount" : 0, "coordinateCount" : 0, "websiteCount" : 0, "visitorsCount" : 0}; //these stats are used in the infobox
var color = ["red", "#fc0", "green"]; //color is used to display the number of properties with data
generateMarkers(); //generate map markers for the default country
//Typeahead + Bloodhound is an autocomplete library for the country input form
//Code derived from https://twitter.github.io/typeahead.js/examples/
var countrySearch = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: 'countriesArray.json'
});
$('#bloodhound .typeahead').typeahead({
hint: false,
highlight: false,
minLength: 1
}, {
name: 'countrySearch',
limit: 10,
source: countrySearch
});
$('#bloodhound .typeahead').keypress(function (e) {
var key = e.which;
if(key == 13) // the enter key code
{
changeCountry();
}
});
function changeCountry() {
//This function removes the markers on the map, then changes country and generates new markers
map.removeLayer(markers[country]);
museumStats = { "totalCount" : 0, "coordinateCount" : 0, "websiteCount" : 0, "visitorsCount" : 0};
country = $('#bloodhound .typeahead').val();
markers[country] = L.layerGroup();
generateMarkers();
}
function generateMarkers() {
//this function uses the Wikidata Query Service to retrieve a json with museum data
//example query (country=Sweden): http://tinyurl.com/hcz542r
//the first json call gets an array of countries to convert country name into country ID, the second json call uses that country ID to query the Wikidata Query API.
//countries.json is a cleaned-up version of the Wikidata query at http://tinyurl.com/zvwukes with Netherlands (Q55) added manually
$.getJSON('countries.json', function(countries) {
//extract country center coordinate from countries.json
var countryCoordText = countries[country][1].split('(');
var countryLatlngText = countryCoordText[1].split(' ');
var countryLat = countryLatlngText[0];
var countryLngText = countryLatlngText[1];
var countryLng = countryLngText.substring(0, countryLngText.length-1);
var countryLatlng = L.latLng(countryLat, countryLng); //this variable can be used by Leaflet
$.getJSON('https://query.wikidata.org/bigdata/namespace/wdq/sparql?query=PREFIX+wd%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%0D%0APREFIX+wdt%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E%0D%0APREFIX+wikibase%3A+%3Chttp%3A%2F%2Fwikiba.se%2Fontology%23%3E%0D%0APREFIX+p%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2F%3E%0D%0APREFIX+v%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fstatement%2F%3E%0D%0APREFIX+q%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fqualifier%2F%3E%0D%0APREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0APREFIX+schema%3A+%3Chttp%3A%2F%2Fschema.org%2F%3E%0D%0A%0D%0ASELECT+%3Fmuseum+%3FmuseumLabel+%3FtypLabel+%3Fcoord+%3Fweb+%3Fvisitors%0D%0AWHERE+%7B%0D%0A++%3Fmuseum+wdt%3AP17+wd%3AQ' + countries[country][0].split('Q')[1] + '+.%0D%0A++%3Fmuseum+wdt%3AP31+%3Ftyp+.%0D%0A++%3Ftyp+(wdt%3AP279)*+wd%3AQ33506+.%0D%0A++OPTIONAL+%7B%0D%0A+++++%3Fmuseum+wdt%3AP625+%3Fcoord+.%0D%0A++%7D%0D%0A++OPTIONAL+%7B%0D%0A+++++%3Fmuseum+wdt%3AP856+%3Fweb+.%0D%0A++%7D%0D%0A++OPTIONAL+%7B%0D%0A+++++%3Fmuseum+wdt%3AP1174+%3Fvisitors+.+%0D%0A++%7D%0D%0A++SERVICE+wikibase%3Alabel+%7B%0D%0A++++bd%3AserviceParam+wikibase%3Alanguage+%22sv%2Cit%2Cen%22+.%0D%0A++%7D%0D%0A%7D&format=json', function (museums) {
for (var i = 0; i < museums.results.bindings.length; i++) { //loops through all museums to create map markers
var colorCount = 0; //colorCount is used to determine the color of the map marker
var museumID = museums.results.bindings[i].museum.value.split('Q')[1]; //retrieve Wikidata Query ID from complete wikidata URL
var popupContent = "<strong>" + museums.results.bindings[i].museumLabel.value + '</strong><br/>Wikidata: <a href="' + museums.results.bindings[i].museum.value + '" target="_blank">' + museumID + '</a>' ; //popupContent is shown when the user selects a museum map marker
museumStats["totalCount"]++;
var logEntry = 'Museum: ' + museums.results.bindings[i].museumLabel.value;
//checks 3 properties (web site, visitor count, geo coordinates) to see if they contain data
if (museums.results.bindings[i].web) {
if( museums.results.bindings[i].web.type == 'uri' ) {
museumStats["websiteCount"]++;
colorCount++;
popupContent += '<br/>Web: <a href="' + museums.results.bindings[i].web.value + '" target="_blank">' + museums.results.bindings[i].web.value + '</a>'; //append popup content with web site link
logEntry += ', Web: ' + museums.results.bindings[i].web.value;
}
if( museums.results.bindings[i].web.type == 'bnode' ) { //"bnode" is returned if the data is not properly formatted
popupContent += '<br/>Web: unknown value';
}
}
if (museums.results.bindings[i].visitors) {
if( museums.results.bindings[i].visitors.type == 'literal' ) {
museumStats["visitorsCount"]++;
colorCount++;
popupContent += '<br/>Visits: ' + museums.results.bindings[i].visitors.value; //append popup content with visitor stats
logEntry += ', Visits: ' + museums.results.bindings[i].visitors.value;
}
if( museums.results.bindings[i].visitors.type == 'bnode' ) { //"bnode" is returned if the data is not properly formatted
popupContent += '<br/>Visits: unknown value';
}
}
if (museums.results.bindings[i].coord) {
museumStats["coordinateCount"]++;
//the following variables transform the string returned from Wikidata into coordinates useable by Leaflet
var coordText = museums.results.bindings[i].coord.value.split('(');
var latlngText = coordText[1].split(' ');
var lng = latlngText[0];
var latText = latlngText[1];
var lat = latText.substring(0, latText.length-1);
var latlng = L.latLng(lat, lng); //this variable can be used by Leaflet
logEntry += ', Coords: ' + latlng;
//make marker with geocoordinates & style
var marker = L.circleMarker(latlng, {radius: 4, color: 'black', fillOpacity: 1, opacity: 1, fillColor: color[colorCount]}).bindPopup(popupContent);
markers[country].addLayer(marker);
}
//console.log(logEntry); //uncomment this if you want the museums listed in the console
} //end of the museum loop
markers[country].addTo(map);
//the following lines update the info box with country name & stats:
$('#country').html(country);
$('#totalCount').html(museumStats["totalCount"]);
$('#coordinateCount').html(museumStats["coordinateCount"]);
$('#websiteCount').html(museumStats["websiteCount"]);
$('#visitorsCount').html(museumStats["visitorsCount"]);
$('#withoutCoordinatesCount').html(museumStats["totalCount"] - museumStats["coordinateCount"]);
$('#withoutCoordinatesLink').attr('href', 'https://query.wikidata.org/#PREFIX%20wd%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%0APREFIX%20wdt%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E%0APREFIX%20wikibase%3A%20%3Chttp%3A%2F%2Fwikiba.se%2Fontology%23%3E%0APREFIX%20p%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2F%3E%0APREFIX%20v%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fstatement%2F%3E%0APREFIX%20q%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fqualifier%2F%3E%0APREFIX%20rdfs%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0APREFIX%20schema%3A%20%3Chttp%3A%2F%2Fschema.org%2F%3E%0A%0ASELECT%20%3Fmuseum%20%3FmuseumLabel%20%3FtypeLabel%20%3Fcoord%0AWHERE%20%7B%0A%20%20%3Fmuseum%20wdt%3AP17%20wd%3AQ' + countries[country][0].split('Q')[1] + '%20.%0A%20%20%3Fmuseum%20wdt%3AP31%20%3Ftype%20.%0A%20%20%3Ftype%20(wdt%3AP279)*%20wd%3AQ33506%20.%0A%20%20MINUS%20%7B%3Fmuseum%20wdt%3AP625%20%3Fcoord%20.%20%7D%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%0A%20%20%20%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20.%0A%20%20%7D%0A%7D');
if(museumStats["totalCount"] > 0) {
$('#coordinatePercentage').html((museumStats["coordinateCount"]*100/museumStats["totalCount"]).toFixed(0));
$('#websitePercentage').html((museumStats["websiteCount"]*100/museumStats["totalCount"]).toFixed(0));
$('#visitorsPercentage').html((museumStats["visitorsCount"]*100/museumStats["totalCount"]).toFixed(0));
$('#withoutCoordinatesPercentage').html(((museumStats["totalCount"] - museumStats["coordinateCount"])*100/museumStats["totalCount"]).toFixed(0));
}
else{
$('#coordinatePercentage').html('0');
$('#websitePercentage').html('0');
$('#visitorsPercentage').html('0');
$('#withoutCoordinatesPercentage').html('0');
}
map.setZoom(5);
map.panTo(countryLatlng);
if($.url('?')) {
var queryStringObject = $.url('?');
}
else{
var queryStringObject = {};
}
if(queryStringObject["country"]) {
queryStringObject["country"] = country;
}
else{
$.extend(queryStringObject, {"country":country});
}
var queryString = $.param(queryStringObject);
var newUrl = '?' + queryString;
console.log(newUrl);
history.replaceState({},'',newUrl)
}); //end of second json call (museums)
}); //end of first json call (countries)
} //end of generateMarkers function
</script>
</body>
</html>