I have an issue that I have tried for seven hours to fix without any success. Basically, I am trying to visualize the airport and flight data from openflights.org. This was supposed to be a simple, straightforward visualization, but it has turned in to a real hair puller.
Everything works up until the for loop. When I try and run the for loop in order to depict the flight paths on my map, it runs for a little and some lines appear on the map, but then it quits with the error:
Error in if (antipodal(p1, p2)) { : missing value where TRUE/FALSE needed
What I tried to do to fix it: As you can see, I have gone through the data set and removed any bad entries. For example, there were some IDs that were "\\N" and therefore I removed those entries entirely. Then I tried to change the IDs to be numbers rather than strings, just to see what would happen.
It always errors out around the same time after I run the for loop, is there a way I can view the line it got to when it returned the error? I am new too R.
I know a similar question has been asked before but none of the solutions they had there worked for me. Thank you for any help you can provide.
Code:
library('geosphere')
library('maps')
# Reading in the files from openflights.org
airports <- read.csv("airports.dat.txt", header=FALSE, col.names=c("Airport ID","Name","City","Country","IATA","ICAO","Latitude","Longitude",
"Altitude","Timezone","DST", "TZ Database","Type","Source"), as.is=TRUE)
flights <- read.csv("routes.dat.txt", header=FALSE, col.names=c("Airline","Airline ID","Source Airport","Source Airport ID","Destination Airport",
"Destination Airport ID","Codeshare","Stops","Equipment"), as.is=TRUE)
# Cleaning the data set, there are some instances of the value "\\N" in the flights data set.
flights <- flights[!grepl("\\N", flights$Source.Airport.ID),]
flights <- flights[!grepl("\\N", flights$Destination.Airport.ID),]
# Converting all of the IDs to numbers (I thought this might work but it did not)
flights$Source.Airport.ID <- as.numeric(flights$Source.Airport.ID)
flights$Destination.Airport.ID <- as.numeric(flights$Destination.Airport.ID)
airports$Airport.ID <- as.numeric(airports$Airport.ID)
# Creating a world background
map("world", col="white", border="gray10", fill=TRUE, bg="gray30")
# Adding all of the airports as points
points(x=airports$Longitude, y=airports$Latitude, pch=19,
cex=0.05, col="blue")
# Generating a color set to be used for mapping the flight paths
col.1 <- adjustcolor("limegreen", alpha=0.05)
col.2 <- adjustcolor("darkgreen", alpha=0.05)
edge.pal <- colorRampPalette(c(col.1, col.2), alpha = TRUE)
edge.col <- edge.pal(100)
# Now, generating the visualization of the flight paths.
# Here is where the error occurs, when I run this.
# It gets through some of the data but then errors out.
for(i in 1:nrow(flights)) {
node1 <- airports[airports$Airport.ID == flights[i,]$Source.Airport.ID,]
node2 <- airports[airports$Airport.ID == flights[i,]$Destination.Airport.ID,]
arc <- gcIntermediate( c(as.numeric(node1[1,]$Longitude), as.numeric(node1[1,]$Latitude)),
c(as.numeric(node2[1,]$Longitude), as.numeric(node2[1,]$Latitude)),
n=1000, addStartEnd=TRUE)
#edge.ind <- round(100*table(flights[i,]$Source.Airport.ID) / table(max(airports$Airport.ID)))
#lines(arc, col=edge.col[edge.ind], lwd=edge.ind/30)
lines(arc, col = "limegreen", lwd = 0.02)
}