首页 > 解决方案 > if (antipodal(p1, p2)) { 中的错误:(R Geosphere Package)中需要 TRUE/FALSE 的缺失值

问题描述

我正在尝试使用他们积累的 openflights.com 数据将所有主要机场及其航线的地图可视化。我不断收到此错误:“if (antipodal(p1, p2)) { : 缺少 TRUE/FALSE 需要的值”在最后并且非常卡住!任何帮助,将不胜感激。

#Source is from openflights.com, had to manually import dataset on R. 
colnames(routes)<-c("Airline","AirlineID","SourceAirport","SourceAirportID","Desination Airport","Desination AirportID","Codeshare","Stops","Equipment")


direct_flights<-routes[which(routes$Stops==0),]
direct_flights=na.omit(direct_flights)

colnames(airports)<-c("AirportID","Name","City","Country","IATA","ICAO","Latitude","Longitude","Alt","TImezone","DST","TZ database")

colnames(airports_all)<-c("AirportID","Name","City","Country","IATA","ICAO","Latitude","Longitude","Alt","TImezone","DST","TZ database")


tab<-table(direct_flights$SourceAirportID)

big.id <- names(tab)[tab>100] #Airports with more than 100 flights in and out
airport.index <- which(
  unlist(lapply(airports$AirportID,function(x){
    any(big.id==x)
  }))
)

airports <- airports[airport.index,]

direct_flights.source.index <- which(
  unlist(lapply(direct_flights$SourceAirportID,function(x){
    any(big.id==x)
  })))

direct_flights.target.index <- which(
  unlist(lapply(direct_flights$`Desination AirportID`,function(x){
    any(big.id==x)
  }))
)

direct_flights <- direct_flights[intersect(direct_flights.source.index,direct_flights.target.index),]

library(geosphere)
library(maps)

plot_my_connection=function( dep_lon, dep_lat, arr_lon, arr_lat, ...){
    inter <- gcIntermediate(c(dep_lon, dep_lat), c(arr_lon, arr_lat), n=10, addStartEnd=TRUE, breakAtDateLine=FALSE)             
    inter=data.frame(inter)
    diff_of_lon=abs(dep_lon) + abs(arr_lon)
    if(diff_of_lon > 180){
        lines(subset(inter, lon>=0), ...)
        lines(subset(inter, lon<0), ...)
    }else{
        lines(inter, ...)
        }
}


for(i in 1:nrow(unique(direct_flights))){
    node1 <- airports[airports$AirportID==direct_flights$SourceAirportID[i],]
    node2 <- airports[airports$AirportID==direct_flights$`Desination AirportID`[i],]
    map("world",col="white",fill=TRUE,bg="black",lwd=0.1,ylim = c(-90,90))
    points(airports_all$Longitude,airports_all$Latitude,pch=19,col="blue",cex=0.2)
    plot_my_connection(node1$Longitude[i], node1$Latitude[i],node2$Longitude[i],node2$Latitude[i],col="red")
    }


我不断得到这个: if (antipodal(p1, p2)) { 中的错误:需要 TRUE/FALSE 的缺失值

不知道这意味着什么,已经尝试了几个小时!任何帮助,将不胜感激。

标签: rdictionarygeosphere

解决方案


推荐阅读