File tree Expand file tree Collapse file tree 4 files changed +27
-37
lines changed
calculate-distance-horizontal Expand file tree Collapse file tree 4 files changed +27
-37
lines changed Original file line number Diff line number Diff line change 1
1
const assert = require ( 'assert' ) ;
2
2
const hdistance = require ( './app.js' ) ;
3
3
4
+ const pos1 = { lat : '46.974974' , lon : '10.311162' } ;
5
+ const pos2 = { lat : '46.97495' , lon : '10.311144' } ;
6
+
4
7
describe ( 'calculate distance horizontal' , ( ) => {
5
8
it ( 'should return a positive result' , ( ) => {
6
- const pos1 = { lat : '46.974974' , lon : '10.311162' } ;
7
- const pos2 = { lat : '46.97495' , lon : '10.311144' } ;
8
9
const result = hdistance ( pos1 , pos2 ) ;
9
10
assert ( result > 0 ) ;
10
11
} ) ;
11
12
12
13
it ( 'should not return a negative value' , ( ) => {
13
- const pos1 = { lat : '46.97495' , lon : '10.311144' } ;
14
- const pos2 = { lat : '46.974974' , lon : '10.311162' } ;
15
- const result = hdistance ( pos1 , pos2 ) ;
14
+ const result = hdistance ( pos2 , pos1 ) ;
16
15
assert ( result > 0 ) ;
17
16
} ) ;
18
17
} ) ;
18
+
Original file line number Diff line number Diff line change 1
- const xml2js = require ( 'xml2js ' ) ;
1
+ const gpxParse = require ( 'gpx-parse ' ) ;
2
2
3
3
const get = function ( gpxData ) {
4
- const json = xml2JSON ( gpxData ) ;
4
+ let convertedPoints = [ ] ;
5
5
6
- return getDataPoints ( json ) ;
7
- } ;
8
-
9
- var xml2JSON = function ( xml ) {
10
- var parser = new xml2js . Parser ( ) ;
11
- var json ;
12
- parser . parseString ( xml , function ( err , result ) {
13
- json = result ;
6
+ gpxParse . parseGpx ( gpxData , ( error , data ) => {
7
+ const points = data . tracks . map ( track => track . segments . map ( segment => segment ) ) ;
8
+ convertedPoints = points [ 0 ] [ 0 ] . map ( point => ( {
9
+ date : point . time ,
10
+ ele : point . elevation ,
11
+ lat : point . lat ,
12
+ lon : point . lon
13
+ } ) ) ;
14
14
} ) ;
15
- return json ;
16
- }
17
15
18
- var getDataPoints = function ( json ) {
19
- var result = json [ "gpx" ] [ "trk" ] [ 0 ] [ "trkseg" ] [ 0 ] [ "trkpt" ] ;
20
- var formated = result . map ( function ( obj ) {
21
- var item = { } ;
22
- item . date = obj . time [ 0 ] ;
23
- item . ele = obj . ele [ 0 ] ;
24
- item . lat = obj . $ . lat ;
25
- item . lon = obj . $ . lon ;
26
- return item ;
27
- } ) ;
28
- return formated ;
29
- }
16
+ return convertedPoints ;
17
+ } ;
18
+
30
19
31
20
module . exports = get ;
Original file line number Diff line number Diff line change 9
9
"author" : " Maarten Groeneweg" ,
10
10
"license" : " ISC" ,
11
11
"dependencies" : {
12
+ "togeojson" : " ^0.16.0" ,
12
13
"xml2js" : " ^0.4.17"
13
14
},
14
15
"devDependencies" : {
Original file line number Diff line number Diff line change @@ -9,14 +9,14 @@ describe('Convert GPX data', () => {
9
9
const content = fs . readFileSync ( filePath , 'utf8' ) ;
10
10
11
11
const expected = [
12
- { date : '2015-01-11T15 :10:11.000+01:00 ' ,
13
- ele : ' 2182.0' ,
14
- lat : ' 46.974974' ,
15
- lon : ' 10.311162' } ,
16
- { date : '2015-01-11T15 :10:12.000+01:00 ' ,
17
- ele : ' 2185.0' ,
18
- lat : ' 46.97495' ,
19
- lon : ' 10.311144' }
12
+ { date : '2015-01-11T14 :10:11.000Z ' ,
13
+ ele : 2182 ,
14
+ lat : 46.974974 ,
15
+ lon : 10.311162 } ,
16
+ { date : '2015-01-11T14 :10:12.000Z ' ,
17
+ ele : 2185 ,
18
+ lat : 46.97495 ,
19
+ lon : 10.311144 }
20
20
] ;
21
21
const result = gpxData ( content ) ;
22
22
You can’t perform that action at this time.
0 commit comments