[R-sig-Geo] Change z coordinates of SpatialGridDataFrame

Nathalie Morin Nathalie.M.Morin at usherbrooke.ca
Thu Feb 14 22:12:12 CET 2013


Dear Edzer,

I transformed my SpatialGridDataFrame into a SpatiaPointsDataFrame (by the
way is there any coercion method such as "as.SpatialPointsDataFrame"
available in sp instead of creating a new one for I have not found it ?).
But this does not change my problem: I need the vector of attributes to
follow through the change in the z position. Right now the order of the
values in the vector of attributes stays the same even if the z coordinates
change. Here is my code starting from the summary of the initial
SpatialGridDataFrame

> summary(tls.spgdf)
Object of class SpatialGridDataFrame
Coordinates:
        min    max
[1,] 828099 828139
[2,] 101834 101874
[3,]    322    374
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
1          828099.5        1        40
2          101834.5        1        40
3             322.5        1        52
Data attributes:
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.000000 0.000000 0.000000 0.007572 0.000000 1.226000 

# (i) 3D Matrix
(x.coord <- floor(coordinates(tls.spgdf)[,1]))
(y.coord <- floor(coordinates(tls.spgdf)[,2]))
(z.coord <- floor(coordinates(tls.spgdf)[,3]))
tls.xyz2 <- cbind(x.coord, y.coord, z.coord)

# (ii) SpatialPoints
tls.sp2 <- SpatialPoints(tls.xyz2, proj4string = CRS("+init=epsg:27561"))

# (iii) DataFrame (attribut z)
tls.df2 <- data.frame(d=tls.spgdf$d)

# (iv) SpatialPointsDataFrame
tls.spdf2 <- SpatialPointsDataFrame(tls.xyz2, tls.df2)

# Summary of DTM values
> summary(sge)
Object of class SpatialGridDataFrame
Coordinates:
      min    max
s1 828099 828139
s2 101834 101874
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          828099.5        1        40
s2          101834.5        1        40
Data attributes:
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  325.9   329.1   330.8   330.8   332.4   335.6 

z.sge <- round(sge[[1]])
z <- coordinates(tls.spdf2)[,3]
tls.spdf2$z <- z
nz <- tls.spdf2[[2]] - z.sge[[1]]
tls.spdf2$nz <- nz

> summary(tls.spdf2)
Object of class SpatialPointsDataFrame
Coordinates:
           min    max
x.coord 828099 828138
y.coord 101834 101873
z.coord    322    373
Is projected: NA 
proj4string : [NA]
Number of points: 83200
Data attributes:
       d                  z               nz        
 Min.   :0.000000   Min.   :322.0   Min.   :-11.00  
 1st Qu.:0.000000   1st Qu.:334.8   1st Qu.:  1.75  
 Median :0.000000   Median :347.5   Median : 14.50  
 Mean   :0.007572   Mean   :347.5   Mean   : 14.50  
 3rd Qu.:0.000000   3rd Qu.:360.2   3rd Qu.: 27.25  
 Max.   :1.226040   Max.   :373.0   Max.   : 40.00 

(x.coord2 <- coordinates(tls.spdf2)[,1])
(y.coord2 <- coordinates(tls.spdf2)[,2])
(z.coord2 <- tls.spdf2$nz)
tls.xyz3 <- cbind(x.coord2, y.coord2, z.coord2)
tls.sp3 <- SpatialPoints(tls.xyz3, proj4string = CRS("+init=epsg:27561"))
tls.df2 <- data.frame(d=tls.spdf2$d)
tls.spdf3 <- SpatialPointsDataFrame(tls.sp3, tls.df2)




--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Change-z-coordinates-of-SpatialGridDataFrame-tp7582612p7582635.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list