Module: Groonga::GeoPointValueConverter

Defined in:
lib/groonga/geo-point.rb

Constant Summary

MSEC_PER_SEC =
1000 * 1000
N_SIGNIFICANT_DIGITS =
(Math.log10(MSEC_PER_SEC) + 1).truncate

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.degree?(value) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/groonga/geo-point.rb', line 39

def degree?(value)
  value.is_a?(Float)
end

.degree_to_msec(degree) ⇒ Object



31
32
33
# File 'lib/groonga/geo-point.rb', line 31

def degree_to_msec(degree)
  round(degree * 3600 * 1000)
end

.msec?(value) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/groonga/geo-point.rb', line 35

def msec?(value)
  value.is_a?(Integer)
end

.msec_to_degree(msec) ⇒ Object



25
26
27
28
29
# File 'lib/groonga/geo-point.rb', line 25

def msec_to_degree(msec)
  degree_integer_part, degree_fraction_part = msec.divmod(3600 * 1000)
  degree = degree_integer_part + (degree_fraction_part.to_f / (3600 * 1000))
  round(degree, N_SIGNIFICANT_DIGITS)
end

.parse(value) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/groonga/geo-point.rb', line 53

def parse(value)
  if /\A[-+]?\d+\z/ =~ value
    value.to_i
  else
    value.to_f
  end
end

.to_degree(value) ⇒ Object



43
44
45
46
# File 'lib/groonga/geo-point.rb', line 43

def to_degree(value)
  return value if degree?(value)
  msec_to_degree(value)
end

.to_msec(value) ⇒ Object



48
49
50
51
# File 'lib/groonga/geo-point.rb', line 48

def to_msec(value)
  return value if msec?(value)
  degree_to_msec(value)
end

Instance Method Details

#round(number, n_digits = 0) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/groonga/geo-point.rb', line 62

def round(number, n_digits=0)
  if n_digits.zero?
    number.round
  else
    base = 10 ** n_digits
    (number * base).round / base.to_f
  end
end