-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
106 lines (91 loc) · 2.23 KB
/
query.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package panodata
import (
"math"
"net/url"
"strconv"
)
// Set is the target photo set for a query.
type Set byte
const (
SetFull Set = iota // Public photos
SetPublic // All photos
)
// String converts a Set to its string representation in the API.
func (set Set) String() string {
switch set {
case SetFull:
return "full"
case SetPublic:
return "public"
}
return ""
}
// Size is the photo size Panoramio will return links to.
type Size byte
const (
SizeOriginal Size = iota
SizeMedium
SizeSmall
SizeThumbnail
SizeSquare
SizeMiniSquare
)
// String convert a Size to its string representation in the API.
func (size Size) String() string {
switch size {
case SizeOriginal:
return "original"
case SizeMedium:
return "medium"
case SizeSmall:
return "small"
case SizeThumbnail:
return "thumbnail"
case SizeSquare:
return "square"
case SizeMiniSquare:
return "mini_square"
}
return ""
}
// A Query into Panoramio's database.
type Query struct {
MinLat, MaxLat float64
MinLon, MaxLon float64
Size Size
Set Set
MapFilter bool
}
const rEarth = 6371
// QueryRadius returns a Query that looks for images less than radius Km from
// lat, lon. This function uses an approximation that is only valid for small
// values of radius.
func QueryRadius(lat, lon float64, radius float64) Query {
x := radius / (rEarth * math.Sqrt(2))
dLat, dLon := math.Abs(x), math.Abs(x/math.Cos(lat))
return Query{
MinLat: lat - dLat,
MaxLat: lat + dLat,
MinLon: lon - dLon,
MaxLon: lon + dLon,
}
}
// URL to the Panoramio API page that performs the query.
func (query Query) URL() string {
// Create query
params := url.Values{}
params.Add("set", query.Set.String())
params.Add("size", query.Size.String())
params.Add("minx", strconv.FormatFloat(query.MinLon, 'f', -1, 64))
params.Add("miny", strconv.FormatFloat(query.MinLat, 'f', -1, 64))
params.Add("maxx", strconv.FormatFloat(query.MaxLon, 'f', -1, 64))
params.Add("maxy", strconv.FormatFloat(query.MaxLat, 'f', -1, 64))
params.Add("mapfilter", strconv.FormatBool(query.MapFilter))
url := url.URL{
Scheme: "http",
Host: "www.panoramio.com",
Path: "map/get_panoramas.php",
RawQuery: params.Encode(),
}
return url.String()
}