Further adjustments

This commit is contained in:
Fred Boniface 2023-08-11 20:51:46 +01:00
parent f179602f0b
commit 3f34c36c11
7 changed files with 142 additions and 12 deletions

11
go.mod
View File

@ -2,6 +2,13 @@ module git.fjla.uk/fred.boniface/map-dots
go 1.19 go 1.19
require go.uber.org/zap v1.25.0 require (
github.com/fogleman/gg v1.3.0
go.uber.org/zap v1.25.0
)
require go.uber.org/multierr v1.10.0 // indirect require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/image v0.11.0 // indirect
)

37
go.sum
View File

@ -1,10 +1,47 @@
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c= go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk= go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.11.0 h1:ds2RoQvBvYTiJkwpSFDwCcDFNX7DqjL2WsUgTNk0Ooo=
golang.org/x/image v0.11.0/go.mod h1:bglhjqbqVuEb9e9+eNR45Jfu7D+T4Qan+NhQk8Ck2P8=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@ -1 +1,78 @@
package imaging package imaging
import (
"image"
"git.fjla.uk/fred.boniface/map-dots/data"
"git.fjla.uk/fred.boniface/map-dots/log"
"github.com/fogleman/gg"
)
func mapCirclesToCanvas(img *image.RGBA, circles []data.LocationData) {
log.Msg.Debug("Mapping circles to canvas")
dc := gg.NewContextForRGBA(img)
dc.SetRGB(1, 1, 1) // Set canvas background color (white in this case)
circleRadius := 10 // Replace with your desired fixed radius
bounds := img.Bounds()
canvasWidth := bounds.Max.X - bounds.Min.X
canvasHeight := bounds.Max.Y - bounds.Min.Y
var minLat, maxLat, minLon, maxLon float64
for _, circle := range circles {
if circle.Latitude < minLat {
minLat = circle.Latitude
}
if circle.Latitude > maxLat {
maxLat = circle.Latitude
}
if circle.Longitude < minLon {
minLon = circle.Longitude
}
if circle.Longitude > maxLon {
maxLon = circle.Longitude
}
}
for _, circle := range circles {
// Convert latitude and longitude to pixel position on the canvas
pixelX, pixelY := convertCoordinatesToPixels(circle.Latitude, circle.Longitude, minLat, maxLat, minLon, maxLon, canvasWidth, canvasHeight)
// Draw the circle on the canvas using gg
dc.DrawCircle(float64(pixelX), float64(pixelY), float64(circleRadius))
dc.SetRGBA(1, 1, 1, 0.5) // Circle fill color (white with 50% opacity)
dc.Fill()
}
dc.Clip() // Apply clipping
// Optional: Save the canvas as an image file
err := dc.SavePNG("output.png")
if err != nil {
// Handle error
}
}
func convertCoordinatesToPixels(latitude, longitude float64, minLat, maxLat, minLon, maxLon float64, canvasWidth, canvasHeight int) (int, int) {
// Calculate margins as a percentage of canvas dimensions
marginPercentage := 0.1 // 10% margin
marginWidth := int(float64(canvasWidth) * marginPercentage)
marginHeight := int(float64(canvasHeight) * marginPercentage)
// Adjust the bounding box with margins
minLat -= (maxLat - minLat) * marginPercentage
maxLat += (maxLat - minLat) * marginPercentage
minLon -= (maxLon - minLon) * marginPercentage
maxLon += (maxLon - minLon) * marginPercentage
// Calculate the position within the adjusted bounding box
latPercentage := (latitude - minLat) / (maxLat - minLat)
lonPercentage := (longitude - minLon) / (maxLon - minLon)
// Calculate the pixel positions on the canvas
pixelX := int(lonPercentage*float64(canvasWidth-marginWidth*2)) + marginWidth
pixelY := canvasHeight - (int(latPercentage*float64(canvasHeight-marginHeight*2)) + marginHeight)
return pixelX, pixelY
}

View File

@ -11,5 +11,11 @@ import (
func Generate(height, width int, style, format string, data []data.LocationData) image.Image { 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) log.Msg.Debug("Image generation request: " + fmt.Sprint(width) + "x" + fmt.Sprint(height) + " " + format + " " + style)
img := createCanvas(width, height) img := createCanvas(width, height)
switch style {
case "circle":
log.Msg.Debug("Selected style is circles")
mapCirclesToCanvas(img, data)
}
return img return img
} }

BIN
output.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -43,7 +43,11 @@ func handleTraccarRequest(w http.ResponseWriter, r *http.Request) {
return return
} }
fmt.Println(id, from, to) log.Msg.Debug("Requesting Data from Traccar",
zap.String("id", id),
zap.Time("from", from),
zap.Time("to", to),
)
locations, err := traccar.GetPositions(id, from, to) locations, err := traccar.GetPositions(id, from, to)
if err != nil { if err != nil {
@ -52,10 +56,11 @@ func handleTraccarRequest(w http.ResponseWriter, r *http.Request) {
zap.String("id", id), zap.String("id", id),
zap.Time("from", from), zap.Time("from", from),
zap.Time("to", to), zap.Time("to", to),
zap.Error(err)) zap.Error(err),
} )
} else {
log.Msg.Debug("Position data fetched") log.Msg.Debug("Position data fetched")
fmt.Println(locations) }
var _ = imaging.Generate(height, width, style, format, locations) var _ = imaging.Generate(height, width, style, format, locations)

View File

@ -9,18 +9,14 @@ import (
"time" "time"
"git.fjla.uk/fred.boniface/map-dots/data" "git.fjla.uk/fred.boniface/map-dots/data"
"git.fjla.uk/fred.boniface/map-dots/log"
"go.uber.org/zap"
) )
var user string = os.Getenv("TRACCAR_USER") var user string = os.Getenv("TRACCAR_USER")
var pass string = os.Getenv("TRACCAR_PASS") var pass string = os.Getenv("TRACCAR_PASS")
var tUrl string = os.Getenv("TRACCAR_URL") var tUrl string = os.Getenv("TRACCAR_URL")
var params = map[string]string{
"deviceId": "2",
"from": "2023-07-10T20:00:00Z",
"to": "2023-08-10T20:00:00Z",
}
func GetPositions(id string, from, to time.Time) ([]data.LocationData, error) { func GetPositions(id string, from, to time.Time) ([]data.LocationData, error) {
var params = map[string]string{ var params = map[string]string{
"deviceId": id, "deviceId": id,
@ -56,6 +52,8 @@ func GetPositions(id string, from, to time.Time) ([]data.LocationData, error) {
func createRequest(params map[string]string) (*http.Request, error) { func createRequest(params map[string]string) (*http.Request, error) {
baseURL := tUrl + "/api/positions" baseURL := tUrl + "/api/positions"
log.Msg.Debug("Attemting fetch", zap.String("url", baseURL))
u, err := url.Parse(baseURL) u, err := url.Parse(baseURL)
if err != nil { if err != nil {
return nil, err return nil, err