标签: geosphere

使用gcIntermediate在R小册子/ Shiny中映射穿过日期行的最短飞行路径

我正在使用R-Leaflet创建澳大利亚机场及其国际目的地的地图.

这是我的示例数据:

df<-data.frame("Australian_Airport" = "Brisbane", 
           "International" =  c("Auckland", "Bandar Seri Begawan","Bangkok","Christchurch","Denpasar","Dunedin","Hamilton","Hong Kong","Honiara","Kuala Lumpur"),
           "Australian_lon" = c(153.117, 153.117,153.117,153.117,153.117,153.117, 153.117, 153.117, 153.117, 153.117),
           "Australian_lat" = c(-27.3842,-27.3842,-27.3842,-27.3842,-27.3842,-27.3842, -27.3842, -27.3842, -27.3842, -27.3842),
           "International_lon" = c(174.7633, 114.9398, 100.5018, 172.6362, 115.2126,-82.77177, -84.56134, 114.10950, 159.97290, 101.68685),
           "International_lat" = c(-36.848460, 4.903052, 13.756331, -43.532054,-8.670458,28.019740, 39.399501, 22.396428, -9.445638,  3.139003)
           )
Run Code Online (Sandbox Code Playgroud)

我认为使用gcIntermediate使用弯曲的飞行路径会很酷,所以我创建了一个SpatialLines对象:

library(rgeos)
library(geosphere)

p1<-as.matrix(df[,c(3,4)])

p2<-as.matrix(df[,c(5,6)])

df2 <-gcIntermediate(p1, p2, breakAtDateLine=F, 
                    n=100, 
                    addStartEnd=TRUE,
                    sp=T) 
Run Code Online (Sandbox Code Playgroud)

然后我用小册子和Shiny绘制它:

server <-function(input, output) {

airportmap<- leaflet() %>% addTiles() %>% 
    addCircleMarkers(df, lng = df$Australian_lon, lat = df$Australian_lat, 
    radius …
Run Code Online (Sandbox Code Playgroud)

r leaflet shiny geosphere

4
推荐指数
2
解决办法
654
查看次数

了解软件包geosphere的areaPolygon()输出

这个问题中,我得到了一个很棒的函数areaPolygon(),它给了我一个多边形坐标内的面积。但是,当我尝试使用该函数时,计算结果似乎很奇怪:

我首先创建两个点

 require(fields)
 coords <- c(11.3697193956209, 47.233380520521, 11.3723606043791, 
 47.235179479479)
 coords <- matrix(coords, nrow=2, ncol=2, byrow=TRUE)
Run Code Online (Sandbox Code Playgroud)

然后检查这两者之间的距离:

 rdist.earth(coords,coords,miles=FALSE)[1,2]
Run Code Online (Sandbox Code Playgroud)

获得:0.2827821公里(将是矩形的对角线)

我继续创建一个矩形

 polygon <- matrix(coords, nrow=2, ncol=2)
 polygon <- rbind(polygon, polygon)
 polygon[4,2] <- polygon[1,2]
 polygon[4,1] <- polygon[2,1]
 polygon[3,2] <- polygon[2,2]
 polygon[3,1] <- polygon[1,1]
 polygon <- rbind(polygon, polygon[1,])
Run Code Online (Sandbox Code Playgroud)

看看这看起来是否不错: plot(polygon)

第四步:计算多边形内的面积。

geosphere::areaPolygon(polygon)
[1] 31.99288 #from the help file I know this ought to be square metres.
Run Code Online (Sandbox Code Playgroud)

但是,我可以预料到,200*200=40000 m²因为我的矩形的边长是200 x 200米。可以通过检查

rdist.earth(polygon,coords,miles=FALSE)

           [,1]         [,2]
 [1,] 0.0000000 2.827821e-01
 [2,] 0.2827821 …
Run Code Online (Sandbox Code Playgroud)

r geosphere

3
推荐指数
1
解决办法
813
查看次数

使用dplyr :: mutate计算到数据点的地理距离

我试图使用R与tidyverse包,并在将函数应用于我的数据时遇到问题.我的数据包括纬度/经度坐标,我想计算从每个位置(我的数据帧的行)到参考位置的距离.我正在尝试使用geosphere :: distm函数.

library(tidyverse)
library(geosphere)

my_long <- 172
my_lat <- -43

data <- data %>%  rowwise() %>% mutate(
  dist = distm(c(myLong, myLat), c(long, lat), fun=distHaversine) # this works
)
Run Code Online (Sandbox Code Playgroud)

我使用该rowwise()函数,如上所述,但这已被弃用,所以我想知道如何使用现代tidyverse,即,dplyr或者purrr,我认为,例如我最接近的是使用map2:

my_distm <- function(long1, lat1, long2, lat2)
  distm(c(long1, lat1), c(long2, lat2), fun=distHaversine)

data <- data %>%  mutate(
  dist = map2(long, lat, my_distm, my_long, my_lat) # this doesn't
)
Run Code Online (Sandbox Code Playgroud)

到目前为止,我失败了.

r dplyr tidyverse geosphere

3
推荐指数
2
解决办法
951
查看次数

R:大数据的区别?计算两个矩阵之间的最小距离

我有两个矩阵,一个是200K行,另一个是20K.对于第一个矩阵中的每一行(也就是一个点),我试图找到第二个矩阵中哪一行(也是一个点)最接近第一个矩阵中的点.这是我在样本数据集上尝试的第一种方法:

#Test dataset
pixels.latlon=cbind(runif(200000,min=-180, max=-120), runif(200000, min=50, max=85))
grwl.latlon=cbind(runif(20000,min=-180, max=-120), runif(20000, min=50, max=85))
#calculate the distance matrix
library(geosphere)
dist.matrix=distm(pixels.latlon, grwl.latlon, fun=distHaversine)
#Pick out the indices of the minimum distance
rnum=apply(dist.matrix, 1, which.min)
Run Code Online (Sandbox Code Playgroud)

但是,Error: cannot allocate vector of size 30.1 Gb当我使用该distm功能时出现错误.

关于这个主题有几个帖子:

这个bigmemory用于计算SAME数据帧中各点之间的距离,但我不确定如何调整它来计算两个不同矩阵中点之间的距离... https://stevemosher.wordpress.com/2012/04/12 /缺口司托克斯-距离代码现在与-大存储器/

这个也适用于计算SAME矩阵中各点之间的距离矩阵... 用于重复距离矩阵计算的高效(记忆方式)函数和超大距离矩阵的分块

这个与我想做的几乎完全相同,但他们实际上没有提出适用于大数据的解决方案:R:使用大内存的distm我试过这个方法,它使用bigmemory但是得到Error in CreateFileBackedBigMatrix(as.character(backingfile), as.character(backingpath), : Problem creating filebacked matrix.错误,我认为因为数据帧太大了.

有没有人想出这个问题的好方法?我对其他包装的想法持开放态度!

更新了解决问题的代码

pixels.latlon=cbind(runif(200000,min=-180, max=-120), runif(200000, min=50, max=85))
grwl.tibble = tibble(long=runif(20000,min=-180, max=-120), lat=runif(20000, min=50, …
Run Code Online (Sandbox Code Playgroud)

r great-circle r-bigmemory geosphere

3
推荐指数
1
解决办法
552
查看次数

R:计算从一个点到另一个点的距离(以英里为单位)

我有以下数据框:

library(dplyr)

d1 <- data_frame(
title = c("base1", "base2", "base3", "base4"),
lat = c(57.3, 58.8, 47.2, 57.8),
long = c(0.4, 3.4, 3.5, 1.2))

d2 <- data_frame(
tas = c("tas1", "tas2", "tas3", "tas4"),
Base= c ("base1", "base2", "base3", "base4"),
lat=c(54.6, 56.4, 54.2, 54.6),
long = c(1.2, 3.4, 3.5, 56.6))
Run Code Online (Sandbox Code Playgroud)

我想做的是计算d2中tas与d1中标题之间的距离(以英里为单位)。因此,在d2中,tas1的坐标为54.6 lat,长度为1.2,在'Base'列中具有'base1'。所以我想计算54.6lat x 1.2long和57.3lat与0.4lon之间的距离。

我尝试使用GeoDistanceInMetresMatrix下面详细介绍的函数来执行此操作,但是该函数并没有给我想要的结构。

下面的文章提供了有关GeoDistanceInMetresMatrix的一些信息

http://eurekastatistics.com/calculating-a-distance-matrix-for-geographic-points-using-r/

这是我希望数据看起来像的样子:

 df <- data_frame(
tas = c("tas1", "tas2", "tas3", "tas4"),
Base= c ("base1", "base2", "base3", "base4"),
lat=c(54.6, 56.4, 54.2, 54.6),
long = c(1.2, …
Run Code Online (Sandbox Code Playgroud)

r geospatial dplyr geosphere

3
推荐指数
2
解决办法
1119
查看次数

R 查找两个美国邮政编码列之间的距离

我想知道使用 R 计算两个美国邮政编码列之间的距离的最有效方法是什么。

我听说过用于计算邮政编码之间差异的 geosphere 包,但并不完全理解它,并且想知道是否还有其他方法。

例如说我有一个看起来像这样的数据框。

 ZIP_START     ZIP_END
 95051         98053
 94534         94128
 60193         60666
 94591         73344
 94128         94128
 94015         73344
 94553         94128
 10994         7105
 95008         94128
Run Code Online (Sandbox Code Playgroud)

我想创建一个看起来像这样的新数据框。

 ZIP_START     ZIP_END     MILES_DIFFERENCE
 95051         98053       x
 94534         94128       x
 60193         60666       x
 94591         73344       x
 94128         94128       x
 94015         73344       x
 94553         94128       x
 10994         7105        x
 95008         94128       x
Run Code Online (Sandbox Code Playgroud)

其中 x 是两个邮政编码之间的英里差。

计算此距离的最佳方法是什么?

这是创建示例数据框的 R 代码。

df <- data.frame("ZIP_START" = c(95051, 94534, 60193, 94591, 94128, 94015, 94553, 10994, 95008), "ZIP_END" = …
Run Code Online (Sandbox Code Playgroud)

r distance tidyverse geosphere

2
推荐指数
1
解决办法
1862
查看次数

使用mutate中的distm函数计算两点之间的距离

我正在尝试计算两组经度和纬度坐标之间的距离。

我正在使用 geosphere 包中的函数 distm() 来执行此操作。

如果我手动将值放入 distm() 函数中,它工作正常,但我无法让它在我的 mutate 命令中工作。

在 mutate 函数中运行它时,出现错误:

Error in mutate_impl(.data, dots) : 
Evaluation error: Wrong length for a vector, should be 2.
Run Code Online (Sandbox Code Playgroud)

@Dotpi 在评论中写道:“一个小笔记。方法 geosphere:distm 没有被矢量化。要对其进行矢量化,请使用 apply 函数。” 当他在此线程中回复时(使用 R 计算两点(纬度,经度)之间的地理空间距离的函数

由此我猜测这就是导致 mutate 函数出错的原因,但我不知道如何解决。我更喜欢 tidyverse 解决方案,但任何帮助表示赞赏。

下面是一个测试数据框,首先是产生错误的代码,然后是一个工作示例,我在 DF 中手动插入第一行的值。

library(tidyverse)
library(geosphere)

set.seed(1)
DF <- tibble(
  Long1 = sample(1:10),
  Lat1 = sample(1:10),
  Long2 = sample(1:10),
  Lat2 = sample(1:10))

DF %>% mutate(
  Dist = distm(x= c(Long1, Lat1), y=c(Long2, Lat2), fun = distHaversine …
Run Code Online (Sandbox Code Playgroud)

r dplyr geosphere

1
推荐指数
1
解决办法
2053
查看次数