-1

我有一个具有“纬度”、“经度”值的数据框,例如:

  index      lat         lon     
 ------- ----------- ----------- 
      1   51.513393   -0.115650  
      2   51.513428   -0.115461  
      3   51.513428   -0.115462  
      4   51.513428   -0.115465  
      5   51.513428   -0.115470  
      6   51.513432   -0.115462  
      7   51.513432   -0.115467  
      8   51.513435   -0.115471  
      9   51.513439   -0.115468  
     10   51.513439   -0.115469  

我试图获得这些点的中心:

center_point <- centroid(df)

我遇到了“geosphere”包引发错误的问题:

Error in .pointsToMatrix(x, poly = TRUE) : longitude > 360
In addition: Warning message:
In .pointsToMatrix(x, poly = TRUE) :
  Suspect column names (longitude and latitude reversed?)

从阅读这个stackoverflow 帖子,问题可能与我以字符串而不是数字/双精度格式格式化的值有关。

然后我继续将这些列值转换为数字:

df$lon <- as.numeric(df$lon)
df$lat <- as.numeric(df$lat)

转换似乎已经弄乱了我的数据,但是就像现在看它一样;它显示了这一点:

  index   lat   lon  
 ------- ----- ----- 
      1     1    35  
      2     2     8  
      3     2     9  
      4     2    11  
      5     2    15  
      6     3     9  
      7     3    12  
      8     4    16  
      9     5    13  
     10     5    14  

有谁知道我应该如何解决这个问题?

4

2 回答 2

1
require(sp)
require(rgeos)

x =  'lat,lon
     51.513393,-0.115650 
     51.513428,-0.115461 
     51.513428,-0.115462 
     51.513428,-0.115465 
     51.513428,-0.115470 
     51.513432,-0.115462 
     51.513432,-0.115467  
     51.513435,-0.115471  
     51.513439,-0.115468  
     51.513439,-0.115469'

x = read.table(text = x, sep = ',', header = TRUE, colClasses= rep('numeric', 2) )

sx = SpatialPoints(coords = x[, c('lon','lat')], proj4string = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") )

gCentroid(sx)


SpatialPoints:
           x        y
1 -0.1154845 51.51343
Coordinate Reference System (CRS) arguments: +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0 
于 2018-07-20T10:54:54.270 回答
1

在 djhurio 的帮助下解决了这个问题:

必须首先将价值转换为:

as.character()

以下代码现在适用于我的应用程序:

df$lon <- as.numeric(as.character(df$lon))
df$lat <- as.numeric(as.character(df$lat))

center_point <- centroid(df)
于 2018-07-20T10:37:13.683 回答