- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 2 个列表( list1
, list2
),其中包含各个位置的纬度/经度。一个列表 ( list2
) 的位置名称是 list1
不具有。
我还想要 list1 中每个点的近似位置。所以我想在 list1
中指出一点, 尝试在 list2
中寻找最近的点并采取那个地方。我对 list1
中的每一点重复一遍.它还需要距离(以米为单位)和点的索引(在 list1
中),以便我可以围绕它构建一些业务规则 - 基本上这些是应该添加到 list1
的 2 个新列。 ( near_dist
, indx
)。
我正在使用 gdist
功能,但我无法让它与数据框输入一起使用。
示例输入列表:
list1 <- data.frame(longitude = c(80.15998, 72.89125, 77.65032, 77.60599,
72.88120, 76.65460, 72.88232, 77.49186,
72.82228, 72.88871),
latitude = c(12.90524, 19.08120, 12.97238, 12.90927,
19.08225, 12.81447, 19.08241, 13.00984,
18.99347, 19.07990))
list2 <- data.frame(longitude = c(72.89537, 77.65094, 73.95325, 72.96746,
77.65058, 77.66715, 77.64214, 77.58415,
77.76180, 76.65460),
latitude = c(19.07726, 13.03902, 18.50330, 19.16764,
12.90871, 13.01693, 13.00954, 12.92079,
13.02212, 12.81447),
locality = c("A", "A", "B", "B", "C", "C", "C", "D", "D", "E"))
最佳答案
要使用纬度/经度坐标计算两点之间的地理距离,您可以使用多个公式。 geosphere
包包含用于计算距离的 distCosine
、 distHaversine
、 distVincentySphere
和 distVincentyEllipsoid
。其中,distVincentyEllipsoid
被认为是最准确的,但在计算上比其他的更密集。
使用这些函数之一,您可以制作距离矩阵。基于该矩阵,您可以根据与 locality
的最短距离和与 which.min
的相应距离(请参阅答案的最后一部分)来分配 min
名称,如下所示:
library(geosphere)
# create distance matrix
mat <- distm(list1[,c('longitude','latitude')], list2[,c('longitude','latitude')], fun=distVincentyEllipsoid)
# assign the name to the point in list1 based on shortest distance in the matrix
list1$locality <- list2$locality[max.col(-mat)]
> list1
longitude latitude locality
1 80.15998 12.90524 D
2 72.89125 19.08120 A
3 77.65032 12.97238 C
4 77.60599 12.90927 D
5 72.88120 19.08225 A
6 76.65460 12.81447 E
7 72.88232 19.08241 A
8 77.49186 13.00984 D
9 72.82228 18.99347 A
10 72.88871 19.07990 A
locality
中
locality
的平均经纬度值分配
list2
:
library(dplyr)
list2a <- list2 %>% group_by(locality) %>% summarise_each(funs(mean)) %>% ungroup()
mat2 <- distm(list1[,c('longitude','latitude')], list2a[,c('longitude','latitude')], fun=distVincentyEllipsoid)
list1 <- list1 %>% mutate(locality2 = list2a$locality[max.col(-mat2)])
data.table
:
library(data.table)
list2a <- setDT(list2)[,lapply(.SD, mean), by=locality]
mat2 <- distm(setDT(list1)[,.(longitude,latitude)], list2a[,.(longitude,latitude)], fun=distVincentyEllipsoid)
list1[, locality2 := list2a$locality[max.col(-mat2)] ]
> list1
longitude latitude locality locality2
1 80.15998 12.90524 D D
2 72.89125 19.08120 A B
3 77.65032 12.97238 C C
4 77.60599 12.90927 D C
5 72.88120 19.08225 A B
6 76.65460 12.81447 E E
7 72.88232 19.08241 A B
8 77.49186 13.00984 D C
9 72.82228 18.99347 A B
10 72.88871 19.07990 A B
locality
。
list1$near_dist <- apply(mat2, 1, min)
max.col
的另一种方法(很可能更快):
list1$near_dist <- mat2[matrix(c(1:10, max.col(-mat2)), ncol = 2)]
# or using dplyr
list1 <- list1 %>% mutate(near_dist = mat2[matrix(c(1:10, max.col(-mat2)), ncol = 2)])
# or using data.table (if not already a data.table, convert it with 'setDT(list1)' )
list1[, near_dist := mat2[matrix(c(1:10, max.col(-mat2)), ncol = 2)] ]
> list1
longitude latitude locality locality2 near_dist
1: 80.15998 12.90524 D D 269966.8970
2: 72.89125 19.08120 A B 65820.2047
3: 77.65032 12.97238 C C 739.1885
4: 77.60599 12.90927 D C 9209.8165
5: 72.88120 19.08225 A B 66832.7223
6: 76.65460 12.81447 E E 0.0000
7: 72.88232 19.08241 A B 66732.3127
8: 77.49186 13.00984 D C 17855.3083
9: 72.82228 18.99347 A B 69456.3382
10: 72.88871 19.07990 A B 66004.9900
关于r - 2 个经纬度点列表(坐标)之间的地理/地理空间距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31668163/
我想知道地理数据、几何数据和数据库系统(例如 Oracle 或 SqlSqerver)中表示空间数据的标准数据类型之间有什么区别? 对我来说看起来是一样的,但我知道肯定有区别。. 最佳答案 首先,地理
我正在寻找用于存储 map 的理想数据库或数据结构的建议。本质上, map 由“道路”组成,如道路、路径等。道路包含节点(具有纬度和经度坐标,有时还有高度。) 任何此类数据库或结构: 应该能够快速(毫
我最近将一些预装的国家和省份形状下载到 Sql Server 2008 中 http://sqlsamplegeo.codeplex.com/ 除了地理列,每个国家和省都有一个“ShapeArea”和
我对google Analytics(分析)地理如何工作(特别是对于出差用户)有疑问。例如,如果我是森尼韦尔市的用户,并且我打开了一个应用程序,则Google ID在用户ID级别的分析将说用户数= 1
我正在使用 Geo::IP 对 IP 地址执行位置查找。一切正常,直到我遇到一个不在地理 IP 查找数据库中的 IP 地址并且程序突然关闭并给出此错误 Can't call method "city"
通用(或设备无关)物理位置的结构是什么?我的猜测是它可能是一个有两个长字段的结构,或者类似的东西。 此外,给定一个目的地位置和两个候选位置,是否有一种简单的算法可以确定哪个候选位置最接近目的地?我并不
我正在使用 d3 geomaps 创建美国的地理 map 。当我将鼠标悬停在状态上时,我希望突出显示该状态。那可能吗?这是我当前的代码:
在geoviews guide for Bokeh ,它指出 Bokeh 仅支持墨卡托投影,但大多数示例投影使用 PlateCarree,然后输出看起来像墨卡托投影。 有人知道图形的投影和输出是怎么回
我有一个geodataframe'all_locations',其中有一个几何列和一个带有点名称的列。在 map 上绘制点工作正常,但我想用位置名称注释这些点。 ['位置'] ['几何'] BUITH
我正在尝试在 Canvas 中缩放 map 。 var projection = d3.geoMercator() projection.fitExtent([[margin.left, margin
我目前正在处理这段代码,一切正常,但是当我用鼠标悬停在一个国家/地区时,我只希望国家/地区名称出现在标签中,而不是相关值。我可以这样做吗?如果是,怎么办? 提前致谢! 这是javascript代码:
我正在尝试在 mac os 上使用 Geodjango,我使用 postgresql 并安装了 GEOS,但出现此错误: dlopen(/usr/local/lib/libgeos_c.dylib,
我正在研究 GeoViews,我想知道我们是否可以将 slider 作为 GeoViews 中等值线图的输入。 我在 gdf 中有另一个变量,它是年份。是否可以使用 slider 显示年度 Tot
我有一个 Geometry (看起来像 WKB) 我的 postgres/postgis 数据库中的对象,我已经转储到 BigQuery 来做一些有趣的事情。我看到它存储为 bytes .我如何构建一
我正在使用 DPM 进行 azure 在线备份,并且想要将冗余从 Geo 更改为 Local,但它显示为灰色。有办法改变吗? 我不想为此创建新的错误,然后我必须再次将所有内容从 DPM 重新上传到 A
我正在实现 Google 地理 map ,我想做的是,例如,如果我单击 US在 map 上,我会将国家/地区名称作为警报(例如,如果我单击美国,我必须将美国作为警报),我最终可以将其用作变量。当我尝试
我正在制作一张具有缩放和平移功能的世界地图。我在某些城市上画了圆圈,圆圈的半径由数据决定。当鼠标悬停在这些圆圈上时,将出现一个工具提示来显示数据。 代码结构为 //在此选择上调用缩放行为 - 让我们调
我需要一个函数来计算一对 WGS 84 之间的距离定位到高精度,我计划使用 boost geometry 中的 geographic 函数. boost geometry Design Rationa
我正在尝试缓冲数据集中半径为 100 公里的点。我正在使用函数 gBuffer来自包裹rgeos .这是我到目前为止所拥有的: head( sampledf ) # postalcode
我正在用 C# 开发一个应用程序,它将使用 SQL Server 2008 中的“地理”数据类型来使用和存储地理位置。我计划使用 Entity Framework ,但很快发现它不支持空间数据。有没有
我是一名优秀的程序员,十分优秀!