【问题标题】:shapefile and dataset aggregate into one by coordinatesshapefile 和 dataset 通过坐标聚合成一个
【发布时间】:2022-08-09 15:26:58
【问题描述】:

我有一个像这样的 shapefile (ms_shp)

Region Residuals Residuals 1 Residuals2 Residuals 3 geometry
1 32 2017 1 1 list(list(c(..)
2 540 2017 2 2 list(list(c(..)

并且是几何类型的多面体,并且具有 xy 维度并且是具有 xmin 和 ymin 的边界框。

我有另一个这样的数据集(ses)

gisid gx gy mean_dist bp2 sep sep_q
1 248779981 111999 785.8 0 9 21
2 249887771 244399 766.6 1 21 39

两个数据集继续。 shapefile 包含佛罗里达州的所有县和佛罗里达州每个坐标位置的数据集。我想将数据集汇总到佛罗里达州的县,以便每个坐标都位于该县。

就像这样,所有针对第一个县的 gisid 以及 bp2 和 ssep1 和 ssep1_q 的平均值。

Region mean(sep_q mean(sep Residuals2 Residuals 3 geometry
1 32 2017 1 1 list(list(c(..)
2 540 2017 2 2 list(list(c(..)

也许为了澄清我的问题,我希望位于这些区域之一的几何图形上的每个 gisid 与位于同一区域几何图形中的其他 gisid 的平均值聚合到该区域

    标签: r


    【解决方案1】:

    我目前正在研究类似的东西,包exactextractr 中的exact_extract 函数在处理时间和易用性方面运行良好。以下代码将根据每个多边形内的每个值计算变量 z 的平均值。

    library(exactextractr)
    library(raster)
    library(terra)
    
    
    #Read in the csv
    ses_csv <- read.csv(ses)
    
    #Cut it down to three columns where the first and second columns are x any y coords and the third is the z variable you want to investigate, use your method of choice for this.
    
    #Create a raster that is compatible with exactextract from ses
    ses_raster <- terra::rast(raster::rasterFromXYZ(ses_csv,crs='whatever the crs is for the data')
    
    #Read in the shapefile
    ms_shp <- terra::vect(ms_shp)
    
    #Calculate the mean z values for each polygon
        exact_extract(ses,ms_shp,'mean')
    

    【讨论】:

    • 我想要位于多边形上的 gisid,因此区域的几何形状聚合到具有相同区域中所有 gisid 的平均 ssep1 的区域
    • 我已经更新了答案以反映这一点,它现在包含对您需要做什么的解释,并且一旦您将自己的变量放入其中就应该起作用。
    【解决方案2】:

    我对您的问题的理解是,您想通过与点相交的多边形来汇总(汇总)与点相关的值。然后将这些作为新变量添加到多边形中。 使用“terra” 1.6-1 版(目前是开发版,您可以使用install.packages('terra', repos='https://rspatial.r-universe.dev') 安装它,您可以使用zonal 来安装它。

    示例数据(请始终包含一些)

    library(terra)
    # terra 1.6.1
    
    # example polygons
    f <- system.file("ex/lux.shp", package="terra")
    polys <- vect(f)[,c(2,4)]
    
    # example points 
    set.seed(1)
    pnts <- spatSample(polys, 100)
    values(pnts) <- data.frame(b2=1:100, ssep1=100:1)
    

    解决方案

    z <-  zonal(pnts, polys, mean, as.polygons=TRUE)
    z
    # class       : SpatVector 
    # geometry    : polygons 
    # dimensions  : 12, 4  (geometries, attributes)
    # extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
    # coord. ref. : lon/lat WGS 84 (EPSG:4326) 
    # names       :   NAME_1   NAME_2    b2 ssep1
    # type        :    <chr>    <chr> <num> <num>
    # values      : Diekirch Clervaux 51.14 49.86
    #               Diekirch Diekirch    28    73
    #               Diekirch  Redange 42.58 58.42
    

    【讨论】:

      猜你喜欢
      • 2013-04-04
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2013-11-27
      • 2018-07-08
      • 2017-12-27
      相关资源
      最近更新 更多