Class: GeoTreeModule::Loc

Inherits:
Object
  • Object
show all
Defined in:
lib/geotree/loc.rb

Overview

Represents an x,y location. Each coordinate is stored internally as an integer, but may be referred to as a latitude and longitude as well.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0) ⇒ Loc

Construct a point. If x is a Float, it assumes that x and y are longitude and latitude respectively, and converts them to integer values.



36
37
38
39
40
41
42
43
# File 'lib/geotree/loc.rb', line 36

def initialize(x = 0, y = 0)
  if x.is_a? Float
    x = Loc.cvt_latlong_to_int(x)
    y = Loc.cvt_latlong_to_int(y)
  end          
  @x = x
  @y = y
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



23
24
25
# File 'lib/geotree/loc.rb', line 23

def x
  @x
end

#yObject

Returns the value of attribute y.



23
24
25
# File 'lib/geotree/loc.rb', line 23

def y
  @y
end

Class Method Details

.cvt_latlong_to_int(n) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
# File 'lib/geotree/loc.rb', line 25

def self.cvt_latlong_to_int(n)
  m = (n / LAT_LONG_FACTOR_ + 0.5).to_i
  raise ArgumentError,"Converting lat/long #{n} is out of range" if m < LOC_MIN || m > LOC_MAX
  m
end

Instance Method Details

#flipObject

Return a version of the point with the coordinates exchanged



72
73
74
# File 'lib/geotree/loc.rb', line 72

def flip
  Loc.new(@y,@x)
end

#inspectObject



61
62
63
# File 'lib/geotree/loc.rb', line 61

def inspect
  to_s
end

#latitObject

Get y as a latitudinal coordinate



53
54
55
# File 'lib/geotree/loc.rb', line 53

def latit
  @y * LAT_LONG_FACTOR_
end

#longitObject

Get x as a longitudinal coordinate



47
48
49
# File 'lib/geotree/loc.rb', line 47

def longit 
  @x * LAT_LONG_FACTOR_
end

#set_to(src) ⇒ Object



65
66
67
68
# File 'lib/geotree/loc.rb', line 65

def set_to(src)
  @x = src.x
  @y = src.y
end

#to_sObject



57
58
59
# File 'lib/geotree/loc.rb', line 57

def to_s
  "(#{x},#{y})"
end