【问题标题】:How to correctly add map to raster image in R如何正确地将地图添加到 R 中的光栅图像
【发布时间】:2018-04-28 00:26:26
【问题描述】:

我正在尝试绘制海面温度数据并添加陆地的彩色图像,以免数据与 NAs 混淆。我尝试了多种方法,但正如您将在下图中看到的那样,地图相对于数据没有正确排列。

为了使这个问题可重现,这里是一个指向我正在使用的文件的保管箱的链接:https://www.dropbox.com/s/e8pwgmnhvw4s0nf/sst.nc4?dl=0

这是我目前开发的代码;

library(ncdf4)
library(raster)
library(mapdata)
library(mapproj)
library(rgeos)
library(ggplot2)

通过 ncdf4、rasterToPoints、map_data 和 ggplot2

eight = nc_open("Downloads/sst.nc4")
sst = ncvar_get(eight, "sst")
sst = raster(sst)
sst = t(flip(sst, 1)) # have to orient the data properly

# extract the dimensions and set the extent
lat.min = min(eight$dim$lat$vals)
lat.max = max(eight$dim$lat$vals)
lon.min = min(eight$dim$lon$vals)
lon.max = max(eight$dim$lon$vals)
sst = setExtent(sst, ext = c(lon.min, lon.max, lat.min, lat.max))

# provide proper projection
crs(sst) = "+init=epsg:4326"

# convert raster to points
sst.p <- rasterToPoints(sst)
df <- data.frame(sst.p)
colnames(df) <- c("Longitude", "Latitude", "sst")
usa = map_data("usa")
ggplot(data=df, aes(y=Latitude, x=Longitude)) +
  geom_raster(aes(fill=sst)) +
  theme_bw() +
  coord_equal() +
  scale_fill_gradient("SST (Celsius)", limits=c(0,35)) +
  geom_polygon(data = usa, aes(x=long, y = lat, group = group)) + 

  theme(axis.title.x = element_text(size=16),
        axis.title.y = element_text(size=16, angle=90),
        axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "right",
        legend.key = element_blank()
  )

通过 Raster、maptools 数据、SP 变换和基础绘图

#read in the data
sst = raster("Downloads/sst.nc4",  varname = "sst", stopIfNotEqualSpaced=FALSE)

# get world map data
data("wrld_simpl", package="maptools")

## Crop to the desired extent, then plot
newext <- c(lon.min, lon.max, lat.min, lat.max)
out <- crop(wrld_simpl, newext)

#transform to proper CRS
out = spTransform(out, "+init=epsg:4326")

#plot
plot(out, col="khaki", bg="azure2")
plot(sst, add = T)

-我用于这个空间数据的投影是EPSG:4326

-这是指示sst.nc4 输出投影的 XML sn-p

<crs>PROJCS["Mercator_1SP / World Geodetic System 1984",
         GEOGCS["World Geodetic System 1984",
         DATUM["World Geodetic System 1984",
         SPHEROID["WGS 84", 6378135.0, 298.257223563, AUTHORITY["EPSG","7030"]],
         AUTHORITY["EPSG","6326"]],
         PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]],
         UNIT["degree", 0.017453292519943295],
         AXIS["Geodetic longitude", EAST],
         AXIS["Geodetic latitude", NORTH]],
         PROJECTION["Mercator_1SP"],
         PARAMETER["latitude_of_origin", 0.0],
         PARAMETER["central_meridian", 0.0],
         PARAMETER["scale_factor", 1.0],
         PARAMETER["false_easting", 0.0],
         PARAMETER["false_northing", 0.0],
         UNIT["m", 1.0],
         AXIS["Easting", EAST],
         AXIS["Northing", NORTH]]</crs>

我还尝试将map() 函数与mapprojprojection 参数一起使用,但它似乎没有伪墨卡托投影作为选项。

【问题讨论】:

  • 你能在 arcmap 或 Qgis 中正确获取它吗?在我看来,你有投影问题。您是否从 AQUA/MODIS 获取数据?
  • @Masoud 我没有尝试过 arcmap 或 Qgis。你知道这些包的教程吗?这是 AQUA/MODIS 数据。
  • 它们不是 R 包。它们是 gis 软件。 NASA 有一个名为 panoply(或类似名称)的软件。安装它并将您的 netcdf 文件加载到其中。如果可行,那么您需要更改 R 中底图的投影以获得所需的输出。

标签: r geospatial projection satellite-image


【解决方案1】:

这个有点混乱。通常最简单的方法是

sst = raster("sst.nc4",  varname = "sst")

但是,对于这个文件,它给出了这个错误:

"cells are not equally spaced; you should extract values as points"

那么让我们这样做吧:

library(ncdf4)
library(raster)
library(maptools)

d <- nc_open("sst.nc4")
sst <- ncvar_get(d, "sst")
lon <- ncvar_get(d, "lon")
lat <- ncvar_get(d, "lat")
nc_close(d)

xy <- cbind(rep(lon, length(lat)), rep(lat, each=length(lon)))

合并并移除 NA 值(大约一半的单元格...

xyv <- na.omit(cbind(xy, as.vector(sst)))

设置分辨率足以满足您的目的的 RasterLayer,并对点进行栅格化

 r <- raster(extent(range(lon), range(lat)), res=1/6)
 r <- rasterize(xyv[, 1:2], r, xyv[,3], fun=mean) 

剧情

data(wrld_simpl)
w <- crop(wrld_simpl, r)

plot(r)
plot(w, col='gray', add=TRUE)

【讨论】:

  • 这行得通!这种方法在不省略 NA 的情况下仍然有效吗?如果可能的话,我想保留 NA。我最初的编辑尝试没有成功
  • 您可以保留 NA,但这不会改变结果。
  • 是因为光栅化似乎具有某种平均填空功能吗?
猜你喜欢
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多