1

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!

www
  • 38,575
  • 12
  • 48
  • 84
Praxis
  • 934
  • 2
  • 17
  • 31

2 Answers2

2

This is not an answer. But only posted here for ability to format debugging output. You are trying to do ... something ... not stated here. The first and second rows of air2 both have NA's.

> which( is.na( geosphere:::.interm( c(as.integer(air1[1, ]$lon), as.integer(air1[1, 
+        ]$lat)), c(as.integer(air2[1, ]$lon), as.integer(air2[1, 
+        ]$lat) ) )
+ )
+ )
Error in if (antipodal(p1, p2)) { : missing value where TRUE/FALSE needed
> traceback()
2: geosphere:::.interm(c(as.integer(air1[1, ]$lon), as.integer(air1[1, 
       ]$lat)), c(as.integer(air2[1, ]$lon), as.integer(air2[1, 
       ]$lat)))
1: which(is.na(geosphere:::.interm(c(as.integer(air1[1, ]$lon), 
       as.integer(air1[1, ]$lat)), c(as.integer(air2[1, ]$lon), 
       as.integer(air2[1, ]$lat)))))
> which( is.na( antipodal( c(as.integer(air1[1, ]$lon), as.integer(air1[1, 
+        ]$lat)), c(as.integer(air2[1, ]$lon), as.integer(air2[1, 
+        ]$lat) ) )
+ ))
[1] 1
> c( c(as.integer(air1[1, ]$lon[1]), as.integer(air1[1, 
+        ]$lat)[1]), c(as.integer(air2[1, ]$lon[1]), as.integer(air2[1, 
+        ]$lat[1]) ) 
+ )
[1] 151 -33  NA  NA
> as.integer(air2[1, ]
+ )
[1] NA NA NA NA
> as.integer(air2[2, ])
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks for the traceback 42- I just wanted to report this problem solved in the end. There were two reasons that the loop was failing: I was converting the string sequence to as.integer rather than as.numeric. This resulted in lat long points being truncated and overlaying which caused null distances in greatcircle draws. The other reason was that my SQL query was not strict enough and was including destination points that did not exist in my lat long table. No great mystery in the end. Just missing data entries. – Praxis Jul 31 '16 at 03:28
  • You might consider upvoting a useful response if it assisted in finding the answer. You might also post a real answer(and checkmark it after a suitable interval) so that others can learn from our efforts. – IRTFM Jul 31 '16 at 15:51
0

Yes, I also encountered this warning. But after I double checked my dataset and made sure that each point in edge list existed in the latitude-longitude node set, the loop worked smoothly. This problem may result from the unmatched data between nodes and edges.