Class: Geokit::LatLng

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

Direct Known Subclasses

GeoLoc

Constant Summary

Constants included from Mappable

Mappable::EARTH_RADIUS_IN_KMS, Mappable::EARTH_RADIUS_IN_MILES, Mappable::EARTH_RADIUS_IN_NMS, Mappable::KMS_PER_LATITUDE_DEGREE, Mappable::KMS_PER_MILE, Mappable::LATITUDE_DEGREES, Mappable::MILES_PER_LATITUDE_DEGREE, Mappable::NMS_PER_LATITUDE_DEGREE, Mappable::NMS_PER_MILE, Mappable::PI_DIV_RAD

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



217
218
219
220
221
222
# File 'lib/geokit/mappable.rb', line 217

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

Returns the value of attribute lat.



213
214
215
# File 'lib/geokit/mappable.rb', line 213

def lat
  @lat
end

#lngObject

Returns the value of attribute lng.



213
214
215
# File 'lib/geokit/mappable.rb', line 213

def lng
  @lng
end

Class Method Details

.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 which acts_as_mappable -- a LatLng will be extracted from it

Raises:

  • (ArgumentError)


271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/geokit/mappable.rb', line 271

def self.normalize(thing,other=nil)
  # if an 'other' thing is supplied, normalize the input by creating an array of two elements
  thing=[thing,other] if other
  
  if thing.is_a?(String)
    thing.strip!
    if match=thing.match(/(\-?\d+\.?\d*)[, ] ?(\-?\d+\.?\d*)$/)
      return Geokit::LatLng.new(match[1],match[2])
    else
      res = Geokit::Geocoders::MultiGeocoder.geocode(thing)
      return res if res.success?
      raise Geokit::Geocoders::GeocodeError  
    end
  elsif thing.is_a?(Array) && thing.size==2
    return Geokit::LatLng.new(thing[0],thing[1])
  elsif thing.is_a?(LatLng) # will also be true for GeoLocs
    return thing
  elsif thing.class.respond_to?(:acts_as_mappable) && thing.class.respond_to?(:distance_column_name)
    return thing.to_lat_lng
  end
  
  raise ArgumentError.new("#{thing} (#{thing.class}) cannot be normalized to a LatLng. We tried interpreting it as an array, string, Mappable, etc., but no dice.")
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.



250
251
252
# File 'lib/geokit/mappable.rb', line 250

def ==(other)
  other.is_a?(LatLng) ? self.lat == other.lat && self.lng == other.lng : false
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/geokit/mappable.rb', line 258

def eql?(other)
  self == other
end

#hashObject



254
255
256
# File 'lib/geokit/mappable.rb', line 254

def hash
  lat.hash + lng.hash
end

#llObject

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



235
236
237
# File 'lib/geokit/mappable.rb', line 235

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

#to_aObject

returns a two-element array



245
246
247
# File 'lib/geokit/mappable.rb', line 245

def to_a
  [lat,lng]
end

#to_sObject

returns a string with comma-separated lat,lng values



240
241
242
# File 'lib/geokit/mappable.rb', line 240

def to_s
  ll
end