0

I want to show points on the map. Those points are represented as coordinates (long/lat) and are stored in a data frame as numerics. I'm wondering if I really need to transform numeric coordinates (lon/lat) to sf object of POINT type. I don't know what benefits of such convertion are. In this example I use numeric coordinates from 2 columns and show them on the map:

data(quakes)

leaflet(data = quakes[1:20,]) %>% addTiles() %>%
  addMarkers(~long, ~lat)

Here data frame (quakes) is not spatial, lon/lat are numeric. However, I can do exactly the same converting numeric coordinates to sf POINTs, then show them on a map:

coords <- quakes %>%
  sf::st_as_sf(coords = c("long","lat"), crs = 4326) %>%
  sf::st_geometry()

leaflet(data = coords[1:20,]) %>% addTiles() %>%
  addMarkers()

Question is: in case of showing points (markers) on the map - should we convert to spatial objects first or just use numeric columns? What's the point of using POINTs at all if I can do exactly the same using numeric lon/lat? Performance issues?

mustafa00
  • 751
  • 1
  • 7
  • 28
  • If you want, you can use points in `tm_dots` in `tmap` rather than the coordinates using `leaflet`. – william3031 Jan 13 '21 at 23:34
  • Ok, I know that I can use it but what's the difference in using sf POINTs or numeric coordinates (float)? Maybe it's no difference? Or maybe it's all about performance or precision on the map? – mustafa00 Jan 14 '21 at 13:10

1 Answers1

1

You don't need to convert, and if the only purpose of your spatial object is drawing a {leaflet} map - i.e. you do no other calculations, statistics, modelling or so on - there is little point in transforming.

The {leaflet} documentation is quite straigforward about data types supported - {sf} objects, {sp} objects & regular lat / lon data.frames.

The only - very slight - benefit of using one of the special spatial formats is that you don't need to specify your latitude & longitude mappings in your addMarker() call. I wouldn't expect any effect on performance.

Note though that while {leaflet} supports multiple ways of data input it will not be able to process data in projected coordinate systems (eastings and northings).

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44