use*_*125 5 r raster google-earth
我无法弄清楚如何在R代码中计算两个(lat,lon)点之间的视线(LOS).任何关于如何处理这个问题的建议将不胜感激.我想使用R包 - 栅格 - 来读取地形高程数据.似乎可以利用spgrass包(基于http://grass.osgeo.org/grass70/manuals/r.viewshed.html),但我想避免加载GIS.谢谢.
如果您只想知道点A是否可以看到点B,那么从连接A到B的线中采样大量高程以形成地形轮廓,然后查看从A到B的直线是否与该轮廓形成的多边形相交.如果没有,则A可以看到B.编码非常简单.相反,您可以沿着从A到B的直线采样多个点,并查看它们中的任何一个是否具有低于地形高程的高程.
如果您有大量要计算的点,或者您的栅格非常详细,或者您想要计算从某个点可见的整个区域,则可能需要一段时间才能运行.
此外,除非您的数据位于地球的大部分区域,否则请转换为常规公制网格(例如UTM区域)并假设平坦的地球.
我不知道任何现有的包具有这个功能,但使用GRASS真的不是那么麻烦.
这是一些使用raster和的代码plyr:
cansee <- function(r, xy1, xy2, h1=0, h2=0){
### can xy1 see xy2 on DEM r?
### r is a DEM in same x,y, z units
### xy1 and xy2 are 2-length vectors of x,y coords
### h1 and h2 are extra height offsets
### (eg top of mast, observer on a ladder etc)
xyz = rasterprofile(r, xy1, xy2)
np = nrow(xyz)-1
h1 = xyz$z[1] + h1
h2 = xyz$z[np] + h2
hpath = h1 + (0:np)*(h2-h1)/np
return(!any(hpath < xyz$z))
}
viewTo <- function(r, xy, xy2, h1=0, h2=0, progress="none"){
## xy2 is a matrix of x,y coords (not a data frame)
require(plyr)
aaply(xy2, 1, function(d){cansee(r,xy,d,h1,h2)}, .progress=progress)
}
rasterprofile <- function(r, xy1, xy2){
### sample a raster along a straight line between two points
### try to match the sampling size to the raster resolution
dx = sqrt( (xy1[1]-xy2[1])^2 + (xy1[2]-xy2[2])^2 )
nsteps = 1 + round(dx/ min(res(r)))
xc = xy1[1] + (0:nsteps) * (xy2[1]-xy1[1])/nsteps
yc = xy1[2] + (0:nsteps) * (xy2[2]-xy1[2])/nsteps
data.frame(x=xc, y=yc, z=r[cellFromXY(r,cbind(xc,yc))])
}
Run Code Online (Sandbox Code Playgroud)
希望相当不言自明,但可能需要一些真实的文档.我用它制作了这个:

这是一个50米高的人可以在红点看到2米高的塔的地图.是的,当我运行它时,我得到了那些数字错误.在我4岁的电脑上跑了大约20分钟.我怀疑GRASS几乎可以立即做到这一点并且更正确.