tracedetrail.js (1400B)
1 import BaseService from './baseService'; 2 import urlViaCorsProxy from '~/lib/CORSProxy'; 3 import L from 'leaflet'; 4 5 class Tracedetrail extends BaseService { 6 urlRe = /^https?:\/\/(?:www\.)?tracedetrail\.[a-z]{2,}.*\/trace\/trace\/([0-9]+)/u; 7 8 isOurUrl() { 9 return this.urlRe.test(this.origUrl); 10 } 11 12 requestOptions() { 13 const m = this.urlRe.exec(this.origUrl); 14 const trackId = this.trackId = m[1]; 15 16 return [{ 17 url: urlViaCorsProxy(`https://tracedetrail.com/en/trace/geomSections/${trackId}`), 18 options: {responseType: 'json'} 19 }]; 20 } 21 22 parseResponse(responses) { 23 const response = responses[0]; 24 let name = `Tracedetrail track ${this.trackId}`; 25 try { 26 name = response.responseJSON.nom_fr || name; 27 const geometry = JSON.parse(response.responseJSON.geometry); 28 const proj = L.CRS.EPSG3857; 29 const points = geometry.map((item) => proj.unproject(L.point(item.lon, item.lat))); 30 31 return [{ 32 name, 33 tracks: [points] 34 }]; 35 } catch (e) { 36 let error = 'UNSUPPORTED'; 37 38 if (response.status === 200) { 39 error = `Track with id ${this.trackId} was deleted or did not exist`; 40 } 41 42 return [{name, error}]; 43 } 44 } 45 } 46 47 export default Tracedetrail;