I'm having an issue when attempting to reproduce the greatcircle connection map over at http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/ . I am encountering the following error when running the loop for the function:
Error in if (antipodal(p1, p2)) { : missing value where TRUE/FALSE needed
Incidentally, the function runs ok when it is outside of the loop, but I can't see what is wrong with the setup of the loop to cause the problem.
This error seems to be a common one encountered when trying to display data this way and has something to do with NULL values being passed to antipodal. I'm having difficulty finding were those might be in my data. I have removed 'to' and 'from' destinations that are the same/overlay which could result in a 0 distance drawn for the greatcircle. That was reported to be the problem to a similar error on SO here:
Antipodal error in Great Circles code
The original code uses SQL queries to assemble the tables for gcIntermediate but I've written these out as tables if anyone wants to run the code and see for themselves.
library(maptools)
library(rgeos)
library(sp)
library(geosphere)
fsub = read.csv("fsub.csv")
dfCord = read.csv("dfCord.csv")
dfBind = cbind(as.numeric(dfCord$lon), as.numeric(dfCord$lat))
sp = SpatialPoints(dfBind)
plot(sp)
for (j in 1:length(fsub$MODE9)) {
air1 <- dfCord[dfCord$sa2_main11 == fsub[j,]$O_SA2_11,]
air2 <- dfCord[dfCord$sa2_main11 == fsub[j,]$D_SA2_11,]
inter <- gcIntermediate(c(as.integer(air1[1,]$lon), as.integer(air1[1,]$lat)), c(as.integer(air2[1,]$lon), as.integer(air2[1,]$lat)), n=100, addStartEnd=TRUE)
lines(inter, col="black")
}
The data files can be found on GIT.
dfCord.csv fsub.csv
https://github.com/GaryPate/R-Greatcircles/commit/e1149ccdb7ab13b89f5f11e8ebad66f26ec3e39b
Many thanks!