예제 #1
0
        public JsonResult GetShapes()
        {
            var graph = new ShapeGroupModel();

            var edges = ReadFile("graph.txt");

            graph.Shapes = edges.GroupBy(p => p.ShapeId).Select(s =>
                                                                new ShapeGroupModel.ShapeModel()
            {
                Points = s.SelectMany(ep => new List <Edge.Point>()
                {
                    ep.PointA, ep.PointB
                }).Distinct().Select(pm => new ShapeGroupModel.PointModel()
                {
                    Lat = pm.Lat,
                    Lng = pm.Lng
                }).ToList()
            }).ToList();


            return(Json(graph, JsonRequestBehavior.AllowGet));
        }
예제 #2
0
        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 }));
        }