Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include approximate compass bearing in output #103

Merged
merged 6 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The other fields may require some explanation:
| Field | Description |
|----|----|
| `routeStreets` | The name(s) of the streets along the corridor. I.e. the path taken. |
| `direction` | The approximate compass direction of travel for a corridor e.g. "Westbound". |
| `startCrossStreets` | The names of any cross-street(s) at the start of the corridor. If the corridor starts mid-block then coordinates of that point will be returned instead. |
| `endCrossStreets` | The names of any cross-street(s) at the end of the corridor. If the corridor ends mid-block then coordinates of that point will be returned instead. |
| `mean_travel_time_minutes` | The mean travel time in minutes is given as a floating point number rounded to two decimal places. Where insufficient data was available to complete the request, the value will be null, and in cases where the request was impossible a value of -999 will be assigned. (See `hoursInRange` below). |
Expand Down
27 changes: 24 additions & 3 deletions frontend/src/corridor.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,30 @@ export class Corridor extends Factor {
}
return ''
}
get bearing(){
// azimuth calculation borrowed from:
// http://www.movable-type.co.uk/scripts/latlong.html
if( ! this.#intersections.length == 2 ) return undefined;
const [A, B] = this.#intersections
const x = Math.cos(d2r(A.lat)) * Math.sin(d2r(B.lat))
- Math.sin(d2r(A.lat)) * Math.cos(d2r(B.lat)) * Math.cos(d2r(B.lng - A.lng))
const y = Math.sin(d2r(B.lng - A.lng)) * Math.cos(d2r(B.lat))
// degrees from true East TODO: adjust this by 17 degrees
const azimuth = r2d(Math.atan2(x,y))
const compass = { NE: 45, SE: -45, SW: -135, NW: 135 }
if( azimuth < compass.NE && azimuth > compass.SE ) return 'Eastbound'
if( azimuth > compass.NE && azimuth < compass.NW ) return 'Northbound'
if( azimuth < compass.SE && azimuth > compass.SW ) return 'Southbound'
if( azimuth > compass.NW || azimuth < compass.SW ) return 'Westbound'
return ''
}
get name(){
if(this.#intersections.length == 1){
return `Incomplete corridor starting from ${this.startCrossStreetsString}`
}else if(this.#intersections.length == 2 && this.viaStreets.size > 0){
return `${this.viaStreetsString} from ${this.startCrossStreetsString} to ${this.endCrossStreetsString}`
return `${this.viaStreetsString} ${this.bearing.toLowerCase()} from ${this.startCrossStreetsString} to ${this.endCrossStreetsString}`
}else if(this.#intersections.length == 2){ // but no via streets (yet?)
return `from ${this.startCrossStreetsString} to ${this.endCrossStreetsString}`
return `${this.bearing.toLowerCase()} from ${this.startCrossStreetsString} to ${this.endCrossStreetsString}`
}
return 'New Corridor'
}
Expand Down Expand Up @@ -111,4 +128,8 @@ function difference(setA, setB) {
setDiff.delete(elem)
}
return setDiff
}
}

// convert between degrees and radians
function d2r(degrees) { return degrees * (Math.PI / 180) }
function r2d(rad) { return rad / (Math.PI / 180) }
4 changes: 3 additions & 1 deletion frontend/src/intersection.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export class Intersection {
this.#streetNames = new Set(streetNames)
}
get id(){ return this.#id }
get latlng(){ return { lat: this.#lat, lng: this.#lng } }
get lat(){ return this.#lat }
get lng(){ return this.#lng }
get latlng(){ return { lat: this.lat, lng: this.lng } }
get displayCoords(){
// for display purposes only
return `${this.#lng.toFixed(5)}, ${this.#lat.toFixed(5)}`
Expand Down
1 change: 1 addition & 0 deletions frontend/src/travelTimeQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class TravelTimeQuery {
const record = new Map()
record.set('URI',this.URI)
record.set('routeStreets',this.corridor.viaStreetsString)
record.set('direction',this.corridor.bearing)
record.set('startCrossStreets',this.corridor.startCrossStreetsString)
record.set('endCrossStreets',this.corridor.endCrossStreetsString)
record.set('timeRange',this.timeRange.name)
Expand Down
Loading