gpt4 book ai didi

r - 在 map 上绘制线条 - gcIntermediate

转载 作者:行者123 更新时间:2023-12-05 01:47:29 25 4
gpt4 key购买 nike

我的代码可以很好地生成 map 和从 A 点到 B 点的线,但是对于远东半球的国家/地区,该线会尝试穿过最短路径(例如从澳大利亚向东)并断开以创建一条直线整个情节。有什么建议么?我缩短了代码并将其全部包含在下面以供使用。

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

谢谢!

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

最佳答案

找出答案。 breakAtDateLine 需要设置为 true。这将列表分开,下面的代码通过分别绘制线的每个部分来说明这一点。向 DA 求助。

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

关于r - 在 map 上绘制线条 - gcIntermediate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25881626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com