0

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
JS93
  • 25
  • 5
  • lon-lat can be plotted easily in a leaflet.. read some documentation on the `sf`-package. Vignettes 1-6 are here: https://r-spatial.github.io/sf/articles/sf1.html. If your data is no lon-lat, then please specify what XY coordinates are used in your sample data. – Wimpel Apr 08 '19 at 11:53
  • 1
    do you know what coordinate system is used for the XY-coordinates? – Wimpel Apr 08 '19 at 12:06
  • I don't, no. The website the data are extracted from doesn't mention which system is used.It's possible it's System 34 (local Danish). – JS93 Apr 08 '19 at 12:09
  • Is Trinbak a typo (i.e. is it Trimbak, India)? We need to find out what CRS the data are in. Please edit your question and link to the data if possible, or if you have your data loaded into `R` get the CRS with `sf::st_crs()` and post the output – Phil Apr 08 '19 at 12:11
  • We really need to know what the CRS is otherwise there's not much we can do unfortunately. Alternatively you could geocode the original addresses https://philmikejones.me/tutorials/2017-05-30-geocoding-google-geocoding-api-googleway/ – Phil Apr 08 '19 at 12:28
  • Trinbak is not a typo (Trinbak, Denmark). the sf::st_crs() command cannot find the system (NA as output). My guess is that it's System 34 (local Danish). And thank you very, very much for all your help so far. ` sf::st_crs(Mappe1) Coordinate Reference System: NA` – JS93 Apr 08 '19 at 12:29

1 Answers1

1

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

Community
  • 1
  • 1
Wimpel
  • 26,031
  • 1
  • 20
  • 37
  • Yes! Though, it doesn't look like it's entirely the correct position. But that might be an error in my database. Thank you very much. :) – JS93 Apr 08 '19 at 12:37
  • Perhaps I did not use the right crs in the line `st_set_crs( 2198 )`. You will have to experiment with that! – Wimpel Apr 08 '19 at 13:03