feat: Add reverse nominatim

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-09-29 21:20:46 +02:00
parent 1e6eaa4b4d
commit 91ab81759f
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
3 changed files with 93 additions and 0 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"reflect"
"strconv" "strconv"
) )
@ -54,7 +55,44 @@ func (n *Nominatim) Search(q string) (*SearchResults, error) {
} }
return &result, nil return &result, nil
}
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
}
if allFieldsNil(result) {
return nil, fmt.Errorf("No result")
}
return &result, nil
} }
type LookupResult struct { type LookupResult struct {
@ -92,3 +130,39 @@ func (n *Nominatim) Lookup(q string) (*LookupResult, error) {
}, },
}, nil }, nil
} }
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
}
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
}

View file

@ -55,3 +55,21 @@ func TestLookup(t *testing.T) {
_, err = c.Lookup("") _, err = c.Lookup("")
assert.Error(err) assert.Error(err)
} }
func TestReverse(t *testing.T) {
assert := assert.New(t)
c, err := New("my siteName")
assert.NoError(err)
r, err := c.Reverse(60.3943055, 5.3259192)
assert.NoError(err)
assert.NotEmpty(r)
assert.Equal("5.325973", *r.Longitude)
assert.Equal("60.394487", *r.Latitude)
assert.Equal("", *r.Name)
_, err = c.Reverse(-10000, -10000)
assert.Error(err)
}

View file

@ -32,6 +32,7 @@ type SearchResult struct {
} }
type SearchResults = []SearchResult type SearchResults = []SearchResult
type ReverseResult = SearchResult
type Coordinates struct { type Coordinates struct {
Latitude float64 `json:"latitude"` Latitude float64 `json:"latitude"`