protected override void ProcessAvailablePoints(List <LocalizationPointDto> availablePoints, double currentAdditionalDistance, double currentAdditionalTime, int?stepSize = null) { while (this.doesRouteMeetParameters) { availablePoints = this.GetPointsDynamicScoreUsingBuffer(availablePoints, currentAdditionalDistance, currentAdditionalTime, countScore: this.dynamicScoreNeedsToBeRecalculated, stepSize); if (!availablePoints.Any()) { break; } LocalizationPointDto biggestScorePoint = availablePoints.Aggregate((i1, i2) => i1.DynamicScore > i2.DynamicScore ? i1 : i2); availablePoints.Remove(biggestScorePoint); if (biggestScorePoint.StaticScore == 0) { this.AddSimplePoint(biggestScorePoint, ref currentAdditionalDistance, ref currentAdditionalTime); } else { this.AddAggregatedPoint(biggestScorePoint, ref currentAdditionalDistance, ref currentAdditionalTime); // TODO: jesli punkt zaagregowany jest rozbity na pomniejsze punkty to pozwala na dalsze przetwarzanie zwyklych punktow // ale za to jest szansa ze wejdziemy w petle nieskoczona - gdyby zostaly tylko punkty zaagregowane // dlatego moze to usunac // availablePoints.Any() chyba chroni przed petla nieskonczona this.doesRouteMeetParameters = true; } } }
private static void InsertDataFromFileToDb(string fileName) { string[] lines = File.ReadAllLines(fileName); var repo = new LocalizationPointRepository(); foreach (string line in lines) { string[] splitted = line.Split(",", StringSplitOptions.None); if (splitted.Count() >= 9 && double.TryParse(splitted[0], NumberStyles.Number, CultureInfo.InvariantCulture, out double longitude) && double.TryParse(splitted[1], NumberStyles.Number, CultureInfo.InvariantCulture, out double latitude)) { var localizationPoint = new LocalizationPointDto() { Point = new Point(new Position(latitude, longitude)), Number = splitted[2], Street = splitted[3], City = splitted[5], District = splitted[6], Region = splitted[7], PostalCode = splitted[8], StaticScore = 0, InnerDistance = 0, InnerTime = 0 }; repo.Add(localizationPoint); } } repo.SaveChanges(); }
public LocalizationPointDto Add(LocalizationPointDto entity) { var entityToAdd = new LocalizationPoint(entity); this.dataSet.Add(entityToAdd); this.SaveChanges(); return(new LocalizationPointDto(entityToAdd)); }
private static LocalizationPointDto GetAggregatedPoint(List <long?> pointIds, List <DbModel.LocalizationPointDto> points, double innerDistance, double innerTime) { var localizationPoint = new LocalizationPointDto() { Point = new GeoJSON.Net.Geometry.Point(GetPositionForAggregatedPoint(pointIds, points)), StaticScore = pointIds.Count(), InnerDistance = innerDistance, InnerTime = innerTime }; return(localizationPoint); }
public bool Update(LocalizationPointDto entityDto) { LocalizationPoint dbPoint = this.dataSet.FirstOrDefault(i => i.PointId == entityDto.PointId); if (dbPoint != null) { dbPoint.GetDataFromLocalizationPointDto(entityDto); this.context.SaveChanges(); return(true); } return(false); }
public MockLocalizationPointRepository() { //pajeczno: 51.144550, 18.999704 // sulmierzyce = 51.185689, 19.194213 // marchewki 51.186707, 18.937751 // wielun 51.220383, 18.564313 // lask 51.589554, 19.136186 // kamien 51.261527, 19.203991 LocalizationPointDto pajeczno = new LocalizationPointDto() { PointId = 1, Point = new Point(new Position(51.144550, 18.999704)), StaticScore = 0 }; LocalizationPointDto sulmierzyce = new LocalizationPointDto() { PointId = 1, Point = new Point(new Position(51.185689, 19.194213)), StaticScore = 0 }; LocalizationPointDto marchewki = new LocalizationPointDto() { PointId = 1, Point = new Point(new Position(51.186707, 18.937751)), StaticScore = 0 }; LocalizationPointDto wielun = new LocalizationPointDto() { PointId = 1, Point = new Point(new Position(51.220383, 18.564313)), StaticScore = 0 }; LocalizationPointDto lask = new LocalizationPointDto() { PointId = 1, Point = new Point(new Position(51.589554, 19.136186)), StaticScore = 0 }; LocalizationPointDto kamien = new LocalizationPointDto() { PointId = 1, Point = new Point(new Position(51.261527, 19.203991)), StaticScore = 0 }; points.Add(pajeczno); points.Add(sulmierzyce); points.Add(marchewki); points.Add(wielun); points.Add(lask); points.Add(kamien); }
private static void InsertAggregatedPointsToDb() { var repo = new LocalizationPointRepository(); List <LocalizationPointDto> points = repo.GetWithoutAggregated(); var mappedPointsIds = new HashSet <long>(); foreach (LocalizationPointDto dbPoint in points) { if (dbPoint.ParentPointId != null || dbPoint.StaticScore != 0 || mappedPointsIds.Contains(dbPoint.PointId.Value)) { continue; } Polygon isochrone = MapboxAPIHelper.GetIsochroneAsPolygon((Position)dbPoint.Point.Coordinates, 15); SqlGeography isochroneSqlGeography = isochrone.ToSqlGeography().MakeValid(); isochroneSqlGeography = GetCorrectlyOrientedGeography(isochroneSqlGeography); // take points that are not parent or child points in aggregation var pointsWithoutParentPoint = points.Where(i => i.ParentPointId == null && i.StaticScore == 0 && !mappedPointsIds.Contains(i.PointId.Value)).ToList(); var pointsInsideIsochrone = pointsWithoutParentPoint.Where(i => (bool)isochroneSqlGeography.STContains(i.Point.ToSqlGeography())).ToList(); if (pointsInsideIsochrone.Count > 1) { Position[] positionsInsideIsochrone = pointsInsideIsochrone.Where(x => x.PointId != dbPoint.PointId).Select(x => (Position)x.Point.Coordinates).ToArray(); TravelTimesMatrixModel travelTimesMatrixModel = OsrmAPIHelper.GetTravelTimesMatrix((Position)dbPoint.Point.Coordinates, positionsInsideIsochrone); var durationsList = travelTimesMatrixModel.durations != null ? travelTimesMatrixModel.durations[0].ToList() : new List <float>(); var durationsListSortedWithIndexes = durationsList.Select((x, index) => new KeyValuePair <int, float>(index, x)).OrderBy(x => x.Value).ToList(); // get route that innerdistance and time is no longer than 30min and x? km double maxInnerDistance = 30 * 1000; double maxInnerTime = 15 * 60; List <long?> pointIds = GetRouteMeetingConditionsAndPointIds(durationsListSortedWithIndexes.Select(x => x.Key).ToList(), pointsInsideIsochrone, maxInnerDistance, maxInnerTime, out RouteModel resultRoute); if (pointIds != null) { pointsInsideIsochrone.ForEach(i => mappedPointsIds.Add(i.PointId.Value)); // Create aggregated point LocalizationPointDto aggregatedPoint = GetAggregatedPoint(pointIds, pointsWithoutParentPoint, resultRoute.Distance, resultRoute.Time); LocalizationPointDto addedPoint = repo.Add(aggregatedPoint); // Update parentId for child points List <LocalizationPointDto> updatedPoints = GetUpdatedChildPointsWithParentId(pointIds, points, addedPoint.PointId); foreach (LocalizationPointDto point in updatedPoints) { repo.Update(point); } } } } }
private void AddSimplePoint(LocalizationPointDto biggestScorePoint, ref double currentAdditionalDistance, ref double currentAdditionalTime) { this.waypoints.Add((Position)biggestScorePoint.Point.Coordinates); RouteModel newRoute = this.GetRouteBetweenTwoPoints(this.waypoints); this.doesRouteMeetParameters = this.DoesRouteMeetParameters(newRoute, this.totalAdditionalDistance, this.totalAdditionalTime); if (this.doesRouteMeetParameters) { this.dynamicScoreNeedsToBeRecalculated = this.DoesDynamicScoreNeedToBeRecalculated(this.lastDynamicScoreCalculatedRoute, newRoute, (Position)biggestScorePoint.Point.Coordinates); this.UpdateResultRoute(newRoute, out currentAdditionalDistance, out currentAdditionalTime); } else { this.waypoints.RemoveAt(waypoints.Count() - 1); } }
private void AddAggregatedPoint(LocalizationPointDto biggestScorePoint, ref double currentAdditionalDistance, ref double currentAdditionalTime) { List <LocalizationPointDto> innerPoints = this.repo.GetByParentId(biggestScorePoint.PointId.Value); var tempWaypoints = this.waypoints.Select(i => i).ToList(); tempWaypoints.AddRange(innerPoints.Select(i => (Position)i.Point.Coordinates)); RouteModel newRoute = this.GetRouteBetweenTwoPoints(tempWaypoints); this.doesRouteMeetParameters = this.DoesRouteMeetParameters(newRoute, currentAdditionalDistance, currentAdditionalTime); if (this.doesRouteMeetParameters) { this.waypoints.Clear(); this.waypoints.AddRange(tempWaypoints); this.dynamicScoreNeedsToBeRecalculated = this.DoesDynamicScoreNeedToBeRecalculated(this.lastDynamicScoreCalculatedRoute, newRoute, (Position)biggestScorePoint.Point.Coordinates); this.UpdateResultRoute(newRoute, out currentAdditionalDistance, out currentAdditionalTime); } else { this.doesRouteMeetParameters = true; this.ProcessAvailablePoints(innerPoints, currentAdditionalDistance, currentAdditionalTime, (int)biggestScorePoint.InnerDistance / 10); } }
public MockLocalizationPointRepositoryMazowieckie() { //plock: 52.546796, 19.708701 //ciolkowo: 52.604040, 19.879803 //sierpc: 52.855707, 19.668891 //torun: 53.013152, 18.611620 //ciechanow: 52.868006, 20.625774 //przasnysz: 53.016507, 20.884518 //lomza: 53.168437, 22.064546 //dzialdowo: 53.231384, 20.167872 //naruszewo: 52.525421, 20.351985 //rzewin: 52.724651, 20.277417 LocalizationPointDto plock = new LocalizationPointDto() { PointId = 1, Point = new Point(new Position(52.546796, 19.708701)), StaticScore = 0 }; LocalizationPointDto ciolkowo = new LocalizationPointDto() { PointId = 2, Point = new Point(new Position(52.604040, 19.879803)), StaticScore = 0 }; LocalizationPointDto sierpc = new LocalizationPointDto() { PointId = 3, Point = new Point(new Position(52.855707, 19.668891)), StaticScore = 0 }; LocalizationPointDto torun = new LocalizationPointDto() { PointId = 4, Point = new Point(new Position(53.013152, 18.611620)), StaticScore = 0 }; LocalizationPointDto ciechanow = new LocalizationPointDto() { PointId = 5, Point = new Point(new Position(52.868006, 20.625774)), StaticScore = 0 }; LocalizationPointDto przasnysz = new LocalizationPointDto() { PointId = 6, Point = new Point(new Position(53.016507, 20.884518)), StaticScore = 0 }; LocalizationPointDto lomza = new LocalizationPointDto() { PointId = 7, Point = new Point(new Position(53.168437, 22.064546)), StaticScore = 0 }; LocalizationPointDto dzialdowo = new LocalizationPointDto() { PointId = 8, Point = new Point(new Position(53.229588, 20.167984)), StaticScore = 0 }; LocalizationPointDto naruszewo = new LocalizationPointDto() { PointId = 9, Point = new Point(new Position(52.525421, 20.351985)), StaticScore = 0 }; LocalizationPointDto rzewin = new LocalizationPointDto() { PointId = 10, Point = new Point(new Position(52.724651, 20.277417)), StaticScore = 0 }; LocalizationPointDto plock2 = new LocalizationPointDto() { PointId = 11, Point = new Point(new Position(52.542984, 19.687158)), StaticScore = 0 }; LocalizationPointDto plock3 = new LocalizationPointDto() { PointId = 12, Point = new Point(new Position(52.549311, 19.713392)), StaticScore = 0 }; points.Add(plock); points.Add(ciolkowo); points.Add(sierpc); points.Add(torun); points.Add(ciechanow); points.Add(przasnysz); points.Add(lomza); points.Add(dzialdowo); points.Add(naruszewo); points.Add(rzewin); points.Add(plock2); points.Add(plock3); }
public void Edit(LocalizationPointDto entity) { var entityToAdd = new LocalizationPoint(entity); this.context.Entry(entityToAdd).State = EntityState.Modified; }