将一个对象的坐标值更改为一个空间点dataframe格式。

时间:2022-12-16 10:24:47

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.

我必须沿着一个经常变换的特性去修正值,因此我必须把普通的笛卡尔坐标(x,y)变换成一个曲线坐标系(s,n)曲线坐标允许我到krig,在krig之后,我可以把坐标倒转到笛卡尔坐标x,所以原来的几何图形被表示出来了。对于一个可复制的示例,将使用meuse数据集和sp包。

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.

给定所需要的krig存储在meuse中。但是在s n坐标的情况下,我想用x,y, cartersian坐标来查看最终结果(将s,n转换为x,y假设完全等于x,y转换为s,n)如果可以,我怎样才能将网格中的(s,n)值替换为(XX,YY)呢?grid已经是一个空间点dataframe。

1 个解决方案

#1


3  

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

要访问SpatialPointsDataFrame中的原始数字,只需使用asn .data.frame将其转换为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.

现在您可以将坐标更改为正确的值。之后,您可以再次使用坐标将对象更改为用于绘图的spatial alpointdataframe。

#1


3  

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

要访问SpatialPointsDataFrame中的原始数字,只需使用asn .data.frame将其转换为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.

现在您可以将坐标更改为正确的值。之后,您可以再次使用坐标将对象更改为用于绘图的spatial alpointdataframe。