Using greatCircle function to create a trade map in R

I am using greatCircle function to create a map. I am able to run the code and get the desired connecting lines across the locations. But additionally I am getting an arbitrary straight line in the middle of the map. I am not able to understand why it's happening. And how it can be dropped? I am pasting the picture and the code below. The data set I here use is in built data in the package "maps"


library("maps")
library("geosphere")


getGreatCircle <- function(userLL,relationLL){
  tmpCircle = greatCircle(userLL,relationLL, n=200)
  start = which.min(abs(tmpCircle[,1] - data.frame(userLL)[1,1]))
  end = which.min(abs(tmpCircle[,1] - relationLL[1]))
  greatC = tmpCircle[start:end,]
  return(greatC)}

par(mar=c(0,0,0,0))

# World map
map('world',
    col="#f2f2f2", fill=TRUE, bg="white", lwd=0.05,
    mar=rep(0,4),border=0, ylim=c(-80,80))



for(i in 1:60){for(j in 1:60){ if(i != j){inter<- getGreatCircle(c(world.cities$long[i],world.cities$lat[i]),c(world.cities$long[j],world.cities$lat[j]))
lines(inter, col="skyblue", lwd=0.2)}}
  
}
  

first I remove your code that truncates the circle, and just take the circle, then I plot for fewer location, but close to and including your problem which occurs at i = 5 and j = 17 ; conditionally colouring this as red to highlight it for you.


library("maps")
library("geosphere")


getGreatCircle <- function(userLL,relationLL){
  greatC = greatCircle(userLL,relationLL, n=200)
  # start = which.min(abs(tmpCircle[,1] - data.frame(userLL)[1,1]))
  # end = which.min(abs(tmpCircle[,1] - relationLL[1]))
  # greatC = tmpCircle[start:end,]
  return(greatC)}

par(mar=c(0,0,0,0))

# World map
map('world',
    col="#f2f2f2", fill=TRUE, bg="white", lwd=0.05,
    mar=rep(0,4),border=0, ylim=c(-80,80))


for(i in 4:5){for(j in 16:17){ if(i != j){
  col <- "skyblue"
  if(i==5 & j==17)
    col <- "red"
  
  inter<- getGreatCircle(c(world.cities$long[i],world.cities$lat[i]),c(world.cities$long[j],world.cities$lat[j]))
lines(inter, col=col, lwd=0.2)}}
}

Thank you for pointing out the problem. Any idea how it can be avoided?


library("maps")
library("geosphere")


getGreatCircle <- function(from,to){
  greatC = greatCircle(from,to, n=200)
  f1 <- which.min(dist(rbind(from,greatC))) 
  t1 <- which.min(dist(rbind(to,greatC)))
  gc_df <- tibble(data.frame(greatC)) %>% mutate(rn=row_number(),
                                                 part_curve=between(rn,f1,t1))
  gc_df %>% filter(part_curve) %>% select(lon,lat) %>% as.matrix()
}

par(mar=c(0,0,0,0))

# World map
map('world',
    col="#f2f2f2", fill=TRUE, bg="white", lwd=0.05,
    mar=rep(0,4),border=0, ylim=c(-80,80))

col <- "skyblue"
for(i in 1:60){for(j in 1:60){
  if(i != j){
  
  inter<- getGreatCircle(c(world.cities$long[i],
                           world.cities$lat[i]),
                         c(world.cities$long[j],
                           world.cities$lat[j]))
  
  lines(inter, col=col, lwd=0.2)
  
  }
}}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.