如何将数据框转换为空间坐标

mk0*_*k04 40 gis r geospatial

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

假设我有以下数据集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
Run Code Online (Sandbox Code Playgroud)

我想将它转换为空间点.像这样的东西:

 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
Run Code Online (Sandbox Code Playgroud)

我使用了以下代码:

library(sp)
df.sp<-df
coordinates(df.sp)<-~x+y
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

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

jaz*_*rro 43

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

在你的情况,你没有任何其他的列,但lonlat,该方法将无法正常工作.我特意留lonlat@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"
Run Code Online (Sandbox Code Playgroud)

  • 这是一个过时的解决方案,因为“ sf”是新方法。请在此线程中或此处查看其他答案:/sf/answers/3706629951/ (2认同)

Val*_*tin 33

或者使用sf而不是sp对象(请查看有关R的简单要素或从此处迁移spsf 此处的更多信息):

require(sf)
my.df <- read.table(text="
                    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",
                    header=TRUE)

my.sf.point <- st_as_sf(x = my.df, 
                        coords = c("longitude", "latitude"),
                        crs = "+proj=longlat +datum=WGS84")
# simple plot
plot(my.sf.point)
# interactive map:
require(mapview)
mapview(my.sf.point)

# convert to sp object if needed
my.sp.point <- as(my.sf.point, "Spatial")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 6

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))
Run Code Online (Sandbox Code Playgroud)

要转换为SpatialPointsDataFrame

coordinates(df) <- cbind(df$longitude , df$latitude)
Run Code Online (Sandbox Code Playgroud)

正如@jazzurro所指出的,您可能需要为空间对象分配CRS.

proj4string(df) = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
Run Code Online (Sandbox Code Playgroud)

将SpatialPointsDataFrame反向处理为原始df

df <- data.frame(longitude = coordinates(df)[,1], latitude = coordinates(df)[,2])
Run Code Online (Sandbox Code Playgroud)