-
Notifications
You must be signed in to change notification settings - Fork 5
/
iNat_taxon_est_means.html
315 lines (299 loc) 路 13.5 KB
/
iNat_taxon_est_means.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, minimum-scale=1.0" />
<meta name="description" content="iNaturalist Taxon Establishment Means" />
<title>iNaturalist Taxon Establishment Means</title>
<style>
:root {
background: var(--color-base);
color: var(--color-text);
font: 14px Sans-Serif;
--color-base: white;
--color-alt: whitesmoke;
--color-brand: forestgreen;
--color-text: black;
--color-text-invert: white;
--color-text-link: royalblue;
--color-border: lightgray;
--color-hover: lightgray;
--color-base-translucent: rgba(255,255,255,0.85);
}
@media (prefers-color-scheme: dark) {
:root {
--color-base: black;
--color-alt: #171717;
--color-brand: forestgreen;
--color-text: #bababa;
--color-text-invert: black;
--color-text-link: cornflowerblue;
--color-border: #444;
--color-hover: #444;
--color-base-translucent: rgba(0,0,0,0.85);
}
}
#main { width:100%; }
table, td, th { border-collapse:collapse; margin:0; padding:4px; }
th { position:-webkit-sticky /*Safari*/; position:sticky; top:0; z-index:100; font-weight:600; background:var(--color-brand); color:var(--color-text-invert); text-align:left; vertical-align:bottom; }
tbody>tr { border-width:1px 0px; border-style:solid; border-color:var(--color-border); background:var(--color-alt);}
tr:nth-child(even) { background:var(--color-base); }
.tar { text-align:right; }
.icon { height:48px; width:48px; border-radius:50%; }
.photo { height:64px; width:64px; }
img { margin:0; padding:0; border:0; }
a { text-decoration:none; color:var(--color-text-link); }
a:hover { background:var(--color-hover); }
</style>
</head>
<body>
<script>
let winurlstr = window.location.href;
let winurlsearchstr = window.location.search;
let winurlexsearchstr = winurlstr.replace(winurlsearchstr,'');
let winurlparams = new URLSearchParams(winurlsearchstr.substring(1));
let p_taxon_id = winurlparams.get('taxon_id');
let p_options = winurlparams.get('options') || [];
winurlparams.delete('options');
function furl(url,txt=url) { return '<a href="'+url+'">'+txt+'</a>'; };
function famp(str) { return str.replace(/&/g,'&'); };
function fcomnum(n) { return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g,',') };
function faddelem(etype,eparent=null,eattributes={}) {
let eobj = document.createElement(etype);
for (let [key,value] of Object.entries(eattributes)) {
if ( typeof value === 'object' && value !== null ) {
for (let [subkey,subvalue] of Object.entries(value)) { eobj[key][subkey] = subvalue; };
}
else { eobj[key] = value; };
};
if (eparent) { eparent.appendChild(eobj); };
return eobj;
};
function faddelems(etype,eparent=null,eattributes=[]) { for (let e of eattributes) { faddelem(etype,eparent,e); }; };
function fround(num,p=0) {
let n = num*1;
return n.toFixed(p);
};
// functions to sort data in a table
function fsort(columnNo,asc=true,blanksatend=true) {
let tb = document.getElementById('tbody');
let ary = Array.from(tb.children);
ary.sort(function(a, b) {
let val_a = a.children[columnNo].innerText;
let val_b = b.children[columnNo].innerText;
if (val_a===''&&val_b==='') { return 0; }
else if (val_a!==''&&val_b==='') { return blanksatend?-1:1; }
else if (val_b!==''&&val_a==='') { return blanksatend?1:-1; }
else if (!isNaN(val_a)&&val_b==='NaN') { return asc?1:-1; }
else if (!isNaN(val_b)&&val_a==='NaN') { return asc?-1:1; }
else if (isNaN(val_a-val_b)) { return (asc?1:-1) * (val_a>val_b?1:val_a<val_b?-1:0); }
else { return asc?(val_a-val_b):(val_b-val_a); };
});
ary.forEach(function(ele) {
tb.appendChild(ele);
});
};
function frenum(columnNo=0) {
let tb = document.getElementById('tbody');
for (let r = 0, row; row = tb.rows[r]; r++) { row.children[columnNo].innerText = r+1; };
};
//need to do some geometry to figure out the vertical position of a point at given lattitude on a Mercator projection
//this is not necessary for longitudes
function latidx(lat) { return (1-Math.log(Math.tan(lat*Math.PI/180)+1/Math.cos(lat*Math.PI/180))/Math.PI)/2; };
//this adds a small map with a point marker
function faddmapthumb (eparent,lat,lng) {
let mappx = 40;
let mapx = (lng+180)/360*mappx;
let mapy = latidx(lat)*mappx;
let map = faddelem('div',eparent,{style:{
height:mappx+'px',
width:mappx+'px',
position:'relative',
border:'0.75px solid black',
borderRadius:(mappx*0.25)+'px',
overflow:'hidden',
marginTop:'0px',
zIndex:0,
}});
let basemap = faddelem('div',map,{style:{
height:mappx+'px',
width:mappx+'px',
//filter:'grayscale(100%) brightness(55%) contrast(400%)',
background:'url(https://tile.openstreetmap.org/0/0/0.png)',
backgroundSize:mappx+'px '+mappx+'px',
backgroundPosition:(-mapx)+'px 0px',
}});
let marker = faddelem('div',map,{style:{
height:'3px',
width:'3px',
//filter:'none',
borderRadius:'1.5px',
position:'absolute',
left:(mappx/2-1)+'px',
top:(mapy-1)+'px',
background:'#000000',
}});
};
async function fresults(taxon) {
if (taxon.listed_taxa_count=0) { return false; };
// table and headers
let table = faddelem('table',document.body,{id:'main'});
let thead = faddelem('thead',table);
let hrow = faddelem('tr',thead);
let labels = [
{innerText:'#'},
{innerText:'Est Means'},
{innerText:'Check List ID'},
{innerText:'Place Name'},
{innerText:'Place ID'},
// {innerText:'Alpha-2 Code'},
// {innerText:'display_name'},
// {innerText:'slug'},
// {innerText:'Create Date'},
// {innerText:'Update Date'},
{innerText:'Parent Place ID'},
{innerText:'Type'},
{innerText:'Type Name'},
{innerText:'Admin Level'},
// {innerText:'source_id'},
// {innerText:'source_identifier'},
// {innerText:'source_name'},
// {innerText:'user_id'},
// {innerText:'woeid'},
// {innerText:'uuid'},
];
if (p_options.includes('coords')) {
let labels_coords = [
{classList:'tar',innerText:'Lat'},
{classList:'tar',innerText:'Long'},
{classList:'tar',innerText:'NE Lat'},
{classList:'tar',innerText:'NE Long'},
{classList:'tar',innerText:'SW Lat'},
{classList:'tar',innerText:'SW Long'},
];
labels = [...labels,...labels_coords];
};
if (p_options.includes('map')) { labels = [...labels.slice(0,1),{innerText:'Tiny Map'},...labels.slice(1)]; };
faddelems('th',hrow,labels);
// table body
let tbody = faddelem('tbody',table,{id:'tbody'});
// a few variables
let estmeans = ['endemic','native','introduced'];
let recnum = 0;
let page = 0;
// there shouldn't be that many establishment records. so i'll keep track of fetch attempts and set an arbitrary max limit for attempts just in case something goes wrong.
let fetchattempt = 0;
let maxfetchattempt = 20;
for (let em of estmeans) {
page++;
while (page>0 && fetchattempt<maxfetchattempt) {
fetchattempt++;
await fetch('https://www.inaturalist.org/places.json?per_page=200&page='+page+'&taxon='+taxon.id+'&establishment_means='+em)
.then((response) => {
if (!response.ok) { throw new Error(response.status+' ('+response.statusText+') returned from '+response.url); };
return response.json();
})
.then((results) => {
if (results.length>0) {
page++;
// add table rows
for (let rec of results) {
recnum++;
let brow = faddelem('tr',tbody);
let values = [
{innerText:recnum},
{innerText:em},
{innerHTML:rec.check_list_id?furl('https://www.inaturalist.org/check_lists/'+rec.check_list_id+'?taxon='+taxon.id,rec.check_list_id):''},
{innerHTML:furl('https://www.inaturalist.org/observations?place_id='+rec.id+'&taxon_id='+taxon.id,rec.name)},
{innerHTML:furl('https://www.inaturalist.org/places/'+rec.id,rec.id)},
// {innerHTML:furl('https://www.iso.org/obp/ui/#iso:code:3166:'+rec.code,rec.code)},
// {innerText:rec.display_name},
// {innerText:rec.slug},
// {innerText:fdate(rec.created_at,true)},
// {innerText:fdate(rec.updated_at,true)},
{innerHTML:rec.parent_id?furl('https://www.inaturalist.org/places/'+rec.parent_id,rec.parent_id):''},
{innerText:rec.place_type},
{innerText:rec.place_type_name},
{innerText:rec.admin_level},
// {innerText:rec.source_id},
// {innerText:rec.source_identifier},
// {innerText:rec.source_name},
// {innerText:rec.user_id},
// {innerText:rec.woeid},
// {innerText:rec.uuid},
];
if (p_options.includes('coords')) {
let values_coords = [
{classList:'tar',innerText:fround(rec.latitude,4)},
{classList:'tar',innerText:fround(rec.longitude,4)},
{classList:'tar',innerText:fround(rec.nelat,4)},
{classList:'tar',innerText:fround(rec.nelng,4)},
{classList:'tar',innerText:fround(rec.swlat,4)},
{classList:'tar',innerText:fround(rec.swlng,4)},
];
values = [...values,...values_coords];
};
if (p_options.includes('map')) {
faddelem('td',brow,values.slice(0,1)[0]);
let tdmap = faddelem('td',brow);
faddmapthumb(tdmap,rec.latitude,rec.longitude);
faddelems('td',brow,values.slice(1));
}
else { faddelems('td',brow,values); };
};
}
else { page = 0; };
})
.catch((err) => {
console.error(err.message);
faddelem('p',document.body,null,null,'There was a problem retrieving establishment means. Error '+err.message+'.')
});
};
};
// the count returned from the taxon endpoint is incorrect. so this is just a workaround to get a proper count for now.
let reccount = document.getElementById('reccount');
reccount.innerText = fcomnum(recnum);
//sort by admin level, parent place id, place name, place id
let coloffset = (p_options.includes('map'))?1:0;
fsort(4+coloffset);
fsort(3+coloffset);
fsort(5+coloffset);
fsort(8+coloffset);
frenum();
};
faddelem('h1',document.body,{innerText:'iNaturalist Taxon Establishment Means'});
if (winurlsearchstr===''||p_taxon_id===null) {
let instructions = [
{innerHTML:'This page provides establishment means records for a given taxon. It is useful when the standard iNaturalist '+furl('https://www.inaturalist.org/taxa','taxon page')+' does not provide full establishment means data for some reason (ex. more than 100 establishment means records for the taxon).'},
{innerHTML:'To use this page, add a taxon_id parameter and value to the URL. For example, if you want to get establishment means for Liquidambar stryaciflua (American Sweetgum), then you would open '+furl(famp(winurlexsearchstr+'?taxon_id=49658'))+' in your browser.'},
{innerHTML:'It it also possible to use an options parameter to display extra columns for coordinates (options=coords) and/or an extra column containing a tiny map (options=map). (Separate columns and maps with a comma if you want to see both.) For example, if you want to get establishment means with both coordinates and map for Berberis aquifolium (Oregon Grape), then you would open '+furl(famp(winurlexsearchstr+'?taxon_id=126887&options=coords,map'))+' in your browser.'},
];
faddelems('p',document.body,instructions);
}
else {
let apibase = 'https://api.inaturalist.org/v1/taxa/';
let apiurl = apibase+p_taxon_id;
fetch(apiurl)
.then((response) => {
if (!response.ok) { throw new Error(response.status+' ('+response.statusText+') returned from '+response.url); };
return response.json();
})
.then((data) => {
let firstrec = data.results[0]; // always just get the first result
let taxon = {
id: firstrec.id,
name: firstrec.name,
listed_taxa_count: firstrec.listed_taxa_count,
};
faddelem('p',document.body,{innerHTML:'taxon: '+taxon.name+' ('+furl('https://www.inaturalist.org/taxa/'+taxon.id,taxon.id)+')<br />'
+'total establishment means records: <span id="reccount">'+fcomnum(taxon.listed_taxa_count)+'</span>'});
fresults(taxon);
})
.catch((err) => {
console.error(err.message);
faddelem('p',document.body,null,null,'There was a problem retrieving taxon. Check to make sure you have input a valid taxon. Error '+err.message+'.')
});
};
</script>
</body>
</html>