-
Notifications
You must be signed in to change notification settings - Fork 6
/
iNat_taxon_counts_by_rank.html
168 lines (157 loc) · 6.67 KB
/
iNat_taxon_counts_by_rank.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="iNaturalist Taxon Counts By Rank" />
<title>iNaturalist Taxon Counts By Rank</title>
<style>
body { font:10pt Sans-Serif; }
.tar { text-align:right; }
#main { width:100%; }
table, thead, tbody, tr, td { margin:0px; padding:3px; border-width:1px 0px;border-style:solid; border-color:lightgray; border-spacing:0px; border-collapse:collapse; }
thead tr { font-weight:600; background:green; color:white; }
tfoot tr { font-weight:600; background:black; color:white; }
tr { background:whitesmoke; }
tr:nth-child(even) {background-color:white }
.icon { height:48px; width:48px; border-radius:50%; }
img { margin:0px; padding:0px; border:0px; }
a { text-decoration:none; }
</style>
</head>
<body>
<script>
let winurlstr = window.location.href;
let winurlsearchstr = window.location.search;
let winurlexsearchstr = winurlstr.replace(winurlsearchstr,'');
var winurlparams = new URLSearchParams(winurlsearchstr.substring(1));
winurlparams.delete('per_page');
winurlparams.delete('only_id');
winurlparams.delete('rank');
winurlparams.delete('is_active');
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 fpct(d,p=1) {
x = d*100;
return x.toFixed(p);
};
function faddelem(etype,eparent,eclass=null,eid=null,ehtml=null) {
var eobj = document.createElement(etype);
if (eclass!==null) { eobj.classList = eclass };
if (eid!==null) { eobj.id = eid };
if (ehtml!==null) { eobj.innerHTML = ehtml };
eparent.appendChild(eobj);
return eobj;
};
function fsettdvalue(rlabel,clabel,val) {
document.getElementById(rlabel+'_'+clabel).innerHTML = val;
};
function fgetcount(url) {
return fetch(url)
.then((response) => {
if (!response.ok) { throw new Error(response.status+': '+response.statusText); };
return response.json();
})
.then((data) => { return data.total_results; })
.catch((err) => { console.error(err); });
};
var apiurl = 'https://api.inaturalist.org/v1/taxa?per_page=1&only_id=true'+((winurlparams!='')?('&'+winurlparams):'');
var ary_tr = [
{taxon_rank:"kingdom",disp_name:"Kingdom",icon:"👑"},
{taxon_rank:"phylum",disp_name:"Phylum"},
{taxon_rank:"subphylum",disp_name:"Subphylum"},
{taxon_rank:"superclass",disp_name:"Superclass"},
{taxon_rank:"class",disp_name:"Class"},
{taxon_rank:"subclass",disp_name:"Subclass"},
{taxon_rank:"infraclass",disp_name:"Infraclass"},
{taxon_rank:"subterclass",disp_name:"Subterclass"},
{taxon_rank:"superorder",disp_name:"Superorder"},
{taxon_rank:"order",disp_name:"Order"},
{taxon_rank:"suborder",disp_name:"Suborder"},
{taxon_rank:"infraorder",disp_name:"Infraorder"},
{taxon_rank:"parvorder",disp_name:"Parvorder"},
{taxon_rank:"zoosection",disp_name:"Zoosection"},
{taxon_rank:"zoosubsection",disp_name:"Zoosubsection"},
{taxon_rank:"superfamily",disp_name:"Superfamily"},
{taxon_rank:"epifamily",disp_name:"Epifamily"},
{taxon_rank:"family",disp_name:"Family"},
{taxon_rank:"subfamily",disp_name:"Subfamily"},
{taxon_rank:"supertribe",disp_name:"Supertribe"},
{taxon_rank:"tribe",disp_name:"Tribe"},
{taxon_rank:"subtribe",disp_name:"Subtribe"},
{taxon_rank:"genus",disp_name:"Genus"},
{taxon_rank:"genushybrid",disp_name:"Genus Hybrid"},
{taxon_rank:"subgenus",disp_name:"Subgenus"},
{taxon_rank:"section",disp_name:"Section"},
{taxon_rank:"subsection",disp_name:"Subsection"},
{taxon_rank:"complex",disp_name:"Complex"},
{taxon_rank:"species",disp_name:"Species",icon:"🗝"},
{taxon_rank:"hybrid",disp_name:"Hybrid"},
{taxon_rank:"subspecies",disp_name:"Subspecies"},
{taxon_rank:"variety",disp_name:"Variety"},
{taxon_rank:"form",disp_name:"Form"},
{taxon_rank:"infrahybrid",disp_name:"Infrahybrid"},
{taxon_rank:"Other",disp_name:"Other",icon:"?"},
{taxon_rank:"All",disp_name:"All",icon:"🌐"},
];
faddelem('h1',document.body,null,null,'iNaturalist Taxon Counts by Rank');
// create the basic structure of the table
var table = faddelem('table',document.body,null,'main');
var thead = faddelem('thead',table);
var hrow = faddelem('tr',thead);
faddelem('td',hrow,null,null,'Taxon Rank');
faddelem('td',hrow);
faddelem('td',hrow,'tar',null,'Active');
faddelem('td',hrow,'tar',null,'Inactive');
faddelem('td',hrow,'tar',null,'Total');
var tbody = faddelem('tbody',table);
var tfoot = faddelem('tfoot',table);
for (i=0;i<ary_tr.length;i++) {
var str_tr = ary_tr[i].taxon_rank;
var str_dn = ary_tr[i].disp_name;
var str_ic = ary_tr[i].icon;
var trow = faddelem('tr',(str_tr==='All'?tfoot:tbody));
faddelem('td',trow,null,str_tr+'_it',str_dn);
faddelem('td',trow,null,str_tr+'_ic',str_ic);
faddelem('td',trow,'tar',str_tr+'_a');
faddelem('td',trow,'tar',str_tr+'_i');
faddelem('td',trow,'tar',str_tr+'_t');
};
faddelem('p',document.body,null,null,'Notes:<br />'
+'1. Because these numbers may be retrieved at slightly different times, they may not all tie exactly.<br />'
+'2. This is the base query: '+furl(famp(apiurl))+'. (This page will accept most parameters from the iNaturalist API '+furl('https://api.inaturalist.org/v1/docs/#!/Taxa/get_taxa','Taxon Search')+' endpoint.)'
)
var rowsdone = 0;
var ttl = {a:0,i:0,t:0};
// pull data into table
// (this is separate from the creation of the table structure just in case there are failures while pulling data)
for (j=0;j<ary_tr.length;j++) {
var str_tr = ary_tr[j].taxon_rank
if (str_tr!=='Other') {
var p_tr = str_tr==='All'?'':'&rank='+str_tr;
var prom0 = Promise.resolve(str_tr);
var prom1 = fgetcount(apiurl+p_tr+'&is_active=true');
var prom2 = fgetcount(apiurl+p_tr+'&is_active=false');
Promise.all([prom0,prom1,prom2]).then(function(values) {
var tr = values[0];
var a = values[1];
var i = values[2];
var t = a+i;
fsettdvalue(tr,'a',fcomnum(a));
fsettdvalue(tr,'i',fcomnum(i));
fsettdvalue(tr,'t',fcomnum(t));
ttl.a += (tr==='All')?a:-a;
ttl.i += (tr==='All')?i:-i;
ttl.t += (tr==='All')?t:-t;
rowsdone++;
if (rowsdone===(ary_tr.length-1)) {
fsettdvalue('Other','a',fcomnum(ttl.a));
fsettdvalue('Other','i',fcomnum(ttl.i));
fsettdvalue('Other','t',fcomnum(ttl.t));
};
});
};
};
</script>
</body>
</html>