Beispiel #1
0
 private dynamic MapStationForList(RoutePiece piece)
 {
     return(new
     {
         name = Utils.Capitalize(piece.Name),
         link = "/directorio/" + piece.Route.Type.SeoName + "/" + piece.Route.SeoName + "/" + piece.SeoName
     });
 }
Beispiel #2
0
 public static ResultRoutePiece FromRoutePiece(RoutePiece piece)
 {
     ResultRoutePiece res = new ResultRoutePiece();
     res.ID = piece.ID;
     res.Lat = piece.Lat;
     res.Lng = piece.Lng;
     res.Name = piece.Name;
     res.Route = ResultRoute.FromRoute(piece.Route);
     return res;
 }
Beispiel #3
0
        public static void ProcessRoute(string url, string name, string from, string to, int transportType)
        {
            Console.WriteLine(name + " " + from + " - " + to);

            using (var context = new DataContext())
            {
                Route route = new Route();
                route.Name     = name;
                route.Status   = 1;
                route.FromName = from;
                route.ToName   = to;
                route.TypeID   = transportType;

                WebClient client = new WebClient();
                string    data   = client.DownloadString(url);
                Regex     regex1 = new Regex("polyLine1(.*?)#ff0000");

                string data1 = regex1.Match(data).Groups[1].Value;

                Regex coordRegex = new Regex("new GLatLng\\((.*?), (.*?)\\)");
                foreach (Match match in coordRegex.Matches(data1))
                {
                    RoutePiece piece = new RoutePiece();
                    piece.Lat = double.Parse(match.Groups[1].Value);
                    piece.Lng = double.Parse(match.Groups[2].Value);
                    route.RoutePieces.Add(piece);
                }

                Regex      regex2     = new Regex("polyLine2(.*?)#0000ff");
                RoutePiece firstPiece = null;
                if (regex2.IsMatch(data))
                {
                    string data2 = regex2.Match(data).Groups[1].Value;
                    foreach (Match match in coordRegex.Matches(data2))
                    {
                        RoutePiece piece = new RoutePiece();
                        piece.Lat = double.Parse(match.Groups[1].Value);
                        piece.Lng = double.Parse(match.Groups[2].Value);
                        route.RoutePieces.Add(piece);

                        if (firstPiece == null)
                        {
                            firstPiece = piece;
                        }
                    }
                }


                context.Routes.InsertOnSubmit(route);
                context.SubmitChanges();

                route.SplitRoutePieceID = (firstPiece == null) ? null : (int?)firstPiece.ID;
                context.SubmitChanges();
            }
        }
Beispiel #4
0
 private dynamic MapRoutePiece(RoutePiece piece)
 {
     return(new
     {
         lat = piece.Lat,
         lng = piece.Lng,
         name = piece.Name,
         typeLink = "/directorio/" + piece.Route.Type.SeoName,
         typeName = piece.Route.Type.Name,
         routeLink = "/directorio/" + piece.Route.Type.SeoName + "/" + piece.Route.SeoName,
         routeName = piece.Route.Name,
         title = piece.Route.Type.Name + " " + piece.Name + " (" + piece.Route.Name + ")",
         connectingRoutes = DataHandler.GetConnectionRoutes(piece, 0.5).Select(r => MapRouteForList(r)).ToList(),
         closeBusinesses = DataHandler.GetCloseBusinesses(piece, 0.25, 50).Select(b => MapBusinessForList(b)).ToList()
     });
 }
Beispiel #5
0
 private dynamic MapRoutePieceForList(RoutePiece piece)
 {
     return(new { lat = piece.Lat, lng = piece.Lng });
 }
Beispiel #6
0
        public ActionResult CrearRuta(CreateRouteModel model)
        {
            bool valid = !string.IsNullOrWhiteSpace(model.routename);

            valid &= !string.IsNullOrWhiteSpace(model.origin);
            valid &= !string.IsNullOrWhiteSpace(model.destination);
            valid &= model.type > 0;
            valid &= !string.IsNullOrWhiteSpace(model.mapdata1);
            valid &= !string.IsNullOrWhiteSpace(model.email);

            if (valid)
            {
                bool routeWasActiveBefore = false;

                // before saving deactivate this route and delete routepieces
                if (model.id.HasValue && Request.IsAuthenticated)
                {
                    DataHandler.DeleteRoutePiecesAndDeactivate(model.id.Value);
                }

                using (DataContext context = new DataContext())
                {
                    Route route;
                    if (model.id.HasValue && Request.IsAuthenticated)
                    {
                        route = context.Routes.FirstOrDefault(x => x.ID == model.id.Value);
                    }
                    else
                    {
                        route = new Route();
                        route.ParentRouteID = model.id;
                        context.Routes.InsertOnSubmit(route);
                    }
                    route.FromName = model.origin;
                    route.ToName   = model.destination;
                    route.Name     = model.routename;

                    if (route.TypeID == (int)TypeEnum.Microbus)
                    {
                        route.SeoName = Utils.FormatSEO(route.FromName + " a " + route.ToName);
                    }
                    else
                    {
                        route.SeoName = Utils.FormatSEO(route.Name);
                    }

                    route.TypeID      = model.type;
                    route.Email       = model.email;
                    route.Description = model.description;
                    route.Status      = (int)StatusEnum.New;
                    context.SubmitChanges();

                    model.id = route.ID;

                    List <RoutePiece> pieces = new List <RoutePiece>();
                    foreach (var coords in model.Positions1)
                    {
                        var latLng     = coords.Split(',');
                        var name       = model.GetMapNameAtPosition(coords);
                        var routePiece = new RoutePiece {
                            Lat = double.Parse(latLng[0]), Lng = double.Parse(latLng[1]), RouteID = route.ID, Name = name, SeoName = Utils.FormatSEO(name)
                        };
                        pieces.Add(routePiece);
                    }
                    context.RoutePieces.InsertAllOnSubmit(pieces);
                    context.SubmitChanges();

                    if (!string.IsNullOrWhiteSpace(model.mapdata2))
                    {
                        List <RoutePiece> pieces2 = new List <RoutePiece>();
                        foreach (var coords in model.Positions2)
                        {
                            var latLng     = coords.Split(',');
                            var name       = model.GetMapNameAtPosition(coords);
                            var routePiece = new RoutePiece {
                                Lat = double.Parse(latLng[0]), Lng = double.Parse(latLng[1]), RouteID = route.ID, Name = name, SeoName = Utils.FormatSEO(name)
                            };
                            pieces2.Add(routePiece);
                        }
                        context.RoutePieces.InsertAllOnSubmit(pieces2);
                        context.SubmitChanges();

                        route.SplitRoutePieceID = pieces2.First().ID;
                        context.SubmitChanges();
                    }
                }

                if (Request.IsAuthenticated)
                {
                    if (routeWasActiveBefore)
                    {
                        DataHandler.ActivateRoute(model.id.Value);
                    }
                    return(RedirectToAction("Routes"));
                }
                else
                {
                    return(RedirectToAction("RutaRegistrada", "Home"));
                }
            }

            model.Valid    = valid;
            model.TypeList = DataHandler.GetEditableTypes();
            return(View(model));
        }
Beispiel #7
0
        public static List <Route> AddRouteToIndex(Route route, List <Route> allIndexedRoutes = null)
        {
            if (route.RoutePieces.Count == 0)
            {
                return(allIndexedRoutes);
            }

            // create all connections inside route
            List <SearchIndex> indexList = new List <SearchIndex>();

            bool bidirectional = !route.SplitRoutePieceID.HasValue;

            RoutePiece previous = null;
            RoutePiece first    = null;

            foreach (var actual in route.RoutePieces)
            {
                if (first == null)
                {
                    first = actual;
                }
                if (previous != null)
                {
                    double cost = CalculateTransportTime(previous.Lat, previous.Lng, actual.Lat, actual.Lng, route, route.Type);
                    indexList.Add(new SearchIndex {
                        RoutePieceID = previous.ID, RoutePiece2ID = actual.ID, Cost = cost
                    });
                    if (bidirectional)
                    {
                        indexList.Add(new SearchIndex {
                            RoutePieceID = actual.ID, RoutePiece2ID = previous.ID, Cost = cost
                        });
                    }
                }
                previous = actual;
            }
            if (!bidirectional && previous != null && first != null)
            {
                indexList.Add(new SearchIndex {
                    RoutePieceID = previous.ID, RoutePiece2ID = first.ID, Cost = CalculateTransportTime(previous.Lat, previous.Lng, first.Lat, first.Lng, route, route.Type)
                });
            }


            var nodes = route.RoutePieces.ToList();

            foreach (var route2 in allIndexedRoutes)
            {
                if (route2.ID == route.ID)
                {
                    continue;
                }

                var nodes2 = route2.RoutePieces.ToList();

                if (nodes2.Count > 0)
                {
                    var bb = new BoundingBox(nodes2.Min(x => x.Lat) - ViaDFGraph.HALF_KM_IN_DEGREES, nodes2.Max(x => x.Lat) + ViaDFGraph.HALF_KM_IN_DEGREES, nodes2.Min(x => x.Lng) - ViaDFGraph.HALF_KM_IN_DEGREES, nodes2.Max(x => x.Lng) + ViaDFGraph.HALF_KM_IN_DEGREES);

                    bool intersectAtAll = nodes.Exists(x => bb.Contains(x.Lat, x.Lng));

                    bool       outsideBefore   = true;
                    bool       outsideAfter    = true;
                    bool       changeLastPiece = false;
                    RoutePiece lastNode        = null;
                    RoutePiece lastNode2       = null;

                    if (intersectAtAll)
                    {
                        foreach (var node in nodes)
                        {
                            RoutePiece node2 = null;
                            if (bb.Contains(node.Lat, node.Lng))
                            {
                                node2 = nodes2.Aggregate((curmin, x) => (curmin == null || Math.Abs(x.Lat - node.Lat) + Math.Abs(x.Lng - node.Lng) < Math.Abs(curmin.Lat - node.Lat) + Math.Abs(curmin.Lng - node.Lng) ? x : curmin));
                                double minDistance = Math.Abs(node2.Lat - node.Lat) + Math.Abs(node2.Lng - node.Lng);
                                outsideAfter = (minDistance > ViaDFGraph.HALF_KM_IN_DEGREES);
                            }
                            else
                            {
                                outsideAfter = true;
                            }

                            if (outsideAfter != outsideBefore)
                            {
                                if (outsideAfter)
                                {
                                    if (!changeLastPiece)
                                    {
                                        double cost = CalculateChangeTransportTime(lastNode.Lat, lastNode.Lng, lastNode2.Lat, lastNode2.Lng, route2, route2.Type);
                                        indexList.Add(new SearchIndex {
                                            RoutePieceID = lastNode.ID, RoutePiece2ID = lastNode2.ID, Cost = cost
                                        });
                                        cost = CalculateChangeTransportTime(lastNode2.Lat, lastNode2.Lng, lastNode.Lat, lastNode.Lng, route, route.Type);
                                        indexList.Add(new SearchIndex {
                                            RoutePieceID = lastNode2.ID, RoutePiece2ID = lastNode.ID, Cost = cost
                                        });
                                    }
                                }
                                else
                                {
                                    double cost = CalculateChangeTransportTime(node.Lat, node.Lng, node2.Lat, node2.Lng, route2, route2.Type);
                                    indexList.Add(new SearchIndex {
                                        RoutePieceID = node.ID, RoutePiece2ID = node2.ID, Cost = cost
                                    });
                                    cost = CalculateChangeTransportTime(node2.Lat, node2.Lng, node.Lat, node.Lng, route, route.Type);
                                    indexList.Add(new SearchIndex {
                                        RoutePieceID = node2.ID, RoutePiece2ID = node.ID, Cost = cost
                                    });
                                }
                                changeLastPiece = true;
                            }
                            else
                            {
                                // handle special case when its a two-way way route & last element
                                if (!outsideAfter && !route.SplitRoutePieceID.HasValue && node == nodes.Last())
                                {
                                    double cost = CalculateChangeTransportTime(node.Lat, node.Lng, node2.Lat, node2.Lng, route2, route2.Type);
                                    indexList.Add(new SearchIndex {
                                        RoutePieceID = node.ID, RoutePiece2ID = node2.ID, Cost = cost
                                    });
                                    cost = CalculateChangeTransportTime(node2.Lat, node2.Lng, node.Lat, node.Lng, route, route.Type);
                                    indexList.Add(new SearchIndex {
                                        RoutePieceID = node2.ID, RoutePiece2ID = node.ID, Cost = cost
                                    });
                                }
                                else
                                {
                                    changeLastPiece = false;
                                }
                            }
                            outsideBefore = outsideAfter;
                            lastNode      = node;
                            lastNode2     = node2;
                        }
                    }
                }
            }

            using (DataContext context = new DataContext())
            {
                // save index
                context.SearchIndexes.InsertAllOnSubmit(indexList);
                context.SubmitChanges();
            }

            return(allIndexedRoutes);
        }
Beispiel #8
0
 public static List<Route> GetConnectionRoutes(RoutePiece piece, double maxDistanceKm)
 {
     return GetConnectionRoutes(piece.Lat, piece.Lng, maxDistanceKm, piece.RouteID);
 }
Beispiel #9
0
 public static List<Business> GetCloseBusinesses(RoutePiece piece, double maxDistanceKm, int max)
 {
     return GetCloseBusinesses(piece.Lat, piece.Lng, maxDistanceKm, max);
 }