Skip to content

Commit 2956c7a

Browse files
committed
Basic places manager
1 parent 18d6baa commit 2956c7a

File tree

6 files changed

+191
-88
lines changed

6 files changed

+191
-88
lines changed

Arc-app-export-converter/Arc-app-export-converter.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
<Compile Include="BurnedCalCalculator.cs" />
4141
<Compile Include="XmlReader.cs" />
4242
<Compile Include="JsonParser.cs" />
43+
<Compile Include="PlacesManager.cs" />
44+
<Compile Include="HelpMethods.cs" />
4345
</ItemGroup>
4446
<ItemGroup>
4547
<None Include="packages.config" />
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
3+
public static class HelpMethods {
4+
5+
// Copied from
6+
// https://stackoverflow.com/questions/6366408/calculating-distance-between-two-latitude-and-longitude-geocoordinates
7+
8+
public static float DistanceTo(XmlTimeline.Coordinates wp1, XmlTimeline.Coordinates wp2, char unit = 'K') {
9+
double rlat1 = Math.PI * wp1.lat / 180;
10+
double rlat2 = Math.PI * wp2.lat / 180;
11+
double theta = wp1.lon - wp2.lon;
12+
double rtheta = Math.PI * theta / 180;
13+
double dist =
14+
Math.Sin(rlat1) * Math.Sin(rlat2) + Math.Cos(rlat1) *
15+
Math.Cos(rlat2) * Math.Cos(rtheta);
16+
dist = Math.Acos(dist);
17+
dist = dist * 180 / Math.PI;
18+
dist = dist * 60 * 1.1515;
19+
20+
float distF = (float)dist;
21+
22+
switch (unit) {
23+
case 'K': //Kilometers -> default
24+
return distF * 1.609344f;
25+
case 'N': //Nautical Miles
26+
return distF * 0.8684f;
27+
case 'M': //Miles
28+
return distF;
29+
}
30+
31+
return distF;
32+
}
33+
34+
public static DateTime ParseIso8601(string iso8601Time) {
35+
return DateTime.Parse(iso8601Time, null, System.Globalization.DateTimeStyles.RoundtripKind);
36+
}
37+
38+
public static XmlTimeline.Coordinates GetLatLon(string line) {
39+
string lat = "";
40+
string lon = "";
41+
bool captureMode = false;
42+
string capture = "";
43+
foreach (char item in line) {
44+
if (item == '"') {
45+
captureMode = !captureMode;
46+
if (captureMode == false) {
47+
if (lat == "")
48+
lat = capture;
49+
else
50+
lon = capture;
51+
capture = "";
52+
}
53+
} else if (captureMode) {
54+
capture += item;
55+
}
56+
}
57+
return new XmlTimeline.Coordinates(lat, lon);
58+
}
59+
public static string LeaveCenterFromString(string text, string removeLeft, string removeRight) {
60+
string temp = text;
61+
temp = temp.Replace(removeLeft, "");
62+
temp = temp.Replace(removeRight, "");
63+
return temp;
64+
}
65+
66+
public static string ConvertToIso1601(DateTime time) {
67+
string output = time.ToString(Iso1601Format);
68+
return output.Replace(":", "");
69+
}
70+
public static string Iso1601Format = "yyyyMMddTHHmmsszzz";
71+
72+
public static JsonMoves.ActivityGroup ReturnGroup(ActivityType type) {
73+
switch (type) {
74+
case ActivityType.walking:
75+
return JsonMoves.ActivityGroup.walking;
76+
case ActivityType.running:
77+
return JsonMoves.ActivityGroup.running;
78+
case ActivityType.cycling:
79+
return JsonMoves.ActivityGroup.cycling;
80+
default:
81+
return JsonMoves.ActivityGroup.transport;
82+
}
83+
}
84+
}
85+

Arc-app-export-converter/JsonParser.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ public Location(double lat, double lon) {
4747
this.lon = lon;
4848
}
4949
}
50-
public long id = 1234567;
50+
public int id;
5151
public string name;
5252
public string type = "user";
5353
public Location location;
5454

55-
public Place(string name, Location location) {
55+
public Place(string name, Location location, DateTime startTime, DateTime endTime) {
5656
this.name = name;
5757
this.location = location;
58+
this.id = PlacesManager.ReturnPlaceId(this, startTime, endTime);
5859
}
5960

6061
}
@@ -179,7 +180,10 @@ static void ParseSegments(int i, JsonMoves json, List<XmlReader> xml) {
179180
}
180181
static JsonMoves.Day.Segment SegmentPlace(XmlTimeline.Place item) {
181182
JsonMoves.Day.Segment output = new JsonMoves.Day.Segment(JsonMoves.SegmentType.place, item.startTime.Value, item.endTime.Value);
182-
output.place = new JsonMoves.Day.Segment.Place(item.name, new JsonMoves.Day.Segment.Place.Location(item.position.lat, item.position.lon));
183+
output.place = new JsonMoves.Day.Segment.Place(item.name,
184+
new JsonMoves.Day.Segment.Place.Location(item.position.lat, item.position.lon),
185+
item.startTime.Value,
186+
item.endTime.Value);
183187
return output;
184188
}
185189
static JsonMoves.Day.Segment SegmentMove(XmlTimeline.Activity item) {
@@ -217,7 +221,7 @@ static void WriteToFile(string json) {
217221
sw.Write(json);
218222
sw.Close();
219223
Console.ForegroundColor = ConsoleColor.DarkGreen;
220-
Console.Write("Json file created!");
224+
Console.WriteLine("Json file created!");
221225
}
222226
}
223227

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Newtonsoft.Json;
5+
6+
public class Place {
7+
public class PlaceVisit {
8+
public DateTime startTime;
9+
public DateTime endTime;
10+
11+
public PlaceVisit(DateTime startTime, DateTime endTime) {
12+
this.startTime = startTime;
13+
this.endTime = endTime;
14+
}
15+
}
16+
public int id;
17+
public string name;
18+
public JsonMoves.Day.Segment.Place.Location location;
19+
public List<PlaceVisit> visits = new List<PlaceVisit>();
20+
21+
public Place(int id, string name, JsonMoves.Day.Segment.Place.Location location) {
22+
this.id = id;
23+
this.name = name;
24+
this.location = location;
25+
}
26+
27+
public void AddVisit(DateTime startTime, DateTime endTime) {
28+
visits.Add(new PlaceVisit(startTime, endTime));
29+
}
30+
}
31+
32+
public class PlacesSave {
33+
public List<Place> places = new List<Place>();
34+
public int currentId;
35+
36+
public PlacesSave(List<Place> places, int currentId) {
37+
this.places = places;
38+
this.currentId = currentId;
39+
}
40+
}
41+
42+
public class PlacesManager {
43+
static bool loaded;
44+
static string placesFileName = "places.json";
45+
46+
static List<Place> places = new List<Place>();
47+
static int currentId;
48+
49+
public static bool Loaded {
50+
get {
51+
if (!loaded) {
52+
LoadPlaces();
53+
loaded = true;
54+
}
55+
return true;
56+
}
57+
}
58+
59+
public static int ReturnPlaceId(JsonMoves.Day.Segment.Place place, DateTime startTime, DateTime endTime) {
60+
foreach (var item in places) {
61+
if (place.name == item.name) {
62+
item.AddVisit(startTime, endTime);
63+
return item.id;
64+
}
65+
}
66+
Place newPlace = new Place(currentId++, place.name, place.location);
67+
places.Add(newPlace);
68+
newPlace.AddVisit(startTime, endTime);
69+
return newPlace.id;
70+
}
71+
72+
// Loading files
73+
static void LoadPlaces() {
74+
if (File.Exists(placesFileName)) {
75+
StreamReader sr = new StreamReader(placesFileName);
76+
string line = sr.ReadLine();
77+
sr.Close();
78+
PlacesSave save = JsonConvert.DeserializeObject<PlacesSave>(line);
79+
places = save.places;
80+
currentId = save.currentId;
81+
}
82+
}
83+
public static void SavePlaces() {
84+
StreamWriter sw = new StreamWriter(placesFileName);
85+
sw.Write(JsonConvert.SerializeObject(new PlacesSave(places, currentId)));
86+
sw.Close();
87+
Console.WriteLine("Places database saved to file " + placesFileName);
88+
}
89+
90+
}

Arc-app-export-converter/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ class MainClass {
66

77
[STAThread]
88
public static void Main() {
9+
if (PlacesManager.Loaded)
10+
Console.WriteLine("Places initialised");
911
Console.Write("Input your weight (in kg): ");
1012
weight = Convert.ToInt32(Console.ReadLine());
1113
XmlReader xr = new XmlReader();
1214
xr.LoadFile();
1315
xr.ParseToJson();
16+
17+
// On finish
18+
PlacesManager.SavePlaces();
1419
}
1520
}

Arc-app-export-converter/XmlReader.cs

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -335,87 +335,4 @@ public void ParseToJson() {
335335
};
336336
JsonParser.Parse(tempList);
337337
}
338-
}
339-
340-
public static class HelpMethods {
341-
342-
// Copied from
343-
// https://stackoverflow.com/questions/6366408/calculating-distance-between-two-latitude-and-longitude-geocoordinates
344-
345-
public static float DistanceTo(XmlTimeline.Coordinates wp1, XmlTimeline.Coordinates wp2, char unit = 'K') {
346-
double rlat1 = Math.PI * wp1.lat / 180;
347-
double rlat2 = Math.PI * wp2.lat / 180;
348-
double theta = wp1.lon - wp2.lon;
349-
double rtheta = Math.PI * theta / 180;
350-
double dist =
351-
Math.Sin(rlat1) * Math.Sin(rlat2) + Math.Cos(rlat1) *
352-
Math.Cos(rlat2) * Math.Cos(rtheta);
353-
dist = Math.Acos(dist);
354-
dist = dist * 180 / Math.PI;
355-
dist = dist * 60 * 1.1515;
356-
357-
float distF = (float)dist;
358-
359-
switch (unit) {
360-
case 'K': //Kilometers -> default
361-
return distF * 1.609344f;
362-
case 'N': //Nautical Miles
363-
return distF * 0.8684f;
364-
case 'M': //Miles
365-
return distF;
366-
}
367-
368-
return distF;
369-
}
370-
371-
public static DateTime ParseIso8601 (string iso8601Time) {
372-
return DateTime.Parse(iso8601Time, null, System.Globalization.DateTimeStyles.RoundtripKind);
373-
}
374-
375-
public static XmlTimeline.Coordinates GetLatLon(string line) {
376-
string lat = "";
377-
string lon = "";
378-
bool captureMode = false;
379-
string capture = "";
380-
foreach (char item in line) {
381-
if (item == '"') {
382-
captureMode = !captureMode;
383-
if (captureMode == false) {
384-
if (lat == "")
385-
lat = capture;
386-
else
387-
lon = capture;
388-
capture = "";
389-
}
390-
} else if (captureMode) {
391-
capture += item;
392-
}
393-
}
394-
return new XmlTimeline.Coordinates(lat, lon);
395-
}
396-
public static string LeaveCenterFromString(string text, string removeLeft, string removeRight) {
397-
string temp = text;
398-
temp = temp.Replace(removeLeft, "");
399-
temp = temp.Replace(removeRight, "");
400-
return temp;
401-
}
402-
403-
public static string ConvertToIso1601(DateTime time) {
404-
string output = time.ToString(Iso1601Format);
405-
return output.Replace(":", "");
406-
}
407-
public static string Iso1601Format = "yyyyMMddTHHmmsszzz";
408-
409-
public static JsonMoves.ActivityGroup ReturnGroup(ActivityType type) {
410-
switch (type) {
411-
case ActivityType.walking:
412-
return JsonMoves.ActivityGroup.walking;
413-
case ActivityType.running:
414-
return JsonMoves.ActivityGroup.running;
415-
case ActivityType.cycling:
416-
return JsonMoves.ActivityGroup.cycling;
417-
default:
418-
return JsonMoves.ActivityGroup.transport;
419-
}
420-
}
421-
}
338+
}

0 commit comments

Comments
 (0)