diff --git a/imaging/circles.go b/imaging/circles.go index ca7ee29..debb548 100644 --- a/imaging/circles.go +++ b/imaging/circles.go @@ -1,43 +1,49 @@ package imaging import ( + "fmt" "image" + "math" "git.fjla.uk/fred.boniface/map-dots/data" "git.fjla.uk/fred.boniface/map-dots/log" "github.com/fogleman/gg" ) -func mapCirclesToCanvas(img *image.RGBA, circles []data.LocationData) { - log.Msg.Debug("Mapping circles to canvas") +func mapCirclesToCanvas(img *image.RGBA, locations []data.LocationData) { + log.Msg.Debug("Mapping locations to canvas") dc := gg.NewContextForRGBA(img) dc.SetRGB(1, 1, 1) // Set canvas background color (white in this case) - circleRadius := 10 // Replace with your desired fixed radius + circleRadius := 4 // Replace with your desired fixed radius bounds := img.Bounds() canvasWidth := bounds.Max.X - bounds.Min.X canvasHeight := bounds.Max.Y - bounds.Min.Y var minLat, maxLat, minLon, maxLon float64 - for _, circle := range circles { - if circle.Latitude < minLat { - minLat = circle.Latitude + for _, loc := range locations { + if loc.Latitude < minLat { + minLat = loc.Latitude } - if circle.Latitude > maxLat { - maxLat = circle.Latitude + if loc.Latitude > maxLat { + maxLat = loc.Latitude } - if circle.Longitude < minLon { - minLon = circle.Longitude + if loc.Longitude < minLon { + minLon = loc.Longitude } - if circle.Longitude > maxLon { - maxLon = circle.Longitude + if loc.Longitude > maxLon { + maxLon = loc.Longitude } } - for _, circle := range circles { - // Convert latitude and longitude to pixel position on the canvas - pixelX, pixelY := convertCoordinatesToPixels(circle.Latitude, circle.Longitude, minLat, maxLat, minLon, maxLon, canvasWidth, canvasHeight) + centerLat := (maxLat + minLat) / 2.0 + centerLon := (maxLon + minLon) / 2.0 + latRange := maxLat - minLat + lonRange := maxLon - minLon + + for _, loc := range locations { + pixelX, pixelY := convertCoordinatesToPixels(loc.Latitude, loc.Longitude, centerLat, centerLon, latRange, lonRange, canvasWidth, canvasHeight) // Draw the circle on the canvas using gg dc.DrawCircle(float64(pixelX), float64(pixelY), float64(circleRadius)) @@ -50,29 +56,27 @@ func mapCirclesToCanvas(img *image.RGBA, circles []data.LocationData) { // Optional: Save the canvas as an image file err := dc.SavePNG("output.png") if err != nil { - // Handle error + log.Msg.Error("Error saving file") + } else { + fmt.Println("Canvas saves to file") } } -func convertCoordinatesToPixels(latitude, longitude float64, minLat, maxLat, minLon, maxLon float64, canvasWidth, canvasHeight int) (int, int) { - // Calculate margins as a percentage of canvas dimensions - marginPercentage := 0.1 // 10% margin - marginWidth := int(float64(canvasWidth) * marginPercentage) - marginHeight := int(float64(canvasHeight) * marginPercentage) +func convertCoordinatesToPixels(latitude, longitude float64, centerLat, centerLon, latRange, lonRange float64, canvasWidth, canvasHeight int) (int, int) { + // Calculate normalized latitude and longitude distances from the center + latDist := (latitude - centerLat) / (latRange * 0.5) + lonDist := (longitude - centerLon) / (lonRange * 0.5) - // Adjust the bounding box with margins - minLat -= (maxLat - minLat) * marginPercentage - maxLat += (maxLat - minLat) * marginPercentage - minLon -= (maxLon - minLon) * marginPercentage - maxLon += (maxLon - minLon) * marginPercentage + // Calculate the maximum distance from the center as a proportion of canvas size + maxDistance := math.Max(math.Abs(latDist), math.Abs(lonDist)) - // Calculate the position within the adjusted bounding box - latPercentage := (latitude - minLat) / (maxLat - minLat) - lonPercentage := (longitude - minLon) / (maxLon - minLon) + // Adjust the normalized distances to match the canvas size with margins + adjustedLatDist := latDist / maxDistance * 0.45 + adjustedLonDist := lonDist / maxDistance * 0.45 - // Calculate the pixel positions on the canvas - pixelX := int(lonPercentage*float64(canvasWidth-marginWidth*2)) + marginWidth - pixelY := canvasHeight - (int(latPercentage*float64(canvasHeight-marginHeight*2)) + marginHeight) + // Calculate pixel positions + pixelX := int((adjustedLonDist + 0.5) * float64(canvasWidth)) + pixelY := int((0.5 - adjustedLatDist) * float64(canvasHeight)) return pixelX, pixelY } diff --git a/output.png b/output.png index 3197ff3..6d9e8d7 100644 Binary files a/output.png and b/output.png differ diff --git a/run/server.go b/run/server.go index 66fde6b..9b8f895 100644 --- a/run/server.go +++ b/run/server.go @@ -60,6 +60,10 @@ func handleTraccarRequest(w http.ResponseWriter, r *http.Request) { ) } else { log.Msg.Debug("Position data fetched") + for _, loc := range locations { + fmt.Printf("Latitude: %.7f, Longitude: %.7f, Speed: %d, Altitude: %.4f\n", + loc.Latitude, loc.Longitude, loc.Speed, loc.Altitude) + } } var _ = imaging.Generate(height, width, style, format, locations)