departure point can be null

This commit is contained in:
Sebastian Hugentobler 2024-02-16 11:14:32 +01:00
parent 7ea79b6f44
commit 1fdd4e71b5
Signed by: shu
GPG Key ID: BB32CF3CA052C2F0
2 changed files with 44 additions and 42 deletions

View File

@ -17,8 +17,8 @@ let gpxTrack = null;
*/ */
function toWGS84(point) { function toWGS84(point) {
// convert LV95 into the civilian system // convert LV95 into the civilian system
let y_aux = (point[0] - 2600000) / 1000000; const y_aux = (point[0] - 2600000) / 1000000;
let x_aux = (point[1] - 1200000) / 1000000; const x_aux = (point[1] - 1200000) / 1000000;
// calculate longitude and latitude in the unit 10000" // calculate longitude and latitude in the unit 10000"
let lat = 16.9023892 + let lat = 16.9023892 +
@ -26,7 +26,7 @@ function toWGS84(point) {
0.270978 * Math.pow(y_aux, 2) - 0.270978 * Math.pow(y_aux, 2) -
0.002528 * Math.pow(x_aux, 2) - 0.002528 * Math.pow(x_aux, 2) -
0.0447 * Math.pow(y_aux, 2) * x_aux - 0.0447 * Math.pow(y_aux, 2) * x_aux -
0.0140 * Math.pow(x_aux, 3); 0.014 * Math.pow(x_aux, 3);
let lon = 2.6779094 + let lon = 2.6779094 +
4.728982 * y_aux + 4.728982 * y_aux +
@ -35,8 +35,8 @@ function toWGS84(point) {
0.0436 * Math.pow(y_aux, 3); 0.0436 * Math.pow(y_aux, 3);
// unit 10000" to 1" and seconds to degrees (dec) // unit 10000" to 1" and seconds to degrees (dec)
lat = lat * 100 / 36; lat = (lat * 100) / 36;
lon = lon * 100 / 36; lon = (lon * 100) / 36;
return { lat: lat, lon: lon }; return { lat: lat, lon: lon };
} }
@ -48,7 +48,7 @@ function toWGS84(point) {
* @returns Track point xml node for gpx. * @returns Track point xml node for gpx.
*/ */
function toTrackPoint(point) { function toTrackPoint(point) {
let wgs84Point = toWGS84(point); const wgs84Point = toWGS84(point);
return `<trkpt lat="${wgs84Point.lat}" lon="${wgs84Point.lon}"/>`; return `<trkpt lat="${wgs84Point.lat}" lon="${wgs84Point.lon}"/>`;
} }
@ -58,7 +58,7 @@ function toTrackPoint(point) {
* @returns Way point xml node for gpx. * @returns Way point xml node for gpx.
*/ */
function toWayPoint(point) { function toWayPoint(point) {
let wgs84Point = toWGS84(point.geom.coordinates); const wgs84Point = toWGS84(point.geom.coordinates);
return ` return `
<wpt lat="${wgs84Point.lat}" lon="${wgs84Point.lon}"> <wpt lat="${wgs84Point.lat}" lon="${wgs84Point.lon}">
<ele>${point.altitude}</ele> <ele>${point.altitude}</ele>
@ -73,7 +73,6 @@ function toWayPoint(point) {
* @returns Combined route number, id and route title. * @returns Combined route number, id and route title.
*/ */
function trackTitle(geoJson) { function trackTitle(geoJson) {
const route = geoJson.segments[0];
const book = geoJson.book_route_number const book = geoJson.book_route_number
? `${geoJson.book_route_number} - ` ? `${geoJson.book_route_number} - `
: ""; : "";
@ -88,18 +87,25 @@ function trackTitle(geoJson) {
* @returns Simple gpx string. * @returns Simple gpx string.
*/ */
function toGpx(geoJson) { function toGpx(geoJson) {
let trackSegments = geoJson.segments.map((segment) => { const trackSegments = geoJson.segments
if (segment.geom == null) return ""; .map((segment) => {
return `<trkseg> if (segment.geom == null) return "";
return `<trkseg>
${segment.geom.coordinates.map(toTrackPoint).join("")} ${segment.geom.coordinates.map(toTrackPoint).join("")}
</trkseg>`; </trkseg>`;
}).join(""); })
.join("");
let endPoint = geoJson.end_point ? toWayPoint(geoJson.end_point) : ""; const departurePoint = geoJson.departure_point
let waypoints = geoJson.waypoints ? toWayPoint(geoJson.departure_point)
? geoJson.waypoints.map((wp) => { : "";
return toWayPoint(wp.reference_poi); const endPoint = geoJson.end_point ? toWayPoint(geoJson.end_point) : "";
}).join("") const waypoints = geoJson.waypoints
? geoJson.waypoints
.map((wp) => {
return toWayPoint(wp.reference_poi);
})
.join("")
: ""; : "";
const routeTitle = trackTitle(geoJson); const routeTitle = trackTitle(geoJson);
@ -111,7 +117,7 @@ function toGpx(geoJson) {
xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd"
version="1.0" version="1.0"
creator="SAC-Tourenportal GPX Downloader"> creator="SAC-Tourenportal GPX Downloader">
${toWayPoint(geoJson.departure_point)} ${departurePoint}
${toWayPoint(geoJson.destination_poi)} ${toWayPoint(geoJson.destination_poi)}
${waypoints} ${waypoints}
${endPoint} ${endPoint}
@ -123,7 +129,7 @@ function toGpx(geoJson) {
`; `;
const parser = new DOMParser(); const parser = new DOMParser();
let xmlDoc = parser.parseFromString(xmlString, "text/xml"); const xmlDoc = parser.parseFromString(xmlString, "text/xml");
return new XMLSerializer().serializeToString(xmlDoc.documentElement); return new XMLSerializer().serializeToString(xmlDoc.documentElement);
} }
@ -132,26 +138,26 @@ function toGpx(geoJson) {
* Intercept the download of GeoJSON data and save it for the background script. * Intercept the download of GeoJSON data and save it for the background script.
*/ */
function listener(details) { function listener(details) {
let filter = browser.webRequest.filterResponseData(details.requestId); const filter = browser.webRequest.filterResponseData(details.requestId);
let decoder = new TextDecoder("utf-8"); const decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder(); const encoder = new TextEncoder();
let data = []; const data = [];
filter.ondata = (event) => { filter.ondata = (event) => {
data.push(event.data); data.push(event.data);
}; };
filter.onstop = async (event) => { filter.onstop = async (_event) => {
let blob = new Blob(data, { type: "text/html" }); const blob = new Blob(data, { type: "text/html" });
let buffer = await blob.arrayBuffer(); const buffer = await blob.arrayBuffer();
let str = decoder.decode(buffer); const str = decoder.decode(buffer);
updateActiveTab(browser.tabs); updateActiveTab(browser.tabs);
filter.write(encoder.encode(str)); filter.write(encoder.encode(str));
filter.close(); filter.close();
let geoJson = JSON.parse(str); const geoJson = JSON.parse(str);
const routeTitle = trackTitle(geoJson); const routeTitle = trackTitle(geoJson);
gpxTrack = { title: routeTitle, data: toGpx(geoJson) }; gpxTrack = { title: routeTitle, data: toGpx(geoJson) };
}; };
@ -182,10 +188,10 @@ function handleClick(tab) {
return; return;
} }
let blob = new Blob([gpxTrack.data], { type: "application/gpx+xml" }); const blob = new Blob([gpxTrack.data], { type: "application/gpx+xml" });
let objectURL = URL.createObjectURL(blob); const objectURL = URL.createObjectURL(blob);
let downloading = browser.downloads.download({ const downloading = browser.downloads.download({
url: objectURL, url: objectURL,
filename: `${gpxTrack.title}.gpx`, filename: `${gpxTrack.title}.gpx`,
saveAs: true, saveAs: true,
@ -202,14 +208,14 @@ function handleClick(tab) {
/** /**
* Update the download icon and text. * Update the download icon and text.
*/ */
function updateActiveTab(tabs) { function updateActiveTab(_tabs) {
function updateTab(tabs) { function updateTab(tabs) {
if (tabs[0]) { if (tabs[0]) {
updateIcon(tabs[0]); updateIcon(tabs[0]);
} }
} }
let gettingActiveTab = browser.tabs.query({ const gettingActiveTab = browser.tabs.query({
active: true, active: true,
currentWindow: true, currentWindow: true,
}); });

View File

@ -1,19 +1,15 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "SAC Route Portal GPX Downloader", "name": "SAC Route Portal GPX Downloader",
"version": "0.7", "version": "0.8",
"developer": { "developer": {
"name": "Sebastian Hugentobler", "name": "Sebastian Hugentobler",
"url": "https://code.vanwa.ch/sebastian/sac-route-portal-gpx-fx" "url": "https://code.vanwa.ch/sebastian/sac-route-portal-gpx-fx"
}, },
"description": "Download gpx tracks from the sac route portal.", "description": "Download gpx tracks from the sac route portal.",
"icons": { "icons": {
"48": "icons/map.png" "48": "icons/map.png"
}, },
"permissions": [ "permissions": [
"activeTab", "activeTab",
"downloads", "downloads",
@ -21,13 +17,13 @@
"webRequestBlocking", "webRequestBlocking",
"https://www.sac-cas.ch/*" "https://www.sac-cas.ch/*"
], ],
"background": { "background": {
"scripts": ["background.js"] "scripts": [
"background.js"
]
}, },
"browser_action": { "browser_action": {
"default_icon": "icons/map.png", "default_icon": "icons/map.png",
"default_title": "To GPX" "default_title": "To GPX"
} }
} }