Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R如何将多行GeoJson文件转换为具有长列和lat列的数据帧?_R_Geojson - Fatal编程技术网

R如何将多行GeoJson文件转换为具有长列和lat列的数据帧?

R如何将多行GeoJson文件转换为具有长列和lat列的数据帧?,r,geojson,R,Geojson,我有一个从gqig gis软件导出的多行Geogeson文件。 一个小例子: { "type": "FeatureCollection", "name": "route1", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } }, "features": [ { "type": "Feature", "properties": { "FID": 0 }, "geometry":

我有一个从gqig gis软件导出的多行Geogeson文件。 一个小例子:

{
"type": "FeatureCollection",
"name": "route1",
 "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" 
 } },
 "features": [
 { "type": "Feature", "properties": { "FID": 0 }, "geometry": { "type": 
 "MultiLineString", "coordinates": [ [ [ 1936131.287994222715497, 
 -4335318.772792792879045 ], [ -2633407.770391199737787, 
  1763382.609922708477825 ], [ -2922369.195528693497181, 
  4600947.908943663351238 ], [ -1640888.092745035886765, 
  5275789.498084637336433 ], [ -361201.781421858817339, 5970373.793290910311043 
  ], [ -361201.781421858817339, 5970373.793290910311043 ] ] ] 
 } }
]
}
如何在具有长列和横列的数据帧绑定节点中转换它? 预期结果:

node    long                    lat 
1   1936131.287994222715497    -4335318.772792792879045 
2   -2633407.770391199737787    1763382.609922708477825 
我尝试创建一个列表:

  route1 <- jsonlite::fromJSON(readr::read_file("routes/route1.geojson"))

如果使用strroute1检查获取的列表的结构,可以看到数据存储在一个数组中,您可以提取该数组

a <- route1$features$geometry$coordinates[[1]]
a

# , , 1
# 
#         [,1]     [,2]     [,3]     [,4]      [,5]      [,6]
# [1,] 1936131 -2633408 -2922369 -1640888 -361201.8 -361201.8
# 
# , , 2
# 
#          [,1]    [,2]    [,3]    [,4]    [,5]    [,6]
# [1,] -4335319 1763383 4600948 5275789 5970374 5970374
或作为数据帧:

d <- data.frame(long=a[, , 1], lat=a[, , 2])
d <- cbind(node=rownames(d), d)
d
#   node       long      lat
# 1    1  1936131.3 -4335319
# 2    2 -2633407.8  1763383
# 3    3 -2922369.2  4600948
# 4    4 -1640888.1  5275789
# 5    5  -361201.8  5970374
# 6    6  -361201.8  5970374
librarysf可以读取GeoJSON。这将为您提供一个sf对象。如果需要坐标,可以使用st_坐标函数

library(sf)

sf <- sf::st_read( geo, quiet = T )
df <- as.data.frame( sf::st_coordinates( sf ) )

#            X        Y L1 L2
# 1  1936131.3 -4335319  1  1
# 2 -2633407.8  1763383  1  1
# 3 -2922369.2  4600948  1  1
# 4 -1640888.1  5275789  1  1
# 5  -361201.8  5970374  1  1
# 6  -361201.8  5970374  1  1

这个额外的L1和L2列告诉您每个坐标对所属的多重线串中的哪个线串。

states GeoJSON使用地理坐标参考系,世界大地测量系统1984,和十进制度数单位-因此,您这里的数据在技术上不是实际有效的GeoJSONOther CRS值在以前的规范中是允许的,但是rfc7946中更改了此规则
library(sf)

sf <- sf::st_read( geo, quiet = T )
df <- as.data.frame( sf::st_coordinates( sf ) )

#            X        Y L1 L2
# 1  1936131.3 -4335319  1  1
# 2 -2633407.8  1763383  1  1
# 3 -2922369.2  4600948  1  1
# 4 -1640888.1  5275789  1  1
# 5  -361201.8  5970374  1  1
# 6  -361201.8  5970374  1  1