首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用geosphere::distm计算的两点之间的距离与使用leaflet绘制的距离不同

使用geosphere::distm计算的两点之间的距离与使用leaflet绘制的距离不同
EN

Stack Overflow用户
提问于 2019-06-11 20:27:45
回答 1查看 164关注 0票数 1

我有两个后来的长点的数据帧。DatasetA是一组位置。数据集B是一组距离数据集A 50英里(纵向)的位置。

我可以在没有事件的情况下绘制这两个数据集,如下面的代码所示。但是,当我计算DatasetA和DatasetB的点(最近的点)之间的距离时,我得到了不同的距离:

代码语言:javascript
复制
> points$Distance2radius
[1] 44.13807 41.92467 39.39219 36.55992 33.44940

我很难理解为什么它们会有所不同。我假设在distm中使用Haversine公式将考虑到距离计算中的任何球形影响。

任何帮助都将不胜感激。

代码语言:javascript
复制
library(leaflet)
library(geosphere)

### Make a dataframe of some test points ###

## Center of the US
dc.lat <- c(38.0000)
dc.long <- c(-97.0000)

## Make the data frame
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

## The radius we are interested in, in miles
radius <- 50

## Add points that are the radius miles away from the center

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

## Get distances with distm
distances <- distm (points, points.at.radius,
                    fun = distHaversine) / 1609

# Find the closest pint and add this distance to the data set
points$Distance2radius <-
  apply(distances , 1, min)

# Plot these points and the points that are 50 miles away
m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addCircleMarkers(data = points,
                   points$long,
                   points$lat,
                   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)
             ) 

  m  # Print the map
EN

回答 1

Stack Overflow用户

发布于 2019-08-20 06:23:17

使用geobuffer非常简单。

代码

代码语言:javascript
复制
# Load libraries.
library(leaflet)
library(geosphere)
library(geobuffer)

# Set coordinates for center points.
coordinates <- data.frame(lon = -97,
                          lat = seq(28, 48, 5))

# Create SpatialPoints in the WGS84 spatial reference system,
# a.k.a. unprojected lon/lat.
points <- SpatialPoints(coords = coordinates,
                        proj4string = CRS("+init=epsg:4326"))

# Set radius as 50 miles.
radius <- 50 * 1609

# Created SpatialPolygons in the shape of an octagon. 
buffered_points <- geobuffer::geobuffer_pts(xy = points,
                                            dist_m = radius,
                                            step_dg = 360 / 8)

测试

代码语言:javascript
复制
> distm(points@coords[1,], 
      coordinates(buffered_points@polygons[[1]]@Polygons[[1]])) / 1609

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]   50   50   50   50   50   50   50   50   50

你会发现,这不仅适用于SpatialPolygon #1,也适用于其他三个对象。

结果

代码语言:javascript
复制
leaflet() %>%
    addTiles() %>%
    addCircleMarkers(data = points,
                     color = "red") %>%
    addPolygons(
        data = buffered_points,
        color = "gray70"
    )

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56543743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档