Skip to content

Commit

Permalink
Added geoLines()
Browse files Browse the repository at this point in the history
Returns a lines geometry to plot on a map
  • Loading branch information
BolverBlitz committed Mar 31, 2024
1 parent 3eca06b commit c497d59
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
39 changes: 37 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Abfahrten = require("./src/abfahrten");
const Fahrten = require("./src/fahrten");
const WebProcessor = require("./src/web_processor");
const routen = require("./src/routen");
const reverseGeocode = require("./src/mapandroute");
const mapandroute = require("./src/mapandroute");
const { Fuhrpark_Bus, Fuhrpark_Tram, Fuhrpark_PVU, Steighoehen_Tram, StopInfo_Tram, StopInfo_Ubahn } = require("./static");
const Fuhrpark_Total = {...Fuhrpark_Bus, ...Fuhrpark_Tram, ...Fuhrpark_PVU};
const allowed_apiparameter = {
Expand All @@ -23,6 +23,7 @@ class openvgn {
this.api_url = api_url || "https://start.vag.de/dm/api";
this.vag_url = vag_url || "https://efa-gateway.vag.de";
this.map_and_route_url = "https://iw.mapandroute.de/MapAPI-1.4/servlet/FrontController";
this.vag_livemap_url = "https://livemap.vag.de"; // VAG often forgets to update SSL for couple of weeks, be warned (Downtime about 5 Weeks a year (2022 & 2023)) - Also IPv4 only
};

/**
Expand Down Expand Up @@ -74,6 +75,20 @@ class openvgn {
return xy;
};

#WGS84toXY(xy) {
// Constants for conversion
const D2R = Math.PI / 180;
const R2D = 180 / Math.PI;
const A = 6378137.0;

// Inverse transformations:
// λ=x/(A⋅D2R)​
// ϕ=2⋅tan^⁡−1(e*(y/A))−π/2
const lon = xy[0] / (A * D2R);
const lat = (Math.atan(Math.exp(xy[1] / A)) * 2 - Math.PI / 2) * R2D;

return [lon, lat];
}

/**
* Transform coordinates into a string that can be used for routes.
Expand Down Expand Up @@ -268,13 +283,33 @@ class openvgn {
if (!lat || !lon) { return new Error("reverseGeocode: Coordinates can´t be empty.") }
const xy = this.#XYtoWGS84([lon, lat]);
const url = `${this.map_and_route_url}?cmd=reverseGeocode&VNR=0&PNR=0&country=EU&x=${xy[0]}&y=${xy[1]}&hits=1`;
return reverseGeocode.reverseGeocode(url).then(function (locations) {
return mapandroute.reverseGeocode(url).then(function (locations) {
return locations;
}).catch(function (err) {
return err;
});
}

/**
* Will reutrn the exact geomoetry of a given line in lat and lon
* @param {String} line
* @returns
*/
geoLines(line) {
const possibleLines = [ "4", "5", "6", "7", "8", "10", "11", "U1", "U2", "U3" ]
if (!line) { return new Error("geoLines: Line can´t be empty.") }
if (!possibleLines.includes(line)) { return new Error("geoLines: Line not found.") }
const url = `${this.vag_livemap_url}/lines.xhr?${new Date().getTime()}`;
return mapandroute.geoLines(url, line).then(cords => {
for (let i = 0; i < cords.Cords.length; i++) {
cords.Cords[i] = this.#WGS84toXY(cords.Cords[i]);
}
return cords;
}).catch(function (err) {
return err;
});
}

/**
* Will calculate the time it takes to get from A to B from a getTrip response
* Uses Expected (SOLL) data
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oepnv-nuremberg",
"version": "0.4.0",
"version": "0.4.1",
"description": "A wrapper for multiple datapoints from VAG and VGN",
"main": "index.js",
"scripts": {
Expand Down
35 changes: 33 additions & 2 deletions src/mapandroute.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { customFetch_mapandroute } = require("../data/newRequest");
const { customFetch, customFetch_mapandroute } = require("../data/newRequest");

const reverseGeocode = (url) => {
return new Promise(function(resolve, reject) {
Expand Down Expand Up @@ -30,6 +30,37 @@ const reverseGeocode = (url) => {
});
}

const geoLines = (url, line) => {
return new Promise(function(resolve, reject) {
let Time_Started = new Date().getTime();
customFetch(url, { json: true, gzip: true}, (err, res, body) => {
if (err) { reject(err); return; }
try {
if (res.statusCode === 200) {
let Metadata = {
RequestTime: new Date().getTime() - Time_Started,
url: url
};

resolve({
Cords: body[line].geojson.geometry.coordinates,
Meta: Metadata
});
} else {
reject({ code: res.statusCode, url: url });
}
} catch (error) {
if (error instanceof TypeError) {
reject({ code: 500, message: "TypeError", url: url });
} else {
reject({ code: 500, message: "Unknown Error", url: url });
}
}
});
});
}

module.exports = {
reverseGeocode
reverseGeocode,
geoLines
}
19 changes: 2 additions & 17 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
const util = require('util')
const vgn_wrapper = require('./index');
/* Import Request to also be able to query urls directly for compared output */
const package = require('./package.json');
const os = require('os');
const request = require("request");
const geolib = require("geolib");
const customFetch = request.defaults({
headers: {'User-Agent': `OpenVGN/${package.version} (NodeJS_${process.env.NODE_VERSION}) ${os.platform()} (${os.arch()}) NodeJS Wrapper`}
})

const api_url = "https://start.vag.de/dm/api";
const vag_url= "https://efa-gateway.vag.de";

const vgn = new vgn_wrapper.openvgn(api_url, vag_url);

const askURL = function(URL) {
return new Promise(function(resolve, reject) {
customFetch(URL, { json: true }, (err, res, body) => {
if (err) { reject(err); }
resolve(body)
});
});
};

/*
Plärrer: 704
Hardhöhe: 2390
Expand Down Expand Up @@ -56,7 +40,8 @@ GT6N: 1000er
//console.log("Should only contain departures from line 4 but using normal Departures and filters output")
//console.log(Output.Departures.filter(entry => entry.Linienname == "4"))
//const Output = await vgn.getTrip(1000560, {product: "ubahn"})
const Output = await vgn.reverseGeocode('49.4480881582118', '11.0647882822154')
//const Output = await vgn.reverseGeocode('49.4480881582118', '11.0647882822154')
const Output = await vgn.geoLines("8")
//const Output = await vgn.getTrips("Ubahn", {timespan: 10})
//const Output = await vgn.getLocations("Plärrer")
console.log(util.inspect(Output, false, null, true /* enable colors */))
Expand Down

0 comments on commit c497d59

Please sign in to comment.