This repository has been archived by the owner on Jul 3, 2019. It is now read-only.
forked from jhpoelen/effechecka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
192 lines (172 loc) · 8.24 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
<!DOCTYPE html>
<html>
<head>
<title>Effechecka - Taxonomic Checklist Generator</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css"/>
<link rel="stylesheet" href="https://rawgit.com/heyman/leaflet-areaselect/master/src/leaflet-areaselect.css"/>
</head>
<body>
<h1>Effechecka - Taxonomic Checklist Generator</h1>
<div id="map" style="width: 600px; height: 400px"></div>
<div id="taxonFilter">Include only taxa related to <input id='scientificName' type="text"
placeholder="enter taxon name" autocomplete="off" value=""/>.
</div>
<div id="status"></div>
<table id="checklist">
<tr>
<th>name</th>
<th>hierarchy</th>
</tr>
</table>
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script src="https://rawgit.com/heyman/leaflet-areaselect/master/src/leaflet-areaselect.js"></script>
<script>
var map = L.map('map').setView([51.505, -0.09], 1);
L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {
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: 'examples.map-i875mjb7'
}).addTo(map);
var areaSelect = L.areaSelect({width: 200, height: 300});
areaSelect.addTo(map);
function createEllipsis() {
var ellipsis = document.createElement('td');
ellipsis.textContent = '...';
return ellipsis;
}
var updateChecklist = function () {
var req = null;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
req = new XMLHttpRequest();
} else if ((typeof window !== 'undefined') && window.ActiveXObject) { // IE
try {
req = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
req = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {
}
}
}
if (req !== undefined) {
var baseUrl = 'http://api.gbif.org/v1/occurrence/search';
var dataFilter = getDataFilter();
var query = Object.keys(dataFilter).reduce(function (accum, key) {
if (dataFilter[key] !== null) {
return accum + key + '=' + encodeURIComponent(dataFilter[key]) + '&';
} else {
return accum;
}
}, '?');
var url = baseUrl + query;
req.open('GET', url, true);
req.onreadystatechange = function () {
if (req.readyState === 4) {
setStatus('received response');
if (req.status === 200) {
var resp = JSON.parse(req.responseText);
setStatus('got [' + resp.count + '] matches from gbif');
if (resp.results) {
checklist.setAttribute('data-results', resp.results);
var headerRow = document.createElement('tr');
var header = document.createElement('th');
header.textContent = 'taxon checklist';
headerRow.appendChild(header);
header = document.createElement('th');
header.textContent = '(lat,lng)';
headerRow.appendChild(header);
checklist.appendChild(headerRow);
resp.results.forEach(function (occurrence) {
var row = document.createElement('tr');
var path = document.createElement('td');
var pathElems = ['kingdom', 'phylum', 'class', 'order', 'family', 'genus', 'species'].reduce(function (pathFull, pathPart) {
var pathPartValue = occurrence[pathPart];
if (pathPartValue !== undefined) {
var pathPartElem = document.createElement('a');
pathPartElem.setAttribute('href', 'http://eol.org/' + pathPartValue);
pathPartElem.textContent = pathPartValue;
pathPartElem.setAttribute('title', 'search EOL for [' + pathPartValue + '] by name');
var sepElem = document.createElement('span');
sepElem.textContent = ' | ';
return pathFull.concat([pathPartElem, sepElem])
} else {
return pathFull;
}
}, []);
pathElems.forEach(function(elem) {
path.appendChild(elem);
});
row.appendChild(path);
var latLng = document.createElement('td');
latLng.textContent = '(' + occurrence.decimalLatitude + ',' + occurrence.decimalLongitude + ')';
row.appendChild(latLng);
checklist.appendChild(row);
});
var ellipsisRow = document.createElement('tr');
ellipsisRow.appendChild(createEllipsis());
ellipsisRow.appendChild(createEllipsis());
checklist.appendChild(ellipsisRow);
}
} else {
setStatus('not ok. Received a [' + req.status + '] status with text [' + req.statusText + '] in response to [' + url + ']');
}
}};
var checklist = document.querySelector('#checklist');
while (checklist.firstChild) {
checklist.removeChild(checklist.firstChild);
}
setStatus('building new list...');
req.send(null);
}
};
var setStatus = function (status) {
document.querySelector('#status').textContent = status;
}
var getDataFilter = function () {
var checklist = document.querySelector('#checklist');
var dataFilter = { hasSpatialIssue: 'false' };
if (checklist.hasAttribute('data-filter')) {
dataFilter = JSON.parse(checklist.getAttribute('data-filter'));
}
return dataFilter;
}
var setDataFilter = function (dataFilter) {
var dataFilterString = JSON.stringify(dataFilter);
document.querySelector('#checklist').setAttribute('data-filter', dataFilterString);
}
var updateBBox = function (areaSelect) {
var wktPoints = areaSelect._northEast.lng + ' ' + areaSelect._northEast.lat
+ ',' + areaSelect._northEast.lng + ' ' + areaSelect._southWest.lat
+ ',' + areaSelect._southWest.lng + ' ' + areaSelect._southWest.lat
+ ',' + areaSelect._southWest.lng + ' ' + areaSelect._northEast.lat
+ ',' + areaSelect._northEast.lng + ' ' + areaSelect._northEast.lat;
var dataFilter = getDataFilter();
dataFilter.geometry = 'POLYGON((' + wktPoints + '))';
setDataFilter(dataFilter);
}
areaSelect.on("change", function () {
updateBBox(this.getBounds());
updateChecklist();
});
var nameInput = document.querySelector('#scientificName');
nameInput.onchange = function (event) {
var filter = getDataFilter();
filter.scientificName = event.target.value.trim();
setDataFilter(filter);
updateChecklist();
};
var init = function () {
updateBBox(areaSelect.getBounds());
updateChecklist();
}
window.addEventListener('load', function () {
init();
});
</script>
</body>
</html>