/
Circle.go
165 lines (137 loc) · 3.72 KB
/
Circle.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package latlong
import (
"math"
"github.com/golang/geo/s1"
"github.com/golang/geo/s2"
)
// Circle is Circle
type Circle struct {
Point
s1.ChordAngle
}
// Type returns this type
func (Circle) Type() string {
return "Circle"
}
// NewCircle is constuctor for Circle
func NewCircle(latlng Point, km Km) *Circle {
circle := Circle{
Point: latlng,
ChordAngle: s1.ChordAngleFromAngle(km.EarthAngle()),
}
return &circle
}
// NewPointCircle is constructor for Circle with radius = prec
func NewPointCircle(latlng Point) *Circle {
latprecchordangle := s1.ChordAngleFromAngle(latlng.lat.PrecS1Angle())
lngprecchordangle := s1.ChordAngleFromAngle(latlng.lng.PrecS1Angle()) * s1.ChordAngle(math.Abs(math.Cos(float64(latlng.lat.PrecS1Angle()))))
var precchordangle s1.ChordAngle
if latprecchordangle > lngprecchordangle {
precchordangle = latprecchordangle
} else {
precchordangle = lngprecchordangle
}
circle := Circle{
Point: latlng,
ChordAngle: precchordangle,
}
return &circle
}
// NewEmptyCircle is constructor for Circle with empty.
func NewEmptyCircle() *Circle {
circle := Circle{
ChordAngle: s1.NegativeChordAngle,
}
return &circle
}
// S2Cap is getter for s2.Cap.
func (c Circle) S2Cap() s2.Cap {
return s2.CapFromCenterChordAngle(s2.PointFromLatLng(c.Point.S2LatLng()), c.ChordAngle)
}
// S2Region is getter for s2.Region.
func (c Circle) S2Region() s2.Region {
return c.S2Cap()
}
// CapBound is for s2.Region interface.
func (c *Circle) CapBound() s2.Cap {
return c.S2Cap().CapBound()
}
// RectBound is for s2.Region interface.
func (c *Circle) RectBound() s2.Rect {
return c.S2Cap().RectBound()
}
// ContainsCell is for s2.Region interface.
func (c *Circle) ContainsCell(cell s2.Cell) bool {
return c.S2Cap().ContainsCell(cell)
}
// IntersectsCell is for s2.Region interface.
func (c *Circle) IntersectsCell(cell s2.Cell) bool {
return c.S2Cap().IntersectsCell(cell)
}
// ContainsPoint is for s2.Region interface.
func (c *Circle) ContainsPoint(p s2.Point) bool {
return c.S2Cap().ContainsPoint(p)
}
// CellUnionBound is for s2.Region interface.
func (c *Circle) CellUnionBound() []s2.CellID {
return c.S2Cap().CellUnionBound()
}
// Radiusp is un-used
func (c Circle) Radiusp() *float64 {
r := float64(c.Radius())
return &r
}
// Radius returns radius of circle.
func (c Circle) Radius() Km {
return EarthArcFromChordAngle(c.ChordAngle)
}
func (c Circle) String() string {
return c.Point.String() + "/" + c.Radius().String()
}
// S2Point is Center LatLng
func (c Circle) S2Point() s2.Point {
return c.Point.S2Point()
}
// S2Loop is circumference loop.
// div is number of vertices.
func (c *Circle) S2Loop(div int) (loop *s2.Loop) {
return s2.RegularLoop(s2.PointFromLatLng(c.Point.S2LatLng()), c.Angle(), div)
}
// S2LatLngs is circumference loop by []s2.LatLng.
// radian is one vertex degree.
func (c *Circle) S2LatLngs(div int) (lls []s2.LatLng) {
vs := c.S2Loop(div).Vertices()
lls = make([]s2.LatLng, len(vs))
for i := range vs {
lls[i] = s2.LatLngFromPoint(vs[i])
}
return
}
// MultiPoint is circumference loop by MultiPoint
// radian is one vertex degree.
func (c *Circle) MultiPoint(div int) (lls MultiPoint) {
vs := c.S2Loop(div).Vertices()
lls = make(MultiPoint, len(vs))
for i := range vs {
lls[i] = NewPointFromS2Point(vs[i])
}
return
}
// Equal return bool
func (c Circle) Equal(c1 Geometry) bool {
return c == c1
}
// NewGeoJSONGeometry returns GeoJSONGeometry.
func (c Circle) NewGeoJSONGeometry() *GeoJSONGeometry {
var g GeoJSONGeometry
g.geo = c
return &g
}
// NewGeoJSONFeature returns GeoJSONFeature.
func (c Circle) NewGeoJSONFeature(property interface{}) *GeoJSONFeature {
var g GeoJSONFeature
g.Type = "Feature"
g.Geometry = c.NewGeoJSONGeometry()
g.Property = property
return &g
}