user2507608
user2507608

Reputation: 365

Changing the coordinate values of an object already in a SpatialPointsDataFrame format

I have to krig values along a feature that meanders a lot. Therefore I had to transform normal cartesian coordinates (x,y) to a curvilinear coordinate system (s,n). The curvilinear coordinates allow me to krig and after the krig, I can reverse the coordinates to cartesian x,y so the original geometry is represented. For a reproducible example, will use the meuse data set and the sp package.

data(meuse)
meuse$s<-meuse$x           # Assuming that s is the curvilinear transformed coordinate from the orginal x cartessian coordinate
meuse$n<-meuse$y           # Assuming that n is the curvilinear transformed coordinate from the orginal y cartessian coordinate
coordinates(meuse) <- ~s+n # Using the curvilinear coordinates to do the krigging
proj4string(meuse) <- nl.rd # projection not defined, so just used a random example

## load grid:

data(meuse.grid)
meuse.grid$XX<-meuse.grid$x+105  # Fake transformation value to give original X
meuse.grid$YY<-meuse.grid$y-77  # Fake transformation value to give original Y
meuse.grid$s <-meuse.grid$x     # Assuming that s is the curvilinear transformed coordinate from the orginal x cartessian coordinate
meuse.grid$n <-meuse.grid$y # Assuming that n is the curvilinear transformed coordinate from the orginal y cartessian coordinate
coordinates(meuse.grid) <- ~s+n
gridded(meuse.grid) <- TRUE

## A simple inverse distance krig
zinc.id <- krige(zinc~1, meuse, meuse.grid)
meuse.grid$zinc.id <- zinc.id$var1.pred
str(meuse.grid)

Given the desired krig is stored in meuse.grid but with s,n coordinates, I would like to use the x,y cartersian coordinates to view the final result (transforming the s,n to x,y is assumed to be exactly equal x,y to s,n). Can I, and if so, how may I be able to replace the (s,n) values in the grid to (XX,YY), given that meuse.grid is already a SpatialPointsDataFrame.

Upvotes: 4

Views: 1408

Answers (1)

Paul Hiemstra
Paul Hiemstra

Reputation: 60924

To access the raw numbers in the SpatialPointsDataFrame, simply use as.data.frame to cast it to a data.frame:

> head(as.data.frame(meuse.grid))
       x      y part.a part.b      dist soil ffreq     XX     YY  zinc.id
1 181180 333740      1      0 0.0000000    1     1 181285 333663 633.6864
2 181140 333700      1      0 0.0000000    1     1 181245 333623 712.5450
3 181180 333700      1      0 0.0122243    1     1 181285 333623 654.1617
4 181220 333700      1      0 0.0434678    1     1 181325 333623 604.4422
5 181100 333660      1      0 0.0000000    1     1 181205 333583 857.2558
6 181140 333660      1      0 0.0122243    1     1 181245 333583 755.5061
       s      n
1 181180 333740
2 181140 333700
3 181180 333700
4 181220 333700
5 181100 333660
6 181140 333660

Now you can change the coordinates to the correct values. After that, you can use coordinates again to change the object back to a SpatialPointDataFrame for plotting.

Upvotes: 4

Related Questions