diff options
Diffstat (limited to 'pkg/geo')
-rw-r--r-- | pkg/geo/geo.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/pkg/geo/geo.go b/pkg/geo/geo.go new file mode 100644 index 0000000..0c2e9ee --- /dev/null +++ b/pkg/geo/geo.go @@ -0,0 +1,69 @@ +package geo + +import ( + "strconv" + "strings" + + "github.com/serjvanilla/go-overpass" +) + +var queries = map[string]string{ + // "[out:json];relation(1673881);>>;out body;" + "shops": `[out:json][timeout:25]; + ( + nwr["shop"="mall"](around:1000, {{lat}},{{lon}}); + nwr["shop"="supermarket"](around:1000, {{lat}},{{lon}}); + nwr["shop"="convenience"](around:1000, {{lat}},{{lon}}); + ); + out body; + >; + out skel qt;`, + "farma": `[out:json][timeout:25]; + ( + nwr["amenity"="pharmacy"](around:1000, {{lat}},{{lon}}); + ); + out body; + >; + out skel qt;`, +} + +func Search(lat, lng float64, search string) ([]Point, error) { + client := overpass.New() + query := queries[search] + query = strings.ReplaceAll(query, "{{lat}}", strconv.FormatFloat(lat, 'g', 5, 64)) + query = strings.ReplaceAll(query, "{{lon}}", strconv.FormatFloat(lng, 'g', 5, 64)) + + result, err := client.Query(query) + if err != nil { + return nil, err + } + + ret := make([]Point, 0, len(result.Nodes)+len(result.Ways)) + for _, n := range result.Nodes { + if n.Tags["name"] == "" { + continue + } + ret = append(ret, Point{ + Lat: n.Lat, + Lon: n.Lon, + Name: n.Tags["name"], + }) + } + for _, n := range result.Ways { + if n.Tags["name"] == "" { + continue + } + ret = append(ret, Point{ + Lat: n.Nodes[0].Lat, + Lon: n.Nodes[0].Lon, + Name: n.Tags["name"], + }) + } + return ret, nil +} + +type Point struct { + Lat float64 + Lon float64 + Name string +} |