本文介绍了如何将数据框转换为空间坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在处理具有lat long值的地震数据,我想将这些lat long值转换为空间坐标.

I have been working on earthquake data that has lat long values, and I want to convert those lat long values to spatial coordinates.

假设我有以下数据集df:

longitude          latitude
        128.6979    -7.4197
        153.0046    -4.7089
        104.3261    -6.7541
        124.9019    4.7817
        126.7328    2.1643
        153.2439    -5.6500
        142.8673    23.3882
        152.6890    -5.5710

我想将其转换为空间点.像这样:

I want to convert it into spatial points.Something like this:

 lon        lat
[1,] 2579408.24 1079721.15
[2,] 2579333.69 1079729.18
[3,] 2579263.65 1079770.55
[4,] 2579928.04 1080028.46
[5,] 2579763.65 1079868.92
[6,] 2579698.00 1079767.97

我使用了以下代码:

library(sp)
df.sp<-df
coordinates(df.sp)<-~x+y

但是我收到以下错误:

Error in `[.data.frame`(object, , -coord.numbers, drop = FALSE) :
  undefined columns selected

推荐答案

首先,获取lonlat的列,并为coord创建一个对象.然后,从原始数据框中减去它们,然后创建一个新对象.最后,您使用SpatialPointsDataFrame()创建一个SpatialPointsDataFrame.创建SpatialPointsDataFrame时,需要分配proj4string.为您选择一个合适的.

First, you take the columns of lon and lat and create an object for coord. Then, you subtract them from the original data frame and create a new object. You finally use SpatialPointsDataFrame() to create a SpatialPointsDataFrame. When you create a SpatialPointsDataFrame, you need to assign proj4string. Choose an appropriate one for you.

在您的情况下,除了lonlat之外,您没有其他任何列,该方法将不起作用.我特意离开了lonlat @data.

In your case, you do not have any other columns but lon and lat, the method won't work. I purposely left lon and lat @data.

数据

mydf <- structure(list(longitude = c(128.6979, 153.0046, 104.3261, 124.9019,
126.7328, 153.2439, 142.8673, 152.689), latitude = c(-7.4197,
-4.7089, -6.7541, 4.7817, 2.1643, -5.65, 23.3882, -5.571)), .Names = c("longitude",
"latitude"), class = "data.frame", row.names = c(NA, -8L))


### Get long and lat from your data.frame. Make sure that the order is in lon/lat.

xy <- mydf[,c(1,2)]

spdf <- SpatialPointsDataFrame(coords = xy, data = mydf,
                               proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))


#> str(spdf)
#Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
#..@ data       :'data.frame':  8 obs. of  2 variables:
#.. ..$ longitude: num [1:8] 129 153 104 125 127 ...
#.. ..$ latitude : num [1:8] -7.42 -4.71 -6.75 4.78 2.16 ...
#..@ coords.nrs : num(0)
#..@ coords     : num [1:8, 1:2] 129 153 104 125 127 ...
#.. ..- attr(*, "dimnames")=List of 2
#.. .. ..$ : NULL
#.. .. ..$ : chr [1:2] "longitude" "latitude"
#..@ bbox       : num [1:2, 1:2] 104.33 -7.42 153.24 23.39
#.. ..- attr(*, "dimnames")=List of 2
#.. .. ..$ : chr [1:2] "longitude" "latitude"
#.. .. ..$ : chr [1:2] "min" "max"
#..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
#.. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"

这篇关于如何将数据框转换为空间坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 05:50