struct Geo::Coord
- Geo::Coord
- Struct
- Value
- Object
Overview
A Coord
is a point in geographical coordinates: latitude and longitude.
Included Modules
- Comparable(Geo::Coord)
Defined in:
geo/bearing.crgeo/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
-
#<=>(other : Geo::Coord)
The comparison operator.
-
#bearing(to : Geo::Coord, final = false) : Float64
Calculates initial and final bearings between two points using great-circle distance formulas
- #between?(p1 : Geo::Coord, p2 : Geo::Coord)
-
#destination(distance : Number, bearing : Number, unit : Symbol = :kilometers) : Geo::Coord
Calculates the location of a destination point
-
#distance(other : Geo::Coord) : Haversine::Distance
Calculates distance to
other
. -
#geohash(precision = 12)
Returns a geohash representing coordinates.
- #lat : Float32 | Float64 | Int32
-
#latd : Int32
Returns latitude degrees
- #latds
-
#lath : String
Returns latitude hemisphere
-
#latm : Int32
Returns latitude minutes
-
#lats : Number
Returns latitude seconds
- #ll
- #lng : Float32 | Float64 | Int32
-
#lngd : Int32
Returns longitude degrees
- #lngds
-
#lngh : String
Returns longitude hemisphere
-
#lngm : Int32
Returns longitude minutes
-
#lngs : Number
Returns longitude seconds
-
#strfcoord(formatstr) : String
Formats coordinates according to directives in
formatstr
. - #to_geojson : GeoJSON::Coordinates
-
#to_s(io)
Returns a string representing coordinates.
Constructor Detail
Instance Method Detail
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]
Calculates initial and final bearings between two points using great-circle distance formulas
Calculates the location of a destination point
Calculates distance to other
.
Haversine formula is used.
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"
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"