【问题标题】:SpatialLines instead of segments (spplot)空间线而不是线段 (spplot)
【发布时间】:2015-04-05 01:36:33
【问题描述】:

我想使用spplot + sp.lines(格)而不是plot + segments。你知道实现这一点的简单方法吗,例如R: Calculating the shortest distance between two point layers

library(dismo)  
require(rgdal)
require(FNN)

laurus <- gbif("Laurus", "nobilis")
locs <- subset(laurus, !is.na(lat) & !is.na(lon),
               select = c("country", "lat", "lon"))

locs.uk  <- subset(locs, locs$country=="United Kingdom")
locs.ire <- subset(locs, locs$country=="Ireland")

uk_coord <- SpatialPoints(locs.uk[,c("lon","lat")])
ire_coord <- SpatialPoints(locs.ire[,c("lon","lat")])
crs.geo<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")  
proj4string(uk_coord) <- crs.geo 
proj4string(ire_coord) <- crs.geo 

uk_coord  <- spTransform(uk_coord, CRS("+init=epsg:27700"))
ire_coord <- spTransform(ire_coord, CRS("+init=epsg:27700"))


g = get.knnx(coordinates(uk_coord), coordinates(ire_coord),k=1)

可视化这个

plot(uk_coord, col=2, xlim=c(-1e5,6e5))
plot(ire_coord, add=TRUE)
segments(coordinates(ire_coord)[,1], 
         coordinates(ire_coord)[,2], 
         coordinates(uk_coord[g$nn.index[,1]])[,1], 
         coordinates(uk_coord[g$nn.index[,1]])[,2])

可能会转换成类似的东西

ire <- list("sp.points", ire_coord)

spplot(uk_coord, sp.layout=list(ire))

但是有没有一种简单的方法可以将segments 转换为SpatialLineslist("sp.lines", Lines(...))

【问题讨论】:

  • 注意:以前版本的 pkg:dismo 的用户应该在运行此代码之前更新。 (并且我在 ire_coord 分配中收到关于非有限坐标的错误。)
  • 需要在创建 locs.ire 之后多加一行:locs.ire &lt;- locs.ire[ with(locs.ire, !is.na(lat)&amp;!is.na(lon)), ]

标签: r geospatial lattice sp


【解决方案1】:

lattice-package 中尝试panel.segments()

library("lattice")
spplot(rbind(uk_coord, ire_coord), auto.key=FALSE,
       panel=function(...) {
         panel.xyplot(...)
         panel.segments(coordinates(ire_coord)[,1], 
                        coordinates(ire_coord)[,2],
                        coordinates(uk_coord[g$nn.index[,1]])[,1],
                        coordinates(uk_coord[g$nn.index[,1]])[,2])
       })

【讨论】:

  • 谢谢,比起经常使用的sp.layout=list(...) 样式,我更喜欢这种方式
【解决方案2】:

理解面板函数比在spplot 中依赖sp.layout 更强大——直接使用latticegrid 函数也是如此。 sp.layout 的解决方案可能如下所示:

spplot(uk_coord, auto.key=FALSE, col.regions = 'black', 
  sp.layout = list(ire, 
    list("panel.segments", 
        coordinates(ire_coord)[,1],
        coordinates(ire_coord)[,2],
        coordinates(uk_coord[g$nn.index[,1]])[,1],
        coordinates(uk_coord[g$nn.index[,1]])[,2])), 
    xlim = c(-140000,700000))

注意它不限于sp.lines等函数;在即将发布的 sp 1.1-0 中,也可以省略函数名的引号。

spplot 默认尝试用颜色绘制特征的属性,这在这里没有意义,所以你基本上想要的是一个具有受控纵横比的xyplot (asp="iso")。

【讨论】:

    猜你喜欢
    • 2012-10-21
    • 2016-03-27
    • 2014-10-20
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    相关资源
    最近更新 更多