Search code examples
rcoordinatesgeocodingggmapr-leaflet

Creating a map based on address or xy coordinates - convert to lat and long?


I have a probably very simple question (I'm rather new to R), but after searching for a while I didn't manage to find what I was looking for.

I need to create a map in R. Preferably with the Leaflet packages, but I'm absolutely open to other suggestions. My data is what causes issues. I have addresses and x y coordinates for all the points, but most map-making packages need latitude and longitude data.

Is there any way to convert either the addresses or the x y coordinates rather easily?

I've read that the geocode function should do that, but Google requires API to function for this, and I must admit I get lost at that point.

The data contains around 50 points, so it would be nice with a method to mass-convert to lat and long.

Thank you very much in advance.

Example of x,y coordinates

Name     x       y
Point_1  556305  6306381

Solution

  • something like this?

    looked up crs using: https://epsg.io/?q=denmark

    sample data

    df <- data.frame( city = "Trinbak", lon = 556305, lat = 6306381 )
    

    code

    library(sf)
    library(leaflet)
    
    df.sf <- st_as_sf( df, coords = c("lon", "lat") ) %>%
      st_set_crs( 2198 ) %>%   #set coordinate system used
      st_transform( 4326 )     #transform coordinates to WGS84 coordinates
    
    leaflet() %>% addTiles() %>% addMarkers( data = df.sf )
    

    output

    enter image description here

    update

    perhaps

    df.sf <- st_as_sf( df, 
                       coords = c("lon", "lat") ) %>%
      st_set_crs( 23032 ) %>%
      st_transform( 4326 )
    

    is more accurate?

    enter image description here