代码之家  ›  专栏  ›  技术社区  ›  et is

定义路径宽度并计算面积

  •  2
  • et is  · 技术社区  · 7 年前

    x <- c(1,2,3,4,4,4,4,3,2,1)
    y <- c(1,2,3,4,3,2,1,2,3,4)
    z <- 1:length(x)
    df <- data.frame(x, y, z)
    

    我对动物的行为感兴趣,并想计算“探索的竞技场面积”的指标我不确定最好的方法是什么,但我最初的想法是从点创建一条分步路径,然后定义路径的缓冲区宽度以创建多边形,最后,计算多边形下的平面面积。从视觉上看,我可以想象如下:

    plot(x, y, type = "o", lwd = 20)
    

    enter image description here

    我对这样的空间数据没有经验,但我“认为”我可以光栅化这条路径?不幸的是 lwd

    我更喜欢使用R而不是GIS软件。

    2 回复  |  直到 7 年前
        1
  •  2
  •   www    7 年前

    我不是研究动物路径的专家,但我认为这是一个好主意。

    sf 包,这是下一代标准的类和方法来分析空间数据的R。

    c(0, 1) c(1, 1) . 然后,我们将创建一个距离等于1的缓冲区。最后,我们将计算缓冲区的面积。

    library(sf)
    
    # Create a simple feature object based on df
    ps <- as.matrix(rbind(c(0, 1), c(1, 1)))
    ls <- st_linestring(ps)
    # View the line
    plot(ls)
    

    enter image description here

    # Create a buffer zone
    ls_buffer <- st_buffer(ls, dist = 1)
    
    # View ls_buffer
    plot(ls_buffer)
    

    enter image description here

    # Calculate the area
    st_area(ls_buffer)
    [1] 5.140157
    

    st_area 作用缓冲区的形状类似于宽度为1、高度为2的矩形加上半径为1的圆。我们可以使用以下代码计算面积。结果与我们从 st_地区

    2 * 1 + 1 * 1 * pi
    [1] 5.141593
    

    现在,很容易为您的示例修改上述代码。我将缓冲距离设置为0.1,但需要确定合理的缓冲距离。

    # Create example data frame
    x <- c(1,2,3,4,4,4,4,3,2,1)
    y <- c(1,2,3,4,3,2,1,2,3,4)
    z <- 1:length(x)
    df <- data.frame(x, y, z)
    
    # Create a simple feature object based on df
    ps <- as.matrix(df[, c("x", "y")])
    ls <- st_linestring(ps)
    # View the line
    plot(ls)
    

    enter image description here

    # Create a buffer zone
    ls_buffer <- st_buffer(ls, dist = 0.1)
    
    # View ls_buffer
    plot(ls_buffer)
    

    enter image description here

    # Calculate the area
    st_area(ls_buffer)
    [1] 2.263725
    

    旧金山 https://cran.r-project.org/web/packages/sf/index.html )如果你想了解更多,请访问这些小插曲。

        2
  •  1
  •   Jul    7 年前

    library(sp)
    library(rgeos)
    library(dplyr)
    
    #Convert points to path (SpatialLines object)    
    SOlines <- Line(coordinates(df[,1:2])) %>% list() %>% Lines(ID="test") %>% list() %>% SpatialLines()
    #Buffer the path into a polygon
    area <- rgeos::gBuffer(SOlines,width = .1, byid = T) 
    #Calculate the area within the Polygon
    rgeos::gArea(area,byid=T)
            test 
        2.262897
    

    这个 sf 包(如ycw的回答中所示)有一种更直接的创建线路路径的方法。我还不太熟悉这个软件包,但我现在会继续回答这个问题,因为使用SpatialLines方法的一个好处是,可以在一个对象中存储多条线(ID),然后在一次调用中计算所有面积 gArea