public List <Edge> ReadFile(string fileName) { using (var sr = new StreamReader(Server.MapPath($"~/App_Data/Shapes/{fileName}"))) { var edges = new List <Edge>(); while (!sr.EndOfStream) { var edgeStr = sr.ReadLine(); if (edgeStr != null) { var split = edgeStr.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (split.Length == 6) { var pointA = new Edge.Point(double.Parse(split[0]), double.Parse(split[1])); var pointB = new Edge.Point(double.Parse(split[2]), double.Parse(split[3])); var distance = double.Parse(split[4]); edges.Add(new Edge() { PointA = pointA, PointB = pointB, Distance = distance, ShapeId = Guid.Parse(split[5]) }); } else { throw new Exception("File Corrupt"); } } } return(edges); } }
public ActionResult SaveGraph(ShapeGroupModel model) { var existingEdges = new List <Edge>(); var existingPoints = new List <Edge.Point>(); foreach (var shape in model.Shapes) { int index = 0; Edge currentEdge = null; var shapeId = Guid.NewGuid(); foreach (var point in shape.Points) { // need we snap first and last points to the nearest vertice, within 20km var ordered = existingPoints.Select(p => new { Point = p, Distance = GetDistance(p.Lat, p.Lng, point.Lat, point.Lng) }).OrderBy(p => p.Distance).ToList(); var snapIn = ordered.FirstOrDefault(p => p.Distance < 5000); if (snapIn != null) { point.Lat = snapIn.Point.Lat; point.Lng = snapIn.Point.Lng; } var edge = new Edge.Point(point.Lat, point.Lng); existingPoints.Add(edge); if (currentEdge == null) { currentEdge = new Edge() { PointA = edge, ShapeId = shapeId }; } else { currentEdge.PointB = edge; currentEdge.Distance = GetDistance(currentEdge.PointA.Lat, currentEdge.PointA.Lng, currentEdge.PointB.Lat, currentEdge.PointB.Lng); existingEdges.Add(currentEdge); // We start up a new edge currentEdge = new Edge() { PointA = edge, ShapeId = shapeId }; } index++; } } SaveFile("graph.txt", existingEdges); return(Json(new { success = true })); }