Show routing demonstration on radar page.

This commit is contained in:
Eli Ribble 2026-02-18 04:48:12 +00:00
parent 18c7a5f84b
commit b4817546df
No known key found for this signature in database
9 changed files with 392 additions and 129 deletions

View file

@ -5,7 +5,7 @@ tmp_dir = "tmp"
[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ./example/route"
cmd = "go build -o ./tmp/main ./example/geocode-and-route"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []

View file

@ -0,0 +1,31 @@
package main
import (
"fmt"
"log"
"github.com/Gleipnir-Technology/nidus-sync/tomtom"
)
func main() {
client := tomtom.NewClient()
// Example 1: Calculate a simple route
traffic := false
routeRequest := &tomtom.CalculateRouteRequest{
Locations: []tomtom.Point{
tomtom.P(52.50931, 13.42936),
tomtom.P(52.50274, 13.43872),
},
Traffic: &traffic,
TravelMode: tomtom.TravelModeCar,
RouteType: tomtom.RouteTypeFastest,
}
routeResp, err := client.CalculateRoute(routeRequest)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Route distance: %d meters\n", routeResp.Routes[0].Summary.LengthInMeters)
}

View file

@ -1,52 +0,0 @@
package main
import (
"fmt"
"log"
"github.com/Gleipnir-Technology/nidus-sync/tomtom"
)
func main() {
client := tomtom.NewClient()
// Example 1: Calculate a simple route
traffic := false
routeRequest := &tomtom.CalculateRouteRequest{
Locations: []tomtom.Point{
tomtom.P(52.50931, 13.42936),
tomtom.P(52.50274, 13.43872),
},
Traffic: &traffic,
TravelMode: tomtom.TravelModeCar,
RouteType: tomtom.RouteTypeFastest,
}
routeResp, err := client.CalculateRoute(routeRequest)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Route distance: %d meters\n", routeResp.Routes[0].Summary.LengthInMeters)
// Example 2: Calculate reachable range
/*
energyBudget := 43.0
rangeRequest := &tomtom.CalculateReachableRangeRequest{
Params: tomtom.CalculateReachableRangeParams{
VersionNumber: 1,
Origin: "52.50931,13.42936",
ContentType: "json",
EnergyBudgetInkWh: &energyBudget,
VehicleEngineType: "electric",
},
}
rangeResp, err := client.CalculateReachableRange(rangeRequest)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Reachable range includes %d points in the polygon\n", len(rangeResp.Polygon.Exterior))
*/
}

99
tomtom/geocode.go Normal file
View file

@ -0,0 +1,99 @@
package tomtom
import (
"fmt"
)
type PointShort struct {
Latitude float64 `json:"lat"`
Longitude float64 `json:"lon"`
}
func (ps PointShort) AsPoint() Point {
return Point{
Latitude: ps.Latitude,
Longitude: ps.Longitude,
}
}
type GeocodeResult struct {
Type string `json:"type"`
ID string `json:"id"`
Score float64 `json:"score"`
Dist float64 `json:"dist"`
MatchConfidence MatchConfidence `json:"matchConfidence"`
Address Address `json:"address"`
Position PointShort `json:"position"`
Viewport Viewport `json:"viewport"`
EntryPoints []EntryPoint `json:"entryPoints"`
}
// MatchConfidence represents the confidence score for a match
type MatchConfidence struct {
Score float64 `json:"score"`
}
// Address contains detailed address information
type Address struct {
StreetNumber string `json:"streetNumber"`
StreetName string `json:"streetName"`
Municipality string `json:"municipality"`
CountrySecondarySubdivision string `json:"countrySecondarySubdivision"`
CountrySubdivision string `json:"countrySubdivision"`
CountrySubdivisionName string `json:"countrySubdivisionName"`
CountrySubdivisionCode string `json:"countrySubdivisionCode"`
PostalCode string `json:"postalCode"`
ExtendedPostalCode string `json:"extendedPostalCode"`
CountryCode string `json:"countryCode"`
Country string `json:"country"`
CountryCodeISO3 string `json:"countryCodeISO3"`
FreeformAddress string `json:"freeformAddress"`
LocalName string `json:"localName"`
}
// Viewport defines a geographic bounding box
type Viewport struct {
TopLeftPoint PointShort `json:"topLeftPoint"`
BtmRightPoint PointShort `json:"btmRightPoint"`
}
// EntryPoint contains information about a point of entry to a location
type EntryPoint struct {
Type string `json:"type"`
Position PointShort `json:"position"`
}
type GeocodeSummary struct {
Query string `json:"query"`
QueryType string `json:"queryType"`
QueryTime uint `json:"queryTime"`
NumResults uint `json:"numResults"`
Offset uint `json:"offset"`
TotalResults uint `json:"totalResults"`
FuzzyLevel uint `json:"fuzzyLevel"`
GeoBias PointShort `json:"geoBias"`
}
type GeocodeResponse struct {
Summary GeocodeSummary `json:"summary"`
Results []GeocodeResult `json:"results"`
}
// CalculateRoute sends a route calculation request to TomTom API
func (c *TomTom) Geocode(address string) (*GeocodeResponse, error) {
var result GeocodeResponse
resp, err := c.client.R().
SetResult(&result).
SetPathParam("address", address).
SetPathParam("urlBase", c.urlBase).
SetQueryParam("key", c.APIKey).
SetQueryParam("storeResult", "false").
Get("https://{urlBase}/search/2/geocode/{address}.json")
if err != nil {
return nil, fmt.Errorf("calculate route get: %w", err)
}
if !resp.IsSuccess() {
return nil, fmt.Errorf("calculate route status: %d", resp.Status)
}
return &result, nil
}

27
tomtom/geojson.go Normal file
View file

@ -0,0 +1,27 @@
package tomtom
import (
"fmt"
"strings"
)
// Convert a slice of points to GeoJSON
func PolylineToGeoJSON(polyline []Point) string {
var sb strings.Builder
sb.WriteString(`{"type":"LineString","coordinates":[`)
for i, point := range polyline {
// IMPORTANT: GeoJSON uses [longitude, latitude] order!
sb.WriteString(fmt.Sprintf("[%g,%g]", point.Longitude, point.Latitude))
// Add comma if not the last point
if i < len(polyline)-1 {
sb.WriteString(",")
}
}
sb.WriteString("]}")
return sb.String()
}

View file

@ -68,6 +68,10 @@ func (sgr CalculateRouteRequest) toQueryParams() (url.Values, error) {
return query.Values(sgr)
}
type CalculateRouteResponse struct {
Routes []Route `json:"routes"`
}
// CalculateRoute sends a route calculation request to TomTom API
func (c *TomTom) CalculateRoute(req *CalculateRouteRequest) (*CalculateRouteResponse, error) {
/*url, err := req.BuildURL(c.APIKey)

View file

@ -14,11 +14,8 @@ type TomTom struct {
}
func NewClient() *TomTom {
//logger := NewLogger(log.Logger)
//r := resty.New().SetLogger(logger).SetDebug(true)
r := resty.New().SetDebug(true)
api_key := os.Getenv("TOMTOM_API_KEY")
//r := resty.New()
r := resty.New()
return &TomTom{
APIKey: api_key,
client: r,
@ -29,3 +26,12 @@ func NewClient() *TomTom {
func (s *TomTom) Close() {
s.client.Close()
}
func (s *TomTom) SetDebug(enabled bool) {
s.client.Close()
if enabled {
s.client = resty.New().SetDebug(true)
} else {
s.client = resty.New().SetDebug(false)
}
}

View file

@ -68,24 +68,14 @@ type Point struct {
Longitude float64 `json:"longitude"`
}
type Polyline struct {
Points []Point `json:"points"`
}
type Leg struct {
Summary Summary `json:"summary"`
Points []Point `json:"points,omitempty"`
Polyline Polyline `json:"polyline,omitempty"`
Summary Summary `json:"summary"`
Points []Point `json:"points,omitempty"`
}
type Route struct {
Summary Summary `json:"summary"`
Legs []Leg `json:"legs,omitempty"`
Polyline Polyline `json:"polyline,omitempty"`
}
type CalculateRouteResponse struct {
Routes []Route `json:"routes"`
Summary Summary `json:"summary"`
Legs []Leg `json:"legs,omitempty"`
}
// CalculateReachableRange API structures