在 R 中为 Leaflet 创建和显示栅格
Create and display a raster for Leaflet in R
我一直在通读文档,但对这个问题一无所知。
我有一个包含三列的数据框。前两个是 gps 坐标,[例如 42.06,-70.19(美国马萨诸塞州普罗文斯敦)],第三列是每个坐标的值。
那个数据框叫做forRaster
这是我目前的情况:
library(raster)
library(leaflet)
library(rgdal)
needsRaster = rasterFromXYZ(forRaster)
plot(needsRaster)
needsImage = image(needsRaster)
needsLeafletRaster = projectRasterForLeaflet(needsImage)
needsMap = leaflet()
addRasterImage(needsMap, needsLeafletRaster)
needsMap
但是在 projectRasterForLeaflet
调用之后我一直收到这个错误:
Error in raster::projectRaster(x, raster::projectExtent(x, crs = sp::CRS(epsg3857))) :
input projection is NA
我发现传单文档并不完全简单,想知道是否有人需要帮助。如果我需要以任何方式制作不同的光栅,我很乐意这样做。谢谢
我想通了!
# 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")
我一直在通读文档,但对这个问题一无所知。
我有一个包含三列的数据框。前两个是 gps 坐标,[例如 42.06,-70.19(美国马萨诸塞州普罗文斯敦)],第三列是每个坐标的值。
那个数据框叫做forRaster
这是我目前的情况:
library(raster)
library(leaflet)
library(rgdal)
needsRaster = rasterFromXYZ(forRaster)
plot(needsRaster)
needsImage = image(needsRaster)
needsLeafletRaster = projectRasterForLeaflet(needsImage)
needsMap = leaflet()
addRasterImage(needsMap, needsLeafletRaster)
needsMap
但是在 projectRasterForLeaflet
调用之后我一直收到这个错误:
Error in raster::projectRaster(x, raster::projectExtent(x, crs = sp::CRS(epsg3857))) :
input projection is NA
我发现传单文档并不完全简单,想知道是否有人需要帮助。如果我需要以任何方式制作不同的光栅,我很乐意这样做。谢谢
我想通了!
# 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")