Get nearest roads to a given location, or all roads within a bounding box.
npm install nearest-roads --save
const nearestRoads = require('nearest-roads');
Returns an array containing the names of the roads that are within distance
metres radius of lat
long
location.
nearestRoads.fromLocation(51.42, -0.148, 100, (err, data) => {
if (err) console.log(err);
else console.log(data);
});
nearestRoads.boundingBox(51.5707755427, 0.922651543, 51.5046221724, 0.6790402342, (err, data) => {
if (err) console.log(err);
else console.log(data);
});
[ { name: 'Cuckoo Corner',
type: 'trunk',
oneway: true,
lit: true,
lanes: 2,
maxspeed: 40 },
{ name: 'Prince Avenue',
type: 'trunk',
oneway: true,
lit: true,
lanes: 2,
maxspeed: 40 },
{ name: 'Queensway',
type: 'primary',
oneway: true,
lit: true,
maxspeed: 40 },
{ name: 'Southchurch Avenue',
type: 'primary',
oneway: true,
lit: true,
maxspeed: 30 },
{ name: 'Prince Avenue',
type: 'trunk',
oneway: true,
lit: true,
lanes: 2,
maxspeed: 40 },
...
{ name: 'Eastern Avenue',
type: 'primary',
maxspeed: 30 },
... 1526 more items ]
Previously, the roads that would be returned were in no sort(!) of order. Nor was it the case that Overpass was returning these sorted in distance order - which would have been really useful might I add!
Since the previous ordering has no semantic meaning I decided to sort roads alphabetically.
[
{ "name": "Nimrod Road", "type": "residential", "maxspeed": 20 },
{ "name": "Gracedale Road", "type": "residential", "maxspeed": 20 },
{ "name": "Parklands Road", "type": "residential", "maxspeed": 20 },
{ "name": "Woodnook Road", "type": "residential", "maxspeed": 20 },
{ "name": "Brookview Road", "type": "residential","maxspeed": 20 },
{ "name": "Nimrod Road", "type": "residential", "maxspeed": 20 },
{ "name": "Ribblesdale Road", "type": "residential", "maxspeed": 20 }
]
[
{ "name": "Brookview Road", "type": "residential", "maxspeed": 20 },
{ "name": "Gracedale Road", "type": "residential", "maxspeed": 20 },
{ "name": "Nimrod Road", "type": "residential", "maxspeed": 20 },
{ "name": "Nimrod Road", "type": "residential", "maxspeed": 20 },
{ "name": "Parklands Road", "type": "residential", "maxspeed": 20 },
{ "name": "Ribblesdale Road", "type": "residential", "maxspeed": 20 },
{ "name": "Woodnook Road", "type": "residential", "maxspeed": 20 }
]
Previously, this module would only return an array containing all the names of roads that matched the query. Quite clearly though, this was fairly limited. It also didn't take full advantage of the data coming from OpenStreetMaps.
Therefore, this update now returns an array of objects - one for each road. Each road object contains the following fields:
name
: Name of the road (yes, would you believe it.)type
: If the road is a [residential, tertiary, secondary, service, ...] roadoneway
*:true
if the road is a onewaylit
*:true
if the road has lightinglanes
*: No. of lanes the road hasmaxspeed
*: The maximum permitted speed limit as an integer
*The existence of these fields for each road is entirely dependent on if there is such data in OSM i.e., these will only exist if the relevant data is present in OSM, otherwise the fields will be undefined.
N.B. Some of the fields of the road objects have been converted to a more programmatically consumable type when compared to OSM data.
I have also removed the array-unique
dependency that this module used to have, as it is no longer relevant.
Finally, this completes the intended requirements that were laid out when I aimed to create this module. Hence due to breaking backwards compatibility, and completing the intended functionality of this module, this is officially version 1.x
Previously, you could only retrieve the nearest roads to a central (lat, lon) location and a given radius distance.
Now, there is support for getting all the roads inside a Bounding box.
This project adheres to the Airbnb JavaScript Style Guide.
Add unit tests for any new or changed functionality. Lint and test your code.