Class: Geokit::LatLng

Inherits:
Object show all
Includes:
Mappable
Defined in:
lib/geokit/lat_lng.rb

Direct Known Subclasses

GeoLoc

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mappable

#distance_to, #endpoint, #heading_from, #heading_to, included, #midpoint_to, #to_lat_lng

Constructor Details

#initialize(lat = nil, lng = nil) ⇒ LatLng

Accepts latitude and longitude or instantiates an empty instance if lat and lng are not provided. Converted to floats if provided



18
19
20
21
22
23
# File 'lib/geokit/lat_lng.rb', line 18

def initialize(lat = nil, lng = nil)
  lat = lat.to_f if lat && !lat.is_a?(Numeric)
  lng = lng.to_f if lng && !lng.is_a?(Numeric)
  @lat = lat
  @lng = lng
end

Instance Attribute Details

#latObject Also known as: latitude

Returns the value of attribute lat.



5
6
7
# File 'lib/geokit/lat_lng.rb', line 5

def lat
  @lat
end

#lngObject Also known as: longitude

Returns the value of attribute lng.



5
6
7
# File 'lib/geokit/lat_lng.rb', line 5

def lng
  @lng
end

Class Method Details

.from_json(json) ⇒ Object



25
26
27
# File 'lib/geokit/lat_lng.rb', line 25

def self.from_json(json)
  new(json['lat'], json['lng'])
end

.from_string(thing) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/geokit/lat_lng.rb', line 119

def self.from_string(thing)
  thing.strip!
  if match = thing.match(/(\-?\d+\.?\d*)[, ] ?(\-?\d+\.?\d*)$/)
    Geokit::LatLng.new(match[1], match[2])
  else
    res = Geokit::Geocoders::MultiGeocoder.geocode(thing)
    return res if res.success?
    raise Geokit::Geocoders::GeocodeError
  end
end

.normalize(thing, other = nil) ⇒ Object

A class method to take anything which can be inferred as a point and generate a LatLng from it. You should use this anything you’re not sure what the input is, and want to deal with it as a LatLng if at all possible. Can take:

1) two arguments (lat,lng)
2) a string in the format "37.1234,-129.1234" or "37.1234 -129.1234"
3) a string which can be geocoded on the fly
4) an array in the format [37.1234,-129.1234]
5) a LatLng or GeoLoc (which is just passed through as-is)
6) anything responding to to_lat_lng -- a LatLng will be extracted from
   it


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/geokit/lat_lng.rb', line 96

def self.normalize(thing, other = nil)
  return Geokit::LatLng.new(thing, other) if other

  case thing
  when String
    from_string(thing)
  when Array
    thing.size == 2 or raise ArgumentError.new(
      'Must initialize with an Array with both latitude and longitude')
    Geokit::LatLng.new(thing[0], thing[1])
  when LatLng # will also be true for GeoLocs
    thing
  else
    if thing.respond_to? :to_lat_lng
      thing.to_lat_lng
    else
      raise ArgumentError.new(
        "#{thing} (#{thing.class}) cannot be normalized to a LatLng. " +
        "We tried interpreting it as an array, string, etc., but no dice.")
    end
  end
end

Instance Method Details

#==(other) ⇒ Object

Returns true if the candidate object is logically equal. Logical equivalence is true if the lat and lng attributes are the same for both objects.



67
68
69
70
# File 'lib/geokit/lat_lng.rb', line 67

def ==(other)
  return false unless other.is_a?(LatLng)
  lat == other.lat && lng == other.lng
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/geokit/lat_lng.rb', line 76

def eql?(other)
  self == other
end

#hashObject



72
73
74
# File 'lib/geokit/lat_lng.rb', line 72

def hash
  lat.hash + lng.hash
end

#lat_dmsObject

returns latitude as [ degree, minute, second ] array



45
46
47
# File 'lib/geokit/lat_lng.rb', line 45

def lat_dms
  self.class.decimal_to_dms(lat)
end

#latitude=Object

Sets the attribute lat

Parameters:

  • value

    the value to set the attribute lat to.



11
12
13
# File 'lib/geokit/lat_lng.rb', line 11

def lat=(value)
  @lat = value
end

#llObject

Returns the lat and lng attributes as a comma-separated string.



40
41
42
# File 'lib/geokit/lat_lng.rb', line 40

def ll
  "#{lat},#{lng}"
end

#lng_dmsObject

returns longitude as [ degree, minute, second ] array



50
51
52
# File 'lib/geokit/lat_lng.rb', line 50

def lng_dms
  self.class.decimal_to_dms(lng)
end

#longitude=Object

Sets the attribute lng

Parameters:

  • value

    the value to set the attribute lng to.



14
15
16
# File 'lib/geokit/lat_lng.rb', line 14

def lng=(value)
  @lng = value
end

#reverse_geocode(options = { using: Geokit::Geocoders::MultiGeocoder }) ⇒ Object

Reverse geocodes a LatLng object using the MultiGeocoder (default), or optionally using a geocoder of your choosing. Returns a new Geokit::GeoLoc object

Options

  • :using - Specifies the geocoder to use for reverse geocoding. Defaults

    to MultiGeocoder. Can be either the geocoder class (or any
    class that implements do_reverse_geocode for that matter), or
    the name of the class without the "Geocoder" part
    (e.g. :google)
    

Examples

LatLng.new(51.4578329, 7.0166848).reverse_geocode

> #<Geokit::GeoLoc:0x12dac20 @state…>

LatLng.new(51.4578329, 7.0166848).reverse_geocode(:using => :google)

> #<Geokit::GeoLoc:0x12dac20 @state…>

LatLng.new(51.4578329, 7.0166848).reverse_geocode(:using =>

Geokit::Geocoders::GoogleGeocoder)

> #<Geokit::GeoLoc:0x12dac20 @state…>



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/geokit/lat_lng.rb', line 149

def reverse_geocode(options = { using: Geokit::Geocoders::MultiGeocoder })
  if options[:using].is_a?(String) || options[:using].is_a?(Symbol)
    class_name =
      "#{Geokit::Inflector.camelize(options[:using].to_s)}Geocoder"
    provider = Geokit::Geocoders.const_get(class_name)
  elsif options[:using].respond_to?(:do_reverse_geocode)
    provider = options[:using]
  else
    raise ArgumentError.new("#{options[:using]} is not a valid geocoder.")
  end

  provider.send(:reverse_geocode, self)
end

#to_aObject

returns a two-element array



60
61
62
# File 'lib/geokit/lat_lng.rb', line 60

def to_a
  [lat, lng]
end

#to_sObject

returns a string with comma-separated lat,lng values



55
56
57
# File 'lib/geokit/lat_lng.rb', line 55

def to_s
  ll
end

#valid?Boolean

Returns true if both lat and lng attributes are defined

Returns:

  • (Boolean)


81
82
83
# File 'lib/geokit/lat_lng.rb', line 81

def valid?
  lat && lng
end