Calculating Distances between Two Points with Geosphere::distm and Leaflet

Calculating Distances between Two Points with Geosphere::distm and Leaflet

Introduction

When working with geographic data, calculating distances between two points is a common task. In this article, we’ll explore how to calculate distances using the geosphere package in R and compare the results with those obtained from the popular mapping library, Leaflet.

Understanding Geosphere::distm

The geosphere package provides functions for geographic calculations, including distance calculations between two points on the Earth’s surface. The distm() function calculates the distance between two points using the Haversine formula, which is a method for calculating distances over spherical surfaces such as the Earth.

Understanding Leaflet

Leaflet is a JavaScript library used for creating interactive maps in web browsers. It provides an easy-to-use API for adding tiles, markers, and polygons to a map. When working with Leaflet, it’s often necessary to calculate distances between points on the map to determine their spatial relationships.

Comparing Geosphere::distm and Leaflet

In this article, we’ll compare the results of calculating distances using geosphere::distm and Leaflet. We’ll start by creating two data frames containing lat-long points: points for a set of locations and points.at.radius for a set of locations 50 miles away from the first dataset.

Creating the Data Frames

# Create test points
dc.lat <- c(38.0000)
dc.long <- c(-97.0000)

lats <- c(dc.lat - 10, dc.lat - 5, dc.lat, 5 + dc.lat, 10 + dc.lat)
points <- data.frame(cbind(dc.long, lats))
names(points) <- c("long", "lat")
coordinates(points) <- ~ long + lat

radius <- 50 * 1609
points.at.radius <- data.frame(points)
# points$lat <- points$lat + radius/110.54
points.at.radius$long <- points$long + radius / 69.2
coordinates(points.at.radius) <- ~ long + lat

Calculating Distances with Geosphere::distm

Next, we’ll calculate the distances between the two data frames using geosphere::distm.

distances <- distm(points@coords[1,], points.at.radius@coords,
                  fun = distHaversine) / 1609
points$Distance2radius <- apply(distances, 1, min)

Plotting the Data with Leaflet

Now that we have calculated the distances, let’s plot the data using Leaflet.

m <- leaflet() %>% 
  addTiles() %>%
  addCircleMarkers(data = points,
                   color = "red") %>%
  addPolygons(
    data = gBuffer(points,
                  width = radius / 69.2,
                  joinStyle = "ROUND",
                  byid = FALSE
    ),
    color = "gray70",
    group = "IWER area"
  ) %>%
  addMarkers(data = points.at.radius,
             points.at.radius$long,
             points.at.radius$lat) %>%
  addPopups(points$long,
            points$lat,
            47.597131,
            points$Distance2radius,
            options = popupOptions(closeButton = FALSE)
  )

Calculating Distances with Geobuffer

To calculate the distances, we can use the geobuffer package.

library(geobuffer)

coordinates <- data.frame(lon = -97,
                          lat = seq(28, 48, 5))

points <- SpatialPoints(coords = coordinates,
                      proj4string = CRS("+init=epsg:4326"))

radius <- 50 * 1609

buffered_points <- geobuffer::geobuffer_pts(xy = points,
                                            dist_m = radius,
                                            step_dg = 360 / 8)

Calculating Distances with Leaflet

We can also calculate the distances using Leaflet.

m <- leaflet() %>% 
  addTiles() %>%
  addCircleMarkers(data = points,
                   color = "red") %>%
  addPolygons(
    data = buffered_points,
    color = "gray70"
  )

Comparing Results

When we compare the results, we can see that both methods give us different distances.

distances <- distm(points@coords[1,], 
                  points.at.radius@coords,
                  fun = distHaversine) / 1609
print(distances)

The geobuffer method gives us a more accurate result.

Conclusion

In this article, we’ve compared the results of calculating distances between two points using geosphere::distm and Leaflet. We’ve seen that both methods can be used to calculate distances, but they have different assumptions about the shape of the Earth’s surface. The geobuffer method gives us a more accurate result.

Additional Information

If you want to learn more about how to use geosphere, I recommend checking out their documentation and examples.

Additionally, if you’re interested in learning more about Leaflet, I recommend checking out their documentation and examples.

References

  • “geosphere” R package
  • “geobuffer” R package
  • Leaflet documentation

Last modified on 2024-08-02