1
0
Fork 0
mirror of https://github.com/Findus23/RainbowRoad.git synced 2024-09-19 16:03:52 +02:00
RainbowRoad/commands/fetch.ts

94 lines
3 KiB
TypeScript
Raw Permalink Normal View History

2022-08-07 18:38:22 +02:00
import * as fs from "fs";
import type {Crossing, OSMNodeSource, OSMWaySource, OverPassNode, OverPassResponse, OverpassWay} from "../interfaces";
import {nodeData, wayData} from "./overpass";
import {lineLengthInM} from "../utils/geo";
2022-08-15 15:54:18 +02:00
async function runfetch(filename: string) {
const data: Crossing[] = JSON.parse(fs.readFileSync(filename, 'utf8'));
2022-08-07 18:38:22 +02:00
function firstLastNodeCoords(nodeMap: { [id: number]: OverPassNode }, nodeList: number[]) {
const coords: number[][] = []
const firstNode = nodeMap[nodeList[0]]
const lastNode = nodeMap[nodeList[nodeList.length - 1]]
coords.push([firstNode.lon, firstNode.lat])
coords.push([lastNode.lon, lastNode.lat])
return coords;
}
async function fetchWayData(geosource: OSMWaySource) {
const coords: number[][] = []
const response = await wayData(geosource.wayID)
const data: OverPassResponse = response.data
const nodes: { [key: number]: OverPassNode } = {}
data.elements.forEach(e => {
if (e.type != "node") {
return
}
nodes[e.id] = e
})
const way = data.elements.find(e => e.type == "way") as OverpassWay
return firstLastNodeCoords(nodes, way.nodes)
}
async function fetchNodeData(geosource: OSMNodeSource) {
const response = await nodeData(geosource.nodes)
const data: OverPassResponse = response.data
console.log("data.elements")
console.log(data.elements)
const nodes: { [key: number]: OverPassNode } = {}
data.elements.forEach(e => {
if (e.type != "node") {
return
}
nodes[e.id] = e
})
console.log(geosource.nodes)
return firstLastNodeCoords(nodes, geosource.nodes);
}
for (const d of data) {
2022-08-27 17:51:07 +02:00
if (d.id < 100) {
const max = 2 ** 32
d.id = Math.floor(Math.random() * max);
}
2022-08-07 18:38:22 +02:00
if (typeof d.geo !== "undefined") {
2022-08-07 21:12:54 +02:00
d.geo.length = lineLengthInM(d.geo.coords[0], d.geo.coords[1])
2022-08-07 18:38:22 +02:00
continue
}
const geosource = d.geosource
console.log("fetching")
let coords: number[][];
switch (geosource.type) {
case "OSMway":
coords = await fetchWayData(geosource);
break
case "OSMnodes":
coords = await fetchNodeData(geosource);
break
2022-08-07 21:12:54 +02:00
case "RawCoords":
coords = geosource.coords
console.info(geosource.coords)
break
2022-08-07 18:38:22 +02:00
}
d.geo = {
coords: coords,
length: lineLengthInM(coords[0], coords[1])
}
// crossings[i] = d
2022-08-15 15:54:18 +02:00
fs.writeFileSync(filename, JSON.stringify(data, null, 2))
2022-08-07 18:38:22 +02:00
}
fs.writeFileSync(filename, JSON.stringify(data, null, 2).concat('\n'))
2022-08-26 00:10:59 +02:00
2022-08-07 18:38:22 +02:00
}
2022-08-15 15:54:18 +02:00
fs.readdirSync("../data/").forEach(file => {
2022-12-11 21:12:27 +01:00
if (file === "schema.json" || file === "LICENSE") {
2022-08-15 15:54:18 +02:00
return
}
console.info(file)
runfetch("../data/" + file)
})