Exemplo n.º 1
0
        public async Task <ActionResult> CalculateDistanceForAll()
        {
            var adminClaim = (User.Claims.SingleOrDefault(c => c.Type == "admin").Value ?? "false");

            if (string.Equals(adminClaim, "false", StringComparison.OrdinalIgnoreCase))
            {
                return(Forbid());
            }

            var routes = await _dbContext.Routes.ToListAsync();

            routes.ForEach(route =>
            {
                try
                {
                    DistanceCalculationHelper.ComputeDistance(route);
                } catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            });
            await _dbContext.SaveChangesAsync();

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task <ActionResult> CalculateDistanceById(int id)
        {
            var adminClaim = (User.Claims.SingleOrDefault(c => c.Type == "admin").Value ?? "false");

            if (string.Equals(adminClaim, "false", StringComparison.OrdinalIgnoreCase))
            {
                return(Forbid());
            }

            var route = await _dbContext.Routes.FindAsync(id);

            if (route != null)
            {
                DistanceCalculationHelper.ComputeDistance(route);
            }
            await _dbContext.SaveChangesAsync();

            return(Ok());
        }
Exemplo n.º 3
0
        public async Task <ActionResult> CalculateDistanceForAllMissing()
        {
            var adminClaim = (User.Claims.SingleOrDefault(c => c.Type == "admin").Value ?? "false");

            if (string.Equals(adminClaim, "false", StringComparison.OrdinalIgnoreCase))
            {
                return(Forbid());
            }

            var routes = await _dbContext.Routes.Where(r => r.CalculatedDistance == 0).ToListAsync();

            routes.ForEach(route =>
            {
                DistanceCalculationHelper.ComputeDistance(route);
            });
            await _dbContext.SaveChangesAsync();

            return(Ok());
        }
Exemplo n.º 4
0
        private async Task <List <Route> > PostGpxToDatabase(StreamReader stream, int mapId, string fileName, int userId)
        {
            var gpxReader = new Gpx.GpxReader(stream.BaseStream);

            do
            {
                var read = gpxReader.Read();
                if (!read)
                {
                    throw new Exception("Cannot read file");
                }
            } while (gpxReader.ObjectType != Gpx.GpxObjectType.Track);

            var name = gpxReader.Track.Name;

            if (string.Equals(name, "Tessellated", StringComparison.OrdinalIgnoreCase))
            {
                name = fileName;
                if (name.EndsWith(".gpx"))
                {
                    name = name.Substring(0, name.Length - 4);
                }
            }
            var metadata    = gpxReader.Metadata;
            var coordinates = gpxReader.Track.ToGpxPoints().Select(p => p.Longitude.ToString(CultureInfo.InvariantCulture) + "," + p.Latitude.ToString(CultureInfo.InvariantCulture)).ToList();
            var route       = new Route
            {
                Coordinates = string.Join('\n', coordinates),
                Name        = name,
                Share       = Guid.NewGuid(),
                RouteMaps   = new List <RouteMap>
                {
                    new RouteMap
                    {
                        MapId = mapId
                    }
                }
            };

            if (!string.IsNullOrWhiteSpace(gpxReader.Track.Description))
            {
                route.Description = gpxReader.Track.Description;
            }


            var maps = await _context.Maps.Where(m => m.UserId == userId).ToListAsync();

            var defaultMap = maps.Where(m => m.Default == true).SingleOrDefault();

            if (defaultMap == null)
            {
                defaultMap = maps.First();
            }
            DistanceCalculationHelper.ComputeDistance(route);


            _context.Add(route);

            await _context.SaveChangesAsync();

            return(new List <Route> {
                route
            });
        }
Exemplo n.º 5
0
        private async Task <List <Route> > PostKmlToDatabase(StreamReader stream, string fileName, int userId)
        {
            var body = stream.ReadToEnd();

            var parser = new Parser();

            parser.ParseString(body, false);
            var kml        = parser.Root as Kml;
            var placeMarks = kml.Flatten().Where(e => e.GetType() == typeof(Placemark)).Select(p => (Placemark)p).ToList();
            var routes     = new List <Route>();

            foreach (Placemark placeMark in placeMarks.Where(pl => pl.Geometry is LineString))
            {
                var child       = (LineString)placeMark.Flatten().Single(e => e.GetType() == typeof(LineString));
                var coordinates = child.Coordinates.Select(c => "" + c.Longitude.ToString(CultureInfo.InvariantCulture) + "," + c.Latitude.ToString(CultureInfo.InvariantCulture)).ToList();

                var name = placeMark.Name;
                if (string.Equals(name, "Tessellated", StringComparison.OrdinalIgnoreCase))
                {
                    name = fileName;
                    if (name.EndsWith(".kml"))
                    {
                        name = name.Substring(0, name.Length - 4);
                    }
                }
                var route = new Route
                {
                    Coordinates    = string.Join('\n', coordinates),
                    Name           = name,
                    RouteMaps      = new List <RouteMap>(),
                    Share          = Guid.NewGuid(),
                    RouteInstances = new List <RouteInstance>()
                };
                if (placeMark.Description != null)
                {
                    route.Description = placeMark.Description.Text;
                }

                var types = await _context.RouteTypes.Where(r => r.UserId == userId).ToListAsync();

                if (placeMark.ExtendedData != null)
                {
                    var extendedData = placeMark.ExtendedData.Data;
                    if (extendedData.Any(d => d.Name == "firstDateTime"))
                    {
                        route.RouteInstances.Add(new RouteInstance {
                            Date = DateTime.ParseExact(extendedData.Single(d => d.Name == "firstDateTime").Value, "yyyy-MM-dd", CultureInfo.InvariantCulture)
                        });
                    }

                    if (extendedData.Any(d => d.Name == "type"))
                    {
                        var typeString = extendedData.Single(d => d.Name == "type").Value;
                        var type       = types.SingleOrDefault(t => string.Compare(t.Name, typeString.Trim(), true) == 0 || string.Compare(t.NameNL, typeString.Trim(), true) == 0);
                        if (type != null)
                        {
                            route.RouteTypeId = type.TypeId;
                        }
                        else
                        {
                            var newRouteType = new RouteType
                            {
                                Colour = "#ff0000",
                                UserId = userId,
                                Name   = typeString.Trim()
                            };
                            route.RouteType = newRouteType;
                        }
                    }
                    if (extendedData.Any(d => d.Name == "line"))
                    {
                        route.LineNumber = extendedData.Single(d => d.Name == "line").Value;
                    }
                    if (extendedData.Any(d => d.Name == "nameNL"))
                    {
                        route.NameNL = extendedData.Single(d => d.Name == "nameNL").Value;
                    }
                    if (extendedData.Any(d => d.Name == "descriptionNL"))
                    {
                        route.DescriptionNL = extendedData.Single(d => d.Name == "descriptionNL").Value;
                    }
                    if (extendedData.Any(d => d.Name == "color"))
                    {
                        route.OverrideColour = extendedData.Single(d => d.Name == "color").Value;
                    }
                    if (extendedData.Any(d => d.Name == "company"))
                    {
                        route.OperatingCompany = extendedData.Single(d => d.Name == "company").Value;
                    }
                    if (extendedData.Any(d => d.Name == "from"))
                    {
                        route.From = extendedData.Single(d => d.Name == "from").Value;
                    }
                    if (extendedData.Any(d => d.Name == "to"))
                    {
                        route.To = extendedData.Single(d => d.Name == "to").Value;
                    }
                    if (extendedData.Any(d => d.Name == "countries"))
                    {
                        route.RouteCountries = new List <RouteCountry>();
                        var dbCountries = await _context.Countries.Where(r => r.UserId == userId).ToListAsync();

                        var countries = extendedData.Single(d => d.Name == "countries").Value.Split(',').ToList();
                        countries.ForEach(inputCountry =>
                        {
                            var county = dbCountries.SingleOrDefault(c => string.Compare(c.Name, inputCountry.Trim(), true) == 0 || string.Compare(c.NameNL, inputCountry.Trim(), true) == 0);
                            if (county != null)
                            {
                                route.RouteCountries.Add(new RouteCountry {
                                    CountryId = county.CountryId
                                });
                            }
                            else
                            {
                                var newCountry = new Country
                                {
                                    Name   = inputCountry.Trim(),
                                    UserId = userId
                                };
                                route.RouteCountries.Add(new RouteCountry {
                                    Country = newCountry
                                });
                            }
                        });
                    }

                    if (extendedData.Any(d => d.Name == "maps"))
                    {
                        route.RouteMaps = new List <RouteMap>();
                        var dbMaps = await _context.Maps.Where(r => r.UserId == userId).ToListAsync();

                        var inputMaps = extendedData.Single(d => d.Name == "maps").Value.Split(',').ToList();
                        inputMaps.ForEach(inputMap =>
                        {
                            var map = dbMaps.SingleOrDefault(c => string.Compare(c.Name, inputMap.Trim(), true) == 0);
                            if (map != null)
                            {
                                route.RouteMaps.Add(new RouteMap {
                                    MapId = map.MapId
                                });
                            }
                            else
                            {
                                var newMap = new Map
                                {
                                    Name    = inputMap.Trim(),
                                    MapGuid = Guid.NewGuid(),
                                    UserId  = userId,
                                    Default = false
                                };
                                route.RouteMaps.Add(new RouteMap {
                                    Map = newMap
                                });
                            }
                        });
                    }
                }
                if (!route.RouteMaps.Any())
                {
                    var maps = await _context.Maps.Where(m => m.UserId == userId).ToListAsync();

                    var defaultMap = maps.Where(m => m.Default == true).FirstOrDefault();
                    if (defaultMap == null)
                    {
                        defaultMap = maps.First();
                    }

                    route.RouteMaps.Add(new RouteMap {
                        MapId = defaultMap.MapId
                    });
                }

                DistanceCalculationHelper.ComputeDistance(route);

                routes.Add(route);
                _context.Routes.Add(route);
                await _context.SaveChangesAsync();
            }
            return(routes);
        }
Exemplo n.º 6
0
        public async Task <ActionResult <Route> > ImportRouteToDatabaseAsync([FromBody] OSMLineDTO line)
        {
            var userIdClaim = int.Parse(User.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value ?? "-1");

            if (userIdClaim < 0)
            {
                return(Forbid());
            }

            var geojson     = line.GeoJson;
            var coordinates =
                ((LineString)geojson.Features.Single().Geometry).Coordinates
                .Select(c => c.Longitude.ToString(CultureInfo.InvariantCulture) + ", " + c.Latitude.ToString(CultureInfo.InvariantCulture))
                .ToList();


            var route = new Route
            {
                Coordinates = string.Join('\n', coordinates),
                Name        = line.Name,
                Share       = Guid.NewGuid(),
                RouteMaps   = new List <RouteMap>()
            };

            if (route.Name == null)
            {
                route.Name = "Name";
            }

            if (!string.IsNullOrWhiteSpace(line.Description))
            {
                route.Description = line.Description;
            }
            if (!string.IsNullOrWhiteSpace(line.Colour))
            {
                route.OverrideColour = line.Colour;
            }
            if (!string.IsNullOrWhiteSpace(line.Operator))
            {
                route.OperatingCompany = line.Operator;
            }
            if (!string.IsNullOrWhiteSpace(line.Ref))
            {
                route.LineNumber = line.Ref;
            }
            if (!string.IsNullOrWhiteSpace(line.From))
            {
                route.From = line.From;
            }
            if (!string.IsNullOrWhiteSpace(line.To))
            {
                route.To = line.To;
            }
            var maps = await _context.Maps.Where(m => m.UserId == userIdClaim).ToListAsync();

            var defaultMap = maps.Where(m => m.Default == true).FirstOrDefault();

            if (defaultMap == null)
            {
                defaultMap = maps.First();
            }

            route.RouteMaps.Add(new RouteMap {
                MapId = defaultMap.MapId
            });
            DistanceCalculationHelper.ComputeDistance(route);

            _context.Routes.Add(route);
            await _context.SaveChangesAsync();

            return(Ok(route));
        }