R/dms.R
dms_decode.RdParse strings representing degrees, minutes, and seconds and return the angle in degrees. Format an angle in degrees as degrees, minutes, and seconds strings.
dms_decode(x)
dms_decode_latlon(dmsa, dmsb, longfirst = FALSE)
dms_decode_angle(x)
dms_decode_azimuth(x)
dms_encode(x, prec = 5L, component = NULL, indicator = "none", sep = "")
dms_split(x, seconds = FALSE)
dms_combine(d, m = 0, s = 0)Character vector of DMS strings to parse, or numeric vector of angles in degrees to encode.
Character vectors of DMS strings for latitude/longitude parsing.
Logical; if TRUE, assume longitude is given before latitude when no hemisphere designators are present.
Integer precision for output strings. For dms_encode() this
is the number of digits after the decimal point for the trailing
component. For automatic encoding, prec < 2 gives degrees, prec 2-3
gives minutes, prec >= 4 gives seconds.
Character indicating the trailing unit: "degree",
"minute", or "second".
Character indicating formatting: "none" (signed result),
"latitude" (trailing N/S), "longitude" (trailing E/W),
"azimuth" (0-360, no sign), or "number" (plain number).
Character to use as DMS separator instead of d, ', ".
Use ":" for colon-separated output.
Logical; if TRUE, split into degrees, minutes, and seconds. If FALSE (default), split into degrees and minutes only.
Numeric vectors of degrees, minutes, and seconds.
dms_decode(): Data frame with columns angle (degrees) and indicator
(0=NONE, 1=LATITUDE, 2=LONGITUDE)
dms_decode_latlon(): Data frame with columns lat and lon (degrees)
dms_decode_angle(): Numeric vector of angles in degrees
dms_decode_azimuth(): Numeric vector of azimuths in degrees (range
-180 to 180)
dms_encode(): Character vector of DMS strings
dms_split(): Data frame with columns d, m, and optionally s
dms_combine(): Numeric vector of angles in degrees
The dms_decode() function accepts various input formats:
Degrees, minutes, seconds: "40d26'47\"N", "40°26'47\"N"
Degrees and minutes: "40d26.783'N", "40:26.783N"
Decimal degrees: "40.446N", "-40.446"
Colon-separated: "40:26:47", "-74:0:21.5"
Hemisphere designators (N, S, E, W) can appear at the beginning or end. Many Unicode symbols are supported for degrees (°, º, ˚), minutes (', ′), and seconds (", ″).
For dms_encode(), the prec parameter controls decimal places in the
trailing component:
prec = 0: whole degrees/minutes/seconds
prec = 1: one decimal place
prec = 2: two decimal places, etc.
For automatic component selection:
prec < 2: output in degrees
prec = 2, 3: output in degrees and minutes
prec >= 4: output in degrees, minutes, and seconds
geocoords_parse() for parsing complete coordinate strings
# Parse DMS strings
dms_decode("40d26'47\"N")
#> angle indicator
#> 1 40.44639 1
dms_decode(c("40:26:47", "-74:0:21.5", "51d30'N"))
#> angle indicator
#> 1 40.44639 0
#> 2 -74.00597 0
#> 3 51.50000 1
# Parse latitude/longitude pairs
dms_decode_latlon("40d26'47\"N", "74d0'21.5\"W")
#> lat lon
#> 1 40.44639 -74.00597
# Parse angles (no hemisphere designator)
dms_decode_angle(c("45:30:0", "123d45'6\""))
#> [1] 45.5000 123.7517
# Parse azimuths (E/W allowed)
dms_decode_azimuth(c("45:30:0", "90W", "45E"))
#> [1] 45.5 -90.0 45.0
# Encode to DMS strings
dms_encode(40.446, indicator = "latitude")
#> [1] "40d26'45.6\"N"
dms_encode(-74.006, indicator = "longitude")
#> [1] "074d00'21.6\"W"
dms_encode(c(40.446, -74.006), prec = 2)
#> [1] "40d27'" "-74d00'"
# With colon separator
dms_encode(40.446, sep = ":")
#> [1] "40:26:45.6"
# Split angle into components
dms_split(40.446)
#> d m
#> 1 40 26.76
dms_split(c(40.446, -74.006), seconds = TRUE)
#> d m s
#> 1 40 26 45.6
#> 2 -74 0 -21.6
# Combine components to decimal degrees
dms_combine(40, 26, 47)
#> [1] 40.44639
dms_combine(d = c(40, -74), m = c(26, 0), s = c(47, 21.5))
#> [1] 40.44639 -73.99403