Dorothy
Dorothy

Reputation: 563

iterate SpatialPoints and SpatialPolygonsDataFrame merge in R

I am trying to iterate thru the process of creating centroids of a list of SpatialPolygonsDataFrames (each with several polygon features within), with the resulting SpatialPoints preserving the attributes (data) of the parent polygons. I have tried the sp::over function, but it seems to be problematic because centroids do not necessarily overlap with parent polygons. Furthermore, I am new to coding/R and do not know how to achieve this in a for loop and/or using an apply function.

So specifically, I need to (1) find a different function that will link the SpatialPolygonsDataFrames with the associated SpatialPoints (centroids); and (2) iterate thru the entire process and merge the SpatialPolygonsDataFrames data with the appropriate SpatialPoints - I do not know how to match the index value of one list to the index value in another while running a loop.

Here is a reproducible example for a single SpatialPolygonsDataFrames object showing that using sp::over doesn't work because some centroids do not end up overlapping parent polygons:

library(maptools)  ## For wrld_simpl
library(sp)

## Example SpatialPolygonsDataFrames (SPDF)
data(wrld_simpl) #polygon of world countries
spdf1 <- wrld_simpl[1:25,] #country subset 1
spdf2 <- wrld_simpl[26:36,] #subset 2
spdf3 <- wrld_simpl[36:50,] #subset 3

spdfl[[1]]@data


#plot, so you can see it
plot(spdf1)    
plot(spdf2, add=TRUE, col=4) 
plot(spdf3, add=TRUE, col=3) 

#make list of SPDF objects
spdfl<-list()
spdfl[[1]]<-spdf1
spdfl[[2]]<-spdf2
spdfl[[2]]<-spdf3

#create polygon centroids for each polygon feature (country) within spdfl[[1]]
spdf1c <- gCentroid(spdfl[[1]], byid=TRUE)
plot(spdfl[[1]])
plot(spdf1c, add=TRUE)

#add attributes 'NAME' and 'AREA' to SpatialPoints (centroid) object from SPDF data
spdf.NAME <- over(spdf1c, spdfl[[1]][,"NAME"])
spdf.AREA <- over(spdf1c, spdfl[[1]][,"AREA"])
spdf1c$NAME <- spdf.NAME
spdf1c$AREA <- spdf.AREA

spdf1c@data   
#`sp::over` output error = name and area for ATG, ASM, BHS, and SLB are missing

Upvotes: 1

Views: 1027

Answers (1)

Mwatch
Mwatch

Reputation: 36

I find SF is a great package for working with spatial data in R. I have fixed a few typos and added the right for loop below.

library(maptools)  ## For wrld_simpl
library(sp)
library(sf)


## Example SpatialPolygonsDataFrames (SPDF)

data(wrld_simpl) #polygon of world countries
spdf1 <- wrld_simpl[1:25,] #country subset 1
spdf2 <- wrld_simpl[26:36,] #subset 2
spdf3 <- wrld_simpl[36:50,] #subset 3

spdfl[[1]]@data


#plot, so you can see it
plot(spdf1)    
plot(spdf2, add=TRUE, col=4) 
plot(spdf3, add=TRUE, col=3) 

#make list of SPDF objects
spdfl<-list()
spdfl[[1]]<-spdf1
spdfl[[2]]<-spdf2
spdfl[[3]]<-spdf3

# Empty List for Centroids
centres <-list()

# For Loop
for (i in 1:length(spdfl)) {
  centres[[i]] <- spdfl[[i]] %>% 
    sf::st_as_sf() %>%          # translate to SF object
    sf::st_centroid() %>%       # get centroids 
    as(.,'Spatial')   # If you want to keep as SF objects remove this line

}


#Plot your Spatial Objects
plot(spdfl[[1]])
plot(centres[[1]], add=TRUE)

Here is a solution which uses SF and sp. SF is great as it keeps things as dataframes which can easy to deal with. More information here: https://r-spatial.github.io/sf/

Upvotes: 1

Related Questions