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

47

我一直在处理具有纬度和经度值的地震数据,想要将这些纬度和经度值转换为空间坐标。

假设我有以下数据集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

我想把它转换成空间点。类似这样:

 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

这里的错误来自于xy未被定义。在这个例子中,你的xydf$longitudedf$latitude。因此,一个解决方案是运行: coordinates(df.sp)<-~ df$longitude + df$latitude - Jessica Burnett
4个回答

62

首先,您将lonlat的列合并,并创建一个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"

以上的反向操作是什么?我想将SpatialPointsDataFrame xy坐标转换为LAT LON。谢谢! - M.Qasim
@M.Qasim 如果您使用上面的示例,似乎以下两种方式可以让您提取经度和纬度。spdf@data是一个数据框架,或者spdf@coords是一个矩阵。 - jazzurro
@jazzurro,假设您拥有一个城市的所有纬度和经度信息,是否可以生成类似于http://stackoverflow.com/questions/40179966/boundary-polygon-of-lat-lon-collection中的边界多边形?如果不行,还需要哪些其他信息集来创建类似于链接中发送的边界多边形? - MHOOS
@MHOOS 我看了一下这个问题。恐怕我不知道你如何绘制这样的多边形。我能想到的所有想法都已经在答案中了。 - jazzurro
2
这是一种过时的解决方案,因为 sf 是新的前进方式。请参见此线程中的其他答案或此处:https://dev59.com/x1UM5IYBdhLWcg3wMd8k#52951856 - Fitzroy Hogsflesh

57

或者使用 sf 替代 sp 对象(了解有关R的简单特征或从 sp 迁移到 sf,可以在这里查看更多信息):

library(sf)

# the given data above
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)

# Convert data frame to sf object
my.sf.point <- st_as_sf(x = my.df, 
                        coords = c("longitude", "latitude"),
                        crs = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

# simple plot
plot(my.sf.point)

# interactive map:
library(mapview)
mapview(my.sf.point)

# convert to sp object if needed
my.sp.point <- as(my.sf.point, "Spatial")

enter image description here


10

随着

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))

转换为 SpatialPointsDataFrame

coordinates(df) <- cbind(df$longitude , df$latitude)

正如 @jazzurro 指出的那样,您可能需要为您的空间对象分配一个CRS。

proj4string(df) = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

将SpatialPointsDataFrame反转为原始df的过程

df <- data.frame(longitude = coordinates(df)[,1], latitude = coordinates(df)[,2])

0
问题在于 'x' 和 'y' 没有定义。您更正后的列名是 'longitude' 和 'latitude'。一种解决方法是运行:
coordinates(df.sp) <- c('longitude', 'latitude')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接