Class: Tsuga::Model::Tile

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from ClassMethods

enclosing_viewport, including

Constructor Details

#initialize(prefix: nil) ⇒ Tile

Returns a new instance of Tile.

Raises:

  • (ArgumentError)


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

#depthObject (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

#northeastObject (readonly)

corner points



7
8
9
# File 'lib/tsuga/model/tile.rb', line 7

def northeast
  @northeast
end

#prefixObject (readonly)

geohash prefix



14
15
16
# File 'lib/tsuga/model/tile.rb', line 14

def prefix
  @prefix
end

#southwestObject (readonly)

corner points



7
8
9
# File 'lib/tsuga/model/tile.rb', line 7

def southwest
  @southwest
end

Instance Method Details

#childrenObject

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

Returns:

  • (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

#inspectObject



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

#neighboursObject

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