Working output

This commit is contained in:
Fred Boniface 2023-08-11 22:18:29 +01:00
parent 47a4490dc8
commit e5d0cc30d6
2 changed files with 29 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.env .env
map-dots map-dots
*.png
# If you prefer the allow list template instead of the deny list, see community template: # 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 # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore

View File

@ -8,6 +8,7 @@ import (
"git.fjla.uk/fred.boniface/map-dots/data" "git.fjla.uk/fred.boniface/map-dots/data"
"git.fjla.uk/fred.boniface/map-dots/log" "git.fjla.uk/fred.boniface/map-dots/log"
"github.com/fogleman/gg" "github.com/fogleman/gg"
"go.uber.org/zap"
) )
func mapCirclesToCanvas(img *image.RGBA, locations []data.LocationData) { 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 := gg.NewContextForRGBA(img)
dc.SetRGB(1, 1, 1) // Set canvas background color (white in this case) 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() bounds := img.Bounds()
canvasWidth := bounds.Max.X - bounds.Min.X canvasWidth := bounds.Max.X - bounds.Min.X
canvasHeight := bounds.Max.Y - bounds.Min.Y 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 { for _, loc := range locations {
if loc.Latitude < minLat { if loc.Latitude < minLat {
minLat = loc.Latitude 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 latRange := maxLat - minLat
lonRange := maxLon - minLon 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 { for _, loc := range locations {
pixelX, pixelY := convertCoordinatesToPixels(loc.Latitude, loc.Longitude, centerLat, centerLon, latRange, lonRange, canvasWidth, canvasHeight) x := int((loc.Longitude - minLon) * longScale)
// Invert the Y-axis calculation
// Draw the circle on the canvas using gg y := canvasHeight - int((equirectangularProjection(loc.Latitude)-minLatEqui)*latScale)
dc.DrawCircle(float64(pixelX), float64(pixelY), float64(circleRadius)) // Draw a dot (circle) at (x, y)
dc.SetRGBA(1, 1, 1, 0.5) // Circle fill color (white with 50% opacity) dc.DrawCircle(float64(x), float64(y), 2)
dc.SetRGBA(1, 1, 1, 0.3333333333) // Set dot color (black)
dc.Fill() dc.Fill()
} }
dc.Clip() // Apply clipping
// Optional: Save the canvas as an image file // Optional: Save the canvas as an image file
err := dc.SavePNG("output.png") err := dc.SavePNG("output.png")
if err != nil { if err != nil {
@ -60,6 +69,9 @@ func mapCirclesToCanvas(img *image.RGBA, locations []data.LocationData) {
} else { } else {
fmt.Println("Canvas saves to file") 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) { 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 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
}