API: project heatmap using R and quarto (html)

Hi everyone!

I’m creating a template using R, Quarto, and the iNat API, to report stuff on projects on iNat. One of the things I want to display is a map with the observations of the project.

As an example, let’s say I wan to report on this project: https://www.inaturalist.org/projects/gbs-2024-uruguay

The option I see on the API (see https://api.inaturalist.org/v1/docs/#/UTFGrid) doesn’t work well for me. Also, it’s very poorly documented.

And when I try with this https://www.inaturalist.org/observations/map?project_id=214543#7/-32.768/-54.951 (which is super cool), the map is not shown on my html. I think there’s some problem with Google and JavaScript.

This is the code:

<iframe width="780" height="500" src="https://www.inaturalist.org/observations/map?project_id=gbs-2024-uruguay#7/-32.5/-55" title="Heatmap"></iframe>

And the output:

Any ideas on how I can include this map on my html report?

Cheers!
Flo

2 Likes

if you like iNat’s standard heatmap, then those map tiles are available via https://api.inaturalist.org/v1/heatmap/{z}/{x}/{y}.png.

if you’re using R for just visualization (not interactivity), you probably need to use tmap or leaflet to display one of iNat’s observation map tilesets over some sort of basemap.

UTFGrids are only needed if you want to be able to interactively click on an XYZ tile map to show additional data or provide other functionality. that’s more complicated to do, and i’m not sure if anyone has already written something to do this in R, although i bet it would be possible (though maybe not easy) to extend leaflet in R to to use these.

you can also use the data from UTFGrids to make interesting custom maps, but that’s also more complex to do, and you’d probably have to extend leaflet or tmap to do this, as well.

if you don’t understand XYZ map tiles, you can read a little bit about them here: https://forum.inaturalist.org/t/in-pursuit-of-mappiness-part-1/21864#what-are-map-tiles-3.

3 Likes

This is very helpful @pisum, thanks a lot!

I would like to show something like you can see here with your code: https://jumear.github.io/stirfry/iNat_map?view=heatmap&project_id=gbs-2024-uruguay, but using tmap and leaflet that can render to html (so I can zoom in/out and move around the map).

I see that using the heatmap request will get me an png. So, I will need the UTFGrids call and to have some sort of map server to display this, right? I’ll keep exploring.

Thanks!

1 Like

no. iNat serves up the map tiles. (those PNGs are the map tiles that iNat provides.) tmap or leaflet retrieves the appropriate map tiles and stitches them together as a map visualization when you’re using R. that version of my map page uses a HTML + Javascript version of leaflet to display the heatmap tiles over a Stamen basemap.

here’s the equivalent using leaflet in R, displaying the iNat heatmap tiles over a Carto DarkMatter basemap:

library(jsonlite)
library(leaflet)

reqparams <- "project_id=gbs-2024-uruguay"
bounds <- fromJSON(paste0("https://api.inaturalist.org/v1/observations?per_page=0&return_bounds=true&",reqparams))

leaflet() %>%
  fitBounds(lng1 = bounds$total_bounds$swlng, lat1 = bounds$total_bounds$swlat, lng2 = bounds$total_bounds$nelng, lat2 = bounds$total_bounds$nelat) %>%
  addProviderTiles("CartoDB.DarkMatter", group = "Carto Dark Matter") %>%
  addTiles(urlTemplate = paste0("https://api.inaturalist.org/v1/heatmap/{z}/{x}/{y}.png?",reqparams), group = "iNaturalist Observations Heatmap") %>%
  addLayersControl(
    baseGroups = c("Carto Dark Matter"),
    overlayGroups = c("iNaturalist Observations Heatmap")
  )

1 Like

Wow @pisum this is awesome!

In the end I included a map with the data, but it would be awesome to show this.
I’ll adapt my code to this.

It’s in Spanish, but here are the two examples: https://bienflorencia.github.io/LatinR2024/

Thanks a lot for your help :)

1 Like