Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NASA tiles with leaflet in R

Tags:

r

leaflet

I would like to ask some help with regard to the leaflet package. When you draw an interactive map, you can do something like this.

library(leaflet)
library(magrittr)

m <- leaflet() %>% 
     setView(lng = -71.0589, lat = 42.3601, zoom = 8) %>%
     addTiles() 

m

If you want to add a third-party tile, you can do that too. The following link offers options for third-party tiles (http://leaflet-extras.github.io/leaflet-providers/preview/index.html) The following image is created with OpenWeatherMap.Precipitation.

### They work
m %>% addProviderTiles("MtbMap")
m %>% addProviderTiles("HikeBike.HikeBike")
m %>% addProviderTiles("OpenWeatherMap.Precipitation")

enter image description here

Some of the tile options in the link include tiles by NASA. I wanted to use one of them. So I tried the following codes. None of them unfortunately worked.

### The default map appears, then a black layer appears on top of the default layer.
m %>% addProviderTiles("NASAGIBS.ModisTerraTRUEColorCR")
m %>% addProviderTiles("NASAGIBS.ModisTerraBands367CR")

The only option which is working is the following.

m %>% addProviderTiles("NASAGIBS.ViirsEarthAtNight2012")

enter image description here

My next attempt was to use custom URL template with addTiles(). The URL is from the link above. But, this was not successful either; no error message appeared, but no change in tile.

m %>%addTiles(urlTemplate = "http://map1.vis.earthdata.nasa.gov/wmts-webmerc/MODIS_Terra_CorrectedReflectance_Bands367/default/{time}/{tilematrixset}{maxZoom}/{z}/{y}/{x}.{format}",
              tileOptions(minZoom = 1, maxZoom = 8))

My final attempt was the following. This showed the default map, but an additional tile did not appear either.

leaflet() %>%
addTiles() %>%
setView(lng = -71.0589, lat = 42.3601, zoom = 8) %>%
addTiles(urlTemplate = "http://map1.vis.earthdata.nasa.gov/wmts-webmerc/MODIS_Terra_CorrectedReflectance_Bands367/default/{time}/{tilematrixset}{maxZoom}/{z}/{y}/{x}.{format}",
         tileOptions(minZoom = 1, maxZoom = 8))

My question is whether this is a potential bug specifically with NASA tiles. Alternatively, what revision do I need in these scripts? Thank you for your help in advance.

UPDATE

I found a website which uses same NASA tiles. I specified NASAGIBS.ModisTerraTRUEColorCR and got the following image. The image is showing how a mail traveled from the US to Sweden. As you see, there is no image for both US and Europe. I think this could be the reason why I saw a black tile. I would like to know if anybody knows some details of NASA tiles. I chose the area which I can see the NASA image. But, I had no luck.

### I expected to see Japan area this time.
foo <- leaflet() %>% 
       setView(lng = 137.37, lat = 35.93, zoom = 5) %>%
       addTiles() 

foo %>% addProviderTiles("NASAGIBS.ModisTerraTRUEColorCR")

enter image description here

UPDATE 2

Today, I gave one more shot. At this moment, I managed to get the following image. I zoomed out a bit when I captured it. In UPDATE, I provided a map which you cannot see the US and Europe. In the new image, you see West coast of the States is in black. Given all observations, it seems to me that one may not get NASA images of a location all the time. Depending on when you ask NASA tiles, you may/may not have an image you want.

m <- leaflet() %>% 
     setView(lng = -71.0589, lat = 42.3601, zoom = 8) %>%
     addTiles()

m %>% addProviderTiles("NASAGIBS.ModisTerraBands367CR")

enter image description here

like image 848
jazzurro Avatar asked Aug 23 '15 06:08

jazzurro


1 Answers

Your final conclusion is correct: depending on what location you request imagery for and on the time of the request, the satellite may or may not yet have acquired the image. So you may get an actual image or just an empty one. (This is also stated in the GIBS API documentation.)

However, you can specify what day to request the image for via the 'time' option for addProviderTiles(). By specifying a date in the near past, you can get non-empty images for all locations if that is what you prefer.

This is the syntax:

> library(leaflet)
> library(magrittr)

> m <- leaflet() %>% 
       setView(lng = 4.5, lat = 51, zoom = 1) %>%
       addTiles() %>% 
       addProviderTiles("NASAGIBS.ModisTerraTrueColorCR",
                        options = providerTileOptions(time = "2015-08-31", opacity = 0.5))

> m

At the time of writing (2015-08-31) I get this result:

Specified today's date (or no date). Some images are not yet acquired.

It's cloudy in England, who would have guessed?

Most data happens to be there already, but there is no imagery for Alaska yet. If on the other hand, I specify yesterday's date

options = providerTileOptions(time = "2015-08-30", opacity = 0.5)

we get the full image:

Specified a date in the past. Imagery everywhere alreadycacquired.

Finally, the reason why

m %>% addProviderTiles("NASAGIBS.ModisTerraTRUEColorCR")

didn't work was probably because of a typo. It should be

m %>% addProviderTiles("NASAGIBS.ModisTerraTrueColorCR")
like image 139
WhiteViking Avatar answered Nov 06 '22 07:11

WhiteViking