-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpx-nominatim.go
67 lines (56 loc) · 1.49 KB
/
gpx-nominatim.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/ptrv/go-gpx"
)
// Nominatim ...
type Nominatim struct {
Address struct {
Country string `json:"country"`
CountryCode string `json:"country_code"`
County string `json:"county"`
Road string `json:"road"`
State string `json:"state"`
Suburb string `json:"suburb"`
Village string `json:"village"`
} `json:"address"`
Boundingbox []string `json:"boundingbox"`
DisplayName string `json:"display_name"`
Lat string `json:"lat"`
Licence string `json:"licence"`
Lon string `json:"lon"`
OsmID string `json:"osm_id"`
OsmType string `json:"osm_type"`
PlaceID string `json:"place_id"`
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) != 1 {
fmt.Println("Please provide a GPX file path!")
return
}
gpxfile := args[0]
tracks, err := gpx.ParseFile(gpxfile)
if err != nil {
fmt.Println("Error opening gpx file: ", err)
return
}
lat := tracks.Tracks[0].Segments[0].Waypoints[0].Lat
lon := tracks.Tracks[0].Segments[0].Waypoints[0].Lon
reverseAPI := fmt.Sprintf("https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=%f&lon=%f&zoom=18&addressdetails=1", lat, lon)
resp, err := http.Get(reverseAPI)
if err != nil {
log.Fatal("error")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var place Nominatim
json.Unmarshal(body, &place)
fmt.Println(place.DisplayName)
}