Skip to content

Commit

Permalink
feat(taxon): make getAllTopNTaxon generic -> will accept future api…
Browse files Browse the repository at this point in the history
… connections
  • Loading branch information
CynthiaBorotPNV committed Nov 7, 2024
1 parent c793ac3 commit a50b280
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 41 deletions.
2 changes: 1 addition & 1 deletion js/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
function getStatusForATaxon(taxonData) {
return fetch(
`https://taxref.mnhn.fr/api/taxa/${taxonData.cd_ref}/status/columns`
`https://taxref.mnhn.fr/api/taxa/${taxonData.cdRef}/status/columns`
)
.then((response) => {
return response.json();
Expand Down
38 changes: 31 additions & 7 deletions js/taxon.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function getGbifTaxon(
* @param {number} [nbMaxTaxons=DEFAULT_NB_MAX_TAXONS] - number of results to return
* @returns {Promise<import('gbif-types').GBIFTaxon[]>}
*/
function getPgRestTaxon(wkt, nbMaxTaxons = DEFAULT_NB_MAX_TAXONS) {
function getPgRestTaxon(wkt, nbMaxTaxons = DEFAULT_NB_MAX_TAXONS, params = {}) {
const geometry = wkt;
return (
fetch(
Expand Down Expand Up @@ -122,19 +122,43 @@ function getPgRestTaxon(wkt, nbMaxTaxons = DEFAULT_NB_MAX_TAXONS) {
);
}

function validateApiDefinition(api_def) {
if (!api_def.hasOwnProperty("function_")) {
throw Error("'function_' attribute is missing");
}
if (!api_def.hasOwnProperty("params")) {
throw Error("'params' attribute is missing");
}
}

/**
* Get the top N taxons from both the GTSI API and the GBIF API given a WKT string
* @param {string} wkt - Well-Known Text representation of a polygon
* @param {number} [nbMaxTaxons=DEFAULT_NB_MAX_TAXONS] - number of results to return
* @returns {Promise<import('gbif-types').GBIFTaxon[]>} - a list of taxons with their occurrence count and GBIF id
*/
function getAllTopNTaxon(wkt, nbMaxTaxons = DEFAULT_NB_MAX_TAXONS) {
let promises = [
getPgRestTaxon(wkt, nbMaxTaxons),
getGbifTaxon(wkt, nbMaxTaxons),
];
function getAllTopNTaxon(
wkt,
nbMaxTaxons = DEFAULT_NB_MAX_TAXONS,
apiList = [
{ function_: getGbifTaxon, params: { limit: 300 } },
{ function_: getPgRestTaxon, params: {} },
]
) {
let promises = [];
apiList.forEach((api) => {
validateApiDefinition(api);
promises.push(api.function_(wkt, nbMaxTaxons, api.params));
});
if (promises.length == 0) return Promise.resolve([]);

return Promise.all(promises).then((listOfData) => {
const allData = [...listOfData[0], ...listOfData[1]];
console.log("listOfData", listOfData);
let allData = [];
listOfData.forEach((element) => {
allData = [...allData, ...element];
});

allData.sort(function (a, b) {
return b["occCount"] - a["occCount"];
});
Expand Down
48 changes: 29 additions & 19 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,22 @@ function getQueryParams() {
x: parseFloat(params.get("x")),
y: parseFloat(params.get("y")),
radius: parseInt(params.get("radius")),
use_gbif: JSON.parse(params.get("use_gbif")),
use_gn2: JSON.parse(params.get("use_gn2")),
gn_2_url: params.get("gn_2_url"),
nb_results: parseInt(params.get("nb_results")),
useGbif: JSON.parse(params.get("use_gbif")),
useGn2: JSON.parse(params.get("use_gn2")),
gn2Url: params.get("gn_2_url"),
nbResults: parseInt(params.get("nb_results")),
};
}

// function() {
// //TODO generate wkt
// return {
// wkt: params.get("wkt"),
// useGbif: JSON.parse(params.get("use_gbif")),
// useGn2: JSON.parse(params.get("use_gn2")),
// gn2Url: params.get("gn_2_url"),
// nbResults: parseInt(params.get("nb_results")),
// };
// }

/**
* Given a params object, returns a WKT string representing a localisation.
Expand All @@ -88,28 +97,29 @@ function getQueryParams() {
* @returns {string|undefined} - A WKT string or undefined.
*/


function processLocalisation(params) {
if (params.wkt) {
const geojson = wellknown.parse(params.wkt)
//Test geom type :
if (geojson.type.endsWith('Polygon')) {
return params.wkt;
}
else {
// Buffer
const buffered = turf.buffer(geojson, params.radius | 100, { units: "meters" });
return wellknown.stringify(buffered)
}
const geojson = wellknown.parse(params.wkt);
//Test geom type :
if (geojson.type.endsWith("Polygon")) {
return params.wkt;
} else {
// Buffer
const buffered = turf.buffer(geojson, params.radius | 100, {
units: "meters",
});
return wellknown.stringify(buffered);
}
}
if (params.x && params.x) {
const point = turf.point([params.x, params.x]);
const buffered = turf.buffer(point, params.radius | 100, { units: "meters" });
return wellknown.stringify(buffered)
const buffered = turf.buffer(point, params.radius | 100, {
units: "meters",
});
return wellknown.stringify(buffered);
}
}


/**
* Completes the data with the status and the picture of each taxon in the provided list.
* @param {Array<Object>} taxonsData - An array of taxon data objects.
Expand Down
14 changes: 0 additions & 14 deletions media.html

This file was deleted.

0 comments on commit a50b280

Please sign in to comment.