nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

wikiloc.js (2444B)


      1 import BaseService from './baseService';
      2 import urlViaCorsProxy from '~/lib/CORSProxy';
      3 import {stringToArrayBuffer} from '~/lib/binary-strings';
      4 import twkb from 'twkb';
      5 
      6 class Wikiloc extends BaseService {
      7     urlRe = /^https?:\/\/(?:.+\.)?wikiloc\.com\/.*-(\d+)/u;
      8     mapDataRe = /mapData\s*=\s*(.+);/u;
      9 
     10     getTrackId() {
     11         const m = this.urlRe.exec(this.origUrl);
     12         return m[1];
     13     }
     14 
     15     isOurUrl() {
     16         return this.urlRe.test(this.origUrl);
     17     }
     18 
     19     requestOptions() {
     20         return [{
     21             url: urlViaCorsProxy(this.origUrl),
     22             options: {
     23                 isResponseSuccess: (xhr) => xhr.status === 200 || xhr.status === 404
     24             }
     25         }];
     26     }
     27 
     28     parseResponse(responses) {
     29         const trackId = this.getTrackId();
     30         const name = `Wikiloc track ${trackId}`;
     31         const response = responses[0];
     32         if (response.status === 404) {
     33             return [{error: 'Wikiloc trail does not exist'}];
     34         }
     35         const m = this.mapDataRe.exec(response.responseText);
     36         try {
     37             const data = JSON.parse(m[1]);
     38 
     39             let points = [];
     40             let tracks = [];
     41 
     42             for (let i = 0; i < data.mapData.length; i++) {
     43                 const mapData = data.mapData[i];
     44                 const geom = twkb.toGeoJSON(new Uint8Array(stringToArrayBuffer(atob(mapData.geom))));
     45                 if (mapData.waypoint) {
     46                     points.push({
     47                         name: mapData.nom,
     48                         lat: geom.features[0].geometry.coordinates[0][1],
     49                         lng: geom.features[0].geometry.coordinates[0][0]
     50                     });
     51                 } else if (mapData.spaId.toString() === trackId) {
     52                     tracks.push(geom.features[0].geometry.coordinates.map((obj) => ({lat: obj[1], lng: obj[0]})));
     53                 }
     54             }
     55             if (data.waypoints) {
     56                 for (const waypoint of data.waypoints) {
     57                     points.push({
     58                         name: waypoint.name,
     59                         lat: waypoint.lat,
     60                         lng: waypoint.lon,
     61                     });
     62                 }
     63             }
     64 
     65             return [{
     66                 name: data.mapData?.[0]?.nom ?? name,
     67                 points,
     68                 tracks
     69             }];
     70         } catch (e) {
     71             return [{name: name, error: 'UNSUPPORTED'}];
     72         }
     73     }
     74 }
     75 
     76 export default Wikiloc;