all repos — paprika @ 014e7ec5f807bba7c2d6ccb9b6fcb699b0f7e185

go rewrite of taigabot

plugins/location/geocode.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
package location

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type LocationInfo struct {
	Features []struct {
		Properties struct {
			Geocoding struct {
				Label string `json:"label"`
			} `json:"geocoding"`
		} `json:"properties"`
		Geometry struct {
			Coordinates [2]float64 `json:"coordinates"`
		} `json:"geometry"`
	} `json:"features"`
}

// Get location data (lat,lon) from a given query.
func GetLocationInfo(query string) (*LocationInfo, error) {
	url := "https://nominatim.openstreetmap.org/search?q=%s&format=geocodejson&limit=1"
	r, err := http.Get(fmt.Sprintf(url, query))
	if err != nil {
		return nil, err
	}

	li := LocationInfo{}
	json.NewDecoder(r.Body).Decode(&li)
	defer r.Body.Close()

	return &li, nil
}