33using System . IO ;
44using System . Linq ;
55using System . Text ;
6+ using GpxTools ;
67
78public class XmlTimeline {
89
@@ -33,12 +34,15 @@ public override string ToString() {
3334 }
3435
3536 //Time
36- public DateTime ReturnDate ( ) {
37+ public DateTime ? ReturnDate ( ) {
3738 DateTime tempDate = new DateTime ( ) ;
3839 if ( type == TimelineItemType . activity )
3940 tempDate = activity . startTime ;
40- else
41+ else {
42+ if ( ! place . startTime . HasValue )
43+ return null ;
4144 tempDate = place . startTime . Value ;
45+ }
4246 return new DateTime ( tempDate . Year , tempDate . Month , tempDate . Day , 12 , 0 , 0 , tempDate . Kind ) ;
4347 }
4448
@@ -76,13 +80,19 @@ public DateTime StartTime {
7680 public class Coordinates {
7781 public double lat ;
7882 public double lon ;
79- public string ele = null ;
83+ public double ? ele = null ;
8084 public DateTime ? time = null ;
8185
8286 public Coordinates ( string lat , string lon ) {
8387 this . lat = Convert . ToDouble ( lat ) ;
8488 this . lon = Convert . ToDouble ( lon ) ;
8589 }
90+ public Coordinates ( double lat , double lon , double ? ele , DateTime ? time ) {
91+ this . lat = lat ;
92+ this . lon = lon ;
93+ this . ele = ele ;
94+ this . time = time ;
95+ }
8696 public Coordinates ( double lat , double lon ) {
8797 this . lat = lat ;
8898 this . lon = lon ;
@@ -97,7 +107,7 @@ public class Place {
97107 public Coordinates position ;
98108 public DateTime ? startTime ;
99109 public DateTime ? endTime ;
100- public string ele ;
110+ public double ? ele ;
101111 public string link ;
102112
103113 public int Duration {
@@ -111,10 +121,11 @@ public int Duration {
111121 }
112122 }
113123
114- public Place ( Coordinates position , string name , DateTime ? startTime = null , string ele = null , string link = null ) {
124+ public Place ( Coordinates position , string name , DateTime ? startTime = null , double ? ele = null , string link = null ) {
115125 this . position = position ;
116126 this . startTime = startTime ;
117127 this . name = name ;
128+ this . ele = ele ;
118129 this . link = link ;
119130 }
120131
@@ -245,12 +256,16 @@ public class XmlReader {
245256 // Activity and places loading
246257 public XmlReader ( string path , bool isPath , float weight ) {
247258 this . weight = weight ;
259+
260+ string allText = "" ;
248261 if ( isPath ) {
262+ allText = File . ReadAllText ( path ) ;
249263 originalName = path . Replace ( ".gpx" , "" ) ;
250- LoadFile ( path ) ;
251- } else {
252- LoadString ( path ) ;
253- }
264+ } else
265+ allText = path ;
266+ byte [ ] byteArray = Encoding . UTF8 . GetBytes ( allText ) ;
267+ MemoryStream stream = new MemoryStream ( byteArray ) ;
268+ Load ( stream ) ;
254269 }
255270 public XmlReader ( List < XmlTimeline . TimelineItem > timelineItems ) {
256271 for ( int i = 0 ; i < 10 ; i ++ ) {
@@ -262,119 +277,72 @@ public XmlReader(List<XmlTimeline.TimelineItem> timelineItems) {
262277 SetXmlDate ( ) ;
263278 }
264279
265- public void Load ( StreamReader sr ) {
280+ public void Load ( Stream stream ) {
266281 for ( int i = 0 ; i < 10 ; i ++ ) {
267282 activitySummary [ i ] = new List < XmlTimeline . Activity > ( ) ;
268283 }
269284
270- // Ignore first 2 lines
271- sr . ReadLine ( ) ;
272- sr . ReadLine ( ) ;
273-
274285 // Loop
275286 timelineItems . Clear ( ) ;
276- while ( true ) {
277- string line = sr . ReadLine ( ) . Replace ( "\t " , "" ) ;
278- if ( line . StartsWith ( "<wpt" , StringComparison . Ordinal ) )
279- GetPlace ( line , sr ) ;
280- else if ( line . StartsWith ( "<trk" , StringComparison . Ordinal ) )
281- GetMove ( sr ) ;
282- else if ( line . StartsWith ( "</gpx" , StringComparison . Ordinal ) )
283- break ;
284- } ;
285- sr . Close ( ) ;
287+ GpxReader gpxReader = new GpxReader ( stream ) ;
288+ while ( gpxReader . Read ( ) ) {
289+ switch ( gpxReader . ObjectType ) {
290+ case GpxObjectType . Metadata :
291+ //gpxReader.Metadata;
292+ break ;
293+ case GpxObjectType . WayPoint :
294+ GetPlace ( gpxReader . WayPoint ) ;
295+ break ;
296+ case GpxObjectType . Track :
297+ GetMove ( gpxReader . Track ) ;
298+ break ;
299+ }
300+ }
286301 SetStartEnd ( ) ;
287302 SetSummary ( ) ;
288303 SetXmlDate ( ) ;
289304
290305 //Display();
291306 }
292307
293- public void LoadString ( string text ) {
294- MemoryStream mStrm = new MemoryStream ( Encoding . UTF8 . GetBytes ( text ) ) ;
295- StreamReader tempSteamR = new StreamReader ( mStrm , System . Text . Encoding . UTF8 , true ) ;
296- Load ( tempSteamR ) ;
297- }
298- public void LoadFile ( string path ) {
299- StreamReader sr = new StreamReader ( path ) ;
300- Load ( sr ) ;
301- }
302-
303- void GetPlace ( string line , StreamReader sr ) {
304- if ( line . EndsWith ( "/>" ) )
305- return ;
306- XmlTimeline . Coordinates location = HelpMethods . GetLatLon ( line ) ;
307-
308+ void GetPlace ( GpxTools . Gpx . GpxWayPoint waypoint ) {
309+ // location
310+ XmlTimeline . Coordinates location = new XmlTimeline . Coordinates ( waypoint . Latitude , waypoint . Longitude ) ;
308311 // time
309- DateTime ? startTime = null ;
310-
311- // time
312- string tempLine = sr . ReadLine ( ) . Replace ( "\t " , "" ) ;
313- if ( tempLine . StartsWith ( "<time>" ) ) {
314- startTime = HelpMethods . ParseIso8601 (
315- HelpMethods . LeaveCenterFromString (
316- tempLine ,
317- "<time>" ,
318- "</time>" ) ) ;
319- tempLine = sr . ReadLine ( ) . Replace ( "\t " , "" ) ;
320- }
321-
312+ DateTime ? startTime = waypoint . Time ;
322313 // ele
323- string ele = "" ;
324- tempLine = sr . ReadLine ( ) . Replace ( "\t " , "" ) ;
325- if ( tempLine . StartsWith ( "<ele>" ) ) {
326- ele = HelpMethods . LeaveCenterFromString ( tempLine , "<ele>" , "</ele>" ) ;
327- tempLine = sr . ReadLine ( ) . Replace ( "\t " , "" ) ;
328- }
329-
314+ double ? ele = waypoint . Elevation ;
330315 // name (if exist)
331- string name = "" ;
332- if ( tempLine . StartsWith ( "<name>" ) ) {
333- name = HelpMethods . LeaveCenterFromString ( tempLine . Replace ( "&" , "&" ) . Replace ( "<" , "<" ) . Replace ( ">" , ">" ) . Replace ( """ , "\" " ) . Replace ( "'" , "'" ) , "<name>" , "</name>" ) ;
334- tempLine = sr . ReadLine ( ) . Replace ( "\t " , "" ) ;
335- }
316+ string name = waypoint . Name ;
336317 string link = "" ;
337- if ( tempLine . StartsWith ( "<link" ) ) {
338- link = HelpMethods . LeaveCenterFromString ( tempLine , "<link href=\" " , "\" />" ) ;
339- tempLine = sr . ReadLine ( ) . Replace ( "\t " , "" ) ;
340- }
341-
318+ if ( waypoint . Links . Count > 0 )
319+ link = waypoint . Links [ 0 ] . Href ;
342320 // If previous is place
343- if ( timelineItems . Count >= 1 && timelineItems . Last ( ) . type == XmlTimeline . TimelineItemType . place )
344- timelineItems . Last ( ) . place . endTime = startTime ;
345-
346- //if (timelineItems.Count >= 1 && timelineItems.Last().type == XmlTimeline.TimelineItemType.activity)
347- // startTime = timelineItems.Last().activity.endTime;
321+ if ( timelineItems . Count >= 1 && timelineItems . Last ( ) . type == XmlTimeline . TimelineItemType . place ) {
322+ if ( ! timelineItems . Last ( ) . place . endTime . HasValue && startTime . HasValue )
323+ timelineItems . Last ( ) . place . endTime = startTime ;
324+ else {
325+ startTime = timelineItems . Last ( ) . place . startTime ;
326+ }
327+ } else if ( timelineItems . Count >= 1 && timelineItems . Last ( ) . type == XmlTimeline . TimelineItemType . activity && ! startTime . HasValue )
328+ startTime = timelineItems . Last ( ) . activity . endTime ;
348329 timelineItems . Add ( new XmlTimeline . TimelineItem ( new XmlTimeline . Place ( location , name , startTime , ele , link ) ) ) ;
349330 }
350- void GetMove ( StreamReader sr ) {
331+ void GetMove ( GpxTools . Gpx . GpxTrack track ) {
332+
351333 // Type
352- string line = sr . ReadLine ( ) . Replace ( "\t " , "" ) ;
353- if ( line == "<trkseg />" ) {
354- sr . ReadLine ( ) ;
355- return ;
356- }
357334 ActivityType type = ActivityType . car ;
358- if ( line . StartsWith ( "<type>" , StringComparison . CurrentCulture ) ) {
359- line = HelpMethods . LeaveCenterFromString ( line , "<type>" , "</type>" ) ;
360- Enum . TryParse ( line , out type ) ;
335+ Enum . TryParse ( track . Type , out type ) ;
361336
362- // Track points
363- line = sr . ReadLine ( ) . Replace ( "\t " , "" ) ;
364- }
365- if ( line == "<trkseg />" ) {
366- sr . ReadLine ( ) ;
367- return ;
368- }
337+ // Track points
369338 List < XmlTimeline . Coordinates > coords = new List < XmlTimeline . Coordinates > ( ) ;
370- while ( true ) {
371- line = sr . ReadLine ( ) . Replace ( "\t " , "" ) ;
372- if ( line == "</trkseg>" )
373- break ;
374- else {
375- AddWaypoint ( line , sr , coords ) ;
376- }
339+ GpxTools . Gpx . GpxPointCollection < GpxTools . Gpx . GpxPoint > points = new GpxTools . Gpx . GpxPointCollection < GpxTools . Gpx . GpxPoint > ( ) ;
340+
341+ points = track . ToGpxPoints ( ) ;
342+ foreach ( var item in points ) {
343+ coords . Add ( new XmlTimeline . Coordinates ( item . Latitude , item . Longitude , item . Elevation , item . Time ) ) ;
377344 }
345+
378346 if ( coords . Count >= 2 ) {
379347 if ( timelineItems . Count > 0 &&
380348 timelineItems [ timelineItems . Count - 1 ] . type == XmlTimeline . TimelineItemType . activity &&
@@ -385,39 +353,40 @@ void GetMove(StreamReader sr) {
385353 AddTimeToPreviousPlace ( newActivity ) ;
386354 timelineItems . Add ( new XmlTimeline . TimelineItem ( newActivity ) ) ;
387355 AddTimeToPreviousPlace ( newActivity ) ;
388- //activitySummary[(int)type].Add(newActivity);
389356 }
390357 }
391- sr . ReadLine ( ) ;
392- }
393- void AddWaypoint ( string line , StreamReader sr , List < XmlTimeline . Coordinates > coords ) {
394- XmlTimeline . Coordinates location = HelpMethods . GetLatLon ( line ) ;
395- location . ele = HelpMethods . LeaveCenterFromString ( sr . ReadLine ( ) . Replace ( "\t " , "" ) , "<ele>" , "</ele>" ) ;
396- location . time = HelpMethods . ParseIso8601 (
397- HelpMethods . LeaveCenterFromString (
398- sr . ReadLine ( ) . Replace ( "\t " , "" ) ,
399- "<time>" ,
400- "</time>" ) ) ;
401- sr . ReadLine ( ) ;
402- coords . Add ( location ) ;
403358 }
404359 void AddTimeToPreviousPlace ( XmlTimeline . Activity activity ) {
405360 if ( timelineItems . Count >= 1 ) {
406361 if ( timelineItems . Last ( ) . type == XmlTimeline . TimelineItemType . place )
407362 timelineItems . Last ( ) . place . endTime = activity . startTime ;
363+ if ( timelineItems . Count >= 2 )
364+ if ( timelineItems [ timelineItems . Count - 2 ] . type == XmlTimeline . TimelineItemType . place && ! timelineItems [ timelineItems . Count - 2 ] . place . endTime . HasValue )
365+ timelineItems [ timelineItems . Count - 2 ] . place . endTime = activity . startTime ;
408366 }
409367 }
410368
411369 // End calculations
412370 void SetStartEnd ( ) {
413371 if ( timelineItems . First ( ) . type == XmlTimeline . TimelineItemType . place && ! timelineItems . First ( ) . place . startTime . HasValue ) {
414- DateTime time = timelineItems . First ( ) . place . endTime . Value ;
372+ DateTime time = new DateTime ( ) ;
373+ if ( timelineItems . First ( ) . place . endTime . HasValue )
374+ time = timelineItems . First ( ) . place . endTime . Value ;
375+ else if ( timelineItems [ 1 ] . type == XmlTimeline . TimelineItemType . place && timelineItems [ 1 ] . place . startTime . HasValue )
376+ time = timelineItems [ 1 ] . place . startTime . Value ;
377+ else if ( timelineItems [ 1 ] . type == XmlTimeline . TimelineItemType . activity )
378+ time = timelineItems [ 1 ] . activity . startTime ;
415379 DateTime newTime = new DateTime ( time . Year , time . Month , time . Day , 0 , 0 , 0 , time . Kind ) ;
416380 timelineItems . First ( ) . place . startTime = newTime ;
417381 }
418-
419382 if ( timelineItems . Last ( ) . type == XmlTimeline . TimelineItemType . place && ! timelineItems . Last ( ) . place . endTime . HasValue ) {
420- DateTime time = timelineItems . Last ( ) . place . startTime . Value ;
383+ DateTime time = new DateTime ( ) ;
384+ if ( timelineItems . Last ( ) . place . startTime . HasValue )
385+ time = timelineItems . Last ( ) . place . startTime . Value ;
386+ else if ( timelineItems [ timelineItems . Count - 2 ] . type == XmlTimeline . TimelineItemType . place && timelineItems [ timelineItems . Count - 2 ] . place . endTime . HasValue )
387+ time = timelineItems [ timelineItems . Count - 2 ] . place . endTime . Value ;
388+ else if ( timelineItems [ timelineItems . Count - 2 ] . type == XmlTimeline . TimelineItemType . activity )
389+ time = timelineItems [ timelineItems . Count - 2 ] . activity . endTime ;
421390 DateTime newTime = new DateTime ( time . Year , time . Month , time . Day , 23 , 59 , 59 , time . Kind ) ;
422391 timelineItems . Last ( ) . place . endTime = newTime ;
423392 }
@@ -462,6 +431,7 @@ void Display() {
462431 }
463432 Console . WriteLine ( ) ;
464433 Console . ForegroundColor = ConsoleColor . DarkGray ;
434+ Console . WriteLine ( "Lenght: " + summary . Length ) ;
465435 foreach ( var item in summary ) {
466436 if ( item . duration > 0 )
467437 Console . WriteLine ( item . ToString ( ) ) ;
@@ -488,7 +458,9 @@ public static List<XmlReader> Split(XmlReader xml) {
488458 currentDate = item . ReturnDate ( ) ;
489459 }
490460
491- if ( currentDate == item . ReturnDate ( ) ) {
461+ if ( item . ReturnDate ( ) == null ) {
462+
463+ } if ( currentDate == item . ReturnDate ( ) ) {
492464 tempList . Add ( item ) ;
493465 lastItem = item ;
494466 } else {
0 commit comments