Home > Enterprise >  shapefile and dataset aggregate into one by coordinates
shapefile and dataset aggregate into one by coordinates

Time:07-13

I have a shapefile like this (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(..)

and is geometry type multipolygon and has de dimension xy and is boundyîng box with xmin and ymin .

I have another dataset like this (ses)

gisid geox geoy mean_dist bp2 ssep1 ssep1_q
1 248779981 111999 785.8 0 9 21
2 249887771 244399 766.6 1 21 39

both datasets continues. The shapefile contains all counties of FLorida and the dataset every coordinates place in Florida. I would llike to summarise the dataset into counties in Florida so that every coordinates which will lie on the county.

Like this that all gisids that are for the first county and also the mean of bp2 and ssep1 and ssep1_q.

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

Maybe to clarify my question, I would like that every gisid that lies on the geometry of one of these regions, aggregate to this region with the mean of other gisid's that are in the same regions geometry

CodePudding user response:

I'm currently working on something similar, the function exact_extract from the package exactextractr has worked very well in terms of processing time and ease of use. The following code will calculate the mean of variable z from every value within each polygon.

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')

CodePudding user response:

My understanding of your question is that you want to summarize (aggregate) values associated with points by the polygons that they intersect with. And then add these as new variables to the polygons. With "terra" version 1.6-1 (currently the development version, you can install it with install.packages('terra', repos='https://rspatial.r-universe.dev') you can use zonal for that.

Example data (please always include some)

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)

Solution

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
  •  Tags:  
  • r
  • Related