From e5d0cc30d640e3704ba1898b6c730d8d00ef6285 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Fri, 11 Aug 2023 22:18:29 +0100 Subject: [PATCH] Working output --- .gitignore | 1 + imaging/circles.go | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 8bb83ee..2f7aab3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .env map-dots +*.png # If you prefer the allow list template instead of the deny list, see community template: # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore diff --git a/imaging/circles.go b/imaging/circles.go index debb548..4f5edff 100644 --- a/imaging/circles.go +++ b/imaging/circles.go @@ -8,6 +8,7 @@ import ( "git.fjla.uk/fred.boniface/map-dots/data" "git.fjla.uk/fred.boniface/map-dots/log" "github.com/fogleman/gg" + "go.uber.org/zap" ) func mapCirclesToCanvas(img *image.RGBA, locations []data.LocationData) { @@ -15,13 +16,14 @@ func mapCirclesToCanvas(img *image.RGBA, locations []data.LocationData) { dc := gg.NewContextForRGBA(img) dc.SetRGB(1, 1, 1) // Set canvas background color (white in this case) - circleRadius := 4 // 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 + margin := 0.1 - var minLat, maxLat, minLon, maxLon float64 + var minLat, maxLat, minLon, maxLon = math.MaxFloat64, -math.MaxFloat64, math.MaxFloat64, -math.MaxFloat64 for _, loc := range locations { if loc.Latitude < minLat { minLat = loc.Latitude @@ -37,22 +39,29 @@ func mapCirclesToCanvas(img *image.RGBA, locations []data.LocationData) { } } - centerLat := (maxLat + minLat) / 2.0 - centerLon := (maxLon + minLon) / 2.0 latRange := maxLat - minLat lonRange := maxLon - minLon + minLat -= latRange * margin + maxLat += latRange * margin + minLon -= lonRange * margin + maxLon += lonRange * margin + + minLatEqui := equirectangularProjection(minLat) + maxLatEqui := equirectangularProjection(maxLat) + + latScale := float64(canvasHeight) / (maxLatEqui - minLatEqui) + longScale := float64(canvasWidth) / (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)) - dc.SetRGBA(1, 1, 1, 0.5) // Circle fill color (white with 50% opacity) + x := int((loc.Longitude - minLon) * longScale) + // Invert the Y-axis calculation + y := canvasHeight - int((equirectangularProjection(loc.Latitude)-minLatEqui)*latScale) + // Draw a dot (circle) at (x, y) + dc.DrawCircle(float64(x), float64(y), 2) + dc.SetRGBA(1, 1, 1, 0.3333333333) // Set dot color (black) dc.Fill() } - dc.Clip() // Apply clipping - // Optional: Save the canvas as an image file err := dc.SavePNG("output.png") if err != nil { @@ -60,6 +69,9 @@ func mapCirclesToCanvas(img *image.RGBA, locations []data.LocationData) { } else { fmt.Println("Canvas saves to file") } + + fmt.Println(minLat) + fmt.Println(minLon) } func convertCoordinatesToPixels(latitude, longitude float64, centerLat, centerLon, latRange, lonRange float64, canvasWidth, canvasHeight int) (int, int) { @@ -80,3 +92,8 @@ func convertCoordinatesToPixels(latitude, longitude float64, centerLat, centerLo return pixelX, pixelY } + +func equirectangularProjection(lat float64) float64 { + log.Msg.Debug("Running equirectangular calculation", zap.Float64("lat", lat)) + return lat * (math.Pi / 180) // Convert degrees to radians +}