struct Geo::Coord

Overview

A Coord is a point in geographical coordinates: latitude and longitude.

Included Modules

Defined in:

geo/bearing.cr
geo/coord.cr
geo/distance.cr

Constant Summary

DIRECTIVES = {/%(#{FLOATUFLAGS})?lats/ => ->(m : Regex::MatchData) do "%<lats>#{m[1]? || "0."}f" end, "%latm" => "%<latm>i", /%(#{INTFLAGS})?latds/ => ->(m : Regex::MatchData) do "%<latds>#{m[1]}i" end, "%latd" => "%<latd>i", "%lath" => "%<lath>s", /%(#{FLOATFLAGS})?lat/ => ->(m : Regex::MatchData) do "%<lat>#{m[1]}f" end, /%(#{FLOATUFLAGS})?lngs/ => ->(m : Regex::MatchData) do "%<lngs>#{m[1]? || "0."}f" end, "%lngm" => "%<lngm>i", /%(#{INTFLAGS})?lngds/ => ->(m : Regex::MatchData) do "%<lngds>#{m[1]}i" end, "%lngd" => "%<lngd>i", "%lngh" => "%<lngh>s", /%(#{FLOATFLAGS})?lng/ => ->(m : Regex::MatchData) do "%<lng>#{m[1]}f" end}
FLOATFLAGS = /\+?#{FLOATUFLAGS}?/
FLOATUFLAGS = /0\.\d+/
INTFLAGS = /\+?/

Constructors

Instance Method Summary

Constructor Detail

def self.new(lat : Number, lng : Number) #

[View source]

Instance Method Detail

def <=>(other : Geo::Coord) #
Description copied from module Comparable(Geo::Coord)

The comparison operator. Returns 0 if the two objects are equal, a negative number if this object is considered less than other, a positive number if this object is considered greater than other, or nil if the two objects are not comparable.

Subclasses define this method to provide class-specific ordering.

The comparison operator is usually used to sort values:

# Sort in a descending way:
[3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]

# Sort in an ascending way:
[3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]

[View source]
def bearing(to : Geo::Coord, final = false) : Float64 #

Calculates initial and final bearings between two points using great-circle distance formulas


[View source]
def between?(p1 : Geo::Coord, p2 : Geo::Coord) #

[View source]
def destination(distance : Number, bearing : Number, unit : Symbol = :kilometers) : Geo::Coord #

Calculates the location of a destination point


[View source]
def distance(other : Geo::Coord) : Haversine::Distance #

Calculates distance to other. Haversine formula is used.


[View source]
def geohash(precision = 12) #

Returns a geohash representing coordinates.


[View source]
def lat : Float32 | Float64 | Int32 #

[View source]
def latd : Int32 #

Returns latitude degrees


[View source]
def latds #

[View source]
def lath : String #

Returns latitude hemisphere


[View source]
def latm : Int32 #

Returns latitude minutes


[View source]
def lats : Number #

Returns latitude seconds


[View source]
def ll #

[View source]
def lng : Float32 | Float64 | Int32 #

[View source]
def lngd : Int32 #

Returns longitude degrees


[View source]
def lngds #

[View source]
def lngh : String #

Returns longitude hemisphere


[View source]
def lngm : Int32 #

Returns longitude minutes


[View source]
def lngs : Number #

Returns longitude seconds


[View source]
def strfcoord(formatstr) : String #

Formats coordinates according to directives in formatstr.

Each directive starts with % and can contain some modifiers before its name.

Acceptable modifiers:

  • unsigned integers: none;
  • signed integers: + for mandatory sign printing;
  • floats: same as integers and number of digits modifier, like 0.3.

List of directives:

  • %lat - Full latitude, floating point, signed
  • %latds - Latitude degrees, integer, signed
  • %latd - Latitude degrees, integer, unsigned
  • %latm - Latitude minutes, integer, unsigned
  • %lats - Latitude seconds, floating point, unsigned
  • %lath - Latitude hemisphere, "N" or "S"
  • %lng - Full longitude, floating point, signed
  • %lngds - Longitude degrees, integer, signed
  • %lngd - Longitude degrees, integer, unsigned
  • %lngm - Longitude minutes, integer, unsigned
  • #lngs - Longitude seconds, floating point, unsigned
  • `%lngh`` - Longitude hemisphere, "E" or "W"

Examples:

g = Geo::Coord.new(50.004444, 36.231389)
g.strfcoord('%+lat, %+lng')
# => "+50.004444, +36.231389"
g.strfcoord("%latd°%latm'%lath -- %lngd°%lngm'%lngh")
# => "50°0'N -- 36°13'E"

#strfcoord handles seconds rounding implicitly:

pos = Geo::Coord.new(0.033333, 91.333333)
pos.strfcoord('%latd %latm %0.5lats') # => "0 1 59.99880"
pos.strfcoord('%latd %latm %lats')  # => "0 2 0"

[View source]
def to_geojson : GeoJSON::Coordinates #

[View source]
def to_s(io) #

Returns a string representing coordinates.

g.to_s             # => "50°0'16\"N 36°13'53\"E"
g.to_s(dms: false) # => "50.004444,36.231389"

[View source]