route_boxer 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/Rakefile +1 -0
- data/lib/route_boxer.rb +16 -0
- data/lib/route_boxer/core.rb +312 -0
- data/lib/route_boxer/lat_lng.rb +81 -0
- data/lib/route_boxer/lat_lng_bounds.rb +105 -0
- data/lib/route_boxer/lat_lng_collection.rb +24 -0
- data/lib/route_boxer/version.rb +3 -0
- data/route_boxer.gemspec +23 -0
- metadata +84 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: c13179907ea808fe1843c087608df116f773eb6d
|
|
4
|
+
data.tar.gz: 4c138435eff6ced2d30ac2fd235242fc487fb3ae
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 23a097f08acd1fe0553b0ee4ccda115300a95c0800678c99d2144cb0cab8c442a5d4b3b13e3dff1c37c487b9d826003c8684b28537a2bd51e6959db052e527e4
|
|
7
|
+
data.tar.gz: 2bb58a402165025c47c5cecc0a28beaf6f68f1757c7c8fd6b6f28cc014dbd724fa0d0b225a7428b6b533dd86a0ef3f7ad31a75258faa0c060121fb45f3a93aaf
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2014 Sebastian Badura
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# RouteBoxer
|
|
2
|
+
|
|
3
|
+
Ruby implementation of Google RouteBoxer
|
|
4
|
+
|
|
5
|
+
http://google-maps-utility-library-v3.googlecode.com/svn/trunk/routeboxer/src/RouteBoxer.js
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Add this line to your application's Gemfile:
|
|
11
|
+
|
|
12
|
+
gem 'route_boxer'
|
|
13
|
+
|
|
14
|
+
And then execute:
|
|
15
|
+
|
|
16
|
+
$ bundle
|
|
17
|
+
|
|
18
|
+
Or install it yourself as:
|
|
19
|
+
|
|
20
|
+
$ gem install route_boxer
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
1. Get route with Google Directions API:
|
|
25
|
+
```ruby
|
|
26
|
+
results = JSON.parse(open("http://maps.googleapis.com/maps/api/directions/json?alternative=true&destination=Warsaw&language=en&mode=driving&origin=Cracow&sensor=false").read)
|
|
27
|
+
```
|
|
28
|
+
2. Decode response to LatLng points with 'polylines' gem:
|
|
29
|
+
```ruby
|
|
30
|
+
points = Polylines::Decoder.decode_polyline(result["routes"][0]["overview_polyline"]["points"])
|
|
31
|
+
```
|
|
32
|
+
3. Parse points to RouteBoxer::LatLng objects:
|
|
33
|
+
```ruby
|
|
34
|
+
points = points.map { |p| RouteBoxer::LatLng.new(p[0], p[1])}
|
|
35
|
+
```
|
|
36
|
+
4. Use RouteBoxer to get 'boxed' route:
|
|
37
|
+
```ruby
|
|
38
|
+
RouteBoxer::Core.new.box(points, 10)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Contributing
|
|
42
|
+
|
|
43
|
+
1. Fork it ( http://github.com/<my-github-username>/route_boxer/fork )
|
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/route_boxer.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require "route_boxer/version"
|
|
2
|
+
|
|
3
|
+
require "route_boxer/lat_lng"
|
|
4
|
+
require "route_boxer/lat_lng_collection"
|
|
5
|
+
require "route_boxer/lat_lng_bounds"
|
|
6
|
+
require "route_boxer/core"
|
|
7
|
+
|
|
8
|
+
module RouteBoxer
|
|
9
|
+
def deg2rad(deg)
|
|
10
|
+
deg * Math::PI / 180
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def rad2deg(rad)
|
|
14
|
+
rad * 180 / Math::PI
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
module RouteBoxer
|
|
2
|
+
class Core
|
|
3
|
+
include RouteBoxer
|
|
4
|
+
R = 6371
|
|
5
|
+
|
|
6
|
+
attr_accessor :grid, :lat_grid, :lng_grid, :boxes_x, :boxes_y
|
|
7
|
+
|
|
8
|
+
def initialize
|
|
9
|
+
@lat_grid = []
|
|
10
|
+
@lng_grid = []
|
|
11
|
+
@boxes_x = []
|
|
12
|
+
@boxes_y = []
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def box(collection, range)
|
|
16
|
+
vertices = collection.to_a
|
|
17
|
+
@grid = build_grid(vertices, range)
|
|
18
|
+
|
|
19
|
+
find_intersecting_cells(vertices)
|
|
20
|
+
merge_intersecting_cells
|
|
21
|
+
|
|
22
|
+
return (@boxes_x.length <= @boxes_y.length ? @boxes_x : @boxes_y)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def build_grid(vertices, range)
|
|
28
|
+
route_bounds = RouteBoxer::LatLngBounds.new
|
|
29
|
+
vertices.each do |v|
|
|
30
|
+
route_bounds.extend(v)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
route_bounds_center = route_bounds.get_center
|
|
34
|
+
@lat_grid << route_bounds_center.lat
|
|
35
|
+
@lat_grid << route_bounds_center.rhumb_destination_point(0, range, R).lat
|
|
36
|
+
|
|
37
|
+
i = 2
|
|
38
|
+
while @lat_grid[i - 2] < route_bounds.get_north_east.lat
|
|
39
|
+
@lat_grid << route_bounds_center.rhumb_destination_point(0, range * i, R).lat
|
|
40
|
+
i += 1
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
i = 1
|
|
44
|
+
while @lat_grid[1] > route_bounds.get_south_west.lat
|
|
45
|
+
@lat_grid.unshift(route_bounds_center.rhumb_destination_point(180, range * i, R).lat)
|
|
46
|
+
i += 1
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
@lng_grid << route_bounds_center.lng
|
|
50
|
+
@lng_grid << route_bounds_center.rhumb_destination_point(90, range, R).lng
|
|
51
|
+
|
|
52
|
+
i = 2
|
|
53
|
+
while @lng_grid[i - 2] < route_bounds.get_north_east.lng
|
|
54
|
+
@lng_grid << route_bounds_center.rhumb_destination_point(90, range * i, R).lng
|
|
55
|
+
i += 1
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
i = 1
|
|
59
|
+
while @lng_grid[1] > route_bounds.get_south_west.lng
|
|
60
|
+
@lng_grid.unshift(route_bounds_center.rhumb_destination_point(270, range * i, R).lng)
|
|
61
|
+
i += 1
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
lng_grid_length = @lng_grid.length
|
|
65
|
+
lat_grid_length = @lat_grid.length
|
|
66
|
+
|
|
67
|
+
grid = create_empty_array(lng_grid_length)
|
|
68
|
+
|
|
69
|
+
i = 0
|
|
70
|
+
while i < grid.length
|
|
71
|
+
grid[i] = create_empty_array(lat_grid_length)
|
|
72
|
+
i += 1
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
grid
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def create_empty_array(length)
|
|
79
|
+
[nil] * length
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def find_intersecting_cells(vertices)
|
|
83
|
+
hint_xy = get_cell_coords(vertices[0])
|
|
84
|
+
mark_cell(hint_xy)
|
|
85
|
+
|
|
86
|
+
i = 1
|
|
87
|
+
while i < vertices.length
|
|
88
|
+
grid_xy = get_grid_coords_from_hint(vertices[i], vertices[i - 1], hint_xy)
|
|
89
|
+
|
|
90
|
+
if grid_xy[0] == hint_xy[0] && grid_xy[1] == hint_xy[1]
|
|
91
|
+
i += 1
|
|
92
|
+
next
|
|
93
|
+
elsif (((hint_xy[0] - grid_xy[0]).abs == 1 && hint_xy[1] == grid_xy[1]) || (hint_xy[0] == grid_xy[0] && (hint_xy[1] - grid_xy[1]).abs == 1))
|
|
94
|
+
mark_cell(grid_xy)
|
|
95
|
+
else
|
|
96
|
+
get_grid_intersects(vertices[i - 1], vertices[i], hint_xy, grid_xy)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
hint_xy = grid_xy
|
|
100
|
+
i += 1
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def get_cell_coords(latlng)
|
|
105
|
+
x = 0
|
|
106
|
+
while @lng_grid[x] < latlng.lng
|
|
107
|
+
x += 1
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
y = 0
|
|
111
|
+
while @lat_grid[y] < latlng.lat
|
|
112
|
+
y += 1
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
[x - 1, y - 1]
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def get_grid_coords_from_hint(latlng, hint_latlng, hint)
|
|
119
|
+
x = nil
|
|
120
|
+
y = nil
|
|
121
|
+
|
|
122
|
+
if latlng.lng > hint_latlng.lng
|
|
123
|
+
x = hint[0]
|
|
124
|
+
while @lng_grid[x + 1] < latlng.lng
|
|
125
|
+
x += 1
|
|
126
|
+
end
|
|
127
|
+
else
|
|
128
|
+
x = hint[0]
|
|
129
|
+
while @lng_grid[x] > latlng.lng
|
|
130
|
+
x -= 1
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
if latlng.lat > hint_latlng.lat
|
|
135
|
+
y = hint[1]
|
|
136
|
+
while @lat_grid[y + 1] < latlng.lat
|
|
137
|
+
y += 1
|
|
138
|
+
end
|
|
139
|
+
else
|
|
140
|
+
y = hint[1]
|
|
141
|
+
while @lat_grid[y] > latlng.lat
|
|
142
|
+
y -= 1
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
[x, y]
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def get_grid_intersects(start, stop, start_xy, stop_xy)
|
|
150
|
+
brng = start.rhumb_bearing_to(stop)
|
|
151
|
+
hint = start
|
|
152
|
+
hint_xy = start_xy
|
|
153
|
+
|
|
154
|
+
if stop.lat > start.lat
|
|
155
|
+
i = start_xy[1] + 1
|
|
156
|
+
while i <= stop_xy[1]
|
|
157
|
+
edge_point = get_grid_intersect(start, brng, @lat_grid[i])
|
|
158
|
+
edge_xy = get_grid_coords_from_hint(edge_point, hint, hint_xy)
|
|
159
|
+
fill_in_grid_squares(hint_xy[0], edge_xy[0], i - 1)
|
|
160
|
+
hint = edge_point
|
|
161
|
+
hint_xy = edge_xy
|
|
162
|
+
i += 1
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
fill_in_grid_squares(hint_xy[0], stop_xy[0], i - 1)
|
|
166
|
+
else
|
|
167
|
+
i = start_xy[1]
|
|
168
|
+
while i > stop_xy[1]
|
|
169
|
+
edge_point = get_grid_intersect(start, brng, @lat_grid[i])
|
|
170
|
+
edge_xy = get_grid_coords_from_hint(edge_point, hint, hint_xy)
|
|
171
|
+
fill_in_grid_squares(hint_xy[0], edge_xy[0], i)
|
|
172
|
+
hint = edge_point
|
|
173
|
+
hint_xy = edge_xy
|
|
174
|
+
i -= 1
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
fill_in_grid_squares(hint_xy[0], stop_xy[0], i)
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def get_grid_intersect(start, brng, grid_line_lat)
|
|
182
|
+
d = R * ((deg2rad(grid_line_lat) - deg2rad(start.lat))) / Math.cos(deg2rad(brng))
|
|
183
|
+
start.rhumb_destination_point(brng, d)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def fill_in_grid_squares(start_x, stop_x, y)
|
|
187
|
+
if start_x < stop_x
|
|
188
|
+
x = start_x
|
|
189
|
+
while x <= stop_x
|
|
190
|
+
mark_cell([x, y])
|
|
191
|
+
x += 1
|
|
192
|
+
end
|
|
193
|
+
else
|
|
194
|
+
x = start_x
|
|
195
|
+
while x >= stop_x
|
|
196
|
+
mark_cell([x, y])
|
|
197
|
+
x -= 1
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def mark_cell(cell)
|
|
203
|
+
x = cell[0]
|
|
204
|
+
y = cell[1]
|
|
205
|
+
@grid[x - 1][y - 1] = 1
|
|
206
|
+
@grid[x][y - 1] = 1
|
|
207
|
+
@grid[x + 1][y - 1] = 1
|
|
208
|
+
@grid[x - 1][y] = 1
|
|
209
|
+
@grid[x][y] = 1
|
|
210
|
+
@grid[x + 1][y] = 1
|
|
211
|
+
@grid[x - 1][y + 1] = 1
|
|
212
|
+
@grid[x][y + 1] = 1
|
|
213
|
+
@grid[x + 1][y + 1] = 1
|
|
214
|
+
@grid
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def merge_intersecting_cells
|
|
218
|
+
current_box = nil
|
|
219
|
+
|
|
220
|
+
y = 0
|
|
221
|
+
while y < @grid[0].length
|
|
222
|
+
x = 0
|
|
223
|
+
while x < @grid.length
|
|
224
|
+
if @grid[x][y]
|
|
225
|
+
box = get_cell_bounds([x, y])
|
|
226
|
+
if current_box
|
|
227
|
+
current_box.extend(box.get_north_east)
|
|
228
|
+
else
|
|
229
|
+
current_box = box
|
|
230
|
+
end
|
|
231
|
+
else
|
|
232
|
+
merge_boxes_y(current_box)
|
|
233
|
+
current_box = nil
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
x += 1
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
merge_boxes_y(current_box)
|
|
240
|
+
current_box = nil
|
|
241
|
+
|
|
242
|
+
y += 1
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
x = 0
|
|
246
|
+
|
|
247
|
+
while x < @grid.length
|
|
248
|
+
y = 0
|
|
249
|
+
while y < @grid[0].length
|
|
250
|
+
if @grid[x][y]
|
|
251
|
+
if current_box
|
|
252
|
+
box = get_cell_bounds([x, y])
|
|
253
|
+
current_box.extend(box.get_north_east)
|
|
254
|
+
else
|
|
255
|
+
current_box = get_cell_bounds([x, y])
|
|
256
|
+
end
|
|
257
|
+
else
|
|
258
|
+
merge_boxes_x(current_box)
|
|
259
|
+
current_box = nil
|
|
260
|
+
end
|
|
261
|
+
y += 1
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
merge_boxes_x(current_box)
|
|
265
|
+
current_box = nil
|
|
266
|
+
|
|
267
|
+
x += 1
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def merge_boxes_x(box)
|
|
272
|
+
if box != nil
|
|
273
|
+
i = 0
|
|
274
|
+
while i < @boxes_x.length
|
|
275
|
+
if @boxes_x[i].get_north_east.lng == box.get_south_west.lng &&
|
|
276
|
+
@boxes_x[i].south_west.lat == box.south_west.lat &&
|
|
277
|
+
@boxes_x[i].get_north_east.lat == box.get_north_east.lat
|
|
278
|
+
|
|
279
|
+
@boxes_x[i].extend(box.get_north_east)
|
|
280
|
+
return
|
|
281
|
+
end
|
|
282
|
+
i += 1
|
|
283
|
+
end
|
|
284
|
+
@boxes_x << box
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def merge_boxes_y(box)
|
|
289
|
+
if box != nil
|
|
290
|
+
i = 0
|
|
291
|
+
while i < @boxes_y.length
|
|
292
|
+
if @boxes_y[i].get_north_east.lat == box.get_south_west.lat &&
|
|
293
|
+
@boxes_y[i].get_south_west.lng == box.get_south_west.lng &&
|
|
294
|
+
@boxes_y[i].get_north_east.lng == box.get_north_east.lng
|
|
295
|
+
|
|
296
|
+
@boxes_y[i].extend(box.get_north_east)
|
|
297
|
+
return
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
i += 1
|
|
301
|
+
end
|
|
302
|
+
@boxes_y << box
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def get_cell_bounds(cell)
|
|
307
|
+
south_west = RouteBoxer::LatLng.new(@lat_grid[cell[1]], @lng_grid[cell[0]])
|
|
308
|
+
north_east = RouteBoxer::LatLng.new(@lat_grid[cell[1] + 1], @lng_grid[cell[0] + 1])
|
|
309
|
+
return RouteBoxer::LatLngBounds.new(south_west, north_east)
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
module RouteBoxer
|
|
2
|
+
class LatLng
|
|
3
|
+
include RouteBoxer
|
|
4
|
+
attr_accessor :lat, :lng
|
|
5
|
+
|
|
6
|
+
alias_method :latitude, :lat
|
|
7
|
+
alias_method :latitude=, :lat=
|
|
8
|
+
|
|
9
|
+
alias_method :longitude, :lng
|
|
10
|
+
alias_method :longitude=, :lng=
|
|
11
|
+
|
|
12
|
+
def initialize(lat = nil, lng = nil)
|
|
13
|
+
lat = lat.to_f if lat && !lat.is_a?(Numeric)
|
|
14
|
+
lng = lng.to_f if lng && !lng.is_a?(Numeric)
|
|
15
|
+
@lat = lat
|
|
16
|
+
@lng = lng
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def lat=(lat)
|
|
20
|
+
@lat = lat.to_f if lat
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def lng=(lng)
|
|
24
|
+
@lng = lng.to_f if lng
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def ll
|
|
28
|
+
"#{lat},#{lng}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_a
|
|
32
|
+
[lat, lng]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def equals(other)
|
|
36
|
+
lat == other.lat && lng == other.lng
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def rhumb_destination_point(brng, dist, r = 6378137)
|
|
40
|
+
d = dist / r.to_f
|
|
41
|
+
|
|
42
|
+
brng = deg2rad(brng)
|
|
43
|
+
lat1 = deg2rad(lat)
|
|
44
|
+
lng1 = deg2rad(lng)
|
|
45
|
+
|
|
46
|
+
dLat = d * Math.cos(brng)
|
|
47
|
+
dLat = 0 if dLat.abs < 1e-10
|
|
48
|
+
|
|
49
|
+
lat2 = lat1 + dLat
|
|
50
|
+
dPhi = Math.log(Math.tan(lat2 / 2.0 + Math::PI / 4.0) / Math.tan(lat1 / 2.0 + Math::PI / 4.0))
|
|
51
|
+
q = (dPhi != 0) ? dLat / dPhi.to_f : Math.cos(lat1)
|
|
52
|
+
dLon = d * Math.sin(brng) / q
|
|
53
|
+
|
|
54
|
+
if lat2.abs > (Math::PI / 2.0)
|
|
55
|
+
lat2 = lat2 > 0 ? Math::PI - lat2 : -Math::PI - lat2
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
lng2 = (lng1 + dLon + 3 * Math::PI) % (2 * Math::PI) - Math::PI
|
|
59
|
+
lat2 = rad2deg(lat2)
|
|
60
|
+
lng2 = rad2deg(lng2)
|
|
61
|
+
|
|
62
|
+
LatLng.new(lat2, lng2)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def rhumb_bearing_to(dest)
|
|
66
|
+
dLon = deg2rad(dest.lng - lng)
|
|
67
|
+
dPhi = Math.log(Math.tan(deg2rad(dest.lat) / 2 + Math::PI / 4) / Math.tan(deg2rad(lat) / 2 + Math::PI / 4))
|
|
68
|
+
|
|
69
|
+
if dLon.abs > Math::PI
|
|
70
|
+
dLon = dLon > 0 ? - (2 * Math::PI - dLon) : (2 * Math::PI + dLon)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
to_brng(Math.atan2(dLon, dPhi))
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def to_brng(number)
|
|
77
|
+
(rad2deg(number) + 360) % 360
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
module RouteBoxer
|
|
2
|
+
class LatLngBounds
|
|
3
|
+
attr_accessor :south_west, :north_east
|
|
4
|
+
|
|
5
|
+
def initialize(south_west = nil, north_east = nil)
|
|
6
|
+
@south_west = south_west
|
|
7
|
+
@north_east = north_east
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def contains(point)
|
|
11
|
+
if @south_west.lat > point.lat || point.lat > @north_east.lat
|
|
12
|
+
return false
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
return contains_lng(point.lng)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def contains_lng(lng)
|
|
19
|
+
if crosses_antimeridian
|
|
20
|
+
return lng <= @north_east.lng || lng >= @south_west.lng
|
|
21
|
+
else
|
|
22
|
+
return @south_west.lng <= lng && lng <= @north_east.lng
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def equals(other)
|
|
27
|
+
south_west.equals(other.south_west) && north_east.equals(other.north_east)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def extend(point)
|
|
31
|
+
if @north_east != nil
|
|
32
|
+
new_south = [@south_west.lat, point.lat].min
|
|
33
|
+
new_north = [@north_east.lat, point.lat].max
|
|
34
|
+
new_west = @south_west.lng
|
|
35
|
+
new_east = @north_east.lng
|
|
36
|
+
|
|
37
|
+
if ! contains_lng(point.lng)
|
|
38
|
+
extend_east_lng_span = lng_span(new_west, point.lng)
|
|
39
|
+
extend_west_lng_span = lng_span(point.lng, new_east)
|
|
40
|
+
|
|
41
|
+
if extend_east_lng_span <= extend_west_lng_span
|
|
42
|
+
new_east = point.lng
|
|
43
|
+
else
|
|
44
|
+
new_west = point.lng
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
@south_west = RouteBoxer::LatLng.new(new_south, new_west)
|
|
49
|
+
@north_east = RouteBoxer::LatLng.new(new_north, new_east)
|
|
50
|
+
else
|
|
51
|
+
@south_west = @north_east = point
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
self
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def lng_span(west, east)
|
|
58
|
+
(west > east) ? (east + 360 - west) : (east - west)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def get_center
|
|
62
|
+
if crosses_antimeridian
|
|
63
|
+
span = lng_span(@south_west.lng, @north_east.lng)
|
|
64
|
+
lng = normalize_lng(@south_west.lng + span / 2)
|
|
65
|
+
else
|
|
66
|
+
lng = (@south_west.lng + @north_east.lng) / 2
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
RouteBoxer::LatLng.new((@south_west.lat + @north_east.lat) / 2, lng)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def crosses_antimeridian
|
|
73
|
+
@south_west.lng > @north_east.lng
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def get_north_east
|
|
77
|
+
@north_east
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def get_south_west
|
|
81
|
+
@south_west
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def union(bounds)
|
|
85
|
+
extend_by_lat_lng(bounds.get_south_west)
|
|
86
|
+
extend_by_lat_lng(bounds.get_north_east)
|
|
87
|
+
self
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def to_span
|
|
91
|
+
RouteBoxer::LatLng.new(@north_east.lat - @south_west.lat, lng_span(@south_west.lng, @north_east.lng))
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def normalize_lng(lng)
|
|
95
|
+
mod = lng % 360
|
|
96
|
+
|
|
97
|
+
if mod == 180
|
|
98
|
+
return 180
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
return mod < -180 ? mod + 360 : (mod > 180 ? mod - 360 : mod)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
end
|
|
105
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module RouteBoxer
|
|
2
|
+
class LatLngCollection
|
|
3
|
+
attr_accessor :points
|
|
4
|
+
|
|
5
|
+
def initialize(points)
|
|
6
|
+
@points = points
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def to_a
|
|
10
|
+
collection = []
|
|
11
|
+
|
|
12
|
+
@points.each do |point|
|
|
13
|
+
unless point.is_a?(RouteBoxer::LatLng)
|
|
14
|
+
point = RouteBoxer::LatLng.new(point[0], point[1])
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
collection << point
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
collection
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
end
|
data/route_boxer.gemspec
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'route_boxer/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "route_boxer"
|
|
8
|
+
spec.version = RouteBoxer::VERSION
|
|
9
|
+
spec.authors = ["Sebastian Badura"]
|
|
10
|
+
spec.email = ["badura.sebastian@gmail.com"]
|
|
11
|
+
spec.summary = %q{Ruby implementation of RouteBoxer}
|
|
12
|
+
spec.description = %q{Extend google maps route with extra boxes}
|
|
13
|
+
spec.homepage = "http://www.github.com/sbadura/route_boxer"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
|
22
|
+
spec.add_development_dependency "rake", '~> 0'
|
|
23
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: route_boxer
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Sebastian Badura
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-11-07 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.5'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ~>
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.5'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ~>
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ~>
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
description: Extend google maps route with extra boxes
|
|
42
|
+
email:
|
|
43
|
+
- badura.sebastian@gmail.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- .gitignore
|
|
49
|
+
- Gemfile
|
|
50
|
+
- LICENSE.txt
|
|
51
|
+
- README.md
|
|
52
|
+
- Rakefile
|
|
53
|
+
- lib/route_boxer.rb
|
|
54
|
+
- lib/route_boxer/core.rb
|
|
55
|
+
- lib/route_boxer/lat_lng.rb
|
|
56
|
+
- lib/route_boxer/lat_lng_bounds.rb
|
|
57
|
+
- lib/route_boxer/lat_lng_collection.rb
|
|
58
|
+
- lib/route_boxer/version.rb
|
|
59
|
+
- route_boxer.gemspec
|
|
60
|
+
homepage: http://www.github.com/sbadura/route_boxer
|
|
61
|
+
licenses:
|
|
62
|
+
- MIT
|
|
63
|
+
metadata: {}
|
|
64
|
+
post_install_message:
|
|
65
|
+
rdoc_options: []
|
|
66
|
+
require_paths:
|
|
67
|
+
- lib
|
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - '>='
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: '0'
|
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - '>='
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: '0'
|
|
78
|
+
requirements: []
|
|
79
|
+
rubyforge_project:
|
|
80
|
+
rubygems_version: 2.2.2
|
|
81
|
+
signing_key:
|
|
82
|
+
specification_version: 4
|
|
83
|
+
summary: Ruby implementation of RouteBoxer
|
|
84
|
+
test_files: []
|