Class: Tsuga::Model::Tile
- Inherits:
-
Object
- Object
- Tsuga::Model::Tile
- Extended by:
- ClassMethods
- Defined in:
- lib/tsuga/model/tile.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- WIGGLE_FACTOR =
1e-4
Instance Attribute Summary collapse
-
#depth ⇒ Object
readonly
level in the tile tree, also number of relevant high bits in the geohash.
-
#northeast ⇒ Object
readonly
corner points.
-
#prefix ⇒ Object
readonly
geohash prefix.
-
#southwest ⇒ Object
readonly
corner points.
Instance Method Summary collapse
-
#children ⇒ Object
return the 4 children of this tile.
- #contains?(point) ⇒ Boolean
- #dlat(count = 1) ⇒ Object
- #dlng(count = 1) ⇒ Object
-
#initialize(prefix: nil) ⇒ Tile
constructor
A new instance of Tile.
- #inspect ⇒ Object
-
#neighbour(lat: 0, lng: 0) ⇒ Object
return a neighouring tile offset in tile increments TODO: this could be implemented using bit logic.
-
#neighbours ⇒ Object
return neighbouring tiles to the north, northeast, and east.
Methods included from ClassMethods
Constructor Details
#initialize(prefix: nil) ⇒ Tile
Returns a new instance of Tile.
18 19 20 21 22 23 24 |
# File 'lib/tsuga/model/tile.rb', line 18 def initialize(prefix:nil) raise ArgumentError, 'bad prefix' if prefix !~ /^[0-3]{1,32}$/ @prefix = prefix @depth = prefix.length @southwest = Point.new(geohash: prefix.ljust(32, '0')) @northeast = Point.new(geohash: prefix.ljust(32, '3')) end |
Instance Attribute Details
#depth ⇒ Object (readonly)
level in the tile tree, also number of relevant high bits in the geohash.
11 12 13 |
# File 'lib/tsuga/model/tile.rb', line 11 def depth @depth end |
#northeast ⇒ Object (readonly)
corner points
7 8 9 |
# File 'lib/tsuga/model/tile.rb', line 7 def northeast @northeast end |
#prefix ⇒ Object (readonly)
geohash prefix
14 15 16 |
# File 'lib/tsuga/model/tile.rb', line 14 def prefix @prefix end |
#southwest ⇒ Object (readonly)
corner points
7 8 9 |
# File 'lib/tsuga/model/tile.rb', line 7 def southwest @southwest end |
Instance Method Details
#children ⇒ Object
return the 4 children of this tile
39 40 41 42 43 |
# File 'lib/tsuga/model/tile.rb', line 39 def children %w(0 1 2 3).map { |quadrant| self.class.new(prefix: @prefix + quadrant) } end |
#contains?(point) ⇒ Boolean
26 27 28 |
# File 'lib/tsuga/model/tile.rb', line 26 def contains?(point) point.geohash.start_with?(@prefix) end |
#dlat(count = 1) ⇒ Object
30 31 32 |
# File 'lib/tsuga/model/tile.rb', line 30 def dlat(count = 1) (northeast.lat - southwest.lat) * (count + WIGGLE_FACTOR) end |
#dlng(count = 1) ⇒ Object
34 35 36 |
# File 'lib/tsuga/model/tile.rb', line 34 def dlng(count = 1) (northeast.lng - southwest.lng) * (count + WIGGLE_FACTOR) end |
#inspect ⇒ Object
66 67 68 69 70 71 |
# File 'lib/tsuga/model/tile.rb', line 66 def inspect "<%s depth:%d prefix:%s>" % [ (self.class.name || 'Tile'), depth, prefix ] end |
#neighbour(lat: 0, lng: 0) ⇒ Object
return a neighouring tile offset in tile increments TODO: this could be implemented using bit logic
47 48 49 50 51 52 |
# File 'lib/tsuga/model/tile.rb', line 47 def neighbour(lat:0, lng:0) new_point = Point.new( lat: southwest.lat + dlat(lat), lng: southwest.lng + dlng(lng)) Tile.including(new_point, depth: depth) end |
#neighbours ⇒ Object
return neighbouring tiles to the north, northeast, and east
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/tsuga/model/tile.rb', line 55 def neighbours offsets = (-1..1).to_a.product((-1..1).to_a) offsets.map do |lat, lng| begin neighbour(lat:lat, lng:lng) rescue ArgumentError nil # occurs on world boundaries end end.compact end |