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
}