fix warnings

This commit is contained in:
Anton Martynenko
2025-11-19 17:40:12 +01:00
parent d122497b50
commit a148df1697
6 changed files with 336 additions and 103 deletions
@@ -326,54 +326,49 @@ public class RouteProcessingService : BackgroundService
string? stitchedImagePath = null;
if (route.RequestMaps)
{
var geofenceTileBounds = new List<(Guid RegionId, int MinX, int MinY, int MaxX, int MaxY)>();
int? minX = null, minY = null, maxX = null, maxY = null;
foreach (var geofenceId in geofenceRegionIds)
if (geofenceRegionIds.Count > 0)
{
var region = await _regionRepository.GetByIdAsync(geofenceId);
if (region != null && !string.IsNullOrEmpty(region.CsvFilePath) && File.Exists(region.CsvFilePath))
foreach (var geofenceId in geofenceRegionIds)
{
_logger.LogInformation("Route {RouteId}: Loading geofence region {RegionId} tile bounds",
routeId, region.Id);
var csvLines = await File.ReadAllLinesAsync(region.CsvFilePath, cancellationToken);
int? minX = null, minY = null, maxX = null, maxY = null;
foreach (var line in csvLines.Skip(1))
var region = await _regionRepository.GetByIdAsync(geofenceId);
if (region != null && !string.IsNullOrEmpty(region.CsvFilePath) && File.Exists(region.CsvFilePath))
{
var parts = line.Split(',');
if (parts.Length >= 3)
var csvLines = await File.ReadAllLinesAsync(region.CsvFilePath, cancellationToken);
foreach (var line in csvLines.Skip(1))
{
if (double.TryParse(parts[0], out var lat) && double.TryParse(parts[1], out var lon))
var parts = line.Split(',');
if (parts.Length >= 3)
{
var tile = GeoUtils.WorldToTilePos(new Common.DTO.GeoPoint { Lat = lat, Lon = lon }, route.ZoomLevel);
minX = minX == null ? tile.x : Math.Min(minX.Value, tile.x);
minY = minY == null ? tile.y : Math.Min(minY.Value, tile.y);
maxX = maxX == null ? tile.x : Math.Max(maxX.Value, tile.x);
maxY = maxY == null ? tile.y : Math.Max(maxY.Value, tile.y);
if (double.TryParse(parts[0], out var lat) && double.TryParse(parts[1], out var lon))
{
var tile = GeoUtils.WorldToTilePos(new Common.DTO.GeoPoint { Lat = lat, Lon = lon }, route.ZoomLevel);
minX = minX == null ? tile.x : Math.Min(minX.Value, tile.x);
minY = minY == null ? tile.y : Math.Min(minY.Value, tile.y);
maxX = maxX == null ? tile.x : Math.Max(maxX.Value, tile.x);
maxY = maxY == null ? tile.y : Math.Max(maxY.Value, tile.y);
}
}
}
}
if (minX.HasValue && minY.HasValue && maxX.HasValue && maxY.HasValue)
{
geofenceTileBounds.Add((region.Id, minX.Value, minY.Value, maxX.Value, maxY.Value));
_logger.LogInformation("Route {RouteId}: Geofence {RegionId} tile bounds: X=[{MinX}..{MaxX}], Y=[{MinY}..{MaxY}]",
routeId, region.Id, minX.Value, maxX.Value, minY.Value, maxY.Value);
}
}
else
if (minX.HasValue && minY.HasValue && maxX.HasValue && maxY.HasValue)
{
_logger.LogWarning("Route {RouteId}: Geofence region {RegionId} CSV not found",
routeId, geofenceId);
_logger.LogInformation("Route {RouteId}: Combined geofence tile bounds: X=[{MinX}..{MaxX}], Y=[{MinY}..{MaxY}]",
routeId, minX.Value, maxX.Value, minY.Value, maxY.Value);
}
}
_logger.LogInformation("Route {RouteId}: Starting stitching with {GeofenceCount} geofence regions",
routeId, geofenceTileBounds.Count);
stitchedImagePath = Path.Combine(readyDir, $"route_{routeId}_stitched.jpg");
await StitchRouteTilesAsync(allTiles.Values.ToList(), stitchedImagePath, route.ZoomLevel, geofenceTileBounds, cancellationToken);
var geofenceBounds = (minX.HasValue && minY.HasValue && maxX.HasValue && maxY.HasValue)
? (minX.Value, minY.Value, maxX.Value, maxY.Value)
: ((int, int, int, int)?)null;
await StitchRouteTilesAsync(allTiles.Values.ToList(), stitchedImagePath, route.ZoomLevel, geofenceBounds, cancellationToken);
}
var summaryPath = Path.Combine(readyDir, $"route_{routeId}_summary.txt");
@@ -460,7 +455,7 @@ public class RouteProcessingService : BackgroundService
List<TileInfo> tiles,
string outputPath,
int zoomLevel,
List<(Guid RegionId, int MinX, int MinY, int MaxX, int MaxY)> geofenceTileBounds,
(int MinX, int MinY, int MaxX, int MaxY)? geofenceBounds,
CancellationToken cancellationToken)
{
if (tiles.Count == 0)
@@ -559,46 +554,42 @@ public class RouteProcessingService : BackgroundService
}
}
if (geofenceTileBounds.Count > 0)
if (geofenceBounds.HasValue)
{
_logger.LogInformation("Drawing {Count} geofence borders on image {Width}x{Height} (grid: minX={MinX}, minY={MinY})",
geofenceTileBounds.Count, imageWidth, imageHeight, minX, minY);
var (geoMinX, geoMinY, geoMaxX, geoMaxY) = geofenceBounds.Value;
foreach (var (regionId, geoMinX, geoMinY, geoMaxX, geoMaxY) in geofenceTileBounds)
_logger.LogInformation("Drawing geofence border on image {Width}x{Height} (grid: minX={MinX}, minY={MinY})",
imageWidth, imageHeight, minX, minY);
_logger.LogInformation("Geofence tile range - X=[{MinX}..{MaxX}], Y=[{MinY}..{MaxY}]",
geoMinX, geoMaxX, geoMinY, geoMaxY);
var x1 = (geoMinX - minX) * tileSizePixels;
var y1 = (geoMinY - minY) * tileSizePixels;
var x2 = (geoMaxX - minX + 1) * tileSizePixels - 1;
var y2 = (geoMaxY - minY + 1) * tileSizePixels - 1;
_logger.LogInformation("Geofence pixel coords before clipping - ({X1},{Y1}) to ({X2},{Y2})",
x1, y1, x2, y2);
x1 = Math.Max(0, Math.Min(x1, imageWidth - 1));
y1 = Math.Max(0, Math.Min(y1, imageHeight - 1));
x2 = Math.Max(0, Math.Min(x2, imageWidth - 1));
y2 = Math.Max(0, Math.Min(y2, imageHeight - 1));
if (x1 >= 0 && y1 >= 0 && x2 < imageWidth && y2 < imageHeight && x2 > x1 && y2 > y1)
{
_logger.LogInformation("Geofence {RegionId}: Tile range - X=[{MinX}..{MaxX}], Y=[{MinY}..{MaxY}]",
regionId, geoMinX, geoMaxX, geoMinY, geoMaxY);
_logger.LogInformation("Drawing geofence border at pixel coords ({X1},{Y1}) to ({X2},{Y2})",
x1, y1, x2, y2);
var x1 = (geoMinX - minX) * tileSizePixels;
var y1 = (geoMinY - minY) * tileSizePixels;
var x2 = (geoMaxX - minX + 1) * tileSizePixels - 1;
var y2 = (geoMaxY - minY + 1) * tileSizePixels - 1;
DrawRectangleBorder(stitchedImage, x1, y1, x2, y2, new Rgb24(255, 255, 0));
_logger.LogInformation("Geofence {RegionId}: Pixel coords before clipping - ({X1},{Y1}) to ({X2},{Y2})",
regionId, x1, y1, x2, y2);
x1 = Math.Max(0, Math.Min(x1, imageWidth - 1));
y1 = Math.Max(0, Math.Min(y1, imageHeight - 1));
x2 = Math.Max(0, Math.Min(x2, imageWidth - 1));
y2 = Math.Max(0, Math.Min(y2, imageHeight - 1));
if (x1 >= 0 && y1 >= 0 && x2 < imageWidth && y2 < imageHeight && x2 > x1 && y2 > y1)
{
_logger.LogInformation("Geofence {RegionId}: Drawing border at pixel coords ({X1},{Y1}) to ({X2},{Y2})",
regionId, x1, y1, x2, y2);
DrawRectangleBorder(stitchedImage, x1, y1, x2, y2, new Rgb24(255, 255, 0));
_logger.LogInformation("Successfully drew geofence border for region {RegionId}", regionId);
}
else
{
_logger.LogWarning("Geofence {RegionId}: Border out of bounds or invalid - ({X1},{Y1}) to ({X2},{Y2}), image size: {Width}x{Height}",
regionId, x1, y1, x2, y2, imageWidth, imageHeight);
}
_logger.LogInformation("Successfully drew geofence border");
}
else
{
_logger.LogWarning("Geofence border out of bounds or invalid - ({X1},{Y1}) to ({X2},{Y2}), image size: {Width}x{Height}",
x1, y1, x2, y2, imageWidth, imageHeight);
}
_logger.LogInformation("Completed drawing all geofence borders, now saving image...");
}
await stitchedImage.SaveAsJpegAsync(outputPath, cancellationToken);
@@ -684,21 +675,6 @@ public class RouteProcessingService : BackgroundService
return (-1, -1);
}
private static (double NorthLat, double SouthLat, double WestLon, double EastLon) CalculateGeofenceCorners(
double centerLat,
double centerLon,
double halfSizeMeters)
{
var center = new Common.DTO.GeoPoint { Lat = centerLat, Lon = centerLon };
var north = GeoUtils.GoDirection(center, new Common.DTO.Direction { Distance = halfSizeMeters, Azimuth = 0 });
var south = GeoUtils.GoDirection(center, new Common.DTO.Direction { Distance = halfSizeMeters, Azimuth = 180 });
var east = GeoUtils.GoDirection(center, new Common.DTO.Direction { Distance = halfSizeMeters, Azimuth = 90 });
var west = GeoUtils.GoDirection(center, new Common.DTO.Direction { Distance = halfSizeMeters, Azimuth = 270 });
return (north.Lat, south.Lat, west.Lon, east.Lon);
}
private static void DrawRectangleBorder(Image<Rgb24> image, int x1, int y1, int x2, int y2, Rgb24 color)
{
const int thickness = 5;
+51 -16
View File
@@ -192,24 +192,28 @@ public class RouteService : IRouteService
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();
var geofenceRegions = CreateGeofenceRegionGrid(polygon.NorthWest, polygon.SouthEast, request.RegionSizeMeters);
_logger.LogInformation("Route {RouteId}: Requesting geofence region {RegionId} at center ({Lat}, {Lon}) with size {Size}m",
request.Id, geofenceRegionId, center.Lat, center.Lon, geofenceRegionSize);
_logger.LogInformation("Route {RouteId}: Created grid of {Count} regions to cover geofence area",
request.Id, geofenceRegions.Count);
await _regionService.RequestRegionAsync(
geofenceRegionId,
center.Lat,
center.Lon,
geofenceRegionSize,
request.ZoomLevel,
stitchTiles: false);
await _routeRepository.LinkRouteToRegionAsync(request.Id, geofenceRegionId, isGeofence: true);
foreach (var geofencePoint in geofenceRegions)
{
var geofenceRegionId = Guid.NewGuid();
_logger.LogInformation("Route {RouteId}: Requesting geofence region {RegionId} at ({Lat}, {Lon}) with size {Size}m",
request.Id, geofenceRegionId, geofencePoint.Lat, geofencePoint.Lon, request.RegionSizeMeters);
await _regionService.RequestRegionAsync(
geofenceRegionId,
geofencePoint.Lat,
geofencePoint.Lon,
request.RegionSizeMeters,
request.ZoomLevel,
stitchTiles: false);
await _routeRepository.LinkRouteToRegionAsync(request.Id, geofenceRegionId, isGeofence: true);
}
}
}
@@ -278,5 +282,36 @@ public class RouteService : IRouteService
UpdatedAt = route.UpdatedAt
};
}
private List<GeoPoint> CreateGeofenceRegionGrid(GeoPoint northWest, GeoPoint southEast, double regionSizeMeters)
{
var regions = new List<GeoPoint>();
var northPoint = new GeoPoint(northWest.Lat, (northWest.Lon + southEast.Lon) / 2);
var southPoint = new GeoPoint(southEast.Lat, (northWest.Lon + southEast.Lon) / 2);
var heightMeters = GeoUtils.CalculateDistance(northPoint, southPoint);
var westPoint = new GeoPoint((northWest.Lat + southEast.Lat) / 2, northWest.Lon);
var eastPoint = new GeoPoint((northWest.Lat + southEast.Lat) / 2, southEast.Lon);
var widthMeters = GeoUtils.CalculateDistance(westPoint, eastPoint);
var numLatSteps = Math.Max(1, (int)Math.Ceiling(heightMeters / regionSizeMeters));
var numLonSteps = Math.Max(1, (int)Math.Ceiling(widthMeters / regionSizeMeters));
var latStep = (northWest.Lat - southEast.Lat) / numLatSteps;
var lonStep = (southEast.Lon - northWest.Lon) / numLonSteps;
for (int latIdx = 0; latIdx < numLatSteps; latIdx++)
{
for (int lonIdx = 0; lonIdx < numLonSteps; lonIdx++)
{
var lat = northWest.Lat - (latIdx + 0.5) * latStep;
var lon = northWest.Lon + (lonIdx + 0.5) * lonStep;
regions.Add(new GeoPoint(lat, lon));
}
}
return regions;
}
}