Function Overview
I have created a function to calculate directional changes between consequetive bearings (which was calculated from my latitude and longitude data).
The bearings are always relative to the North-South line (i.e., number of degrees away from north), and thus in parallel to one another and are ethier positive (0 -> +180) or negitive (0 -> -180). I have accounted for the signs (positive or negitive) and the 'magnitude' (>90 or <90) of the bearings which has allowed me to create 'rules' using basic trig to calculate directional changes.
The function evaluates two consequtive bearings (i.e., A --- TO --- B), classifies thier direction (positive or negitive) and the bearing values (>90 or <90). It uses these classifications to determine which 'rule' to use and then calculates directional change using the calculation associated with that rule. See the function below:
The Function
analyzeGPS_DirectionChange <- function(bearingVec1, bearingVec2) {
a1 <- ifelse (bearingVec1 >= 0.00, "positive", "negitive")
a2 <- ifelse (bearingVec2 >= 0.00, "positive", "negitive")
Bearing_Signs <- paste(a1, a2)
b1 <- ifelse (abs(bearingVec1) < 90.00, "less", "greater")
b2 <- ifelse (abs(bearingVec2) < 90.00, "less", "greater")
Bearing_Values <- paste(b1, b2)
print(Bearing_Signs)
print(Bearing_Values)
if (Bearing_Signs == "positive positive" && Bearing_Values == "less less") {
Direction_Change <- (90 - bearingVec1) + (90 + bearingVec2)
} else if (Bearing_Signs == "positive positive" && Bearing_Values == "less greater") {
Direction_Change <- (180 - bearingVec2) + bearingVec1
} else if (Bearing_Signs == "positive positive" && Bearing_Values == "greater less") {
Direction_Change <- (180 - bearingVec1) + bearingVec2
} else if (Bearing_Signs == "positive positive" && Bearing_Values == "greater greater") {
Direction_Change <- (bearingVec1 - 90) + (180 - bearingVec2) + 90
}
if (Bearing_Signs == "positive negitive" && Bearing_Values == "less less") {
Direction_Change <- 180 - (abs(bearingVec2) + bearingVec1)
} else if (Bearing_Signs == "positive negitive" && Bearing_Values == "less greater") {
Direction_Change <- 180 - (90 + (90 - bearingVec1) + (180 - abs(bearingVec2)))
} else if (Bearing_Signs == "positive negitive" && Bearing_Values == "greater less") {
Direction_Change <- 90 - ((bearingVec1 - 90) + abs(bearingVec2))
} else if (Bearing_Signs == "positive negitive" && Bearing_Values == "greater greater") {
Direction_Change <- 180 - ((180 - bearingVec1) + (180 - abs(bearingVec2)))
}
if (Bearing_Signs == "negitive positive" && Bearing_Values == "less less") {
Direction_Change <- 180 - (abs(bearingVec1) + bearingVec2)
} else if (Bearing_Signs == "negitive positive" && Bearing_Values == "less greater") {
Direction_Change <- 180 - (90 + (90 - abs(bearingVec1)) + (180 - bearingVec2))
} else if (Bearing_Signs == "negitive positive" && Bearing_Values == "greater less") {
Direction_Change <- 90 - ((abs(bearingVec1) - 90) + bearingVec2)
} else if (Bearing_Signs == "negitive positive" && Bearing_Values == "greater greater") {
Direction_Change <- 180 - ((180 - abs(bearingVec1)) + (180 - bearingVec2))
}
if (Bearing_Signs == "negitive negitive" && Bearing_Values == "less less") {
Direction_Change <- (90 - abs(bearingVec2)) + (90 + abs(bearingVec1))
} else if (Bearing_Signs == "negitive negitive" && Bearing_Values == "less greater") {
Direction_Change <- (180 - abs(bearingVec2)) + abs(bearingVec1)
} else if (Bearing_Signs == "negitive negitive" && Bearing_Values == "greater less") {
Direction_Change <- (180 - abs(bearingVec1)) + abs(bearingVec2)
} else if (Bearing_Signs == "negitive negitive" && Bearing_Values == "greater greater") {
Direction_Change <- (abs(bearingVec1) - 90) + 90 + (180 - abs(bearingVec2))
}
return(Direction_Change)
}
Issue
When I use the function, it evaluates the bearing inputs correctely (I have included the print statements so that you can easily assess this). See below the working example (I checked it and others manually by hand to double check the math and assignment ect...)
analyzeGPS_DirectionChange(bearingVec1 = 100.6587, bearingVec2 = 24.66356)
[1] "positive positive"
[1] "greater less"
[1] 104.0049
However, when I run two vectors through the function which represent consequtive points some are calculated correctly and others are errors. See below for example:
bearingVec1 <- c(1.988956, 100.658682, 24.663563, 122.423408, -30.556419, 14.702280, -74.235212, -5.619550, 78.729953, -135.463404, -2.600200)
bearingVec2 <- c(100.658682, 24.663563, 122.423408, -30.556419, 14.702280, -74.235212, -5.619550, 78.729953, -135.463404, -2.600200, -6.733987)
analyzeGPS_DirectionChange(bearingVec1, bearingVec2)
[1] "positive positive" "positive positive" "positive positive" "positive negitive" "negitive positive" "positive negitive"
[7] "negitive negitive" "negitive positive" "positive negitive" "negitive negitive" "negitive negitive"
[1] "less greater" "greater less" "less greater" "greater less" "less less" "less less" "less less" "less less"
[9] "less greater" "greater less" "less less"
[1] 81.33027 255.99512 82.24015 332.97983 134.74130 268.93749 111.38434 95.65050 394.19336 47.13680 184.13379
At first sight it seems to have worked, however there are a few errors in the actual value answers, see the first example above which calculated correctly when I entered the bearing values manually - those numbers correspond to bearingVec1[2] and bearingVec2[2] in the sample data I provided.
I do not know why it is incorrectly calculating when I provide the function with vectors. Please help me solve this issue!
Aucun commentaire:
Enregistrer un commentaire