[R-sig-Geo] how to go from SpatialGrid to SpatialGridDataFrame ?

Horacio Samaniego horacio.samaniego at gmail.com
Wed Feb 1 14:18:39 CET 2006


good, thanks...

however,  there seemed to be another problem.. of a different kind I  
fear...

I got:

 > kkp=idw(glmpred~1,data.new_alb[gd.glm,],data.grd)
Error in predict.gstat(g, newdata = newdata, block = block, nsim =  
nsim,  :
	data items in gstat object and newdata have different coordinate  
reference systems

Due to a simple space in the first coord definition... (perhaps easy  
to fix... by some string processing?)
 > rbind(proj4string(data.new_alb),proj4string(data.grd))
      [,1]
[1,] " +proj=aea +lat_1=29.75 +lat_2=45.25 +lat_0=37.5 +lon_0=-96  
+x_0=0 +y_0=0 +no_defs +a=6378137 +rf=298.257222101  
+towgs84=0.000,0.000,0.000"
[2,] "+proj=aea +lat_1=29.75 +lat_2=45.25 +lat_0=37.5 +lon_0=-96  
+x_0=0 +y_0=0 +no_defs +a=6378137 +rf=298.257222101  
+towgs84=0.000,0.000,0.000"



This was an easy problem that got solved using

 > proj4string(data.new_alb)<- CRS(proj4string(data.grd))
 > rbind(proj4string(data.new_alb),proj4string(data.grd))
      [,1]
[1,] "+proj=aea +lat_1=29.75 +lat_2=45.25 +lat_0=37.5 +lon_0=-96  
+x_0=0 +y_0=0 +no_defs +a=6378137 +rf=298.257222101  
+towgs84=0.000,0.000,0.000"
[2,] "+proj=aea +lat_1=29.75 +lat_2=45.25 +lat_0=37.5 +lon_0=-96  
+x_0=0 +y_0=0 +no_defs +a=6378137 +rf=298.257222101  
+towgs84=0.000,0.000,0.000"


Thanks Edzer!
--
Horacio Samaniego
Dept. Biology
University of New Mexico
Albuquerque 87106, NM

http://www.unm.edu/~horacio



On Feb 1, 2006, at 1:41 PM, Edzer J. Pebesma wrote:

Horacio Samaniego wrote:

> Edzer,
>
> the class is:
>
> > class(data.new_alb[gd.glm,])
> [1] "SpatialPointsDataFrame"
> attr(,"package")
> [1] "sp"
> > class(data.grd)
> [1] "SpatialGrid"
> attr(,"package")
> [1] "sp"
>
> I use the [gd.glm,] to avoid the NA in the datasets for that   
> variable. However, I get the same results when omitted
> >

> Error in krige(glmpred ~ 1, data = data.new_alb, newd = data.grd) :
>     no direct or inherited method for function 'krige' for this call
>
> this indicates that the problem is elsewhere.

Yes; sorry it took me a while to recognize.
You should omit the "data =" in the call, i.e.
krige(glmpred~1,data.new_alb[gd.glm,],newd=data.grd)
will work. This is a change in interface, which I recently
posted to R-pkgs:
https://stat.ethz.ch/pipermail/r-packages/2006/000154.html

>
>
> If newdata accepts SpatialGrids will it output a  
> SpatialGridDataFrame  with the prediction as a dataframe of that?

Yes.

>
>
> Regarding the example, I am not quite sure that I understand how  
> to  adapt the example to my data. I have a generated a grid based  
> on my  grass extend, and I need the predictions from my model (idw  
> in this  'toy' example) to go in that grid. In the meuse.grid  
> example, you  have a regular grid of samples and make a grid based  
> on that, I have  irregular samples and need to create a continuous  
> surface of those  values. So, I chose to build the GridTopology  
> from my grass location.

Should work.
--
Edzer

>
> thanks
>
> Horacio
>
>
>
> On Feb 1, 2006, at 12:54 PM, Edzer J. Pebesma wrote:
>
> A Horacio Samaniego wrote:
>
>> I wonder how I should go from a SpatialGrid to a  
>> SpatialGridDataFrame.
>>
>>
> Using for example the function SpatialGridDataFrame()
>
>> I have defined a grid using the functions from the sp, gstat and   
>> spgrass6 packages:
>>
>> G<-gmeta6()
>> kk<-SpatialGrid(GridTopology(c(G$south,G$west),c(G$nsres,G 
>> $ewres),c (G$rows,G$cols)),alb.proj)
>>
>>> str(kk)
>>>
>> Formal class 'SpatialGrid' [package "sp"] with 5 slots
>>   ..@ grid       :Formal class 'GridTopology' [package "sp"] with  
>> 3  slots
>>   .. .. ..@ cellcentre.offset: num [1:2] -1333373 -2356259
>>   .. .. ..@ cellsize         : num [1:2] 1000 1000
>>   .. .. ..@ cells.dim        : int [1:2] 2899 4615
>>   ..@ grid.index : int(0)
>>   ..@ coords     : num [1:2, 1:2] -1333373  1564162 -2356259  2257928
>>   .. ..- attr(*, "dimnames")=List of 2
>>   .. .. ..$ : NULL
>>   .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
>>   ..@ bbox       : num [1:2, 1:2] -1333872 -2356759  1564662  2258428
>>   .. ..- attr(*, "dimnames")=List of 2
>>   .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
>>   .. .. ..$ : chr [1:2] "min" "max"
>>   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>>   .. .. ..@ projargs: chr "+proj=aea +lat_1=29.75 +lat_2=45.25   
>> +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +no_defs +a=6378137   
>> +rf=298.257222101 +towgs84=0.000,0"| __truncated__
>>
>> Now I try to interpolate on that grid, with no success:
>> interp.vals<-idw(var1~1,data,newdata=kk)
>> Error in idw(corrext ~ 1, data = data.new_alb[gd.glm, ], newdata  
>> =  data.grd) :
>>     no direct or inherited method for function 'idw' for this call
>>
>> So, I guess that I need to have a 'SpatialGridDataFrame' instead  
>> of  just 'SpatialGrid' as the interpolation needs to be assigned  
>> to a  spatial location
>>
> newdata can be of class SpatialGrid; probably idw fails because
> of the data argument. What is the class of data.new_alb[gd.glm,] ?
>
>> kk2<-as(kk,'SpatialGridDataFrame');
>> Formal class 'SpatialGridDataFrame' [package "sp"] with 6 slots
>>   ..@ data       :Formal class 'AttributeList' [package "sp"]  
>> with  1 slots
>>   .. .. ..@ att: list()
>>   ..@ grid       :Formal class 'GridTopology' [package "sp"] with  
>> 3  slots
>>   .. .. ..@ cellcentre.offset: num [1:2] -1333373 -2356259
>>   .. .. ..@ cellsize         : num [1:2] 1000 1000
>>   .. .. ..@ cells.dim        : int [1:2] 2899 4615
>>   ..@ grid.index : int(0)
>>   ..@ coords     : num [1:2, 1:2] -1333373  1564162 -2356259  2257928
>>   .. ..- attr(*, "dimnames")=List of 2
>>   .. .. ..$ : NULL
>>   .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
>>   ..@ bbox       : num [1:2, 1:2] -1333872 -2356759  1564662  2258428
>>   .. ..- attr(*, "dimnames")=List of 2
>>   .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
>>   .. .. ..$ : chr [1:2] "min" "max"
>>   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>>   .. .. ..@ projargs: chr "+proj=aea +lat_1=29.75 +lat_2=45.25   
>> +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +no_defs +a=6378137   
>> +rf=298.257222101 +towgs84=0.000,0"| __truncated__
>>
>> promotes the grid to the correct class SpatialGridDataFrame but  
>> do  not assign to space for the variable. Is there any way to  
>> assign an  empty "slot" just as we would do for a data.frame?
>>
>> To sum.  I cant seem to be able to create a "slot" (or z  
>> dimension)  to hold the output of my interpolation. Is there any  
>> trick out  there that I am missing? Any help will be appreciated.
>>
>> thanks,
>>
>> H
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>




More information about the R-sig-Geo mailing list