Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有sf地图和替代crs的背景图像_R_Sf - Fatal编程技术网

具有sf地图和替代crs的背景图像

具有sf地图和替代crs的背景图像,r,sf,R,Sf,是否可以将背景图像与sf地图一起使用,并将图像与使用中的坐标参考系进行转换?例如,下面的第一个图使用的是带有EPSG:4326的。第二幅图像是EPSG:3035。数据点似乎已正确绘制在CRS上,但背景图像未显示 CRS EPSG 4326:显示正确 CRS EPSG 3035:无背景图像 使用的代码: # Download NASA night lights image download.file("https://www.nasa.gov/specials/blackmarble/2016/g

是否可以将背景图像与sf地图一起使用,并将图像与使用中的坐标参考系进行转换?例如,下面的第一个图使用的是带有EPSG:4326的。第二幅图像是EPSG:3035。数据点似乎已正确绘制在CRS上,但背景图像未显示

CRS EPSG 4326:显示正确 CRS EPSG 3035:无背景图像 使用的代码:

# Download NASA night lights image
download.file("https://www.nasa.gov/specials/blackmarble/2016/globalmaps/
              BlackMarble_2016_01deg.jpg", 
              destfile = "BlackMarble_2016_01deg.jpg", mode = "wb")

# Load picture and render
earth <- readJPEG("BlackMarble_2016_01deg.jpg", native = TRUE)
earth <- rasterGrob(earth, interpolate = TRUE)

# Select crs as desired
crs.inuse = 4326  # WGS 84
crs.inuse = 3035  # ETRS89 / LAEA Europe

# Plot geoname localities with population >100k with ggplot    
ggplot() +
  annotation_custom(earth, xmin = -180, xmax = 180, ymin = -90, ymax = 90) +
  geom_sf(data = popn.sf,
          aes(geometry=geometry),
          alpha = 0.4,
          size = 2,
          colour = "white") +
  theme(panel.background = element_rect(fill = "#05050f", colour = "#05050f"), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.title = element_blank(), 
        axis.text = element_blank(), 
        axis.ticks.length = unit(0, "cm"),
        legend.position = "none") +
  coord_sf(crs = st_crs(crs.inuse))
#下载NASA夜光图像
下载文件(“https://www.nasa.gov/specials/blackmarble/2016/globalmaps/
BlackMarble_2016_01度jpg“,
destfile=“BlackMarble_2016_01deg.jpg”,mode=“wb”)
#加载图片并渲染

earth将
jpg
文件导入为
RasterBrick
并将其投影到所需的最终投影的解决方案。通过
绘图
功能创建最终绘图。我还绘制了海岸线,这样可以看到投影是如何与光栅砖一起工作的

你可以找到关于这项技术的进一步解释

#库----
图书馆(sf)
图书馆(光栅)
图书馆(rnaturalearth)#前往雷普雷克斯的城市和海岸
#加载形状文件----
#popm.sf
popn.sf=ne_下载(50,type=“populated_places”,returnclass=“sf”)
popn.sf_4326=st_变换(popn.sf,4326)
popn.sf_3035=st_变换(popn.sf_43263035)
#海岸
coast.sf=ne_下载(10,
category=“物理”,
type=“海岸线”,
returnclass=“sf”)
coast.sf_4326=st_变换(coast.sf,4326)
coast.sf_3035=st_变换(coast.sf,3035)
#背景----
#下载NASA夜光图片
下载文件(
"https://www.nasa.gov/specials/blackmarble/2016/globalmaps/BlackMarble_2016_01deg.jpg",
destfile=“BlackMarble_2016_01deg.jpg”,
mode=“wb”
)
#砌砖
世界
# Libraries----
library(sf)
library(raster)
library(rnaturalearth) #To get cities and coast to reprex

# Load shapefiles----
#popm.sf
popn.sf = ne_download(50, type = "populated_places", returnclass = "sf")
popn.sf_4326 = st_transform(popn.sf, 4326)
popn.sf_3035 = st_transform(popn.sf_4326, 3035)

#coasts
coast.sf = ne_download(10,
                       category = "physical",
                       type = "coastline",
                       returnclass = "sf")
coast.sf_4326 = st_transform(coast.sf, 4326)
coast.sf_3035 = st_transform(coast.sf, 3035)


# Background----
# Download NASA night lights image
download.file(
  "https://www.nasa.gov/specials/blackmarble/2016/globalmaps/BlackMarble_2016_01deg.jpg",
  destfile = "BlackMarble_2016_01deg.jpg",
  mode = "wb"
)

#To brick
earth <- brick("BlackMarble_2016_01deg.jpg")
raster_4326 <- earth
projection(raster_4326) <-
  CRS(st_crs(popn.sf_4326)[["proj4string"]])
extent(raster_4326) <-
  c(-180, 180,-89.99, 89.99) # Sligth offset to 180,180,-90,90 to avoid errors

#Project raster
proj3035 <- st_crs(popn.sf_3035)[["proj4string"]]
raster_3035 = projectRaster(raster_4326, crs = proj3035) # Some warnings, but still working

#Extra: to plots----
png(
  "proj4326.png",
  bg = "#05050f",
  height = dim(raster_4326)[1],
  width = dim(raster_4326)[2]
)
par(mar = c(0, 0, 0, 0))
plotRGB(raster_4326, bgalpha = 0)
plot(coast.sf_4326$geometry, col = "blue", add = T)
plot(
  popn.sf_4326$geometry,
  col = adjustcolor("white", alpha.f = .4),
  pch = 20,
  cex = 3,
  add = T
)
dev.off()

png(
  "proj3035.png",
  bg = "#05050f",
  height = dim(raster_3035)[1],
  width = dim(raster_3035)[2]
)
par(mar = c(0, 0, 0, 0))
plotRGB(raster_3035, bgalpha = 0)
plot(coast.sf_3035$geometry, col = "blue", add = T)
plot(
  popn.sf_3035$geometry,
  col = adjustcolor("white", alpha.f = .4),
  pch = 20,
  cex = 3,
  add = T
)
dev.off()