summaryrefslogtreecommitdiff
path: root/pkg/geo/geo.go
blob: 0c2e9eefde4d65528dd5bad75c65309bea32b76c (plain) (blame)
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
68
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
}