We (the developers from sp) tried hard to relieve you from writing such code. Please try the following code, which requires you to click on the grid map after locator(1):

library(sp)
data(meuse.grid)
coordinates(meuse.grid)=~x+y
gridded(meuse.grid)=TRUE
fullgrid(meuse.grid) = TRUE
image(meuse.grid["dist"])
pt = locator(1)
pt
pt.sp = SpatialPoints(matrix(c(pt[[1]], pt[[2]]), 1, 2))
pt.sp
overlay(meuse.grid, pt.sp)

Besides being equally efficient, this code should remain working even if some internal representations of the sp classes were to change.

While looking at your code below, I have the feeling you forget that R arrays start with index 1, whereas C arrays start with index 0. You can check the sp sources to be 100% certain, but I also believe that the sp organization lets vary x first, then y.
--
Edzer

Harry Kim wrote:
Dear R-sig-geo users,

    I have a question about extracting information from a raster file
after I read it in using rgdal package, and I would be infinitely
grateful if someone could help me.
    The summary of raster file looks like this:

#summary of raster file
summary(xxx)
Object of class SpatialGridDataFrame
Coordinates:
   min max
x -180 180
y  -90  90
Is projected: FALSE
proj4string :
[+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0]
Number of points: 2
Grid attributes:
  cellcentre.offset cellsize cells.dim
x           -179.10      1.8       200
y            -89.55      0.9       200
Data attributes:
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max.      NA's
    0.000     0.000     0.000    47.010     3.264 13540.000 26563.000

I would like to write a code that extracts the value of associated
cell given a specific location in latitude and longitude. I've noticed
that [EMAIL PROTECTED] contains the values as 40000 x 1 matrix. I've wrote the
following code assuming that the values are stored as in C programing
(column wise seperation):


#initiate values
[EMAIL PROTECTED]@cellsize[1]
[EMAIL PROTECTED]@cellsize[2]

[EMAIL PROTECTED]@cellcentre.offset[1]
[EMAIL PROTECTED]@cellcentre.offset[2]

[EMAIL PROTECTED]@cells.dim[1]
[EMAIL PROTECTED]@cells.dim[2]

#given coordinate
x=26
y=45

#find the index
index=floor((x-x_start)/x_inc)*y_dim + floor( (y-y_start)/y_inc )
[EMAIL PROTECTED],1]

The result does not seem to match what I should get. Could anybody
explain to me how the data is stored in SpatialGridDataFrame? Your
help would be much much appreciated.

Thank you and have a good night
Harry

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

--
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de/
http://www.springer.com/978-0-387-78170-9 [EMAIL PROTECTED]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Reply via email to