计算两两地点之间的空间距离
经纬度表格示例
Pop Lon Lat
shenyang 123.3087 41.6467
yingkou 122.4622 40.7922
#!/usr/bin/env Rscript
# 根据经纬度计算距离
require(geosphere);
# 读取数据
lonlat = read.table('Book2.txt', header=TRUE);
# matrix
muer.lonlat = cbind(lonlat$Lon, lonlat$Lat);
# 精确计算,椭圆
muer.dists = distm(muer.lonlat, fun=distVincentyEllipsoid);
rownames(muer.dists) = lonlat$Pop;
colnames(muer.dists) = lonlat$Pop;
write.table(muer.dists, 'muer_geodists.txt', quote=F, sep="\t") # 写出去
网友评论