[R-sig-Geo] spplot point coordinates

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


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