[R-sig-Geo] spplot point coordinates
Maxime Pauwels
Maxime.Pauwels at univ-lille1.fr
Tue Jun 30 15:57:14 CEST 2009
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
More information about the R-sig-Geo
mailing list