1

I've been reading through the documentation, but not getting anywhere with this question.

I have a data frame with three columns. The first two are gps coordinates, [such as 42.06, -70.19 for (Provincetown, Massachusetts, USA)], and the third column is a value for each of those coordinates.

That data frame is called forRaster

Here's what I have so far:

library(raster)
library(leaflet)
library(rgdal)

needsRaster = rasterFromXYZ(forRaster)
plot(needsRaster)
needsImage = image(needsRaster)
needsLeafletRaster = projectRasterForLeaflet(needsImage)

needsMap = leaflet()
addRasterImage(needsMap, needsLeafletRaster)
needsMap

But I keep getting this error after the projectRasterForLeaflet call: Error in raster::projectRaster(x, raster::projectExtent(x, crs = sp::CRS(epsg3857))) : input projection is NA

I find the leaflet documentation not entirely straightforward, and am wondering if anyone cal help. If I need to make the raster differently in any way, I'm happy to do so. Thanks.

Heliornis
  • 391
  • 5
  • 18

1 Answers1

3

I figured it out!

# Made a new data frame with lat, long, and the value
df = data.frame(value = v, lng = y, lat = x)

#Did this....
s = SpatialPixelsDataFrame(df[,c('lng', 'lat')], data = df)
crs(s) = sp::CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")

r = raster(s)

# Set up the colors
val = as.numeric(c(0:max(df$value)))
pal = colorNumeric(c("yellow", "orange", "red"), val,
                na.color = "transparent")

# Made the map
leaflet() %>% addProviderTiles("CartoDB.Positron") %>%
  addRasterImage(r, colors = pal, opacity = 0.5) %>%
  addLegend(pal = pal, values = val, title = "Number of Needs")
Heliornis
  • 391
  • 5
  • 18