在 shapefile 后面绘制栅格 [英] Plotting a raster behind a shapefile

查看:75
本文介绍了在 shapefile 后面绘制栅格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 shapefile 对象后面绘制光栅"对象?两者都可以自行绘制,但点不会绘制在栅格上:

How can I plot a "raster" object behind a shapefile object? Both plot fine on their own but the points don't plot over the raster:

require(rgdal)
require(maptools)
require(raster)

myproj = "+proj=utm +zone=12 +north +ellps=WGS84 +units=m"
shp = readShapeSpatial(fn.shp, proj4string = CRS(myproj))
ras = raster(fn.tif)

plot(ras)
plot(shp, bg="transparent", add=TRUE)

推荐答案

使用点、线和多边形重叠绘制栅格图应该可以正常工作,如下例所示.

Overplotting raster plots with points, lines, and polygons should work just fine, as the following example shows.

我最好的猜测是,您尝试在栅格顶部绘制的 Spatial* 对象落在要绘制的区域之外.您是否检查过 rasterSpatial* 对象是否在同一个 CRS 中,并且(假设它们是)边界框重叠?(即尝试 bbox(shp)bbox(ras),并比较结果).

My best guess would be that the Spatial* objects you are attempting to plot on top of the raster fall outside of the region being plotted. Have you checked that both the raster and Spatial* objects are in the same CRS, and (assuming they are) that the bounding boxes overlap? (i.e. try bbox(shp) and bbox(ras), and compare the results).

library(rgdal)
library(raster)
# Create a raster
ras <- raster(ncols=36, nrows=18)
ras[] <- runif(ncell(ras))
# Create a SpatialPoints object
shpPts <- spsample(Spatial(bbox=bbox(ras)), 20, type="random")
# Create a SpatialPolygons object
p1 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0))
shpPolys <- SpatialPolygons( list(Polygons(list(Polygon(p1)), 1)))

# Plot them, one layer after another
plot(ras)
plot(shpPts, pch=16, col="red", add=TRUE)
plot(shpPolys, col="yellow", add=TRUE)

这篇关于在 shapefile 后面绘制栅格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆