[R-sig-Geo] Query for overlapping points in R

Roger Bivand Roger.Bivand at nhh.no
Sat Mar 24 18:27:58 CET 2007


On Fri, 23 Mar 2007, Katrina Bennett wrote:

> Hello, I am trying to query in a spatial data set where overlapping points
> exist between two data sets, and then average between these two data sets in
> R.
> 
> The problem is that my data sets have slightly difference spatial arrays
> - therefore when I go to subtract them, I get an error.
> 
> The data sets are set up as follows, columns of - Lat, Long, 10, 11, 12, 1,
> 2, 3, 4, 5, 6, 7, 8, 9 (which represent months in the year)
> 
> Here is the portion of my script that does this. I've played around with
> overlay and merge, but neither seems to do what I want to do - just extract
> and then calculate on the common data points between these two files.

Well, why not simply measure the distances between the points in the two
files, treating points within a small threshold as common? Use a small
threshold rather than equality to avoid numerical fuzz problems.
spDistsN1() in sp will do that iterated over points in one of the sets.
Using the two indices, you can build up your output. Please consider using
sp classes - messing around inside the deprecated Map class is not
necessarily robust.

By the way, what is the function clip() in your code below?

Roger

> 
> require(sp)
> 
> for (region in regions){
>           #Create an empty holder
>           monthly_variables = NULL
>           #Clip regions (have to examine maptools package)
> 
>           clip_variance <- clip(cbind(variances, io =
>             point.in.polygon(variances$Long, variances$Lat,
>             shape$shp$shp[[region]]$points$X, 
>             shape$shp$shp[[region]]$points$Y)))
> 
>           clip_normal <- clip(cbind(normals, io =
>             point.in.polygon(normals$Long, normals$Lat,
>             shape$shp$shp[[region]]$points$X, 
>             shape$shp$shp[[region]]$points$Y)))
> 
>           #HERE IS WHERE I WANT TO SELECT AND CALCULATE DIFFERENCES ONLY ON
> THE POINTS THAT MATCH in X, Y SPACE
>           # calculate differences, sd (in the time domain) for the 
> 
>             for (month in 1:12){
>              # generate anomalies
>               monthly_anom <- clip_variance[, 3:ncol(clip_variance)] -
>                 clip_normal[, 3:ncol(clip_normal)]
> 
>               monthly_variables <- rbind(monthly_variables, c(Lat = Lat,
>                 Long = Long, monthly_anom = monthly_anom))
>             }

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list