Convert from miles to reflect the true radius from coordinates

Hello,

I am using the following code: I want my circles to truly reflect the radiuses from a certain coordinate. In my data set I have my Max.Distance.from.C.or.HB (which is the radius of the person from the coordinate) in miles. Do I need to make any conversion to the radius in my code?

map7 <- leaflet() %>%
setView(lng = -95.7129, lat = 34.0902, zoom = 4.499) %>%
addProviderTiles(providers$Esri.DeLorme) %>%
#addPolygons(data = ak_counties,

color = "#660000",

weight = 1,

smoothFactor = 0.5) %>%

addCircleMarkers(lng = coverage_data$Longitude,
lat = coverage_data$Latitude,
color = ~pal(coverage_data$Inspector.Name),
weight = 1,
radius = coverage_data$Max.Distance.from.C.or.HB,
opacity = 0.75,
label = lapply(coverage_Monday$label, HTML),
group = "Monday")

I do not have access to your data, so bear with me with my example :slight_smile:

First of all:

  • you can not want to use addCircleMarkers(), as these have constant width in pixels (not changing with map zoom - see example below). You need an addCircles() call.
  • the default unit for distance in {leaflet} package are meters, you have to multiply your miles figure by 1609.344 (or add dependency to {units}, which seems unnecessary given the task)

Try the code below and watch the difference in behavior between red circles (stable in size, regardless of zoom) an green ones (changing in size with zoom level). Green behaviour is what you want :slight_smile:

library(sf)
library(leaflet)

shape <- st_read(system.file("shape/nc.shp", package="sf")) # included with sf package

cities <- data.frame(name = c("Greensboro", "Wilmington"),
                  lng = c( -79.819444, -77.912222),
                  lat = c(36.08, 34.223333))

leaflet() %>% 
  addProviderTiles(providers$Stamen.Toner) %>% 
  addPolygons(data = shape, fill = NA) %>% 
  addCircleMarkers(lng = cities$lng,
                   lat = cities$lat, 
                   radius = 10, 
                   col = "red") %>% 
  addCircles(lng = cities$lng,
             lat = cities$lat, 
             radius = 10 * 1609.344, # radius is in meters, adjust accordingly :)
             col = "green")

1 Like

Thank you! I just saw this. This is exactly what I did before coming here. I am glad that you confirmed my answers. My circles look great. Now is time for another question :D. But you are awesome for answering! Thanks again.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.