[R-sig-Geo] spplot point coordinates

Paul Hiemstra p.hiemstra at geo.uu.nl
Tue Jun 30 15:04:48 CEST 2009


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:
> 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
>


-- 
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 274 3113 Mon-Tue
Phone:  +3130 253 5773 Wed-Fri
http://intamap.geo.uu.nl/~paul



More information about the R-sig-Geo mailing list