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
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
}

View File

@ -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 {

View File

@ -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")
}

View File

@ -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) {