2024-09-25 20:09:12 +00:00
|
|
|
package nominatim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-11-05 17:49:00 +00:00
|
|
|
"errors"
|
2024-09-25 20:09:12 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2024-09-29 19:49:23 +00:00
|
|
|
"net/url"
|
2024-09-29 19:20:46 +00:00
|
|
|
"reflect"
|
2024-09-25 20:09:12 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Nominatim struct {
|
|
|
|
siteName string
|
|
|
|
}
|
|
|
|
|
2024-11-05 17:49:00 +00:00
|
|
|
var (
|
|
|
|
ErrorMissingSiteName = errors.New("siteName must be defined")
|
|
|
|
)
|
|
|
|
|
2024-09-25 20:09:12 +00:00
|
|
|
func New(siteName string) (*Nominatim, error) {
|
|
|
|
if siteName == "" {
|
2024-11-05 17:49:00 +00:00
|
|
|
return nil, ErrorMissingSiteName
|
2024-09-25 20:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Nominatim{
|
|
|
|
siteName: siteName,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-05 21:36:31 +00:00
|
|
|
// Search for coordinates based on q
|
|
|
|
// API endpoint: https://nominatim.openstreetmap.org/search
|
2024-09-25 20:09:12 +00:00
|
|
|
func (n *Nominatim) Search(q string) (*SearchResults, error) {
|
2024-09-29 19:49:23 +00:00
|
|
|
url := fmt.Sprintf("https://nominatim.openstreetmap.org/search?q=%s&format=jsonv2", url.QueryEscape(q))
|
2024-09-25 20:09:12 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("User-Agent", n.siteName)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("%s", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := SearchResults{}
|
|
|
|
err = json.Unmarshal(body, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
2024-09-29 19:20:46 +00:00
|
|
|
}
|
|
|
|
|
2024-10-05 21:36:31 +00:00
|
|
|
// Reverse search for location based on lat and lon
|
|
|
|
// API endpoint: https://nominatim.openstreetmap.org/reverse
|
2024-09-29 19:20:46 +00:00
|
|
|
func (n *Nominatim) Reverse(lat, lon float64) (*ReverseResult, error) {
|
|
|
|
url := fmt.Sprintf("https://nominatim.openstreetmap.org/reverse?lat=%.16f&lon=%.16f&format=jsonv2", lat, lon)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("User-Agent", n.siteName)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("%s", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := ReverseResult{}
|
|
|
|
err = json.Unmarshal(body, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-25 20:09:12 +00:00
|
|
|
|
2024-09-29 19:20:46 +00:00
|
|
|
if allFieldsNil(result) {
|
2024-11-05 17:49:00 +00:00
|
|
|
return nil, fmt.Errorf("no result")
|
2024-09-29 19:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
2024-09-25 20:09:12 +00:00
|
|
|
}
|
|
|
|
|
2024-09-29 17:50:45 +00:00
|
|
|
type LookupResult struct {
|
|
|
|
Coordinates
|
|
|
|
Location string `json:"location"`
|
|
|
|
}
|
|
|
|
|
2024-10-05 21:36:31 +00:00
|
|
|
// Lookup location coordinates based on q
|
2024-09-29 17:50:45 +00:00
|
|
|
func (n *Nominatim) Lookup(q string) (*LookupResult, error) {
|
2024-09-25 20:09:12 +00:00
|
|
|
r, err := n.Search(q)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(*r) == 0 {
|
|
|
|
return nil, fmt.Errorf("no result")
|
|
|
|
}
|
|
|
|
|
|
|
|
firstResult := (*r)[0]
|
|
|
|
lon, err := strconv.ParseFloat(*firstResult.Longitude, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lat, err := strconv.ParseFloat(*firstResult.Latitude, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-09-29 17:50:45 +00:00
|
|
|
return &LookupResult{
|
|
|
|
Location: *firstResult.Name,
|
|
|
|
Coordinates: Coordinates{
|
|
|
|
Latitude: lat,
|
|
|
|
Longitude: lon,
|
|
|
|
},
|
2024-09-25 20:09:12 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2024-09-29 19:20:46 +00:00
|
|
|
|
2024-10-05 21:36:31 +00:00
|
|
|
// Lookup location name based on lat and lon
|
2024-09-29 19:20:46 +00:00
|
|
|
func (n *Nominatim) ReverseLookup(lat, lon float64) (*LookupResult, error) {
|
|
|
|
r, err := n.Reverse(lat, lon)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LookupResult{
|
|
|
|
Location: *r.Name,
|
|
|
|
Coordinates: Coordinates{
|
|
|
|
Latitude: lat,
|
|
|
|
Longitude: lon,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-05 21:36:31 +00:00
|
|
|
// Check if all fields on s is nil
|
2024-09-29 19:20:46 +00:00
|
|
|
func allFieldsNil(s interface{}) bool {
|
|
|
|
v := reflect.ValueOf(s)
|
|
|
|
|
|
|
|
// Check if the passed value is a struct
|
|
|
|
if v.Kind() != reflect.Struct {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through all fields in the struct
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
field := v.Field(i)
|
|
|
|
|
|
|
|
// Check if the field is a pointer and is nil
|
|
|
|
if field.Kind() == reflect.Ptr && !field.IsNil() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|