Skip to content

spplot point coordinates

6 messages · Maxime Pauwels, Barry Rowlingson, Paul Hiemstra +1 more

#
Dear all,

I think there is a function in the sp package to get the coordinates of 
a point on a sp plot (~equivalent to "locator").
Could someone remember me its name?

Many thanks,

max
#
Hi Maxime,

You need the grid.locator() function from the grid package in 
combination with the trellis.focus() command from the lattice pacakge. 
The following function I wrote has a syntax similar to spplot but allow 
the user to click screen. The output of the function is either a 
SpatialPoints object if attach_data is FALSE or a SpatialPointsDataFrame 
otherwise, the data is extracted from spatial_object using overlay(). 
This function only works if spatial_object is a grid or a polygon. An 
example script plus the code for the function:

library(sp)
data(meuse.grid)
gridded(meuse.grid) = ~x+y
# Be sure to paste the function into the session before using this command
CreateSpatialPointsFromGraphicDevice(meuse.grid, "dist")

CreateSpatialPointsFromGraphicDevice = function(spatial_object, ..., 
proj4string = NA, attach_data = TRUE) {
        require(grid)
        require(lattice)
        cat("Exit with right mouse-click...\n")
        print(spplot(spatial_object, ...))
        trellis.focus()
        x_list = vector()
        y_list = vector()
        data = data.frame()
        while(TRUE) {
                dum = grid.locator()
                if(is.null(dum)) break
                ptdf = data.frame(x = as.numeric(dum$x), y = 
as.numeric(dum$y))
                x_list = c(x_list, as.numeric(dum$x))
                y_list = c(y_list, as.numeric(dum$y))
                lpoints(ptdf, pch = 3)

                if(attach_data) {
                        ovr = overlay(spatial_object, SpatialPoints(ptdf))
                        if(is(spatial_object, "SpatialPolygonsDataFrame")) {
                                data = rbind(data, ovr)
                        }
                        if(is(spatial_object, "SpatialGridDataFrame") || 
is(spatial_object, "SpatialPixelsDataFrame")) {
                                data = rbind(data, 
spatial_object at data[ovr,])
                        }
                        #if(length(data) == 0) stop("No data could be 
extract, input object does not contain data")
                }
        }
        trellis.unfocus()
        #print(data)
        if(length(data) == 0) {
                warning("No data could be extracted, reverting to a 
SpatialPoints object")
                attach_data = FALSE
        }
        if(attach_data) {
                return(SpatialPointsDataFrame(data.frame(x = x_list, y = 
y_list),
                                                                          
data = data, proj4string = CRS(as.character(proj4string))))
        } else {
                return(SpatialPoints(data.frame(x = x_list, y = y_list), 
proj4string = CRS(as.character(proj4string))))
        }
}

cheers and hth,
Paul
Maxime Pauwels wrote:

  
    
#
On Tue, Jun 30, 2009 at 1:33 PM, Maxime
Pauwels<Maxime.Pauwels at univ-lille1.fr> wrote:
spplot.locator would seem to be it!

library(sp)
ls("package:sp")

 will show it, and help(spplot) lists it with some other useful spplot
functions.

Barry
#
Hi Maxime,

Just add the sp.layout argument as you would normally do.

cheers,
Paul
Maxime Pauwels wrote:

  
    
#
Dear Paul,

This function looks very great!
I work on a grid and it could be very useful. However, before clicking 
on the screen, I have to add a SpatialLinesDataFrame and a list of 
points on the spplot (previously using sp.layout). Is it possible with 
your function?
Thank you

Maxime

Paul Hiemstra a ?crit :