Add ability to query for a place by gid(s)
This commit is contained in:
parent
2d5dca3fb5
commit
380b41f695
3 changed files with 104 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -11,6 +11,7 @@ postgrid/cmd/send-pdf/send-pdf
|
||||||
result
|
result
|
||||||
stadia/cmd/bulk-geocode/bulk-geocode
|
stadia/cmd/bulk-geocode/bulk-geocode
|
||||||
stadia/cmd/geocode-autocomplete/geocode-autocomplete
|
stadia/cmd/geocode-autocomplete/geocode-autocomplete
|
||||||
|
stadia/cmd/geocode-bygid/geocode-bygid
|
||||||
stadia/cmd/reverse-geocode/reverse-geocode
|
stadia/cmd/reverse-geocode/reverse-geocode
|
||||||
stadia/cmd/structured-geocode/structured-geocode
|
stadia/cmd/structured-geocode/structured-geocode
|
||||||
static/gen/
|
static/gen/
|
||||||
|
|
|
||||||
62
stadia/cmd/geocode-bygid/main.go
Normal file
62
stadia/cmd/geocode-bygid/main.go
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/Gleipnir-Technology/nidus-sync/stadia"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Define command-line flags
|
||||||
|
gid := flag.String("gid", "", "The GID to query")
|
||||||
|
|
||||||
|
// Parse the flags
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
// Validate required arguments
|
||||||
|
if *gid == "" {
|
||||||
|
log.Println("Error: -gid is required")
|
||||||
|
flag.Usage()
|
||||||
|
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)
|
||||||
|
ctx := context.Background()
|
||||||
|
req := stadia.RequestGeocodeByGID{
|
||||||
|
GIDs: []string{*gid},
|
||||||
|
}
|
||||||
|
resp, err := client.GeocodeByGID(ctx, req)
|
||||||
|
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)
|
||||||
|
if feature.Geometry == nil {
|
||||||
|
log.Printf("\tno geometry")
|
||||||
|
} else {
|
||||||
|
log.Printf("\tgeometry %s\n", feature.Geometry.Type) //, feature.Geometry.Coordinates[0], feature.Geometry.Coordinates[1])
|
||||||
|
}
|
||||||
|
log.Printf("\tproperties %s\n", feature.Properties.Layer)
|
||||||
|
switch feature.Properties.Layer {
|
||||||
|
case "address":
|
||||||
|
log.Printf("\t\t%s", feature.Properties.Name)
|
||||||
|
if feature.Properties.CoarseLocation != nil {
|
||||||
|
log.Printf("\t\t%s", *feature.Properties.CoarseLocation)
|
||||||
|
}
|
||||||
|
log.Printf("\t\t%s", feature.Properties.Precision)
|
||||||
|
log.Printf("\t\t%s", feature.Properties.Layer)
|
||||||
|
log.Printf("\t\t%s", feature.Properties.GID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
41
stadia/geocode_bygid.go
Normal file
41
stadia/geocode_bygid.go
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
package stadia
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/google/go-querystring/query"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RequestGeocodeByGID struct {
|
||||||
|
GIDs []string `url:"ids,comma"`
|
||||||
|
|
||||||
|
// Other parameters
|
||||||
|
Lang *string `url:"lang,omitempty" json:"lang,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StadiaMaps) GeocodeByGID(ctx context.Context, req RequestGeocodeByGID) (*GeocodeResponse, error) {
|
||||||
|
// https://docs.stadiamaps.com/geocoding-search-autocomplete/place-details/
|
||||||
|
var result GeocodeResponse
|
||||||
|
|
||||||
|
query, err := query.Values(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("structured geocode query: %w", err)
|
||||||
|
}
|
||||||
|
//var api_error Error
|
||||||
|
resp, err := s.client.R().
|
||||||
|
SetQueryParamsFromValues(query).
|
||||||
|
SetContext(ctx).
|
||||||
|
SetResult(&result).
|
||||||
|
SetPathParam("urlBase", s.urlBase).
|
||||||
|
SetQueryParam("api_key", s.APIKey).
|
||||||
|
Get("https://{urlBase}/geocoding/v2/place_details")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("autocomplete get: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !resp.IsSuccess() {
|
||||||
|
return nil, parseError(resp)
|
||||||
|
}
|
||||||
|
return &result, nil
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue