如何使用st_read从kml文件获取R中的坐标

问题描述 投票:0回答:1

我正在使用r,正在尝试从KML文件中获取地理坐标(纬度和经度)。我正在使用在Google地球中创建的KML文件,并使用下一个代码中的st_read()中的sf package以正确的方式获取坐标:

Google_earth_kml <- st_read("prueba_direcciones_google_earth.kml")

问题是我得到了下一张桌子:

Name                                                         description                             Geometry
store 1                   address: Barros Luco 2058<br>RUT: 08.180.861-9          c(-71.6132, -33.5985683, 0)
store 2   address: AVENIDA DOMINGO SANTA MARIA 1789<br>RUT: 76.585.397-4       c(-70.6639313, -33.4155609, 0)

我想得到一个像这样的表:

Name                             address          Rut        long         lat
store 1                 Barros Luco 2058 08.180.861-9    -71.6132 -33.5985683
store 2 AVENIDA DOMINGO SANTA MARIA 1789 76.585.397-4 -70.6639313 -33.4155609

这只是我数据的一小部分。我知道,也许您将需要KML文件,但是出于政治和隐私的原因,我无法共享它。

我想知道是否有人可以给我意见或其他观点。任何帮助将不胜感激。

r kml sf
1个回答
0
投票

数据

library(sf)
library(dplyr)

sample_KML <- "https://github.com/mapbox/Simple-KML/raw/master/sample/example.kml"

KML_sf <- st_read(sample_KML) %>% 
  slice(1:4) # keep only the first 4 rows. The 5th row is a polygon

KML_sf

Simple feature collection with 4 features and 2 fields
geometry type:  POINT
dimension:      XYZ
bbox:           xmin: -122.6819 ymin: -22.90833 xmax: 28.97602 ymax: 64.13333
z_range:        zmin: 0 zmax: 0
CRS:            4326
            Name Description                       geometry
1       Portland                POINT Z (-122.6819 45.52 0)
2 Rio de Janeiro             POINT Z (-43.19639 -22.9083...
3       Istanbul              POINT Z (28.97602 41.01224 0)
4      Reykjavik             POINT Z (-21.93333 64.13333 0)

输出

output <- KML_sf %>% 
  mutate(long = st_coordinates(.)[1],
         lat = st_coordinates(.)[2])


output

Simple feature collection with 4 features and 4 fields
geometry type:  POINT
dimension:      XYZ
bbox:           xmin: -122.6819 ymin: -22.90833 xmax: 28.97602 ymax: 64.13333
z_range:        zmin: 0 zmax: 0
CRS:            4326
            Name Description                       geometry      long       lat
1       Portland                POINT Z (-122.6819 45.52 0) -122.6819 -43.19639
2 Rio de Janeiro             POINT Z (-43.19639 -22.9083... -122.6819 -43.19639
3       Istanbul              POINT Z (28.97602 41.01224 0) -122.6819 -43.19639
4      Reykjavik             POINT Z (-21.93333 64.13333 0) -122.6819 -43.19639
© www.soinside.com 2019 - 2024. All rights reserved.