Skip to content
Back to formatted view

Raw Message

Message-ID: <4A4A19BA.3030006@univ-lille1.fr>
Date: 2009-06-30T13:57:14Z
From: Maxime Pauwels
Subject: spplot point coordinates
In-Reply-To: <4A4A0D70.3050404@geo.uu.nl>

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


-- 
Maxime Pauwels
Researcher ID: http://www.researcherid.com/rid/A-1745-2009

Laboratoire de G?n?tique et Evolution des Populations V?g?tales
UMR CNRS 8016
Universit? de Lille, USTL-Lille1
http://www.univ-lille1.fr/gepv
Tel  : +33 3 20 43 40 76
Fax  : +33 3 20 43 69 79
			
?Impose ta chance, serre ton bonheur et va vers ton risque. A te regarder, ils s?habitueront.?
Ren? Char