Skip to content

Apply() function on 3rd dimension of SpatialGridDataFrame

4 messages · Nathalie Morin, Edzer Pebesma

#
Hello,

 

I have a 3D array with an attribute converted to SpatiaGridDataFrame named
?clip? :
Object of class SpatialGridDataFrame

Coordinates:

         min       max

s1 828104.00 828134.00

s2 101839.00 101869.00

s3    324.09    373.09

Is projected: TRUE

proj4string :

[+init=epsg:27561 +proj=lcc +lat_1=49.50000000000001
+lat_0=49.50000000000001 +lon_0=0

+k_0=0.999877341 +x_0=600000 +y_0=200000 +a=6378249.2 +b=6356515

+towgs84=-168,-60,320,0,0,0,0 +pm=paris +units=m +no_defs]

Grid attributes:

   cellcentre.offset cellsize cells.dim

s1         828104.50        1        30

s2         101839.50        1        30

s3            324.59        1        49

Data attributes:

    Min.  1st Qu.   Median     Mean  3rd Qu.     Max.     NA's 

   0.000    0.000    0.000    0.002    0.000    0.806 9506.000

 

The apply() function does not work on the 3rd dimension of the
SpatiaGridDataFrame :
=> I get an error message (sorry it's in French) :

"Erreur dans if (d2 == 0L) { : valeur manquante l? o? TRUE / FALSE est
requis"

 

I tried with z coordinate instead :
The only solution I found is to convert back to standard non spatial 3D
array.

 

Cheers,

 

Nathalie MORIN

Doctorante en t?l?d?tection foresti?re (Ph.D student in remote sensing
applied to forestry)

Centre d?application et de recherches en t?l?d?tection (CARTEL)

D?partement de G?omatique Appliqu?e

Sherbrooke (QC) CANADA J1K 2R1

T?l. : + 1 (819)  821-8000 #62962

e-mail :  <mailto:Nathalie.M.Morin at Usherbrooke.ca>
Nathalie.M.Morin at Usherbrooke.ca

 <http://www.usherbrooke.ca/> Universit? de Sherbrooke

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20121219/bd61efd6/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 1245 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20121219/bd61efd6/attachment.gif>
#
Dear Nathalie,

3D and higher-D support for Points Pixels and Grids in sp is there on
the level of object creation, but very limited on the level of useful
functionality.

apply() is not even a generic, so can only be applied to arrays,
according to ?apply. Would an as.array(clip) method for
SpatialGridDataFrame help you further? Or is it something else you need,
e.g. clipping a SpatialGridDataFrame with more than 2 dimensions?
On 12/19/2012 09:04 PM, Nathalie Morin wrote:

  
    
#
Dear Edzer,

1) as.array()
/"Would an as.array(clip) method for
SpatialGridDataFrame help you further?"/

Yes, as I mentionned in my initial post :
/"The only solution I found is to convert back to standard non spatial 3D
array."/
(in order to make the apply() function work on my 3rd dimension or z layer)

I still encounter a problem though :

D.tls.SPGDF <- SpatialGridDataFrame(D.tls.SPG, D.tls.DF)

buffer <- readOGR("donnees/limites", layer="bure_106_buffer")
buffer.proj <- spTransform(buffer, CRS("+init=epsg:27561"))
g <- geometry(buffer.proj)

fullgrid(D.tls.SPGDF) = FALSE
clip <- D.tls.SPGDF[g,,drop=TRUE] 
fullgrid(clip) = TRUE

A <- as.array(clip$d)
DA <- apply(A, 3, mean, na.rm=T)
=> gives an error message related to a missing value where TRUE / FALSE is
required
"Erreur dans if (d2 == 0L) { : valeur manquante l? o? TRUE / FALSE est
requis"

I suspect this could be related to the fullgrid() ?
Any hint ?

2) /Or is it something else you need,
e.g. clipping a SpatialGridDataFrame with more than 2 dimensions? /

This is another story ! For the moment, I do not need a convex hull because
I do not want to truncate the height information on the z axis. But this is
an interesting question. I should probably open a separate post for that.










--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Apply-function-on-3rd-dimension-of-SpatialGridDataFrame-tp7582023p7582036.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
#
On 12/20/2012 07:56 PM, Nathalie Morin wrote:
No, you should inspect A, check dim(A) and find out that it is a
one-dimensional array;

apply(array(1:3), 3, mean)

triggers the same error.

as.array(clip) will only work if you add the appropriate as.array
method, e.g.

as.array.SpatialGridDataFrame = function(x,...) {
    d = gridparameters(x)$cells.dim
    if (ncol(x at data) > 1)
        d = c(d, ncol(x at data))
    array(do.call(c, x at data), dim = d)
}

then, as.array(clip) will return a 3 or 4 dimensional array (4 if clip
has more than one attribute).

Note that this function produces an array for which y decreases with
increasing index, all other dimensions increase with increasing index.

I will add this method to sp.