0

I'm trying to spatial join/merge two spatial points data frames in R. One data frame is lacking IDs, which is the information that needs to be joined to the other data frame. To be specific, any point from data frame 1 that falls within a 1500 m buffer of a point in data frame 2 should be joined with the respective ID from data frame 2. Here's some code to illustrate my problem:

library(mapview)
library(sp)
library(rgdal)
library(rgeos)

lat1 <- c(4417391,4517826, 4435680,4509372,4449390)
long1 <- c(5557780,5358439,5328731,5323168,5519670) 
attrib1 <- seq(6,10,1)
attrib2 <- seq(11,15,1)
df1 <- data.frame(lat1,long1,attrib1,attrib2)

lat2 <- c(4394822,4488830,4417257,4517995,4435679)
long2 <- c(5293795,5418630,5557927,5358272,5328084)
ID <- seq(1,5,1)
df2 <- data.frame(lat2,long2,ID)

coordinates(df1) <- ~lat1+long1
proj4string(df1) <- CRS("+init=EPSG:31468")

coordinates(df2) <- ~lat2+long2
proj4string(df2) <- CRS("+init=EPSG:31468")

buff <- gBuffer(df2,width=1500)
df1_buff <- df1[buff,]

mapview(buff) + mapview(df1,color="red") + mapview(df1_buff,color="green")

I have now selected the points from df1 that lie within the buffers of df2. However, the buffer obviously does not contain IDs and I'm at a loss about how to merge this infromation. I have looked into over and point.in.poly, but with no success. This is the result I'm looking for:

attrib1 attrib2 lat1     long1    ID
6       11      4417391  5557780  1
7       12      4517826  5358439  2
8       13      4435680  5328731  3 

migrated from stackoverflow.com Aug 10 '18 at 10:36

This question came from our site for professional and enthusiast programmers.

  • Have you try function extract from raster package? – Hugo Aug 9 '18 at 9:06
  • Thanks, looks promising, but I'm having difficulties converting my data to rater format. – M.Teich Aug 9 '18 at 9:30
  • 1
    I guess you can do what you want using raster::extract of sp::over, but doing a buffer by id, like buff <- gBuffer(df2,width=1500,byid=TRUE) – Hugo Aug 9 '18 at 9:47
  • Including the byid=TRUE argument in gBuffer transfers the ID column to the new buffer polygon object. However, over still only gives a df with NAs – M.Teich Aug 9 '18 at 10:07
1

Is this what you are looking for? I just:

  • added one line of code right after proj4string(df1) <- CRS("+init=EPSG:31468")
  • added three lines of code at the end

And this is the code:

library(mapview)
library(sp)
library(rgdal)
library(rgeos)

lat1 <- c(4417391,4517826, 4435680,4509372,4449390)
long1 <- c(5557780,5358439,5328731,5323168,5519670) 
attrib1 <- seq(6,10,1)
attrib2 <- seq(11,15,1)
df1 <- data.frame(lat1,long1,attrib1,attrib2)

lat2 <- c(4394822,4488830,4417257,4517995,4435679)
long2 <- c(5293795,5418630,5557927,5358272,5328084)
ID <- seq(1,5,1)
df2 <- data.frame(lat2,long2,ID)

coordinates(df1) <- ~lat1+long1
proj4string(df1) <- CRS("+init=EPSG:31468")
df1@data<-cbind(df1@data,lat1,long1)

coordinates(df2) <- ~lat2+long2
proj4string(df2) <- CRS("+init=EPSG:31468")

buff <- gBuffer(df2,width=1500,byid=TRUE)
df1_buff <- df1[buff,]

mapview(buff) + mapview(df1,color="red") + mapview(df1_buff,color="green")


library(raster)
o<-over(df1_buff,buff)
df1_buff@data<-cbind(df1_buff@data,o)
df1_buff@data
  • Thanks, that works for me! However, there are two things I don't understand: why does over(df1_buff,buff) work and over(df1_buff,df2) produce NAs? Why does over(df1_buff,buff) only give the ID column and loses the attrib1 and attrib2 columns from df1_buff? Seems odd to have to write those back into the @data slot via cbind. – M.Teich Aug 9 '18 at 16:46
  • 1
    over(df1_buff,df2) produces NAs as both df1_buff and df2 are points and do not overlap. As for the second question, don't know to give you an answer, I guess it was the way the function was written... – Hugo Aug 10 '18 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.