在地图上绘制线 - gcIntermediate [英] Plotting lines on map - gcIntermediate

查看:137
本文介绍了在地图上绘制线 - gcIntermediate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码很好地产生了从A点到B点的地图和线,然而对于远东半球的国家来说,这条线路试图越过最短的路径(例如从澳大利亚东部),并打破直线横跨阴谋有什么建议么?我缩短了代码,并将其全部包含在下面。



使用greatCircle提到了(在代码中的链接),但我无法让这个工作。



谢谢!

 添加<  -  c(阿根廷,
澳大利亚,
德国,
日本,
韩国)

#人们从'所有这些点'到'海德堡
add0< ; - 盐湖城,UT

#从< - geocode(添加)
获得lat / lon
到< - geocode(add0)



#查看:http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/
库(地图)
库(geosphere)


#将需要调整这些限制...
xlim < - c(-170,200)
ylim <-C(-50,95)

quartz(file =UCC.pdf,
type =pdf,
height = 5,width = 9)

#print地图本身
map(world,
fill = TRUE,
xlim = xlim,
ylim = ylim,
#projection =墨西哥,
#orientation = c(90,-111,0),
col = gray(0.50),
bg = gray(0.08),
lwd =

#以下获取在球体上的两点之间移动的线的2D投影
for(i in 1:nrow(from)){
inter < - gcIntermediate( c(from [i,lon],
from [i,lat]),
c(to [1,lon],
to [1,lat ]),
n = 500,addStartEnd = T)

#和绘图线
线(inter,
col = gray(0.90),
lwd = 1)
}

dev.off()


想出答案。 breakAtDateLine需要设置为true。这通过单独绘制线的每个部分来分隔列表和以下代码帐户。向DA发出呼吁以协助此事。

  for(i in 1:nrow(from)){
inter < - gcIntermediate(c(from [我的lon],
来自[i,lat]),
c(到[1,lon],
到[1,lat]),
n = 100,addStartEnd = TRUE,breakAtDateLine = T)

if(is.list(inter)){
inter1 < - inter [[1]]
inter2< - inter [[2]]
lines(inter1,
col = gray(0.90),
lwd = .75)
lines(inter2,
col = gray(0.90),
lwd = .75)
} else {
#和plot lines
lines(inter,
col = gray(0.90)
lwd = .75)

}}

dev.off()


My code works well to produce a map and lines from point A to point B, however for countries in the far eastern hemisphere, the line attempts to cross the shortest path (e.g. east from Australia) and breaks to create a straight line across the plot. Any suggestions? I shortened the code and included it all below to play with.

There was mention (in the link in the code) of using greatCircle, but I could not get this to work.

Thanks!

adds <- c("Argentina",
          "Australia",
          "Germany",
          "Japan",
          "Korea")

# people are coming 'from' all those spots 'to' heidelberg
add0 <- "Salt Lake City, UT"

# get lat / lon
from <- geocode(adds)
to <- geocode(add0)

from

# see: http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/
library(maps)
library(geosphere)


# will need to adjust these limits...
xlim <- c(-170, 200)
ylim <- c(-50, 95)

quartz(file = "UCC.pdf",
       type = "pdf",
       height = 5, width = 9)

#print the map itself
map("world",
    fill=TRUE, 
    xlim=xlim, 
    ylim=ylim,
    # projection = "mercator",
    # orientation=c(90,-111, 0),
    col = grey(0.50),
    bg = grey(0.08), 
    lwd=0.05)

# following gets the 2D projection of a line moving between two points on a sphere
for (i in 1:nrow(from)) {
  inter <- gcIntermediate(c(from[i, "lon"], 
                            from[i, "lat"]), 
                          c(to[1, "lon"], 
                            to[1, "lat"]), 
                          n=500, addStartEnd = T)

  # and plot lines
  lines(inter, 
        col = grey(0.90), 
        lwd = 1)
}

dev.off()

解决方案

Figured out the answer. The breakAtDateLine needs to be set at true. This separates the list and the below code accounts for this by drawing each section of the line separately. Shout-out to DA for the assistance with this.

for (i in 1:nrow(from)) {
  inter <- gcIntermediate(c(from[i, "lon"], 
                            from[i, "lat"]), 
                          c(to[1, "lon"], 
                            to[1, "lat"]), 
                          n=100, addStartEnd=TRUE, breakAtDateLine = T)

if (is.list(inter)) {
  inter1 <- inter[[1]] 
  inter2 <- inter[[2]]
  lines(inter1,
        col = grey(0.90),
        lwd = .75)
  lines(inter2,
        col = grey(0.90),
        lwd = .75)
} else {
  # and plot lines
  lines(inter,
        col = grey(0.90),
        lwd = .75)

}}

dev.off()

这篇关于在地图上绘制线 - gcIntermediate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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