端口之间的距离计算器 [英] Distance calculator between the ports

查看:192
本文介绍了端口之间的距离计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个端口数据库,其中包含将近1万个端口名称及其对应的位置.我想查找端口之间的距离.

I have a port database which contain nearly 10k port names and its corresponding locations.I want to find the distance between the ports.

根据我对Google距离矩阵API的理解,对于一组特定的出行方式,例如自行车,行车,步行和内部交通,我们只能找到两点之间的距离.

Form my understanding using Google distance matrix API, we can only able to find distance between the two points for a set of specific travel modes like bicycle,driving,walking and internal transit.

但是在我的情况下,我想在航行中找到两个港口之间的距离.如果有人做过类似的工作,请分享您的知识...

But In my case I want to find the distance between two ports during the ship voyage. If anyone done Similar sort of work please share your knowledge...

参考网站:[ http://www.portworld.com/map]

谢谢!

推荐答案

我遇到了同样的问题,并找到了解决方案.

I was faced with the same problem and found a solution.

1)首先,您必须创建一个世界地图栅格,其中包含船舶可以使用一个值的所有部分以及所有船舶不能使用另一个值的所有地方.

1) First you have to create a world map raster with all the parts where a ship can go one value and all the places where it can't go another value.

为此,我首先从所有国家/地区边界的形状文件开始,然后使用QGIS手动添加了苏伊士和巴拿马运河.然后,我还在北冰上添加了冰块. 我在R中使用rgdal,raster和gdistance软件包完成了其余工作.

For this I started with a shape file of all countries borders, added the Suez and Panama canal manually using QGIS. Then I also added the Ice in the Artic. The rest of the work I did in R using the packages rgdal, raster and gdistance.

install.packages("rgdal")
install.packages("raster")
install.packages("gdistance")
library(rgdal)
library(raster)
library(gdistance)

我首先创建了一个空栅格:

I first created an empty raster:

 new  <- raster(ncol=360*5, nrow= 180*5)
 projection(new) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

(通过更改列数,可以使栅格更加精确.当栅格较低时,地图看起来好像是由点构建的,而当栅格很大时,它们看起来都非常平滑.绘制的运河"分辨率太低时也会消失,因为它们无法与周围环境区分开.但是当计算时间随光栅的分辨率而大大增加时!!

(By changing the number of columns you make the raster more precise. When it is low the maps looks like it is build out of dots, while when it is very large it all looks very smooth. The "canals" you draw also disappear when you make the resolution too low. Because they can not be distinguished from the surroundings. However when the calculation time increases very strongly with the resolution of your raster!!)

#Import Shapefile
map <- readOGR(dsn = "location of shape file" , layer = "name shape file")
#Create map from shapefile and empty map
r <- rasterize(map, new)

#replace values to 1 and 99999
values(r)[is.na(values(r))] <- 1
values(r)[values(r)>1] <- 99999

因此,您最终得到的地图中,一艘船可以行驶的所有地点都等于1,而一艘船不能行驶的所有地点都等于99999.

Thus you end up with a map where all places that a ship can go equal to 1 and all places where a ship can't go are 99999.

2)将端口的坐标保存在名为端口"的矩阵中.第一列分别是经度,第二列分别是纬度.

2) Save the coordinates of the ports in a matrix named "ports". Where the first column are the longitude and the second column the latitude respectively.

应用计算最短路径所需的过渡,并对地理校正应用更多信息,有关地理校正,您可以找到

Apply the transition needed to calculate the shortest path and apply the geo-correction more info on the geo-correction you can find here.

p <- transition(r, function(x){1/mean(x)}, 8)
p <- geoCorrection(p)

3)运行循环以计算所有端口之间的最短路径.最短路径的长度存储在称为结果"的三列矩阵中. 整个循环并行运行以加快速度.

3) Run a loop to calculate the shortest path between all the ports. The length of the shortest paths are stored in a three column matrix called "results". The whole loop is run in parallel to speed things up.

install.packages("doParallel")
install.packages("foreach")
library(foreach)
library(doParallel)
# Create cluster with number of cores of the system.
cl <- makeCluster(detectCores())
registerDoParallel(cl)

i <- 1
nrow_data <- nrow(ports)
results <- foreach(i=icount(nrow_data), .combine='rbind', .packages="gdistance") %dopar% {
        A <- cbind(ports[i,1],ports[i,2])
        r <- matrix(NA, ncol=3,nrow=nrow_data)
        r[,1] <- i
        j <- i+1
        while(j<=nrow_data){
                r[j,2] <- j
                B <- cbind(ports[j,1],ports[j,2])
                tryCatch({
                        path <- shortestPath(p, A,B, output = "SpatialLines")
                        r[j,3] <- SpatialLinesLengths(path ,longlat=TRUE)
                }, error=function(e){})
                j <- j+1
        }
        r[1:nrow_data,]
}

我添加了"tryCatch"以消除当两个端口彼此非常靠近时有时会收到的错误.由于栅格的分辨率较低,因此无法区分这两个端口.

I added the "tryCatch" to eliminate the errors I sometimes received when two ports where located very close to each other. Because of the low resolution of the raster, it can not make a distinction between these two ports.

这可能不是做所有事情的最好方法,但是对我来说效果很好!

It is probably not the nicest way to do everything but it works very well for me!

这篇关于端口之间的距离计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆