-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New places ids and smart places grouping
- Loading branch information
Showing
7 changed files
with
221 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
|
||
public static class HelpMethods { | ||
|
||
// Distance measure | ||
public static float DistanceTo(XmlTimeline.Coordinates wp1, XmlTimeline.Coordinates wp2, char unit = 'K') { | ||
// Copied from | ||
// https://stackoverflow.com/questions/6366408/calculating-distance-between-two-latitude-and-longitude-geocoordinates | ||
|
||
|
||
double rlat1 = Math.PI * wp1.lat / 180; | ||
|
||
double rlat2 = Math.PI * wp2.lat / 180; | ||
double theta = wp1.lon - wp2.lon; | ||
double rtheta = Math.PI * theta / 180; | ||
double dist = | ||
Math.Sin(rlat1) * Math.Sin(rlat2) + Math.Cos(rlat1) * | ||
Math.Cos(rlat2) * Math.Cos(rtheta); | ||
dist = Math.Acos(dist); | ||
dist = dist * 180 / Math.PI; | ||
dist = dist * 60 * 1.1515; | ||
|
||
float distF = (float)dist; | ||
|
||
switch (unit) { | ||
case 'K': //Kilometers -> default | ||
return distF * 1.609344f; | ||
case 'N': //Nautical Miles | ||
return distF * 0.8684f; | ||
case 'M': //Miles | ||
return distF; | ||
} | ||
|
||
return distF; | ||
} | ||
public static float DistanceTo(JsonMoves.Day.Segment.Place.Location jwp1, JsonMoves.Day.Segment.Place.Location jwp2, char unit = 'K') { | ||
XmlTimeline.Coordinates wp1 = new XmlTimeline.Coordinates(jwp1.lat, jwp1.lon); | ||
XmlTimeline.Coordinates wp2 = new XmlTimeline.Coordinates(jwp2.lat, jwp2.lon); | ||
return DistanceTo(wp1, wp2, unit); | ||
} | ||
|
||
// Time conversations | ||
public static DateTime ParseIso8601(string iso8601Time) { | ||
return DateTime.Parse(iso8601Time, null, System.Globalization.DateTimeStyles.RoundtripKind); | ||
} | ||
public static string ConvertToIso1601(DateTime time) { | ||
string output = time.ToString(Iso1601Format); | ||
return output.Replace(":", ""); | ||
} | ||
public static string Iso1601Format = "yyyyMMddTHHmmsszzz"; | ||
|
||
// GPX conversion | ||
public static XmlTimeline.Coordinates GetLatLon(string line) { | ||
string lat = ""; | ||
string lon = ""; | ||
bool captureMode = false; | ||
string capture = ""; | ||
foreach (char item in line) { | ||
if (item == '"') { | ||
captureMode = !captureMode; | ||
if (captureMode == false) { | ||
if (lat == "") | ||
lat = capture; | ||
else | ||
lon = capture; | ||
capture = ""; | ||
} | ||
} else if (captureMode) { | ||
capture += item; | ||
} | ||
} | ||
return new XmlTimeline.Coordinates(lat, lon); | ||
} | ||
public static string LeaveCenterFromString(string text, string removeLeft, string removeRight) { | ||
string temp = text; | ||
temp = temp.Replace(removeLeft, ""); | ||
temp = temp.Replace(removeRight, ""); | ||
return temp; | ||
} | ||
|
||
// To Json conversion | ||
public static JsonMoves.ActivityGroup ReturnGroup(ActivityType type) { | ||
switch (type) { | ||
case ActivityType.walking: | ||
return JsonMoves.ActivityGroup.walking; | ||
case ActivityType.running: | ||
return JsonMoves.ActivityGroup.running; | ||
case ActivityType.cycling: | ||
return JsonMoves.ActivityGroup.cycling; | ||
default: | ||
return JsonMoves.ActivityGroup.transport; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Newtonsoft.Json; | ||
|
||
public class Place { | ||
public class PlaceVisit { | ||
public DateTime startTime; | ||
public DateTime endTime; | ||
|
||
public PlaceVisit(DateTime startTime, DateTime endTime) { | ||
this.startTime = startTime; | ||
this.endTime = endTime; | ||
} | ||
} | ||
public int id; | ||
public string name; | ||
public JsonMoves.Day.Segment.Place.Location location; | ||
public List<PlaceVisit> visits = new List<PlaceVisit>(); | ||
|
||
public Place(int id, string name, JsonMoves.Day.Segment.Place.Location location) { | ||
this.id = id; | ||
this.name = name; | ||
this.location = location; | ||
} | ||
|
||
public void AddVisit(DateTime startTime, DateTime endTime) { | ||
PlaceVisit tempVisit = new PlaceVisit(startTime, endTime); | ||
foreach (var item in visits) { | ||
if (item.startTime == startTime) | ||
return; | ||
} | ||
visits.Add(tempVisit); | ||
} | ||
} | ||
|
||
public class PlacesSave { | ||
public List<Place> places = new List<Place>(); | ||
public int currentId; | ||
|
||
public PlacesSave(List<Place> places, int currentId) { | ||
this.places = places; | ||
this.currentId = currentId; | ||
} | ||
} | ||
|
||
public class PlacesManager { | ||
static bool loaded; | ||
static string placesFileName = "places.json"; | ||
static float distanceTolerance = 0.07f; | ||
|
||
static List<Place> places = new List<Place>(); | ||
static int currentId; | ||
|
||
public static bool Loaded { | ||
get { | ||
if (!loaded) { | ||
LoadPlaces(); | ||
loaded = true; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
public static int ReturnPlaceId(JsonMoves.Day.Segment.Place place, DateTime startTime, DateTime endTime) { | ||
foreach (var item in places) { | ||
if (CheckIfSamePlace(item, place)) { | ||
item.AddVisit(startTime, endTime); | ||
return item.id; | ||
} | ||
} | ||
Place newPlace = new Place(currentId++, place.name, place.location); | ||
places.Add(newPlace); | ||
newPlace.AddVisit(startTime, endTime); | ||
return newPlace.id; | ||
} | ||
static bool CheckIfSamePlace(Place place, JsonMoves.Day.Segment.Place jsonPlace) { | ||
if (place.name == jsonPlace.name) { | ||
if (HelpMethods.DistanceTo(place.location, jsonPlace.location) <= distanceTolerance) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
// Loading files | ||
static void LoadPlaces() { | ||
if (File.Exists(placesFileName)) { | ||
StreamReader sr = new StreamReader(placesFileName); | ||
string line = sr.ReadLine(); | ||
sr.Close(); | ||
PlacesSave save = JsonConvert.DeserializeObject<PlacesSave>(line); | ||
places = save.places; | ||
currentId = save.currentId; | ||
} | ||
} | ||
public static void SavePlaces() { | ||
StreamWriter sw = new StreamWriter(placesFileName); | ||
sw.Write(JsonConvert.SerializeObject(new PlacesSave(places, currentId))); | ||
sw.Close(); | ||
Console.WriteLine("Places database saved to file " + placesFileName); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters