Server now responds with image in PNG format

This commit is contained in:
Fred Boniface 2023-08-12 08:20:28 +01:00
parent e5d0cc30d6
commit c72a949529
4 changed files with 22 additions and 35 deletions

View File

@ -1,17 +1,15 @@
package imaging package imaging
import ( import (
"fmt"
"image" "image"
"math" "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"
"go.uber.org/zap"
) )
func mapCirclesToCanvas(img *image.RGBA, locations []data.LocationData) { func mapCirclesToCanvas(img *image.RGBA, locations []data.LocationData) image.Image {
log.Msg.Debug("Mapping locations 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)
@ -62,18 +60,10 @@ func mapCirclesToCanvas(img *image.RGBA, locations []data.LocationData) {
dc.Fill() dc.Fill()
} }
// Optional: Save the canvas as an image file return img
err := dc.SavePNG("output.png")
if err != nil {
log.Msg.Error("Error saving file")
} 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) { func convertCoordinatesToPixels(latitude, longitude float64, centerLat, centerLon, latRange, lonRange float64, canvasWidth, canvasHeight int) (int, int) {
// Calculate normalized latitude and longitude distances from the center // Calculate normalized latitude and longitude distances from the center
latDist := (latitude - centerLat) / (latRange * 0.5) latDist := (latitude - centerLat) / (latRange * 0.5)
@ -91,9 +81,8 @@ func convertCoordinatesToPixels(latitude, longitude float64, centerLat, centerLo
pixelY := int((0.5 - adjustedLatDist) * float64(canvasHeight)) pixelY := int((0.5 - adjustedLatDist) * float64(canvasHeight))
return pixelX, pixelY return pixelX, pixelY
} }*/
func equirectangularProjection(lat float64) float64 { func equirectangularProjection(lat float64) float64 {
log.Msg.Debug("Running equirectangular calculation", zap.Float64("lat", lat))
return lat * (math.Pi / 180) // Convert degrees to radians return lat * (math.Pi / 180) // Convert degrees to radians
} }

View File

@ -8,8 +8,8 @@ import (
"git.fjla.uk/fred.boniface/map-dots/log" "git.fjla.uk/fred.boniface/map-dots/log"
) )
func Generate(height, width int, style, format string, data []data.LocationData) image.Image { func Generate(height, width int, style string, data []data.LocationData) image.Image {
log.Msg.Debug("Image generation request: " + fmt.Sprint(width) + "x" + fmt.Sprint(height) + " " + format + " " + style) log.Msg.Debug("Image generation request: " + fmt.Sprint(width) + "x" + fmt.Sprint(height) + " " + style)
img := createCanvas(width, height) img := createCanvas(width, height)
switch style { switch style {

View File

@ -25,7 +25,7 @@ func CLI(height, width uint64, style, format, input string) {
// Use relevent package to parse file // Use relevent package to parse file
// Pass parsed data to imaging package // Pass parsed data to imaging package
var testing []data.LocationData var testing []data.LocationData
var _ image.Image = imaging.Generate(1, 1, "circle", "png", testing) var _ image.Image = imaging.Generate(1, 1, "circle", testing)
fmt.Println("End of implementation") fmt.Println("End of implementation")
} }

View File

@ -1,9 +1,9 @@
package run package run
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"image/png"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
@ -33,8 +33,6 @@ func Server() {
} }
func handleTraccarRequest(w http.ResponseWriter, r *http.Request) { func handleTraccarRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
queryValues := r.URL.Query() queryValues := r.URL.Query()
id, from, to, height, width, style, format, err := validateAndProcessParams(queryValues) id, from, to, height, width, style, format, err := validateAndProcessParams(queryValues)
@ -66,22 +64,22 @@ func handleTraccarRequest(w http.ResponseWriter, r *http.Request) {
} }
} }
var _ = imaging.Generate(height, width, style, format, locations) img := imaging.Generate(height, width, style, locations)
message := map[string]string{ fmt.Printf("Requested format, %s, returning PNG as only supported format", format)
"status": "success",
"message": "Hello from map-dots",
}
// Marshal the JSON data switch format {
jsonData, err := json.Marshal(message) case "png":
w.Header().Set("Content-Type", "image/png")
err = png.Encode(w, img)
if err != nil { if err != nil {
http.Error(w, "JSON encoding error", http.StatusInternalServerError) http.Error(w, "Error encoding image", http.StatusInternalServerError)
return return
} }
default:
// Write the JSON response w.Header().Set("Content-Type", "text/plain")
w.Write(jsonData) w.Write([]byte("Unsupported format, only 'PNG' is supported at present"))
}
} }
func validateAndProcessParams(queryValues url.Values) (string, time.Time, time.Time, int, int, string, string, error) { func validateAndProcessParams(queryValues url.Values) (string, time.Time, time.Time, int, int, string, string, error) {