Plotting lines on map - gcIntermediate

元气小坏坏 提交于 2019-12-01 08:29:02

问题


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()

回答1:


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()


来源:https://stackoverflow.com/questions/25881626/plotting-lines-on-map-gcintermediate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!