-
Notifications
You must be signed in to change notification settings - Fork 6
/
iNatAPIv1_observations_species_counts.html
275 lines (263 loc) · 15 KB
/
iNatAPIv1_observations_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
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
<!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 Observation Leaf Taxa Counts" />
<title>iNaturalist API Get Observation 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 delay(time,value) {
return new Promise(function(resolve) {
setTimeout(resolve.bind(null, value), time)
console.log('Pausing '+time+'ms...')
});
}
async function ffetch(url,delayinms=0) {
await delay(delayinms);
return fetch(url)
.then((response) => {
if (!response.ok) { throw new Error(response.status+': '+response.statusText); };
return response.json();
})
.catch((err) => { console.error(err); });
};
function fexport(count=0) {
let ebutton = document.getElementById('export');
ebutton.innerHTML = 'exporting...';
// generally the API will return no more than the first 10000 records for a given set of parameters, but it turns out this endpoint will allow more
// note that the API recommended request limit is about 1 request/second or no more than 100 requests per minute.
// it's unlikely that we'll violate this if we issue 20 or fewer requests together (@500 records per page = 10000 records).
// so if we have fewer than 10000 records (20 requests), we'll issue all the requests at one time.
// otherwise, we'll add a delay between requests
let maxcount_default = 10000;
let maxcount = p_options.includes('unlimitedexport') ? 500000 : maxcount_default; // currently, there are ~400000 recorded in the system. so we'll add a slightly higher max just to avoid getting caught in an infinite loop.
if (count>maxcount) {
count=maxcount;
alert(`By default, this page exports only up to ${maxcount_default} records in order to avoid API request limits. To export more than ${maxcount_default} records, reload the page with an additional parameter &options=unlimitedexport in the URL. Expect a large export to take at least 1 second per page of results to be exported.`)
};
let pary = [];
let maxper = 500;
for (i=0;i<Math.ceil(count/maxper);i++) {
let delayinms = (count/maxper > 20) ? i * 1000 : 0;
pary.push(ffetch(fpageurl(apibase,winurlparams,maxper,i+1),delayinms));
};
Promise.all(pary)
.then(results=>{
let ary = [[
'id',
'name',
'common_name',
'rank',
'iconic_taxon',
'taxon_obs_count',
'taxon_ttl_obs_count',
]];
if (p_options.includes('ancestry')) { ary[0].splice(5,0,'ancestry'); };
for (let result of results) {
for (let data of result.results) {
let values = [
data.taxon.id||'',
data.taxon.name||'',
(!data.taxon.preferred_common_name)?'':(data.taxon.preferred_common_name.search(',')===-1)?data.taxon.preferred_common_name:'"'+data.preferred_common_name+'"',
data.taxon.rank||'',
data.taxon.iconic_taxon_name||'',
data.count||'',
data.taxon.observations_count||'',
];
if (p_options.includes('ancestry')) { values.splice(5,0,data.taxon.ancestor_ids.join('/')); };
ary.push(values);
};
};
// transfer the contents of the array into a data object, create a hyperlink to the object, and simulate a click on the hyperlink to download the object.
let csv = 'data:text/csv;charset=utf-8,'+ ary.map(e => e.join(',')).join('\n');
let euri = encodeURI(csv);
let link = document.createElement('a');
link.href = euri;
link.download = 'inat_obs_species_counts.csv';
document.body.appendChild(link); // for FF
link.click();
ebutton.innerHTML = 'exported';
});
};
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)+' '
+'[<span id="export"><a href="javascript:fexport('+total_results+');">export</a></span>]<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 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];
// this section sets up parameters for hyperlinks to Explore page
// won't be able properly to handle situations where user inputs subspecies in URL parameters
// also, won't always tie exactly because Explore page always filters for spam=false (cannot change this parameter on that page)
let obsparams = new URLSearchParams(winurlparams);
obsparams.delete('order');
obsparams.delete('page');
obsparams.delete('per_page');
obsparams.delete('taxon_id');
let url_explore = 'https://www.inaturalist.org/observations?taxon_id='+rec.taxon.id
let obsparam_verifiable = (obsparams.get('verifiable')===null)?'&verifiable=any':('&verifiable='+obsparams.get('verifiable'));
obsparams.delete('verifiable');
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',innerHTML:furl(url_explore+obsparam_verifiable+(obsparams==''?'':('&'+obsparams)),fcomnum(rec.count))},
{classList:'tar',innerHTML:furl(url_explore+'&verifiable=any',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/observations/species_counts';
let apiurl = apibase+((winurlparams!='')?('?'+winurlparams):'');
let apirefurl = 'http://api.inaturalist.org/v1/docs/#!/Observations/get_observations_species_counts';
let apirefname = 'iNaturalist Observation 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'))+' in a friendly format, <br />then you would open '+furl(famp(winurlexsearchstr+'?user_id=bob'))+' in your browser.'},
{innerHTML:'You can also invoke a few 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. Adding &options=unlimitedexport will allow you export a lot of records. To invoke multiple options, use a comma-separated value list for the parameter (ex. &options=ancestry,bigger_image,unlimitedexport).'},
{innerHTML:'Note that the counts returned by this page vs the '+furl('https://www.inaturalist.org/observations','Explore page')+' in iNaturalist may not tie exactly because the Explore page will always filter out spam observations. Also, if you filter for a subspecies taxon on this page, the taxon shown here will be the species level, but the count will reflect the subspecies count, and the link to the Explore page will take you to the species level.'},
];
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>