geo fences - wip

This commit is contained in:
Anton Martynenko
2025-11-19 17:26:23 +01:00
parent 5974b0c589
commit d122497b50
22 changed files with 766 additions and 114 deletions
+72 -4
View File
@@ -41,8 +41,12 @@ public class RouteService : IRouteService
throw new ArgumentException("Route name is required");
}
_logger.LogInformation("Creating route {RouteId} with {PointCount} original points",
request.Id, request.Points.Count);
_logger.LogInformation("Creating route {RouteId} with {PointCount} original points and {GeofenceCount} geofences",
request.Id, request.Points.Count, request.Geofences?.Polygons?.Count ?? 0);
_logger.LogInformation("Route {RouteId} - Input coordinates: First point ({Lat}, {Lon}), Last point ({LastLat}, {LastLon})",
request.Id, request.Points[0].Latitude, request.Points[0].Longitude,
request.Points[^1].Latitude, request.Points[^1].Longitude);
var allPoints = new List<RoutePointDto>();
var totalDistance = 0.0;
@@ -67,7 +71,7 @@ public class RouteService : IRouteService
var pointType = isStart ? "start" : (isEnd ? "end" : "action");
allPoints.Add(new RoutePointDto
var routePointDto = new RoutePointDto
{
Latitude = currentPoint.Latitude,
Longitude = currentPoint.Longitude,
@@ -75,7 +79,15 @@ public class RouteService : IRouteService
SequenceNumber = sequenceNumber++,
SegmentIndex = segmentIndex,
DistanceFromPrevious = distanceFromPrevious
});
};
if (segmentIndex == 0 || segmentIndex == request.Points.Count - 1)
{
_logger.LogInformation("Route {RouteId} - Creating {PointType} point: Lat={Lat:F12}, Lon={Lon:F12}",
request.Id, pointType, routePointDto.Latitude, routePointDto.Longitude);
}
allPoints.Add(routePointDto);
if (!isEnd)
{
@@ -143,8 +155,64 @@ public class RouteService : IRouteService
CreatedAt = now
}).ToList();
_logger.LogInformation("Route {RouteId} - Saving {Count} route points to DB. First: Lat={Lat:F12}, Lon={Lon:F12}, Last: Lat={LastLat:F12}, Lon={LastLon:F12}",
request.Id, pointEntities.Count, pointEntities[0].Latitude, pointEntities[0].Longitude,
pointEntities[^1].Latitude, pointEntities[^1].Longitude);
await _routeRepository.InsertRoutePointsAsync(pointEntities);
if (request.Geofences?.Polygons != null && request.Geofences.Polygons.Count > 0)
{
_logger.LogInformation("Route {RouteId}: Processing {GeofenceCount} geofence polygons",
request.Id, request.Geofences.Polygons.Count);
foreach (var polygon in request.Geofences.Polygons)
{
if (polygon.NorthWest is null || polygon.SouthEast is null)
{
throw new ArgumentException("Geofence polygon coordinates are required");
}
if ((Math.Abs(polygon.NorthWest.Lat) < 0.0001 && Math.Abs(polygon.NorthWest.Lon) < 0.0001) ||
(Math.Abs(polygon.SouthEast.Lat) < 0.0001 && Math.Abs(polygon.SouthEast.Lon) < 0.0001))
{
throw new ArgumentException("Geofence polygon coordinates cannot be (0,0)");
}
if (polygon.NorthWest.Lat < -90 || polygon.NorthWest.Lat > 90 ||
polygon.SouthEast.Lat < -90 || polygon.SouthEast.Lat > 90 ||
polygon.NorthWest.Lon < -180 || polygon.NorthWest.Lon > 180 ||
polygon.SouthEast.Lon < -180 || polygon.SouthEast.Lon > 180)
{
throw new ArgumentException("Geofence polygon coordinates must be valid (lat: -90 to 90, lon: -180 to 180)");
}
if (polygon.NorthWest.Lat <= polygon.SouthEast.Lat)
{
throw new ArgumentException("Geofence northWest latitude must be greater than southEast latitude");
}
var center = GeoUtils.CalculateCenter(polygon.NorthWest, polygon.SouthEast);
var diagonalDistance = GeoUtils.CalculatePolygonDiagonalDistance(polygon.NorthWest, polygon.SouthEast);
var geofenceRegionSize = Math.Max(diagonalDistance * 0.6, request.RegionSizeMeters);
var geofenceRegionId = Guid.NewGuid();
_logger.LogInformation("Route {RouteId}: Requesting geofence region {RegionId} at center ({Lat}, {Lon}) with size {Size}m",
request.Id, geofenceRegionId, center.Lat, center.Lon, geofenceRegionSize);
await _regionService.RequestRegionAsync(
geofenceRegionId,
center.Lat,
center.Lon,
geofenceRegionSize,
request.ZoomLevel,
stitchTiles: false);
await _routeRepository.LinkRouteToRegionAsync(request.Id, geofenceRegionId, isGeofence: true);
}
}
if (request.RequestMaps)
{
_logger.LogInformation("Route {RouteId}: Maps requested. Regions will be processed sequentially by background service.",