-
Notifications
You must be signed in to change notification settings - Fork 5
/
iNatAPIv1_taxa.html
289 lines (279 loc) · 16.3 KB
/
iNatAPIv1_taxa.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
<!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 Taxa" />
<title>iNaturalist API Get Taxa</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); }
.bin_true { text-decoration: underline wavy rgba(34,139,34,0.35); }
.bin_false { text-decoration: underline wavy rgba(178,34,34,0.35); }
</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') || 200;
//let p_page = winurlparams.get('page') || 1;
let p_options = winurlparams.get('options') || [];
let p_all_names = winurlparams.get('all_names') || "false";
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 fpageurlplusorderbyid(urlbase,urlparams,per_page,page) {
let params = new URLSearchParams(urlparams);
params.get('order_by') ? params.set('order_by','id') : params.append('order_by','id');
return fpageurl(urlbase,params,per_page,page);
};
function ffetch(url) {
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
let maxcount = 10000;
if (count>maxcount) {
count=maxcount;
alert('Can export only a maximum of '+maxcount+' records due to API limits.')
};
// note that API recommended request limit is about 1 request/second or no more than 100 requests per minute.
// because this particular endpoint allows 500 records, that means we can do 20 requests before we hit a maximum of 10000 records.
// in other words, 20 requests can bring back all the records available, and we are below 100 requests per minute.
// so here, we'll just make all the requests in parallel rather than doing them in serial, since it's unlikely we'll encounter API request limits here.
let pary = [];
let maxper = 500;
for (i=0;i<Math.ceil(count/maxper);i++) { pary.push(ffetch(fpageurlplusorderbyid(apibase,winurlparams,maxper,i+1))); };
Promise.all(pary)
.then(results=>{
let ary = [[
'id',
'name',
'common_name',
'rank',
'rank_level',
'active',
'extinct',
'parent_id',
'observations_count',
]];
if (p_options.includes('ancestry')) { ary[0].splice(8,0,'ancestry'); };
if (p_all_names==="true") { ary[0].splice(3,0,'inactive_sci_names'); };
for (let result of results) {
for (let data of result.results) {
let s;
let values = [
data.id||'',
data.name||'',
(!data.preferred_common_name)?'':(data.preferred_common_name.search(',')===-1)?data.preferred_common_name:'"'+data.preferred_common_name+'"',
data.rank||'',
data.rank_level||'',
data.is_active,
data.extinct,
(data.parent_id!=null)?data.parent_id:'',
data.observations_count,
];
if (p_options.includes('ancestry')) { values.splice(8,0,data.ancestor_ids.join('/')||''); };
if (p_all_names==="true") { values.splice(3,0,(s=data.names?.filter(n=>(n.locale==='sci'&&!n.is_valid)).map(function(e){return e.name;}).join(', '))?`"${s}"`:''); };
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_taxa.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 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:'Rank Level'},
//{innerText:'Iconic Taxon ID'},
{innerText:'Active'},
{innerText:'Extinct'},
{innerText:'Parent ID'},
{innerText:'Synonym ID'},
{innerText:'Atlas ID'},
{classList:'tar',innerText:'Unresolved Flags'},
{classList:'tar',innerText:'Resolved Flags'},
{classList:'tar',innerText:'Taxon Changes'},
{classList:'tar',innerText:'Taxon Schemes'},
{classList:'tar',innerText:'Observations'},
];
if (p_options.includes('ancestry')) { labels.splice(11,0,{innerText:'Ancestry'}); };
if (p_all_names==="true") { labels.splice(5,0,{innerText:'Inactive Sci Names'}); };
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.id,rec.id)},
(p_options.includes('bigger_image'))
? {innerHTML:'<img class="icon_big" src="'+((rec.default_photo)?rec.default_photo.medium_url:'')+'" />'}
: {innerHTML:'<img class="icon" src="'+((rec.default_photo)?rec.default_photo.square_url:'')+'" />'},
{innerHTML:!rec.name?'':rec.wikipedia_url?furl(rec.wikipedia_url,rec.name):rec.name},
{innerText:rec.preferred_common_name||''},
{innerText:rec.rank||''},
{innerText:rec.rank_level||''},
//{innerText:rec.iconic_taxon_id||''},
{innerText:rec.is_active,classList:`bin_${rec.is_active}`},
{innerText:rec.extinct,classList:`bin_${rec.extinct}`},
{innerHTML:rec.parent_id?furl('https://www.inaturalist.org/taxa/'+rec.parent_id,rec.parent_id):''},
{innerHTML:rec.current_synonymous_taxon_ids?rec.current_synonymous_taxon_ids.map(e => furl('https://www.inaturalist.org/taxa/'+e,e)).toString():''},
{innerHTML:rec.atlas_id?furl('https://www.inaturalist.org/atlases/'+rec.atlas_id,rec.atlas_id):''},
{classList:'tar',innerText:fcomnum(rec.flag_counts.unresolved)},
{classList:'tar',innerText:fcomnum(rec.flag_counts.resolved)},
{classList:'tar',innerHTML:furl('https://www.inaturalist.org/taxon_changes?taxon_id='+rec.id,fcomnum(rec.taxon_changes_count))},
{classList:'tar',innerHTML:furl('https://www.inaturalist.org/taxa/'+rec.id+'/schemes',fcomnum(rec.taxon_schemes_count))},
{classList:'tar',innerHTML:furl('https://www.inaturalist.org/observations?taxon_id='+rec.id,fcomnum(rec.observations_count))},
];
if (p_options.includes('ancestry')) { values.splice(11,0,{innerHTML:rec.ancestor_ids.map(function(e){return furl('http://www.inaturalist.org/taxa/'+e,e)}).join('/')||''}); };
if (p_all_names==="true") { values.splice(5,0,{innerText:rec.names?.filter(n=>(n.locale==='sci'&&!n.is_valid)).map(function(e){return e.name;}).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/taxa';
let apiurl = apibase+((winurlparams!='')?('?'+winurlparams):'');
let apirefurl = 'https://api.inaturalist.org/v1/docs/#!/Taxa/get_taxa';
let apirefname = 'iNaturalist Taxa';
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+'?q=dracopis&is_active=any&per_page=500'))+' in a friendly format, <br />then you would open '+furl(famp(winurlexsearchstr+'?q=dracopis&is_active=any&per_page=500'))+' in your browser.'},
{innerHTML:'Note that the API will not return records beyond the 10,000th record for a given set of parameters, but you can work around this by using the id_above and id_below parameters.'},
{innerHTML:'Also note that &is_active=any is undocumented, but that is the parameter to use if you want to pull back both active and inactive taxa. Otherwise, you will get only active taxa by default.'},
{innerHTML:'The parameter &order_by is also undocumented, but if it is not specified, the API will by default sort based on observation count, which provides inconsistent ordering (in cases where observation count is the same for multiple taxa). To ensure you see all taxa across multiple pages, it is recommended that you specify &order_by=id. (The export function on this page will do this by default.) Another possible value is &order_by=created_at, but that could result in inconsistent ordering, too.'},
{innerHTML:'If you invoke the parameter &all_names=true, then you will get an additional column that shows a list of inactive scientific names.'},
{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>