From c72a94952971a5bb977542e60295b75f4624f7a7 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 12 Aug 2023 08:20:28 +0100 Subject: [PATCH] Server now responds with image in PNG format --- imaging/circles.go | 19 ++++--------------- imaging/generate.go | 4 ++-- run/cli.go | 2 +- run/server.go | 32 +++++++++++++++----------------- 4 files changed, 22 insertions(+), 35 deletions(-) diff --git a/imaging/circles.go b/imaging/circles.go index 4f5edff..cf6ea4f 100644 --- a/imaging/circles.go +++ b/imaging/circles.go @@ -1,17 +1,15 @@ 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" - "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") dc := gg.NewContextForRGBA(img) 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() } - // Optional: Save the canvas as an image file - 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) + return img } +/* 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) @@ -91,9 +81,8 @@ func convertCoordinatesToPixels(latitude, longitude float64, centerLat, centerLo pixelY := int((0.5 - adjustedLatDist) * float64(canvasHeight)) 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 } diff --git a/imaging/generate.go b/imaging/generate.go index a99303b..05ff536 100644 --- a/imaging/generate.go +++ b/imaging/generate.go @@ -8,8 +8,8 @@ import ( "git.fjla.uk/fred.boniface/map-dots/log" ) -func Generate(height, width int, style, format string, data []data.LocationData) image.Image { - log.Msg.Debug("Image generation request: " + fmt.Sprint(width) + "x" + fmt.Sprint(height) + " " + format + " " + style) +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) + " " + style) img := createCanvas(width, height) switch style { diff --git a/run/cli.go b/run/cli.go index a6638ff..39adc35 100644 --- a/run/cli.go +++ b/run/cli.go @@ -25,7 +25,7 @@ func CLI(height, width uint64, style, format, input string) { // Use relevent package to parse file // Pass parsed data to imaging package 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") } diff --git a/run/server.go b/run/server.go index 9b8f895..ce7e517 100644 --- a/run/server.go +++ b/run/server.go @@ -1,9 +1,9 @@ package run import ( - "encoding/json" "errors" "fmt" + "image/png" "net/http" "net/url" "strconv" @@ -33,8 +33,6 @@ func Server() { } func handleTraccarRequest(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - queryValues := r.URL.Query() 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{ - "status": "success", - "message": "Hello from map-dots", + fmt.Printf("Requested format, %s, returning PNG as only supported format", format) + + switch format { + case "png": + w.Header().Set("Content-Type", "image/png") + err = png.Encode(w, img) + if err != nil { + http.Error(w, "Error encoding image", http.StatusInternalServerError) + return + } + default: + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("Unsupported format, only 'PNG' is supported at present")) } - - // Marshal the JSON data - jsonData, err := json.Marshal(message) - if err != nil { - http.Error(w, "JSON encoding error", http.StatusInternalServerError) - return - } - - // Write the JSON response - w.Write(jsonData) } func validateAndProcessParams(queryValues url.Values) (string, time.Time, time.Time, int, int, string, string, error) {