Add bulk geocoding by goroutine worker

This is nearly as fast, and doesn't require the corp-only license from
stadia map which is 10x the cost.
This commit is contained in:
Eli Ribble 2026-02-25 14:57:28 +00:00
parent ca88a0eaab
commit 8feabbc489
No known key found for this signature in database
3 changed files with 211 additions and 83 deletions

View file

@ -10,23 +10,83 @@ import (
)
func main() {
key := os.Getenv("STADIA_MAPS_API_KEY")
if key == "" {
log.Println("stadia maps api key is empty")
// 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")
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 != nil && focusLng == nil {
log.Println("Error: you must specify both focus-lat and focus-lng together, not just focus-lat")
flag.Usage()
os.Exit(1)
}
if focusLat == nil && focusLng != nil {
log.Println("Error: you must specify both focus-lat and focus-lng together, not just focus-lng")
flag.Usage()
os.Exit(1)
}
if (boundaryRectMaxLat != nil ||
boundaryRectMinLat != nil ||
boundaryRectMaxLon != nil ||
boundaryRectMinLon != nil) && (boundaryRectMaxLat == nil ||
boundaryRectMinLat == nil ||
boundaryRectMaxLon == nil ||
boundaryRectMinLon == nil) {
log.Println("If you specify one of boundary-rect you need to specify them all")
os.Exit(1)
}
key := os.Getenv("STADIA_MAPS_API_KEY")
if key == "" {
log.Println("STADIA_MAPS_API_KEY is empty")
os.Exit(1)
}
client := stadia.NewStadiaMaps(key)
resp, err := client.StructuredGeocode(stadia.StructuredGeocodeRequest{
Address: strPtr("12932 Ave 404"),
PostalCode: strPtr("93615"),
})
ctx := context.Background()
req := stadia.StructuredGeocodeRequest{
Address: address,
PostalCode: postalCode,
}
if focusLat != nil && focusLng != nil {
req.FocusPointLat = focusLat
req.FocusPointLng = focusLng
}
if boundaryRectMaxLat != nil {
req.BoundaryRectMaxLat = boundaryRectMaxLat
req.BoundaryRectMinLat = boundaryRectMinLat
req.BoundaryRectMaxLon = boundaryRectMaxLon
req.BoundaryRectMinLon = boundaryRectMinLon
}
resp, err := client.StructuredGeocode(ctx, req)
if err != nil {
log.Printf("err: %v\n", err)
os.Exit(2)
}
log.Printf("type: %s", resp.Type)
}
func strPtr(s string) *string {
return &s
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\n", feature.Geometry.Type)
log.Printf("\tproperties %s\n", feature.Properties.Layer)
}
}

View file

@ -20,14 +20,23 @@ type StructuredGeocodeRequest struct {
PostalCode *string `url:"postalcode,omitempty" json:"postalcode,omitempty"`
Country *string `url:"country,omitempty" json:"country,omitempty"`
// Focus point
FocusPoint *FocusPoint `url:",omitempty" json:",omitempty"`
// Boundary circle parameters
BoundaryCircleLat *float64 `url:"boundary.circle.lat,omitempty"`
BoundaryCircleLon *float64 `url:"boundary.circle.lon,omitempty"`
BoundaryCircleRadius *float64 `url:"boundary.circle.radius,omitempty"`
BoundaryCountry []string `url:"boundary.country,omitempty,comma" json:"boundary.country,omitempty,comma"`
BoundaryGid *string `url:"boundary.gid,omitempty" json:"boundary.gid,omitempty"`
// Boundary parameters
BoundaryRect *BoundaryRect `url:",omitempty" json:",omitempty"`
BoundaryCircle *BoundaryCircle `url:",omitempty" json:",omitempty"`
BoundaryCountry []string `url:"boundary.country,omitempty,comma" json:"boundary.country,omitempty,comma"`
BoundaryGid *string `url:"boundary.gid,omitempty" json:"boundary.gid,omitempty"`
BoundaryRectMaxLat *float64 `url:"boundary.rect.max_lat,omitempty"`
BoundaryRectMinLat *float64 `url:"boundary.rect.min_lat,omitempty"`
BoundaryRectMaxLon *float64 `url:"boundary.rect.max_lon,omitempty"`
BoundaryRectMinLon *float64 `url:"boundary.rect.min_lon,omitempty"`
// Focus point
FocusPointLat *float64 `url:"focus.point.lat,omitempty" json:",omitempty"`
FocusPointLng *float64 `url:"focus.point.lon,omitempty" json:",omitempty"`
// Other parameters
Layers []string `url:"layers,omitempty,comma" json:"layers,omitempty,comma"`