r - 在R中按高程多边形过滤空间数据帧
问题描述
我有一个阿尔卑斯山的数字高程模型(包含 x、y、z)。我想提取代表两个海拔高度(<=1000,>1000)的多边形。需要将多边形应用于具有不同 x、y 点 (x,y,snowdepth) 的更精细的阿尔卑斯山配准。必须对 1000m 以上的所有点进行第二次注册过滤。所以。如果我可以存储多边形并将其用于不同的空间数据帧,那就太好了。我的问题是:如何捕获表示 1000m 以上高程的多边形并过滤 R 中的空间数据框。
#packages
packages <- c("RCurl", "RColorBrewer","ggmap","rgeos","fields","dismo","rgdal", "deldir", "dplyr","tidyr","ggplot2","contoureR",
"maptools","raster","gstat", "magick","lubridate", "ggplot2","viridis","scales","inlmisc")
has_available <- packages %in% rownames(installed.packages())
if(any(!has_available)) install.packages(packages[!has_available])
lapply(packages,library,character.only = TRUE)
for(pkg in packages) {
library(pkg, character.only = TRUE)
}
geo_proj = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
alps <- shapefile("Alpine_Convention_Perimeter_2018.shp")
proj4string(alps)
bbalps <- bbox(alps)
alps <- spTransform(alps,geo_proj)
r <- raster(alps,ncols=990,nrows=990)
srtm <- getData('SRTM', lon=15, lat=47)
srtm2 <- getData('SRTM', lon=12, lat=47)
srtm3 <- getData('SRTM', lon=9, lat=47)
srtm4 <- getData('SRTM', lon=6, lat=45)
#Mosaic/merge srtm tiles
srtmmosaic <- mosaic(srtm, srtm2, srtm3, srtm4, fun=mean)
srtmmosaic <- spTransform(srtmmosaic,geo_proj)
rm(srtm, srtm2, srtm3, srtm4)
rst0 <- projectRaster(srtmmosaic, r, crs=geo_proj)
#set the z-factor, which increases contrast
mosaic <- rst0 * 10
#then create the hillshade
slope <- terrain(mosaic, opt="slope", unit='radians')
aspect <- terrain(mosaic, opt="aspect", unit='radians')
hillshade <- hillShade(slope, aspect, angle=45, direction=315)
hillshade <- crop(hillshade, extent(alps))
hillshade <- mask(hillshade, alps)
slope <- crop(slope, extent(alps))
slope <- mask(slope, alps)
rst0 <- crop(rst0, extent(alps))
rst0 <- mask(rst0,alps)
#elevation
elev.df <- rasterToPoints(rst0)
elev.df <- data.frame(elev.df)
colnames(elev.df) <- c("lon", "lat", "elevation")
#ggplot makes me turn the raster into points
hills.df <- rasterToPoints(hillshade)
hills.df <- data.frame(hills.df)
colnames(hills.df) <- c("lon", "lat", "hills")
#merging the slope shade with the hillshade
slope.df <- rasterToPoints(slope)
slope.df <- data.frame(slope.df)
colnames(slope.df) <- c("lon", "lat", "slope")
slope.df$slope <- 1- slope.df$slope #invert the scale so that more slope is darker
#elevation normalised
elev.df$val_norm <- (elev.df[,3]-min(elev.df[,3]))/diff(range(elev.df[,3]))
mnt.df<-hills.df %>%
left_join(slope.df) %>%
full_join(elev.df)
df = getContourLines(mnt.df, binwidth=1000)
ggplot(df,aes(x,y,group=Group,colour=z)) + geom_path()
解决方案
推荐阅读
- c# - 即使数据库为空,保存数据库时也会出现重复实体错误
- excel - 如何使用 VBA 在一系列单元格上用 x 的函数替换 x
- java - 更改 txt 文件中列的顺序
- c# - 是否可以将pdf文件存储在mysql数据库中?
- java - 使用 Eclipse 和 itextpdf 时,并非所有字段都以 PDF 显示
- android - android手游如何连接redis?
- java - Number_format_Exception
- docker - docker.sock 通过 ssh 隧道转发返回“找不到页面”
- ms-access - 将一个文本字段中的信息添加到另一个文本字段而不删除已存在的信息
- cmd - 如何使用命令行 HeadlessChrome(非硒)发送 POST 请求