Skip to content

Commit

Permalink
Simplify logic for nearest roads
Browse files Browse the repository at this point in the history
  • Loading branch information
steinbro committed May 5, 2024
1 parent 025fbde commit 4ae6b19
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 51 deletions.
21 changes: 10 additions & 11 deletions app/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { audioContext, createSpatialPlayer, playSpatialSpeech } from './audio/sound.js'
import createCalloutAnnouncer from './audio/callout.js'
import { getCurrentRoad, getLocation, watchLocation } from './spatial/geo.js';
import { getNearestRoads, getLocation, watchLocation } from './spatial/geo.js';
import { startCompassListener } from './spatial/heading.js';
import createLocationProvider from './spatial/location.js'
import createMap from './visual/map.js';
Expand Down Expand Up @@ -131,16 +131,15 @@ document.addEventListener('DOMContentLoaded', function () {
console.log(coords);
locationProvider.updateLocation(coords.latitude, coords.longitude);
locationProvider.updateOrientation({ alpha: coords.heading });


// Speak nearest road
(async () => {
var road = await getCurrentRoad(locationProvider);
if (road != 0){
audioQueue.addToQueue({ soundUrl: 'app/sounds/sense_mobility.wav' })
audioQueue.addToQueue({ text: `Nearest road: ${road[0].properties.name}` })
}
})();

// Speak nearest road
getNearestRoads(locationProvider).then(roads => {
if (roads.length > 0) {
audioQueue.addToQueue({ soundUrl: 'app/sounds/sense_mobility.wav' })
audioQueue.addToQueue({ text: `Nearest road: ${roads[0].properties.name}` })
}
});

// Call out nearby features once
announcer.calloutAllFeatures(coords.latitude, coords.longitude)
.then(anythingToSay => {
Expand Down
59 changes: 19 additions & 40 deletions app/js/spatial/geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,59 +103,38 @@ export function watchLocation(callback) {
and the announcer function to get all the tiles features.
- Kris
*/
// Gets the street from nearest address and returns its name and city
export async function getCurrentRoad(locationProvider){
const features = await Promise.all(
// Returns a list of named roads, annotated with distance to current location, sorted by proximity
export async function getNearestRoads(locationProvider){
const myLocation = turf.point([locationProvider.longitude, locationProvider.latitude]);
return Promise.all(
enumerateTilesAround(locationProvider.latitude, locationProvider.longitude, locationProvider.radiusMeters)
.map(t => {
t.load();
return t.getFeatures();
})
)
.then(tileFeatures => {
const reduced = tileFeatures
return tileFeatures
// Flatten list of features across all nearby tiles
.reduce((acc, cur) => acc.concat(cur), [])
// Limit to roads
// Limit to named roads
.filter(
f => f.feature_type == "highway" &&
f.geometry.type == "LineString" &&
["primary", "residential", "tertiary"].includes(f.feature_value)
);
return reduced;
});

//To define a turf point you have to put longitude first
const point = turf.point([locationProvider.longitude, locationProvider.latitude]);
features.forEach(road => {
const snap = turf.nearestPointOnLine(road, point, {units: "meters"});
road.distance = locationProvider.distance(
snap, { units: 'meters' }
);
});

const sorted = features
.sort( (a,b) => {
["primary", "residential", "tertiary"].includes(f.feature_value) &&
f.properties.name
)
// Annotate with distance to current location
.map(road => {
const snap = turf.nearestPointOnLine(road, myLocation, { units: "meters"});
road.distance = locationProvider.distance(snap, { units: 'meters' });
return road;
})
// Sort by distance
.sort( (a,b) => {
return a.distance >= b.distance;
});

if( sorted.length != 0){
// Finds all non-duplicate roads under 5 meters
const nearestMap = new Map();
for(let i = 0; sorted[i].distance < 5; i++){
// Gets street name
const name = sorted[i].properties.name;
// Maps street name to its index in the sorted list
if(!nearestMap.has(name)){
nearestMap[name] = i;
}
}
// Combines all the mapped roads into a list
const nearest = Array.from(nearestMap, ([n,i]) => sorted[i]);
return nearest;
}else{
return 0;
}
})
});
}

export function geoToXY(myLocation, myHeading, poiLocation) {
Expand Down

0 comments on commit 4ae6b19

Please sign in to comment.