Search code examples
rkmlr-sf

How to get coordinates in R from kml file using st_read


I'm working with r and i'm trying to get from a KML file the geographic coordinates (lat and long). I'm using a KML file that i created in Google Earth and i get the coordinates in right way using st_read() from sf package in the next code:

Google_earth_kml <- st_read("prueba_direcciones_google_earth.kml")

The thing is that i get the next table:

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)

And i want to get a table like:

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

This is just a little part of my data. I know that maybe you will need the KML file but for reasons of politics and privacy i cant share it.

I wonder if someone can give me an opinion or other point of view. Any help will be grateful thanks.


Solution

  • We can use st_coordinates() for this.

    For the data, I just found an example KML file with point geometries online. I recommend trying that when you have data you are unable to share in the future.

    Data

    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

    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.68194  45.52000
    2 Rio de Janeiro             POINT Z (-43.19639 -22.9083...  -43.19639 -22.90833
    3       Istanbul              POINT Z (28.97602 41.01224 0)   28.97602  41.01224
    4      Reykjavik             POINT Z (-21.93333 64.13333 0)  -21.93333  64.13333
    

    And if you want to get rid of the geometry column:

    output %>% st_drop_geometry()
    
                Name Description       long       lat
    1       Portland             -122.68194  45.52000
    2 Rio de Janeiro              -43.19639 -22.90833
    3       Istanbul               28.97602  41.01224
    4      Reykjavik              -21.93333  64.13333