Compare commits

..

No commits in common. "1fdd4e71b509a9a0cd93f35adbf6e4c1740fe260" and "1a780c6f53d84d7598df11e140b49f4604bd08e8" have entirely different histories.

4 changed files with 117 additions and 175 deletions

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
*~ *~
.DS_Store .DS_Store
*.swp *.swp
web-ext-artifacts

View File

@ -1,4 +1,5 @@
# SAC Route Portal GPX Downloader # SAC Route Portal GPX Downloader
The [Swiss Alpine Club](https://www.sac-cas.ch/en/) has a great The [Swiss Alpine Club](https://www.sac-cas.ch/en/) has a great
[route portal](https://www.sac-cas.ch/en/huts-and-tours/sac-route-portal/) [route portal](https://www.sac-cas.ch/en/huts-and-tours/sac-route-portal/)
for finding interesting hiking routes. for finding interesting hiking routes.
@ -18,6 +19,3 @@ Heed their warning and do not blindly follow the gps track! Use your brain and
plan your route beforehand to be aware of dangerous parts and alternatives. plan your route beforehand to be aware of dangerous parts and alternatives.
Take a look at their [safety instructions](https://www.sac-cas.ch/en/training-and-safety/safety/). Take a look at their [safety instructions](https://www.sac-cas.ch/en/training-and-safety/safety/).
## Contributors
- wizche

View File

@ -13,12 +13,12 @@ let gpxTrack = null;
* (https://www.swisstopo.admin.ch/en/knowledge-facts/surveying-geodesy/reference-systems/map-projections.html). * (https://www.swisstopo.admin.ch/en/knowledge-facts/surveying-geodesy/reference-systems/map-projections.html).
* *
* @param {*} point Array of Swiss projection coordinates, position 0 is E and 1 is N. * @param {*} point Array of Swiss projection coordinates, position 0 is E and 1 is N.
* @returns Calculated lat and lon. * @returns Track point xml node for gpx.
*/ */
function toWGS84(point) { function toTrackPoint(point) {
// convert LV95 into the civilian system // convert LV95 into the civilian system
const y_aux = (point[0] - 2600000) / 1000000; let y_aux = (point[0] - 2600000)/1000000;
const x_aux = (point[1] - 1200000) / 1000000; let 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,44 +26,19 @@ 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.014 * Math.pow(x_aux, 3); 0.0140 * Math.pow(x_aux, 3);
let lon = 2.6779094 + let lng = 2.6779094 +
4.728982 * y_aux + 4.728982 * y_aux +
0.791484 * y_aux * x_aux + 0.791484 * y_aux * x_aux +
0.1306 * y_aux * Math.pow(x_aux, 2) - 0.1306 * y_aux * Math.pow(x_aux, 2) -
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; lng = lng * 100 / 36;
return { lat: lat, lon: lon }; return `<trkpt lat="${lat}" lon="${lng}"/>`;
}
/**
* Get the gpx trackpoint representation of a Swiss projection coordinate point.
*
* @param {*} point Array of Swiss projection coordinates, position 0 is E and 1 is N.
* @returns Track point xml node for gpx.
*/
function toTrackPoint(point) {
const wgs84Point = toWGS84(point);
return `<trkpt lat="${wgs84Point.lat}" lon="${wgs84Point.lon}"/>`;
}
/**
* Get the gpx waypoint representation of a route portal point.
*
* @returns Way point xml node for gpx.
*/
function toWayPoint(point) {
const wgs84Point = toWGS84(point.geom.coordinates);
return `
<wpt lat="${wgs84Point.lat}" lon="${wgs84Point.lon}">
<ele>${point.altitude}</ele>
<name>${point.display_name}</name>
</wpt>`;
} }
/** /**
@ -73,11 +48,8 @@ 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 book = geoJson.book_route_number const route = geoJson.segments[0];
? `${geoJson.book_route_number} - ` return `${geoJson.book_route_number}-${route.route_id} ${route.title}`;
: "";
return `${book}${geoJson.title}`;
} }
/** /**
@ -87,27 +59,7 @@ function trackTitle(geoJson) {
* @returns Simple gpx string. * @returns Simple gpx string.
*/ */
function toGpx(geoJson) { function toGpx(geoJson) {
const trackSegments = geoJson.segments const route = geoJson.segments[0].geom;
.map((segment) => {
if (segment.geom == null) return "";
return `<trkseg>
${segment.geom.coordinates.map(toTrackPoint).join("")}
</trkseg>`;
})
.join("");
const departurePoint = geoJson.departure_point
? toWayPoint(geoJson.departure_point)
: "";
const endPoint = geoJson.end_point ? toWayPoint(geoJson.end_point) : "";
const waypoints = geoJson.waypoints
? geoJson.waypoints
.map((wp) => {
return toWayPoint(wp.reference_poi);
})
.join("")
: "";
const routeTitle = trackTitle(geoJson); const routeTitle = trackTitle(geoJson);
const xmlString = `<?xml version="1.0" encoding="UTF-8" standalone="no" ?> const xmlString = `<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
@ -117,19 +69,17 @@ 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">
${departurePoint}
${toWayPoint(geoJson.destination_poi)}
${waypoints}
${endPoint}
<trk> <trk>
<name>Track ${routeTitle}</name> <name>Track ${routeTitle}</name>
${trackSegments} <trkseg>
${route.coordinates.map(toTrackPoint).join("")}
</trkseg>
</trk> </trk>
</gpx> </gpx>
`; `;
const parser = new DOMParser(); const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "text/xml"); let xmlDoc = parser.parseFromString(xmlString, "text/xml");
return new XMLSerializer().serializeToString(xmlDoc.documentElement); return new XMLSerializer().serializeToString(xmlDoc.documentElement);
} }
@ -138,28 +88,25 @@ 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) {
const filter = browser.webRequest.filterResponseData(details.requestId); let filter = browser.webRequest.filterResponseData(details.requestId);
const decoder = new TextDecoder("utf-8"); let decoder = new TextDecoder("utf-8");
const encoder = new TextEncoder(); let encoder = new TextEncoder();
const data = []; let data = [];
filter.ondata = (event) => { filter.ondata = event => {
data.push(event.data); data.push(event.data);
}; };
filter.onstop = async (_event) => { filter.onstop = async event => {
const blob = new Blob(data, { type: "text/html" }); let blob = new Blob(data, {type: 'text/html'});
const buffer = await blob.arrayBuffer(); let buffer = await blob.arrayBuffer();
const str = decoder.decode(buffer); let str = decoder.decode(buffer);
gpxTrack = JSON.parse(str);
updateActiveTab(browser.tabs); updateActiveTab(browser.tabs);
filter.write(encoder.encode(str)); filter.write(encoder.encode(str));
filter.close(); filter.close();
const geoJson = JSON.parse(str);
const routeTitle = trackTitle(geoJson);
gpxTrack = { title: routeTitle, data: toGpx(geoJson) };
}; };
return {}; return {};
@ -188,37 +135,35 @@ function handleClick(tab) {
return; return;
} }
const blob = new Blob([gpxTrack.data], { type: "application/gpx+xml" }); let blob = new Blob([toGpx(gpxTrack)], {type: "application/gpx+xml"});
const objectURL = URL.createObjectURL(blob); let objectURL = URL.createObjectURL(blob);
const downloading = browser.downloads.download({ const routeTitle = trackTitle(gpxTrack);
let downloading = browser.downloads.download({
url : objectURL, url : objectURL,
filename: `${gpxTrack.title}.gpx`, filename : `track-${routeTitle}.gpx`,
saveAs: true, saveAs: true,
conflictAction: "uniquify", conflictAction : 'uniquify'
}); });
downloading.then( downloading.then(
(id) => console.log(`Started downloading: ${id}`), (id) => console.log(`Started downloading: ${id}`),
(error) => console.log(`Download failed: ${error}`), (error) => console.log(`Download failed: ${error}`));
);
gpxTrack = null; gpxTrack = null;
} }
/** /**
* 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]);
} }
} }
const gettingActiveTab = browser.tabs.query({ let gettingActiveTab = browser.tabs.query({active: true, currentWindow: true});
active: true,
currentWindow: true,
});
gettingActiveTab.then(updateTab); gettingActiveTab.then(updateTab);
} }
@ -229,27 +174,23 @@ function updateIcon(tab) {
const hasTrack = checkTrack(tab); const hasTrack = checkTrack(tab);
browser.browserAction.setIcon({ browser.browserAction.setIcon({
path: hasTrack path: hasTrack ? {
? {
48: "icons/map.png", 48: "icons/map.png",
} } : {
: {
48: "icons/map-disabled.png", 48: "icons/map-disabled.png",
}, },
tabId: tab.id, tabId: tab.id
}); });
browser.browserAction.setTitle({ browser.browserAction.setTitle({
title: hasTrack title: hasTrack ? `Download track ${trackTitle(gpxTrack)}` : 'No track selected',
? `Download track "${gpxTrack.title}"` tabId: tab.id
: "No track selected",
tabId: tab.id,
}); });
} }
browser.webRequest.onBeforeRequest.addListener( browser.webRequest.onBeforeRequest.addListener(
listener, listener,
{urls: ["https://www.sac-cas.ch/*[routeId]*"]}, {urls: ["https://www.sac-cas.ch/*[routeId]*"]},
["blocking"], ["blocking"]
); );
browser.browserAction.onClicked.addListener(handleClick); browser.browserAction.onClicked.addListener(handleClick);

View File

@ -1,15 +1,19 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "SAC Route Portal GPX Downloader", "name": "SAC Route Portal GPX",
"version": "0.8", "version": "0.1",
"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",
@ -17,11 +21,11 @@
"webRequestBlocking", "webRequestBlocking",
"https://www.sac-cas.ch/*" "https://www.sac-cas.ch/*"
], ],
"background": { "background": {
"scripts": [ "scripts": ["background.js"]
"background.js"
]
}, },
"browser_action": { "browser_action": {
"default_icon": "icons/map.png", "default_icon": "icons/map.png",
"default_title": "To GPX" "default_title": "To GPX"