-
Notifications
You must be signed in to change notification settings - Fork 6
/
iNatAPIv1_identifications_species_counts.html
186 lines (177 loc) · 10.1 KB
/
iNatAPIv1_identifications_species_counts.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
<!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 API Get Identification Leaf Taxa Counts" />
<title>iNaturalist API Get Identification Leaf Taxa Counts</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; 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%; }
.icon_big { height:250px; width:250px; border-radius:10%; object-fit: cover; object-position: center; }
.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); }
#nav { margin-left:-8px; position:-webkit-sticky /*Safari*/; position:sticky; left:0; bottom:0; background:var(--color-base-translucent); width:188px; height:52px; border-radius:14px; }
@media print { #nav { display:none; } }
.button, .button_inactive { display:inline-block; margin-left:10px; margin-top:10px; height:32px; width:32px; border:1px solid var(--color-border); border-radius:35%; font-size:24px; vertical-align:middle; text-align:center; }
.button { background:var(--color-alt); }
.button_inactive { background:none; color:var(--color-border); }
</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_per_page = winurlparams.get('per_page') || 500;
//let p_page = winurlparams.get('page') || 1;
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 fpageurl(urlbase,urlparams,per_page,page) {
let params = new URLSearchParams(urlparams);
let url_per_page = params.get('per_page');
let url_page = params.get('page');
(url_per_page===null) ? params.append('per_page',per_page) : params.set('per_page',per_page);
(url_page===null) ? params.append('page',page) : params.set('page',page);
if (p_options && p_options.length>0) { params.append('options',p_options); };
return urlbase+'?'+params;
};
function fresults(xobj) {
//faddelem('p',document.body,{innerHTML:furl(famp(apiurl))});
let results = xobj.results;
if (results) {
let total_results = xobj.total_results;
let per_page = xobj.per_page;
let page_curr = xobj.page;
let page_max = Math.ceil(total_results/per_page);
let page_prev = ((page_curr>1)?page_curr-1:null);
let page_next = ((page_curr<page_max)?page_curr+1:null);
faddelem('p',document.body,{innerHTML:'total leaf taxa: '+fcomnum(total_results)+'<br />'
+'per page: '+fcomnum(per_page)+'<br />'
+'page: '+fcomnum(page_curr)+' of '+fcomnum(page_max)+'<br />'
});
let table = faddelem('table',document.body,{id:'main'});
let thead = faddelem('thead',table);
let hrow = faddelem('tr',thead);
let labels = [
{innerText:'#'},
{innerText:'ID'},
{innerText:'Photo'},
{innerText:'Name'},
{innerText:'Common Name'},
{innerText:'Rank'},
{innerText:'Iconic Taxon'},
{classList:'tar',innerText:'Taxon Obs/ID Count'},
{classList:'tar',innerText:'Taxon Ttl Obs Count'},
];
if (p_options.includes('ancestry')) { labels.splice(7,0,{innerText:'Ancestry'}); };
faddelems('th',hrow,labels);
let tbody = faddelem('tbody',table);
for (let i=0; i<results.length; i++) {
let brow = faddelem('tr',tbody);
let rec = results[i];
let values = [
{innerText:i+1},
{innerHTML:furl('https://www.inaturalist.org/taxa/'+rec.taxon.id,rec.taxon.id)},
(p_options.includes('bigger_image'))
? {innerHTML:'<img class="icon_big" src="'+(rec.taxon.default_photo?rec.taxon.default_photo.medium_url:'')+'" />'}
: {innerHTML:'<img class="icon" src="'+(rec.taxon.default_photo?rec.taxon.default_photo.square_url:'')+'" />'},
{innerText:rec.taxon.name||''},
{innerText:rec.taxon.preferred_common_name||''},
{innerText:rec.taxon.rank||''},
{innerText:rec.taxon.iconic_taxon_name||''},
{classList:'tar',innerText:fcomnum(rec.count)},
{classList:'tar',innerText:fcomnum(rec.taxon.observations_count)},
];
if (p_options.includes('ancestry')) { values.splice(7,0,{innerHTML:rec.taxon.ancestor_ids.map(function(e){return furl('http://www.inaturalist.org/taxa/'+e,e)}).join('/')||''}); };
faddelems('td',brow,values);
};
// buttons to go to prev or next page
let nav = faddelem('div',document.body,{id:'nav'});
(page_curr<=1)?faddelem('span',nav,{classList:'button_inactive',title:'already on first page',innerHTML:'«'}):faddelem('a',nav,{classList:'button',title:'first page',id:'button_first',innerHTML:'«',href:fpageurl(winurlexsearchstr,winurlparams,per_page,1)});
(page_prev===null)?faddelem('span',nav,{classList:'button_inactive',title:'no previous page',innerHTML:'‹'}):faddelem('a',nav,{classList:'button',title:'previous page',id:'button_prev',innerHTML:'‹',href:fpageurl(winurlexsearchstr,winurlparams,per_page,page_prev)});
(page_next===null)?faddelem('span',nav,{classList:'button_inactive',title:'no next page',innerHTML:'›'}):faddelem('a',nav,{classList:'button',title:'next page',id:'button_next',innerHTML:'›',href:fpageurl(winurlexsearchstr,winurlparams,per_page,page_next)});
(page_curr>=page_max)?faddelem('span',nav,{classList:'button_inactive',title:'already on last page',innerHTML:'»'}):faddelem('a',nav,{classList:'button',title:'last page',id:'button_last',innerHTML:'»',href:fpageurl(winurlexsearchstr,winurlparams,per_page,page_max)});
}
else { faddelem('p',document.body,{innerText:'No results returned.'}); };
};
let apibase = 'https://api.inaturalist.org/v1/identifications/species_counts';
let apiurl = apibase+((winurlparams!='')?('?'+winurlparams):'');
let apirefurl = 'https://api.inaturalist.org/v1/docs/#!/Identifications/get_identifications_species_counts';
let apirefname = 'iNaturalist Identification Species Counts';
let apiref = furl(apirefurl,apirefname);
faddelem('h1',document.body,{innerText:apirefname});
if (winurlsearchstr==='') {
let instructions = [
{innerHTML:'This page displays the response from the '+apiref+' API endpoint in a human-readable format. To use this page, add at least one query parameter to the URL. See '+furl(apirefurl)+' for available query parameters.'},
{innerHTML:'Suppose the address of this page is '+winurlexsearchstr+', <br />and you want to see the results of '+furl(famp(apibase+'?user_id=bob¤t=true&order=desc&order_by=created_at&taxon_of=identification'))+' in a friendly format, <br />then you would open '+furl(famp(winurlexsearchstr+'?user_id=bob¤t=true&order=desc&order_by=created_at&taxon_of=identification'))+' in your browser.'},
{innerHTML:'You can also invoke a couple of extra options. Adding &options=ancestry to the URL parameter list will include a column with taxon ancestry in the results. Adding &options=bigger_image will display a larger version of the taxon photo. To invoke both options, use a comma-separated value list for the parameter (&options=ancestry,bigger_image).'},
];
faddelems('p',document.body,instructions);
}
else {
faddelem('p',document.body,{innerHTML:'This is the base query: '+furl(famp(apiurl))+'. (This page will accept most parameters from the '+apiref+' API endpoint.)'});
fetch(apiurl)
.then((response) => {
if (!response.ok) { throw new Error(response.status+' ('+response.statusText+') returned from '+response.url); };
return response.json();
})
.then((data) => { fresults(data); })
.catch((err) => {
console.error(err.message);
faddelem('p',document.body,{innerText:'There was a problem retrieving data. Error '+err.message+'.'});
});
};
</script>
</body>
</html>