Skip to content

Commit

Permalink
Migrate from Bing Maps to Azure Maps
Browse files Browse the repository at this point in the history
- Previously this used Bing Maps `Find a Location by Query`
- Now it is using Azure Maps `Get Geocoding`.
- Changed config from `bing_token` to `azure_shared_key`
- Extract lat, lon, name from GeoJSON response
- lat and lon are in reverse order in GeoJSON to the order Bing Maps returned them
  • Loading branch information
tedpearson committed May 23, 2024
1 parent 673a296 commit c53df38
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Config struct {
PrecipProbability float64 `yaml:"precip_probability"`
HttpCacheDir string `yaml:"http_cache_dir"`
OverwriteData bool `yaml:"overwrite_data"`
BingToken string `yaml:"bing_token"`
AzureSharedKey string `yaml:"azure_shared_key"`
ServerConfig ServerConfig `yaml:"server"`
AdHocCacheEntries int `yaml:"ad_hoc_cache_entries"`
Sources struct {
Expand Down
2 changes: 1 addition & 1 deletion forecastmetrics.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ http_cache_dir: /var/lib/forecastmetrics/cache
# with influxdb but not with VictoriaMetrics.
overwrite_data: false
# Bing Maps Location API token to provide location lookup for adhoc forecasts, if enabled
bing_token: your_token_here
azure_shared_key: your_token_here
server:
# port to run http server on for adhoc forecasts
# set to 0 to disable the http server.
Expand Down
19 changes: 10 additions & 9 deletions location.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var latLonRe = regexp.MustCompile(`(\d+\.\d+),\s*(\d+\.\d+)`)

// LocationService parses strings into Location, using the Bing Maps Locations API.
type LocationService struct {
BingToken string
AzureSharedKey string
}

// ParseLocation turns strings into Locations
Expand Down Expand Up @@ -56,9 +56,10 @@ func (l LocationService) ParseLocation(s string) (*Location, error) {
// if it is an empty string.
func (l LocationService) lookup(s string, location *Location) error {
q := url.Values{}
q.Add("q", s)
q.Add("key", l.BingToken)
resp, err := http.Get("http://dev.virtualearth.net/REST/v1/Locations?" + q.Encode())
q.Add("api-version", "2023-06-01")
q.Add("query", s)
q.Add("subscription-key", l.AzureSharedKey)
resp, err := http.Get("https://atlas.microsoft.com/geocode?" + q.Encode())
if err != nil {
fmt.Printf("Failed to look up %s\n", s)
return err
Expand All @@ -74,23 +75,23 @@ func (l LocationService) lookup(s string, location *Location) error {
fmt.Printf("Failed to parse json %s\n", buf.String())
return err
}
record := val.Get("resourceSets", "0", "resources", "0")
coords := record.GetArray("point", "coordinates")
record := val.Get("features", "0")
coords := record.GetArray("geometry", "coordinates")
if record == nil || coords == nil {
return fmt.Errorf("failed to look up location '%s'", s)
}
latF, err := coords[0].Float64()
latF, err := coords[1].Float64()
if err != nil {
fmt.Printf("Failed to get coordinates from json %s\n", buf.String())
return err
}
lonF, err := coords[1].Float64()
lonF, err := coords[0].Float64()
if err != nil {
fmt.Printf("Failed to get coordinates from json %s\n", buf.String())
return err
}
if location.Name == "" {
name := record.GetStringBytes("name")
name := record.GetStringBytes("properties", "address", "formattedAddress")
if name == nil {
fmt.Printf("Failed to get name from json %s\n", buf.String())
return err
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
configService := NewConfigService(*configFile, *locationsFile)
config := configService.Config
locationService := LocationService{
BingToken: config.BingToken,
AzureSharedKey: config.AzureSharedKey,
}
forecasters := MakeForecasters(config.Sources.Enabled, config.HttpCacheDir, config.Sources.VisualCrossing.Key)
c := influxdb2.NewClient(config.InfluxDB.Host, config.InfluxDB.AuthToken)
Expand Down

0 comments on commit c53df38

Please sign in to comment.