gbif / hosted-portals

Support material for establishing the GBIF Hosted Portals
Apache License 2.0
9 stars 6 forks source link

Limit Gadm suggests to a specific area #242

Closed MortenHofft closed 1 year ago

MortenHofft commented 1 year ago

Hi @jloomisVCE you asked a question in our webinar today. Something along "Can we limit GADM suggestions to a specific area"

I said - i think not. I was wrong - you can. The API supports it https://www.gbif.org/developer/occurrence#gadm

But that means you have to overwrite the suggest. You have already done that elsewhere in your portal as far as I remember. To make it a bit simpler, I've just changed so that the suggest configs get merged.

function getSuggests({ client }) {
  return {
    gadmGid: {
      // how to get the list of suggestion data, before you would also have to define how to render the suggestion, but the new part I added means that below is enough
      getSuggestions: ({ q }) => {
        const { promise, cancel } = client.v1Get(`/geocode/gadm/search?gadmGid=USA.46_1&limit=100&q=${q}`); // this gadmGid=USA.46_1 is the new part, that means that the suggester will now only suggest things in Vermont
        return {
          promise: promise.then(response => {
            return {
              data: response.data.results.map(x => ({ title: x.name, key: x.id, ...x }))
            }
          }),
          cancel
        }
      }
    }
  }
}

And you would then provide that as part of the configuration under the occurrence part.

E.g. here if Colombia https://github.com/gbif/hp-colombian-biodiversity/blob/master/_includes/js/config.js#L46

occurrence: {
  ...whatever config you have here
  highlightedFilters: ['taxonKey', 'gadmGid', 'stateProvince','elevation','year', 'basisOfRecord', 'recordedBy','publishingOrg','datasetKey','datasetName','occurrenceIssue'],
  occurrenceSearchTabs: ['TABLE', 'MAP', 'GALLERY', 'DATASETS'],
  // and then the new part, the overwrites for suggests which we defined above
  getSuggest: getSuggest
}

This will not solve other issues you might have with GADM suggest such as lack of granularity and such, but please create issues with examples for those as they are best handled separately.

I will close this issue once the simplification to the overwrites is also in production

jloomisVCE commented 1 year ago

@MortenHofft Thanks I did implement this and it works. I note that I had to name the function exactly 'getSuggests' for it to work.

MortenHofft commented 1 year ago

Great that you got it working. You can call the function whatever you want to I'm certain. As long as you also reference it as such in https://github.com/VtEcostudies/VAL_GBIF_Wordpress/blob/7c1e9c1e931bd2421afa57550ea64caaa0f447d4/js/gbif_data_widget.js#L95

e.g. line 95 could just as well be getSuggest: anyNameOfYourFunction asn long as that is what you called your function in line 31

jloomisVCE commented 1 year ago

Yes, you're correct that my local function name can be anything! Sorry I meant to say that the object-declaration of the suggest function appears to only allow the value 'getSuggests'. I misunderstood which of my coding updates was successful!Thanks for clarifying:

... getSuggests: getAnySuggestFunctionName, ...