Trying to get the canvas looking okay. It doesn't
This commit is contained in:
parent
3f34c36c11
commit
b8b869f981
@ -1,43 +1,49 @@
|
|||||||
package imaging
|
package imaging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
|
"math"
|
||||||
|
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
func mapCirclesToCanvas(img *image.RGBA, circles []data.LocationData) {
|
func mapCirclesToCanvas(img *image.RGBA, locations []data.LocationData) {
|
||||||
log.Msg.Debug("Mapping circles to canvas")
|
log.Msg.Debug("Mapping locations to canvas")
|
||||||
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 := 10 // 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
|
||||||
|
|
||||||
var minLat, maxLat, minLon, maxLon float64
|
var minLat, maxLat, minLon, maxLon float64
|
||||||
for _, circle := range circles {
|
for _, loc := range locations {
|
||||||
if circle.Latitude < minLat {
|
if loc.Latitude < minLat {
|
||||||
minLat = circle.Latitude
|
minLat = loc.Latitude
|
||||||
}
|
}
|
||||||
if circle.Latitude > maxLat {
|
if loc.Latitude > maxLat {
|
||||||
maxLat = circle.Latitude
|
maxLat = loc.Latitude
|
||||||
}
|
}
|
||||||
if circle.Longitude < minLon {
|
if loc.Longitude < minLon {
|
||||||
minLon = circle.Longitude
|
minLon = loc.Longitude
|
||||||
}
|
}
|
||||||
if circle.Longitude > maxLon {
|
if loc.Longitude > maxLon {
|
||||||
maxLon = circle.Longitude
|
maxLon = loc.Longitude
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, circle := range circles {
|
centerLat := (maxLat + minLat) / 2.0
|
||||||
// Convert latitude and longitude to pixel position on the canvas
|
centerLon := (maxLon + minLon) / 2.0
|
||||||
pixelX, pixelY := convertCoordinatesToPixels(circle.Latitude, circle.Longitude, minLat, maxLat, minLon, maxLon, canvasWidth, canvasHeight)
|
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
|
// Draw the circle on the canvas using gg
|
||||||
dc.DrawCircle(float64(pixelX), float64(pixelY), float64(circleRadius))
|
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
|
// Optional: Save the canvas as an image file
|
||||||
err := dc.SavePNG("output.png")
|
err := dc.SavePNG("output.png")
|
||||||
if err != nil {
|
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) {
|
func convertCoordinatesToPixels(latitude, longitude float64, centerLat, centerLon, latRange, lonRange float64, canvasWidth, canvasHeight int) (int, int) {
|
||||||
// Calculate margins as a percentage of canvas dimensions
|
// Calculate normalized latitude and longitude distances from the center
|
||||||
marginPercentage := 0.1 // 10% margin
|
latDist := (latitude - centerLat) / (latRange * 0.5)
|
||||||
marginWidth := int(float64(canvasWidth) * marginPercentage)
|
lonDist := (longitude - centerLon) / (lonRange * 0.5)
|
||||||
marginHeight := int(float64(canvasHeight) * marginPercentage)
|
|
||||||
|
|
||||||
// Adjust the bounding box with margins
|
// Calculate the maximum distance from the center as a proportion of canvas size
|
||||||
minLat -= (maxLat - minLat) * marginPercentage
|
maxDistance := math.Max(math.Abs(latDist), math.Abs(lonDist))
|
||||||
maxLat += (maxLat - minLat) * marginPercentage
|
|
||||||
minLon -= (maxLon - minLon) * marginPercentage
|
|
||||||
maxLon += (maxLon - minLon) * marginPercentage
|
|
||||||
|
|
||||||
// Calculate the position within the adjusted bounding box
|
// Adjust the normalized distances to match the canvas size with margins
|
||||||
latPercentage := (latitude - minLat) / (maxLat - minLat)
|
adjustedLatDist := latDist / maxDistance * 0.45
|
||||||
lonPercentage := (longitude - minLon) / (maxLon - minLon)
|
adjustedLonDist := lonDist / maxDistance * 0.45
|
||||||
|
|
||||||
// Calculate the pixel positions on the canvas
|
// Calculate pixel positions
|
||||||
pixelX := int(lonPercentage*float64(canvasWidth-marginWidth*2)) + marginWidth
|
pixelX := int((adjustedLonDist + 0.5) * float64(canvasWidth))
|
||||||
pixelY := canvasHeight - (int(latPercentage*float64(canvasHeight-marginHeight*2)) + marginHeight)
|
pixelY := int((0.5 - adjustedLatDist) * float64(canvasHeight))
|
||||||
|
|
||||||
return pixelX, pixelY
|
return pixelX, pixelY
|
||||||
}
|
}
|
||||||
|
BIN
output.png
BIN
output.png
Binary file not shown.
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 1.5 KiB |
@ -60,6 +60,10 @@ func handleTraccarRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
log.Msg.Debug("Position data fetched")
|
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)
|
var _ = imaging.Generate(height, width, style, format, locations)
|
||||||
|
Loading…
Reference in New Issue
Block a user