ozi.js (3334B)
1 import {decodeCP1251} from './codePages'; 2 import stripBom from '~/lib/stripBom'; 3 4 function parseOziRte(txt, name) { 5 let error, 6 segments = []; 7 txt = stripBom(txt); 8 const lines = txt.split('\n'); 9 if (lines[0].indexOf('OziExplorer Route File') !== 0) { 10 return null; 11 } 12 let currentSegment = []; 13 for (let i = 4; i < lines.length; i++) { 14 let line = lines[i].trim(); 15 if (!line) { 16 continue; 17 } 18 let fields = line.split(','); 19 if (fields[0] === 'R') { 20 if (currentSegment.length) { 21 segments.push(currentSegment); 22 } 23 currentSegment = []; 24 } else if (fields[0] === 'W') { 25 let lat = parseFloat(fields[5]); 26 let lng = parseFloat(fields[6]); 27 if (isNaN(lat) || isNaN(lng)) { 28 error = 'CORRUPT'; 29 break; 30 } 31 currentSegment.push({lat, lng}); 32 } else { 33 error = 'CORRUPT'; 34 break; 35 } 36 } 37 if (currentSegment.length) { 38 segments.push(currentSegment); 39 } 40 return [{name, tracks: segments, error}]; 41 } 42 43 function parseOziPlt(txt, name) { 44 var error; 45 var segments = []; 46 txt = stripBom(txt); 47 var lines = txt.split('\n'); 48 if (lines[0].indexOf('OziExplorer Track Point File') !== 0) { 49 return null; 50 } 51 var expected_points_num = parseInt(lines[5], 10); 52 var current_segment = []; 53 var total_points_num = 0; 54 for (var i = 6; i < lines.length; i++) { 55 var line = lines[i].trim(); 56 if (!line) { 57 continue; 58 } 59 var fields = line.split(','); 60 var lat = parseFloat(fields[0]); 61 var lon = parseFloat(fields[1]); 62 var is_start_of_segment = parseInt(fields[2], 10); 63 if (isNaN(lat) || isNaN(lon) || isNaN(is_start_of_segment)) { 64 error = 'CORRUPT'; 65 break; 66 } 67 if (is_start_of_segment) { 68 current_segment = []; 69 } 70 if (!current_segment.length) { 71 segments.push(current_segment); 72 } 73 current_segment.push({lat: lat, lng: lon}); 74 total_points_num += 1; 75 } 76 if (isNaN(expected_points_num) || (expected_points_num !== 0 && expected_points_num !== total_points_num)) { 77 error = 'CORRUPT'; 78 } 79 return [{name: name, tracks: segments, error: error}]; 80 } 81 82 function parseOziWpt(txt, name) { 83 var points = [], 84 error, 85 lines, line, 86 i, 87 lat, lng, pointName, fields; 88 txt = stripBom(txt); 89 lines = txt.split('\n'); 90 if (lines[0].indexOf('OziExplorer Waypoint File') !== 0) { 91 return null; 92 } 93 for (i = 4; i < lines.length; i++) { 94 line = lines[i].trim(); 95 if (!line) { 96 continue; 97 } 98 fields = line.split(','); 99 lat = parseFloat(fields[2]); 100 lng = parseFloat(fields[3]); 101 pointName = decodeCP1251(fields[1]).trim(); 102 if (isNaN(lat) || isNaN(lng)) { 103 error = 'CORRUPT'; 104 break; 105 } 106 points.push({ 107 lat: lat, 108 lng: lng, 109 name: pointName 110 } 111 ); 112 } 113 return [{name: name, points: points, error: error}]; 114 } 115 116 export {parseOziPlt, parseOziRte, parseOziWpt};