1
1
import { nodeVal } from "./shared" ;
2
2
3
+ const attributeNames = [
4
+ [ "speed" , "speeds" ] ,
5
+ [ "course" , "courses" ] ,
6
+ [ "hAcc" , "hAccs" ] ,
7
+ [ "vAcc" , "vAccs" ] ,
8
+ [ "heartRate" , "heartRates" ]
9
+ ] ;
10
+
3
11
function getLineStyle ( extensions ) {
4
12
const style = { } ;
5
13
if ( extensions ) {
@@ -72,11 +80,28 @@ function coordPair(x) {
72
80
ll . push ( e ) ;
73
81
}
74
82
}
75
- return {
83
+ const result = {
76
84
coordinates : ll ,
77
85
time : time ? nodeVal ( time ) : null ,
78
86
heartRate : heartRate ? parseFloat ( nodeVal ( heartRate ) ) : null
79
87
} ;
88
+
89
+ const extensions = get1 ( x , "extensions" ) ;
90
+ if ( extensions !== null ) {
91
+ attributeNames
92
+ . map ( r => r [ 0 ] )
93
+ . filter ( n => n !== "heartrate" )
94
+ . forEach ( name => {
95
+ const raw = get1 ( extensions , name ) ;
96
+ if ( raw !== null ) {
97
+ const v = parseFloat ( nodeVal ( raw ) ) ;
98
+ if ( ! isNaN ( v ) ) {
99
+ result [ name ] = v ;
100
+ }
101
+ }
102
+ } ) ;
103
+ }
104
+ return result ;
80
105
}
81
106
function getRoute ( node ) {
82
107
const line = getPoints ( node , "rtept" ) ;
@@ -98,46 +123,70 @@ function getPoints(node, pointname) {
98
123
const line = [ ] ;
99
124
const times = [ ] ;
100
125
const l = pts . length ;
101
- let heartRates = undefined ;
126
+ const extendedValues = { } ;
102
127
if ( l < 2 ) return { } ; // Invalid line in GeoJSON
103
128
for ( let i = 0 ; i < l ; i ++ ) {
104
129
const c = coordPair ( pts [ i ] ) ;
105
130
line . push ( c . coordinates ) ;
106
131
if ( c . time ) times . push ( c . time ) ;
107
- if ( c . heartRate || heartRates ) {
108
- if ( ! heartRates ) heartRates = Array ( i ) . fill ( null ) ;
109
- heartRates . push ( c . heartRate || null ) ;
110
- }
132
+ attributeNames
133
+ . map ( r => r [ 0 ] )
134
+ . forEach ( name => {
135
+ if ( c [ name ] || extendedValues [ name ] ) {
136
+ if ( ! extendedValues [ name ] ) {
137
+ extendedValues [ name ] = Array ( i ) . fill ( null ) ;
138
+ }
139
+ extendedValues [ name ] . push ( c [ name ] || null ) ;
140
+ }
141
+ } ) ;
111
142
}
112
- return {
143
+ const result = {
113
144
line : line ,
114
- times : times ,
115
- heartRates : heartRates || [ ]
145
+ times : times
116
146
} ;
147
+ attributeNames . forEach ( n => {
148
+ if ( extendedValues [ n [ 0 ] ] ) {
149
+ result [ n [ 1 ] ] = extendedValues [ n [ 0 ] ] || [ ] ;
150
+ }
151
+ } ) ;
152
+ return result ;
117
153
}
118
154
function getTrack ( node ) {
119
155
const segments = node . getElementsByTagName ( "trkseg" ) ;
120
156
const track = [ ] ;
121
157
const times = [ ] ;
122
- const heartRates = [ ] ;
158
+ const extendedValues = { } ;
123
159
let line ;
124
160
for ( let i = 0 ; i < segments . length ; i ++ ) {
125
161
line = getPoints ( segments [ i ] , "trkpt" ) ;
126
162
if ( line ) {
127
163
if ( line . line ) track . push ( line . line ) ;
128
164
if ( line . times && line . times . length ) times . push ( line . times ) ;
129
- if ( heartRates . length || ( line . heartRates && line . heartRates . length ) ) {
130
- if ( ! heartRates . length ) {
131
- for ( let s = 0 ; s < i ; s ++ ) {
132
- heartRates . push ( Array ( track [ s ] . length ) . fill ( null ) ) ;
165
+
166
+ attributeNames
167
+ . map ( r => r [ 1 ] )
168
+ . forEach ( name => {
169
+ if (
170
+ ( extendedValues [ name ] && extendedValues [ name ] . length ) ||
171
+ ( line [ name ] && line [ name ] . length )
172
+ ) {
173
+ if ( ! extendedValues [ name ] ) {
174
+ extendedValues [ name ] = [ ] ;
175
+ }
176
+ if ( ! extendedValues [ name ] . length ) {
177
+ for ( let s = 0 ; s < i ; s ++ ) {
178
+ extendedValues [ name ] . push ( Array ( track [ s ] . length ) . fill ( null ) ) ;
179
+ }
180
+ }
181
+ if ( line [ name ] && line [ name ] . length ) {
182
+ extendedValues [ name ] . push ( line [ name ] ) ;
183
+ } else {
184
+ extendedValues [ name ] . push (
185
+ Array ( line . line . length || 0 ) . fill ( null )
186
+ ) ;
187
+ }
133
188
}
134
- }
135
- if ( line . heartRates && line . heartRates . length ) {
136
- heartRates . push ( line . heartRates ) ;
137
- } else {
138
- heartRates . push ( Array ( line . line . length || 0 ) . fill ( null ) ) ;
139
- }
140
- }
189
+ } ) ;
141
190
}
142
191
}
143
192
if ( track . length === 0 ) return ;
@@ -147,8 +196,13 @@ function getTrack(node) {
147
196
) ;
148
197
if ( times . length )
149
198
properties . coordTimes = track . length === 1 ? times [ 0 ] : times ;
150
- if ( heartRates . length )
151
- properties . heartRates = track . length === 1 ? heartRates [ 0 ] : heartRates ;
199
+ attributeNames . forEach ( n => {
200
+ if ( extendedValues [ n [ 1 ] ] && extendedValues [ n [ 1 ] ] . length ) {
201
+ properties [ n [ 1 ] ] =
202
+ track . length === 1 ? extendedValues [ n [ 1 ] ] [ 0 ] : extendedValues [ n [ 1 ] ] ;
203
+ }
204
+ } ) ;
205
+
152
206
return {
153
207
type : "Feature" ,
154
208
properties : properties ,
0 commit comments