nidus-sync/stadia/cmd/structured-geocode/main.go

97 lines
2.9 KiB
Go
Raw Normal View History

2026-02-14 15:40:12 +00:00
package main
import (
2026-02-25 02:44:35 +00:00
"context"
"flag"
2026-02-14 15:40:12 +00:00
"log"
"os"
"github.com/Gleipnir-Technology/nidus-sync/stadia"
)
func main() {
// Define command-line flags
address := flag.String("address", "", "Street address to geocode")
boundaryRectMaxLat := flag.Float64("boundary-rect-max-lat", 0, "The max lat of the boundary")
boundaryRectMinLat := flag.Float64("boundary-rect-min-lat", 0, "The min lat of the boundary")
boundaryRectMaxLon := flag.Float64("boundary-rect-max-lng", 0, "The max lon of the boundary")
boundaryRectMinLon := flag.Float64("boundary-rect-min-lng", 0, "The min lon of the boundary")
city := flag.String("city", "", "City address to geocode")
postalCode := flag.String("postal-code", "", "Postal code")
focusLat := flag.Float64("focus-lat", 0, "The latitude of the focus point")
focusLng := flag.Float64("focus-lng", 0, "The longitude of the focus point")
// Parse the flags
flag.Parse()
// Validate required arguments
if *address == "" {
log.Println("Error: -address is required")
flag.Usage()
os.Exit(1)
}
if *postalCode == "" {
log.Println("Error: -postal-code is required")
flag.Usage()
os.Exit(1)
}
if *focusLat != 0 && *focusLng == 0 {
log.Println("Error: you must specify both focus-lat and focus-lng together, not just focus-lat")
flag.Usage()
os.Exit(1)
}
if *focusLat == 0 && *focusLng != 0 {
log.Println("Error: you must specify both focus-lat and focus-lng together, not just focus-lng")
flag.Usage()
os.Exit(1)
}
if (*boundaryRectMaxLat != 0 ||
*boundaryRectMinLat != 0 ||
*boundaryRectMaxLon != 0 ||
*boundaryRectMinLon != 0) && (*boundaryRectMaxLat == 0 ||
*boundaryRectMinLat == 0 ||
*boundaryRectMaxLon == 0 ||
*boundaryRectMinLon == 0) {
log.Println("If you specify one of boundary-rect you need to specify them all")
os.Exit(1)
}
2026-02-14 15:40:12 +00:00
key := os.Getenv("STADIA_MAPS_API_KEY")
if key == "" {
log.Println("STADIA_MAPS_API_KEY is empty")
2026-02-14 15:40:12 +00:00
os.Exit(1)
}
2026-02-14 15:40:12 +00:00
client := stadia.NewStadiaMaps(key)
ctx := context.Background()
req := stadia.RequestGeocodeStructured{
Address: address,
PostalCode: postalCode,
}
if *focusLat != 0 && *focusLng != 0 {
req.FocusPointLat = focusLat
req.FocusPointLng = focusLng
}
if *boundaryRectMaxLat != 0 {
req.BoundaryRectMaxLat = boundaryRectMaxLat
req.BoundaryRectMinLat = boundaryRectMinLat
req.BoundaryRectMaxLon = boundaryRectMaxLon
req.BoundaryRectMinLon = boundaryRectMinLon
}
if *city != "" {
req.Locality = city
}
2026-03-18 19:59:42 +00:00
resp, err := client.GeocodeStructured(ctx, req)
2026-02-14 15:40:12 +00:00
if err != nil {
log.Printf("err: %v\n", err)
os.Exit(2)
}
log.Printf("type: %s, features: %d\n", resp.Type, len(resp.Features))
for i, feature := range resp.Features {
log.Printf("feature %d: type %s\n", i, feature.Type)
log.Printf("\tgeometry %s (%f %f)\n", feature.Geometry.Type, feature.Geometry.Coordinates[0], feature.Geometry.Coordinates[1])
log.Printf("\tproperties %s\n", feature.Properties.Layer)
}
2026-02-14 15:40:12 +00:00
}