dronefly-garden / dronefly

Red Discord Bot V3 cogs for naturalists.
Other
16 stars 3 forks source link

inatcog: Command to provide a map for 2 specified taxa #18

Closed synrg closed 5 years ago

synrg commented 5 years ago

From mws on iNat discord:

mws: Hey SyntheticBee, it would be cool if Appledore could generate a range map of two
     or more species like this one:
     https://www.inaturalist.org/taxa/map?taxa=24255,24267#7/43.469/-82.442
synrg commented 5 years ago

As we started to discuss on #makers on iNat Discord server, in addition to the taxon_id values for both taxa, the URL contains the Google maps zoom factor (7) and lat,lon (43.469, -82.442) of the center point of the map.

This call could provide just the bounds needed for both taxa:

https://api.inaturalist.org/v1/observations?return_bounds=true&verifiable=true&taxon_id=24255,24267&per_page=0

That would give us the center point. Then we need to compute a zoom level. The result would then be trivial to put into an embed as a link to the map.

As for the command itself, here are suggested inputs:

[p]inat map <taxon1>,<taxon2>,...,<taxonN>

Each taxon is identified by terms that uniquely identify that taxon as per: [p]inat taxon <terms>.

e.g. since the taxon subcommand accepts taxon_id, this would work:

[p]inat map 24255,24267

Or alternatively:

[p]inat map boreal chorus frog,western chorus frog

Although it is not yet supported in the syntax, with some more work on taxon support for advanced syntaxes, this might be possible in future:

[p]inat map (boreal,western) in genus chorus frogs
synrg commented 5 years ago

See https://gis.stackexchange.com/questions/7430/what-ratio-scales-do-google-maps-zoom-levels-correspond-to in order to choose a zoom factor that encompasses the rectangle represented by the coordinate pairs returned by return_bounds.

synrg commented 5 years ago

Roughly speaking, we want to:

  1. pick the larger degrees of the lat or lon from the returned bounds
  2. compute the arc size
  3. pick the zoom level corresponding to the smallest distance that is still larger than the arc size
  4. use the center point of the 2 bounds corners & the zoom level as the 3 parameters to the /taxa/map URL
synrg commented 5 years ago

Zoom levels on which range maps still show seem to max out at 10 (i.e. theoretically you could go up to 21, but then iNat no longer shows the polygons for the ranges at that level of zoom; plus there's not enough context to really see where the range is anyway). So we should only use zoom levels between 2 (about 20,000 km to a side) and 10 (about 78 km to a side).

synrg commented 5 years ago
>>> math.radians(180)*6.3781*10**6
20037392.10386106

Example showing 180 degrees is about 20,000 km (the calculation above is in metres).

synrg commented 5 years ago

If you take zoom level 2 to show about 20,000 km, then this gives you the size in km for other zoom levels by cutting that in half every zoom level up:

>>> z = 2
>>> 20000/2**(z-2)
20000.0
>>> z = 10
>>> 20000/2**(z-2)
78.125
synrg commented 5 years ago

Thanks to michaelpirrello from iNat Discord for suffering through the math with me. :)