-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-road-graph.js
More file actions
56 lines (41 loc) · 1.56 KB
/
build-road-graph.js
File metadata and controls
56 lines (41 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const { MongoClient } = require('mongodb');
async function run() {
console.log('Running script');
const uri = 'mongodb://localhost:27017';
const client = new MongoClient(uri);
await client.connect();
const db = client.db('osm');
const collection = db.collection('all_ways');
// Create a map of node coords to connected way IDs
const nodeMap = {};
const cursor = collection.find().limit(1000);
console.log('Query initialized');
while (await cursor.hasNext()) {
const doc = await cursor.next();
const coords = doc.geometry?.coordinates;
if (!coords || coords.length < 2) continue; // Skip if invalid geometry
const id = doc._id.toString(); // use _id as way ID
const start = coords[0];
const end = coords[coords.length - 1];
const formatCoord = ([lon, lat]) => `${lat.toFixed(7)},${lon.toFixed(7)}`;
const startKey = formatCoord(start);
const endKey = formatCoord(end);
if (!nodeMap[startKey]) nodeMap[startKey] = [];
if (!nodeMap[endKey]) nodeMap[endKey] = [];
nodeMap[startKey].push(id);
nodeMap[endKey].push(id);
}
await client.close();
// Print connections (nodes with multiple ways)
const intersections = Object.entries(nodeMap).filter(([_, ways]) => ways.length > 1);
console.log(`Found ${intersections.length} intersections`);
console.log(intersections.slice(0, 2)); // Show a sample
// save as JSON
const fs = require('fs');
fs.writeFileSync(
'road-node-map.json',
JSON.stringify(nodeMap, null, 2)
);
console.log('Saved road-node-map.json');
}
run().catch(console.error);