| Type: | Package |
| Title: | Parse Messy Geographic Coordinates |
| Description: | Parse messy geographic coordinates from various character formats to decimal degree numeric values. Parse coordinates into their parts (degree, minutes, seconds); calculate hemisphere from coordinates; pull out individually degrees, minutes, or seconds; add and subtract degrees, minutes, and seconds. C++ code herein originally inspired from code written by Jeffrey D. Bogan, but then completely re-written. A 'format' argument reads packed digits, such as coordinates from the International Civil Aviation Organization ('ICAO') or from GPS receivers ('NMEA'). |
| Version: | 0.6.0 |
| Maintainer: | Alban Sagouis <sagouis@pm.me> |
| License: | MIT + file LICENSE |
| URL: | https://github.com/ropensci/parzer, https://docs.ropensci.org/parzer/ |
| BugReports: | https://github.com/ropensci/parzer/issues |
| Imports: | Rcpp (≥ 1.0.2) |
| Suggests: | bench, testthat (≥ 3.0.0), quarto, sf, leaflet |
| Config/testthat/edition: | 3 |
| Config/testthat/parallel: | false |
| Config/testthat/start-first: | *utils*, *format*, *parse_parts*, *parse*, *dms*, *benchmarks* |
| LinkingTo: | Rcpp |
| VignetteBuilder: | quarto |
| Encoding: | UTF-8 |
| Language: | en-US |
| X-schema.org-applicationCategory: | Geospatial |
| X-schema.org-keywords: | geospatial, data, latitude, longitude, parser, coordinates |
| X-schema.org-isPartOf: | https://ropensci.org |
| Config/roxygen2/version: | 8.1.1 |
| NeedsCompilation: | yes |
| Packaged: | 2026-11-24 10:35:27 UTC; as80fywe |
| Author: | Scott Chamberlain |
| Repository: | CRAN |
| Date/Publication: | 2026-11-24 13:20:12 UTC |
parzer: Parse Messy Geographic Coordinates
Description
Parse messy geographic coordinates from various character formats to decimal degree numeric values. Parse coordinates into their parts (degree, minutes, seconds); calculate hemisphere from coordinates; pull out individually degrees, minutes, or seconds; add and subtract degrees, minutes, and seconds. C++ code herein originally inspired from code written by Jeffrey D. Bogan, but then completely re-written. A 'format' argument reads packed digits, such as coordinates from the International Civil Aviation Organization ('ICAO') or from GPS receivers ('NMEA').
Details
parse geographic coordinates
Author(s)
Maintainer: Alban Sagouis sagouis@pm.me (ORCID)
Authors:
Alban Sagouis sagouis@pm.me (ORCID)
Scott Chamberlain sckott@protonmail.com (ORCID)
Other contributors:
Alec L. Robitaille (ORCID) [contributor]
Maëlle Salmon (ORCID) [contributor]
Hiroaki Yutani [contributor]
Jeffrey Bogan (C++ code originally from Jeffrey Bogan, but completely re-written) [contributor]
Julien Brun (ORCID) (Julien Brun reviewed the package, see https://github.com/ropensci/onboarding/issues/341) [reviewer]
Maria Munafó (Maria Munafó reviewed the package, see https://github.com/ropensci/onboarding/issues/341) [reviewer]
rOpenSci (ROR) [funder]
German Centre for Integrative Biodiversity Research (iDiv) Halle-Jena-Leipzig (ROR) [funder]
See Also
Useful links:
Report bugs at https://github.com/ropensci/parzer/issues
extract degree, minutes, and seconds
Description
extract degree, minutes, and seconds
Usage
pz_degree(lon = NULL, lat = NULL)
pz_minute(lon = NULL, lat = NULL)
pz_second(lon = NULL, lat = NULL)
## S3 method for class 'pz'
print(x, ...)
pz_d(x)
pz_m(x)
pz_s(x)
## S3 method for class 'pz'
e1 + e2
## S3 method for class 'pz'
e1 - e2
## S3 method for class 'pz'
e1 / e2
## S3 method for class 'pz'
e1 * e2
Arguments
lon, lat |
(numeric/integer/character) one or more longitude or latitude values. values are internally validated. only one of lon or lat accepted |
x |
(integer) an integer representing a degree, minute or second |
... |
print dots |
e1, e2 |
objects of class pz, from using |
Details
Mathematics operators are exported for +, -, /, and *,
but / and * are only exported with a stop message to say it's not
supported; otherwise you'd be allow to divide degrees by minutes, leading
to nonsense. Both sides of + and - must be pz objects: pz_d(1) + 5
is an error, because a plain number has no unit. Unary minus keeps the
unit: -pz_m(30) is minus 30 minutes.
Value
pz_degree: integer, pz_minute: integer, pz_second: numeric,
pz_d: numeric, pz_m: numeric, pz_s: numeric (adding/subtracting
these also gives numeric)
Examples
# extract parts of a coordinate value
pz_degree(-45.23323)
pz_minute(-45.23323)
pz_second(-45.23323)
pz_degree(lon = 178.23423)
pz_minute(lon = 178.23423)
pz_second(lon = 178.23423)
## Not run:
pz_degree(lat = c(45.23323, "40:25:6N", "40° 25´ 5.994 S"))
pz_minute(lat = c(45.23323, "40:25:6N", "40° 25´ 5.994 S"))
pz_second(lat = c(45.23323, "40:25:6N", "40° 25´ 5.994 S"))
# invalid
pz_degree(445.23323)
# add or subtract
pz_d(31)
pz_m(44)
pz_s(3)
pz_d(31) + pz_m(44)
pz_d(-31) - pz_m(44)
pz_d(-31) + pz_m(44) + pz_s(59)
pz_d(31) - pz_m(44) + pz_s(59)
pz_d(-121) + pz_m(1) + pz_s(33)
unclass(pz_d(31) + pz_m(44) + pz_s(59))
## End(Not run)
get hemisphere from long/lat coordinates
Description
BEWARE: EXPERIMENTAL
Usage
parse_hemisphere(lon, lat)
Arguments
lon |
(character/numeric/integer) one or more longitude values |
lat |
(character/numeric/integer) one or more latitude values |
Details
length(lon) == length(lat)
Value
character vector of quadrants, one of: NE, NW, SE, SW. if one of the coordinate values is invalid, and one is valid, you get a length 1 string. if both coordinate values are bad, you get a zero length string.
Warnings are thrown on invalid values
Examples
# NE
parse_hemisphere("74.123E", "45N54.2356")
## Not run:
# NW
parse_hemisphere(-120, 40.4183318)
# SW
parse_hemisphere(-120, -40.4183318)
# SE
parse_hemisphere(120, -40.4183318)
# bad inputs, get one of the two strings
parse_hemisphere(-181, -40.4183318)
parse_hemisphere(-120, -192.4183318)
## End(Not run)
Parse latitude values
Description
Parse latitude values
Usage
parse_lat(lat, format = NULL)
Arguments
lat |
(numeric/integer/character) one or more latitude values |
format |
(character) |
Value
numeric vector
Format
A pattern describes how the values are written. It is made of letters and
other characters. The idea comes from AngleFormat in Apache SIS and from
CLongLatString.
-
D,MandSstand for the digits of the whole degrees, minutes and seconds. The number of letters is the exact number of digits:DDmeans exactly 2 digits, so it reads"05"but not"5", andDDDmeans exactly 3 digits, as longitudes often need. Values written without leading zeros need a second pattern, for examplec("DD MM SSH", "D MM SSH")reads both"45 03 02N"and"5 03 02N".Dis required.MandSare optional, butSneedsM, and they come in the orderD,M,S. -
d,mandsstand for the decimal digits of the degrees, minutes or seconds. Only the last unit can have them. They come right after it, as in"DDMMmmm", or after a dot that follows it, as in"DDMM.m".Without a dot, the decimal point is not written in the value. The number of letters is then the exact number of decimal digits: it is the only way to know where the hidden decimal point is.
"HDDMMmmmmm"reads"N455698735"as N 45 degrees 56.98735 minutes.After a dot, the value has a decimal point and one or more decimal digits, as many as you like:
"DDMM.m"reads both"4556.9"and"4556.98735".
-
Hstands for the hemisphere letter:NorSinparse_lat(),EorWinparse_lon(), in upper or lower case.SandWgive a negative value.Hcan be at the start, at the end or in the middle, but only once. A letter of the other pair, for exampleEinparse_lat(), givesNA. A space matches one or more spaces (or other white space).
Any other character, for example
°,',",:, or a dot that is not followed byd,mors, must be in the value exactly as in the pattern.A sign can always be written right before the degrees:
+,-, or a Unicode minus (U+2212), en dash–or em dash—. WithH, the rules are the same as withformat = NULL:"-45S"and"+45S"give -45, but"-45N"givesNA, because the minus sign and the letter disagree.
Spaces at the start and at the end of a value are ignored. The pattern is
matched against the value as it is given: the cleaning done with
format = NULL, for example reading d or º as a degree mark, does not
happen.
A value gives NA when it does not match any pattern, when its minutes or
seconds are 60 or more, or when the result is out of range (latitude
outside -90/90, longitude outside -180/360). All these values are listed in
one warning. They are not read again with format = NULL. A missing value
(NA) or an empty string gives NA without a warning. A pattern that
cannot work, for example "MMDD", "DDmmSS" or "DDX", is an error.
Numbers are turned into text first, so they lose their leading zeros:
parse_lon(0002741, format = "DDDMMSS") sees "2741" and gives NA.
Give such values as text: parse_lon("0002741", format = "DDDMMSS").
See the Format examples section for many examples.
Format examples
| Value | format | Result | Why |
"512839N" | "DDMMSSH" | 51.4775 | ICAO latitude |
"4556.98735" | "DDMM.mmmmm" | 45.94979 | NMEA |
"4556.98735" | "DDMM.m" | 45.94979 | after a dot, any number of decimals |
"4556.9" | "DDMM.m" | 45.94833 | after a dot, any number of decimals |
"N455698735" | "HDDMMmmmmm" | 45.94979 | decimal point not written: exactly 5 decimals |
"2343 56N" | "DDMM SSH" | 23.73222 | |
"23 4356N" | "DD MMSSH" | 23.73222 | |
"23 4356N" | "DD MMSSH" | 23.73222 | a space matches several spaces |
"2343.5N" | "DDMM.mH" | 23.725 | |
"45N54.2356" | "DDHMM.m" | 45.90393 | H in the middle |
"s4530" | "HDDMM" | -45.5 | H in front, in lower case |
"40°25'06\"N" | "DD°MM'SS\"H" | 40.41833 | symbols must match exactly |
"40:25:06" | "DD:MM:SS" | 40.41833 | |
"40.25" | "DD.MM" | 40.41667 | a dot before M is a plain dot |
"45.5" | "DD.d" | 45.5 | decimal degrees |
"-4530" | "DDMM" | -45.5 | minus sign |
"\u22124530" | "DDMM" | -45.5 | Unicode minus sign |
"-45S" | "DDH" | -45 | sign and letter agree |
"+45S" | "DDH" | -45 | the letter gives the sign |
"-45N" | "DDH" | NA | sign and letter disagree |
"45E" | "DDH" | NA | E is not a latitude letter |
"4530" | c("DDMM", "DD MM") | 45.5 | the first pattern matches |
"45 30" | c("DDMM", "DD MM") | 45.5 | the second pattern matches |
"453" | "DDMM" | NA | 3 digits do not match 4 letters |
"45 03 02N" | c("DD MM SSH", "D MM SSH") | 45.05056 | the first pattern matches |
"5 03 02N" | c("DD MM SSH", "D MM SSH") | 5.05056 | no leading zero: the second pattern matches |
"5°3'2\"N" | "DD°MM'SS\"H" | NA | DD needs exactly 2 digits |
"4575" | "DDMM" | NA | 75 minutes |
"9000S" | "DDMMH" | -90 | lowest latitude |
"9130" | "DDMM" | NA | latitude above 90 |
Errors
Throws warnings on parsing errors, and returns NA in each case
Types of errors:
invalid argument: e.g., letters passed instead of numbers, see https://en.cppreference.com/cpp/error/invalid_argument
out of range: numbers of out acceptable range, see https://en.cppreference.com/cpp/error/out_of_range
out of latitude range: not within -90/90 range
minutes or seconds out of range (negative or 60 and above): not within 0/60 range, e.g.
"10 30,12345"is read as 10 degrees, 30 minutes and 12345 seconds, so it gives a warning andNAa decimal degree or minute followed by another slot: only the last slot can be decimal, e.g.
"45.5 30"a minus sign that disagrees with the direction letter: minus sign and direction letter disagree, e.g.
"N -45"or"-45N"a number with two decimal points: invalid number, e.g.
"40.25.30"with
format, a value that does not match any pattern: does not match format, see the Format section
A comma or space is always a separator between degrees, minutes and seconds, never a decimal mark.
Examples
parse_lat("")
# with format: say how the values are written (see the Format section)
parse_lat("512839N", format = "DDMMSSH") # ICAO => 51.4775
parse_lat("4556.98735", format = "DDMM.mmmmm") # NMEA => 45.94979
# after a dot, any number of decimals
parse_lat(c("4556.9", "4556.98735"), format = "DDMM.m")
# no dot: 5 letters, so exactly 5 decimals => 45.94979
parse_lat("N455698735", format = "HDDMMmmmmm")
parse_lat("2343 56N", format = "DDMM SSH") # => 23.73222
parse_lat("23 4356N", format = "DD MMSSH") # => 23.73222
parse_lat("2343.5N", format = "DDMM.mH") # => 23.725
parse_lat("45N54.2356", format = "DDHMM.m") # => 45.90393
parse_lat("40°25'06\"N", format = "DD°MM'SS\"H") # => 40.41833
parse_lat(c("-45S", "+45S"), format = "DDH") # => -45 -45
parse_lat(4530, format = "DDMM") # numbers work too => 45.5
# several formats, tried in order for each value
parse_lat(c("4530", "45 30", "N4530"), format = c("DDMM", "DD MM", "HDDMM"))
# no leading zero: a second format with one D
parse_lat(c("45 03 02N", "5 03 02N"), format = c("DD MM SSH", "D MM SSH"))
# NA and one warning: sign and letter disagree, wrong letter, 75 minutes
parse_lat(c("-45N", "45E", "4575"), format = c("DDH", "DDMM"))
# without format, packed digits give NA and a warning that points to format
parse_lat("2343 56N")
## Not run:
parse_lat("-91")
parse_lat("95")
parse_lat("asdfaf")
parse_lat("45")
parse_lat("-45")
parse_lat("-45.2323")
# out of range with std::stod?
parse_lat("-45.23232e24")
parse_lat("-45.23232e2")
# numeric input
parse_lat(1:10)
parse_lat(85:94)
# different formats
parse_lat("40.4183318 N")
parse_lat("40.4183318 S")
parse_lat("40 25 5.994") # => 40.41833
parse_lat("40.4183318N")
parse_lat("N40.4183318")
parse_lat("40.4183318S")
parse_lat("S40.4183318")
parse_lat("N 39 21.440") # => 39.35733
parse_lat("S 56 1.389") # => -56.02315
parse_lat("N40°25’5.994") # => 40.41833
parse_lat("40° 25´ 5.994\" N") # => 40.41833
parse_lat("40:25:6N")
parse_lat("40:25:5.994N")
parse_lat("40d 25’ 6\" N")
## End(Not run)
parse string with lat and lon together
Description
parse string with lat and lon together
Usage
parse_llstr(str, format = NULL, order = c("auto", "lat_lon", "lon_lat"))
Arguments
str |
(character) string with latitude and longitude, one or more in a vector. |
format |
(character) |
order |
(character) which coordinate comes first in |
Details
With format = NULL, each string is cut into two parts, then the latitude
part goes through parse_lat() and the longitude part through
parse_lon(). The Order section says which part is which. The cut is made
at:
the comma, when the string has exactly one comma. Spaces around the comma and inside each part are fine:
"45.5, -74.5","45 30 15 S , 74 30 15 W"the only space, when the string has no comma and exactly one space:
"45.5 -74.5","N45.5 W74.5","45.5N 74.5W". Degrees, minutes and seconds then need a symbol between them, not a space:"45°30'15\"S 74°30'15\"W","45:30:15 -74:30:15"
Strings with no separator ("N45.5W74.5"), with two or more commas, or
with two or more spaces and no comma ("45 30 74 30") give NA with a
warning: use format for them. A dot is never a separator: "45.74" is
one number, not two coordinates.
Each part accepts the formats of parse_lat() and parse_lon():
decimal degrees, signed:
"45.5, -74.5"a hemisphere letter before or after the number:
"N45.5, W74.5","45.5S, 74.5E"degrees and decimal minutes:
"45 30.5, -74 30.5","N45°30.5', W74°30.5'"degrees, minutes and seconds with any symbols or a dash between them:
"45°30'15\", -74°30'15\"","45:30:15N, 74:30:15W","45-30-15, 74-30-15"a Unicode minus sign or a dash (U+2212,
–) in front of a number is read as a minus sign
Value
A data.frame with the columns lat and lon, in decimal degrees,
always in this order, whatever the order in str.
Each part of a string is read on its own: when one part cannot be read,
it gives NA with a warning, and the other part is kept. So
"abc, -74.5" gives lat NA and lon -74.5. There is one exception:
when the latitude is out of range (-90/90) but would be a valid
longitude (-180/360), and the string has no direction letter, as in
"101.347, 4.9196", the two values are probably swapped. Then both are
NA, with a warning that suggests the other order.
Both values are also NA, with a warning, when a string cannot be cut
into two coordinates, does not match any format, or has direction
letters that do not fit the order. NA, "" and blank strings give
NA without a warning.
Order
With order = "auto" (the default), the direction letters N, S, E and W
(in upper or lower case) say which coordinate comes first:
latitude first, when there is no letter (
"45.5, -74.5"), or when the letters fit that ("N45.5, W74.5","45.5, 74.5W")longitude first, when the letters only fit that: the first part has E or W, or the second part has N or S (
"W74.5, N45.5","74.5W, 45.5","-74.5, 45.5N"). When only one part has a letter, that part decides.-
NAfor both values, with a warning, when the letters contradict each other:"E45, E30","N45, N30"
With order = "lat_lon" or order = "lon_lat", the order is forced. Use
it for strings with longitude first and no letters:
parse_llstr("-74.5, 45.5", order = "lon_lat"). A string with letters
that do not fit the forced order gives NA for both values, with a
warning: "N45.5, W74.5" with order = "lon_lat".
Format
With format, one pattern describes the whole string: both coordinates and
what is between them. For example, "DDMMSSH DDDMMSSH" reads
"512839N 0002741W". Each coordinate follows the rules of the Format
section of parse_lat(). In addition:
The pattern needs exactly two
Dblocks, one for each coordinate. It is cut in two at the start of the secondDblock:"DDMMSSH DDDMMSSH"gives"DDMMSSH "and"DDDMMSSH".The two coordinates need no separator:
"DDMMSSHDDDMMSSH"reads"512839N0002741W".Each coordinate can have at most one
H. AnHright between the two coordinates goes with the first one, as in"DDMMSSHDDDMMSSH", unless the first one already has anH. Then it goes with the second one, as in"HD.ddddHDDD.ddd", which reads"N4.9196E101.347".The coordinates follow
order. With"lon_lat", the first one is the longitude, as in"DDDMMSSH DDMMSSH". With"auto", theHletters say which one is the latitude, and a pattern withoutHmeans latitude first. The number ofDletters does not decide.The string is matched as it is: it is not cleaned, and not cut at a comma or a space, as with
format = NULL. Spaces at the start and at the end are ignored.Strings that do not match any pattern, or that give
NAfor one value or both, are listed in one warning. They are not read again withformat = NULL.A pattern that cannot work is an error, for example
"DDMMSS"(only oneDblock) or"DDMMSSHH DDDMMSSH"(twoHfor one coordinate).
Format examples
The rules for format are in the Format section above and in the Format
section of parse_lat(). order is "auto" when not given.
str | format | order | lat | lon | Why |
"512839N 0002741W" | "DDMMSSH DDDMMSSH" | 51.4775 | -0.46139 | ICAO pair | |
"512839N0002741W" | "DDMMSSHDDDMMSSH" | 51.4775 | -0.46139 | ICAO pair without a separator | |
"512839N/0002741W" | "DDMMSSH/DDDMMSSH" | 51.4775 | -0.46139 | any separator, written as in the string | |
"51°28'39\"N 000°27'41\"W" | "DD°MM'SS\"H DDD°MM'SS\"H" | 51.4775 | -0.46139 | symbols must match exactly | |
"4556.98735N 01045.12345W" | "DDMM.mH DDDMM.mH" | 45.94979 | -10.75206 | NMEA pair | |
"4916.45,N,12311.12,W" | "DDMM.m,H,DDDMM.m,H" | 49.27417 | -123.18533 | NMEA sentence, letters in their own fields | |
"N455698735W0104512345" | "HDDMMmmmmmHDDDMMmmmmm" | 45.94979 | -10.75206 | decimal point not written: exactly 5 decimals | |
"N4.9196E101.347" | "HD.ddddHDDD.ddd" | 4.9196 | 101.347 | H in front of each coordinate, no separator |
|
"4.9196N101.347E" | "D.dHDDD.dH" | 4.9196 | 101.347 | H after each coordinate, no separator |
|
"-45.5 -074.5" | "DD.d DDD.d" | -45.5 | -74.5 | minus signs | |
"0002741W 512839N" | "DDDMMSSH DDMMSSH" | 51.4775 | -0.46139 | longitude first: the letters decide | |
"E101.347N4.9196" | "HDDD.dHD.d" | 4.9196 | 101.347 | longitude first, no separator | |
"101.347 4.9196" | "DDD.d D.d" | "lon_lat" | 4.9196 | 101.347 | longitude first, no letters: say so with order |
"101.347 4.9196" | "DDD.d D.d" | NA | NA | no letters, so latitude first, but 101.347 is not a latitude: probably swapped | |
"512839N 0002741W" | "DDMMSSH DDDMMSSH" | "lon_lat" | NA | NA | the letters do not fit the forced order |
"512839N 0002741N" | "DDMMSSH DDDMMSSH" | NA | NA | two latitude letters | |
"517539N 0002741W" | "DDMMSSH DDDMMSSH" | NA | -0.46139 | 75 minutes: the longitude is kept | |
"512839N 1900000W" | "DDMMSSH DDDMMSSH" | 51.4775 | NA | longitude below -180: the latitude is kept | |
"51 28 39N 000 27 41W" | "DDMMSSH DDDMMSSH" | NA | NA | does not match the pattern | |
"51 28 39N 000 27 41W" | c("DDMMSSH DDDMMSSH", "DD MM SSH DDD MM SSH") | 51.4775 | -0.46139 | the second pattern matches | |
"512839N0002741W" | c("DDMMSSH DDDMMSSH", "DDMMSSHDDDMMSSH") | 51.4775 | -0.46139 | the second pattern matches | |
"45.5, -74.5" | NULL | 45.5 | -74.5 | no letters: latitude first | |
"W74.5, N45.5" | NULL | 45.5 | -74.5 | longitude first: the letters decide | |
"74.5W, 45.5" | NULL | 45.5 | -74.5 | only one part has a letter: it decides | |
"-74.5, 45.5" | NULL | "lon_lat" | 45.5 | -74.5 | longitude first, no letters: say so with order |
"N45.5, W74.5" | NULL | "lon_lat" | NA | NA | the letters do not fit the forced order |
"E45, E30" | NULL | NA | NA | two longitude letters | |
"101.347, 4.9196" | NULL | NA | NA | no letters, so latitude first, but 101.347 is not a latitude: probably swapped | |
"abc, -74.5" | NULL | NA | -74.5 | the latitude cannot be read: the longitude is kept | |
"N4.9196E101.347" | NULL | NA | NA | no separator: use format
|
Examples
parse_llstr("N 04.1683, E 101.5823")
parse_llstr("N04.82344, E101.61320")
parse_llstr("N 04.25164, E 101.70695")
parse_llstr("N05.03062, E101.75172")
parse_llstr("N05.03062,E101.75172")
parse_llstr("N4.9196, E101.345")
parse_llstr("N4.9196, E101.346")
parse_llstr("N4.9196, E101.347")
# no comma: the only space is the separator
parse_llstr("N4.9196 E101.347")
# signed decimal degrees
parse_llstr("4.9196 -101.347")
# DMS
parse_llstr("N4 51'36\", E101 34'7\"")
parse_llstr(c("4 51'36\"S, 101 34'7\"W", "N4 51'36\", E101 34'7\""))
parse_llstr("4:51:36S 101:34:7W")
# which coordinate comes first (see the Order section)
parse_llstr("W101.347, N4.9196") # the letters say longitude first
parse_llstr("101.347E, 4.9196") # one letter is enough
parse_llstr("-101.347, 4.9196", order = "lon_lat") # no letters: say it
# NA and a warning: the letters contradict each other
parse_llstr("E45, E30")
# NA for both, and a warning: 101.347 is not a latitude, so the values
# are probably swapped
parse_llstr("101.347, 4.9196")
parse_llstr("101.347, 4.9196", order = "lon_lat")
# one part cannot be read: the other one is kept
parse_llstr("abc, -74.5")
# with format: one pattern for the whole string (see the Format section)
parse_llstr("512839N 0002741W", format = "DDMMSSH DDDMMSSH") # ICAO
parse_llstr("512839N0002741W", format = "DDMMSSHDDDMMSSH") # no separator
parse_llstr("4916.45,N,12311.12,W", format = "DDMM.m,H,DDDMM.m,H") # NMEA
parse_llstr("N4.9196E101.347", format = "HD.ddddHDDD.ddd")
# longitude first: the letters decide
parse_llstr("0002741W 512839N", format = "DDDMMSSH DDMMSSH")
# longitude first, without letters
parse_llstr("101.347 4.9196", format = "DDD.d D.d", order = "lon_lat")
# several formats, tried in order for each string
parse_llstr(
c("512839N 0002741W", "512839N0002741W", "51 28 39N 000 27 41W"),
format = c("DDMMSSH DDDMMSSH", "DDMMSSHDDDMMSSH", "DD MM SSH DDD MM SSH")
)
# NA and one warning: 75 minutes (the longitude is kept), and two
# latitude letters
parse_llstr(
c("517539N 0002741W", "512839N 0002741N"),
format = "DDMMSSH DDDMMSSH"
)
# without format, a string with no separator gives NA and a warning
parse_llstr("N4.9196E101.347")
# add both columns with one call, picking them by name
df <- data.frame(x = c("N4.9196, E101.347", "4.9196 -101.347"))
cbind(df, parse_llstr(str = df[["x"]])[c("lat", "lon")])
# dplyr: df |> dplyr::mutate(parse_llstr(str = x)[c("lat", "lon")])
# data.table: dt[, c("lat", "lon") := parse_llstr(str = x)[c("lat", "lon")]]
Parse longitude values
Description
Parse longitude values
Usage
parse_lon(lon, format = NULL)
Arguments
lon |
(numeric/integer/character) one or more longitude values. |
format |
(character) |
Value
numeric vector
Format examples
The rules for format are in the Format section below.
| Value | format | Result | Why |
"0002741W" | "DDDMMSSH" | -0.46139 | ICAO longitude |
"1452741E" | "DDDMMSSH" | 145.46139 | ICAO longitude, east |
"01045.5W" | "DDDMM.mH" | -10.75833 | NMEA |
"01045.12345W" | "DDDMM.mH" | -10.75206 | after a dot, any number of decimals |
"W0104512345" | "HDDDMMmmmmm" | -10.75206 | decimal point not written: exactly 5 decimals |
"E0451530" | "HDDDMMSS" | 45.25833 | H in front |
"0451530e" | "DDDMMSSH" | 45.25833 | H at the end, in lower case |
"045E15.5" | "DDDHMM.m" | 45.25833 | H in the middle |
"07443 56W" | "DDDMM SSH" | -74.73222 | |
"074 3056W" | "DDD MMSSH" | -74.51556 | |
"074 3056W" | "DDD MMSSH" | -74.51556 | a space matches several spaces |
"074°30'56\"W" | "DDD°MM'SS\"H" | -74.51556 | symbols must match exactly |
"074:30:56" | "DDD:MM:SS" | 74.51556 | |
"120.5" | "DDD.d" | 120.5 | decimal degrees |
"-0743056" | "DDDMMSS" | -74.51556 | minus sign |
"\u22120743056" | "DDDMMSS" | -74.51556 | Unicode minus sign |
"-074W" | "DDDH" | -74 | sign and letter agree |
"+074W" | "DDDH" | -74 | the letter gives the sign |
"-074E" | "DDDH" | NA | sign and letter disagree |
"074N" | "DDDH" | NA | N is not a longitude letter |
"0743056W" | c("DDDMMSSH", "DDD MM SSH") | -74.51556 | the first pattern matches |
"074 30 56W" | c("DDDMMSSH", "DDD MM SSH") | -74.51556 | the second pattern matches |
"74 30 56W" | c("DDD MM SSH", "DD MM SSH", "D MM SSH") | -74.51556 | no leading zero: the second pattern matches |
"7 30 56W" | c("DDD MM SSH", "DD MM SSH", "D MM SSH") | -7.51556 | no leading zeros: the third pattern matches |
"74 30 56W" | "DDD MM SSH" | NA | DDD needs exactly 3 digits |
"0747530W" | "DDDMMSSH" | NA | 75 minutes |
"1800000W" | "DDDMMSSH" | -180 | lowest longitude |
"1800001W" | "DDDMMSSH" | NA | longitude below -180 |
"18030W" | "DDDMMH" | NA | longitude below -180 |
"3600000E" | "DDDMMSSH" | 360 | highest longitude |
"3600001E" | "DDDMMSSH" | NA | longitude above 360 |
Errors
Throws warnings on parsing errors, and returns NA in each case
Types of errors:
invalid argument: e.g., letters passed instead of numbers, see https://en.cppreference.com/cpp/error/invalid_argument
out of range: numbers of out acceptable range, see https://en.cppreference.com/cpp/error/out_of_range
out of longitude range: not within -180/360 range
minutes or seconds out of range (negative or 60 and above): not within 0/60 range, e.g.
"10 30,12345"is read as 10 degrees, 30 minutes and 12345 seconds, so it gives a warning andNAa decimal degree or minute followed by another slot: only the last slot can be decimal, e.g.
"45.5 30"a minus sign that disagrees with the direction letter: minus sign and direction letter disagree, e.g.
"E -45"or"-45E"a number with two decimal points: invalid number, e.g.
"40.25.30"with
format, a value that does not match any pattern: does not match format, see the Format section
A comma or space is always a separator between degrees, minutes and seconds, never a decimal mark.
Format
A pattern describes how the values are written. It is made of letters and
other characters. The idea comes from AngleFormat in Apache SIS and from
CLongLatString.
-
D,MandSstand for the digits of the whole degrees, minutes and seconds. The number of letters is the exact number of digits:DDmeans exactly 2 digits, so it reads"05"but not"5", andDDDmeans exactly 3 digits, as longitudes often need. Values written without leading zeros need a second pattern, for examplec("DD MM SSH", "D MM SSH")reads both"45 03 02N"and"5 03 02N".Dis required.MandSare optional, butSneedsM, and they come in the orderD,M,S. -
d,mandsstand for the decimal digits of the degrees, minutes or seconds. Only the last unit can have them. They come right after it, as in"DDMMmmm", or after a dot that follows it, as in"DDMM.m".Without a dot, the decimal point is not written in the value. The number of letters is then the exact number of decimal digits: it is the only way to know where the hidden decimal point is.
"HDDMMmmmmm"reads"N455698735"as N 45 degrees 56.98735 minutes.After a dot, the value has a decimal point and one or more decimal digits, as many as you like:
"DDMM.m"reads both"4556.9"and"4556.98735".
-
Hstands for the hemisphere letter:NorSinparse_lat(),EorWinparse_lon(), in upper or lower case.SandWgive a negative value.Hcan be at the start, at the end or in the middle, but only once. A letter of the other pair, for exampleEinparse_lat(), givesNA. A space matches one or more spaces (or other white space).
Any other character, for example
°,',",:, or a dot that is not followed byd,mors, must be in the value exactly as in the pattern.A sign can always be written right before the degrees:
+,-, or a Unicode minus (U+2212), en dash–or em dash—. WithH, the rules are the same as withformat = NULL:"-45S"and"+45S"give -45, but"-45N"givesNA, because the minus sign and the letter disagree.
Spaces at the start and at the end of a value are ignored. The pattern is
matched against the value as it is given: the cleaning done with
format = NULL, for example reading d or º as a degree mark, does not
happen.
A value gives NA when it does not match any pattern, when its minutes or
seconds are 60 or more, or when the result is out of range (latitude
outside -90/90, longitude outside -180/360). All these values are listed in
one warning. They are not read again with format = NULL. A missing value
(NA) or an empty string gives NA without a warning. A pattern that
cannot work, for example "MMDD", "DDmmSS" or "DDX", is an error.
Numbers are turned into text first, so they lose their leading zeros:
parse_lon(0002741, format = "DDDMMSS") sees "2741" and gives NA.
Give such values as text: parse_lon("0002741", format = "DDDMMSS").
See the Format examples section for many examples.
Examples
parse_lon("")
# with format: say how the values are written (see the Format section)
parse_lon("0002741W", format = "DDDMMSSH") # ICAO => -0.46139
parse_lon("E0451530", format = "HDDDMMSS") # => 45.25833
parse_lon("01045.5W", format = "DDDMM.mH") # NMEA => -10.75833
parse_lon(c("0002741W", "000 27 41W"), format = c("DDDMMSSH", "DDD MM SSH"))
# no leading zeros: one format for each number of degree digits
parse_lon(
c("074 30 56W", "74 30 56W", "7 30 56W"),
format = c("DDD MM SSH", "DD MM SSH", "D MM SSH")
)
# NA and one warning: N is not a longitude letter, below -180
parse_lon(c("045N", "18030W"), format = c("DDDH", "DDDMMH"))
## Not run:
parse_lon("-181")
parse_lon("-361")
parse_lon("95")
parse_lon("asdfaf")
parse_lon("45")
parse_lon("-45")
parse_lon("-45.2323")
parse_lon("334")
# out of range with std::stod?
parse_lon("-45.23232e24")
parse_lon("-45.23232e2")
parse_lon("-45.23232")
# numeric input
parse_lon(1:10)
parse_lon(85:94)
# different formats
parse_lon("40.4183318 E")
parse_lon("40.4183318 W")
parse_lon("40 25 5.994") # => 40.41833
parse_lon("40.4183318W")
parse_lon("W40.4183318")
parse_lon("E40.4183318")
parse_lon("40.4183318E")
parse_lon("E 39 21.440") # => 39.35733
parse_lon("W 56 1.389") # => -56.02315
parse_lon("E40°25’5.994") # => 40.41833
parse_lon("40° 25´ 5.994\" E") # => 40.41833
parse_lon("40:25:6E")
parse_lon("40:25:5.994E")
parse_lon("40d 25’ 6\" E")
## End(Not run)
parse longitude and latitude
Description
parse longitude and latitude
Usage
parse_lon_lat(lon, lat, lon_format = NULL, lat_format = NULL)
Arguments
lon |
(character/numeric/integer) one or more longitude values |
lat |
(character/numeric/integer) one or more latitude values |
lon_format, lat_format |
(character) |
Details
length(lon) == length(lat). Longitude comes first, in the arguments and in the result.
Value
A data.frame with the columns lon and lat. An invalid value gives
NA with a warning. Each value is read on its own, so the other value
of the row is kept.
Format examples
lon | lat | lon_format | lat_format | Result lon | Result lat | Why |
"0002741W" | "512839N" | "DDDMMSSH" | "DDMMSSH" | -0.46139 | 51.4775 | ICAO |
"01045.12345W" | "4556.98735N" | "DDDMM.mH" | "DDMM.mH" | -10.75206 | 45.94979 | NMEA |
"W0104512345" | "N455698735" | "HDDDMMmmmmm" | "HDDMMmmmmm" | -10.75206 | 45.94979 | decimal point not written: exactly 5 decimals |
"-120.43" | "4907.2" | NULL | "DDMM.m" | -120.43 | 49.12 | a format for the latitude only |
"12043W" | "49.12" | "DDDMMH" | NULL | -120.71667 | 49.12 | a format for the longitude only |
"0743056W" | "453015N" | c("DDDMMSSH", "DDD MM SSH") | c("DDMMSSH", "DD MM SSH") | -74.51556 | 45.50417 | the first patterns match |
"074 30 56W" | "45 30 15N" | c("DDDMMSSH", "DDD MM SSH") | c("DDMMSSH", "DD MM SSH") | -74.51556 | 45.50417 | the second patterns match |
"1800001W" | "512839N" | "DDDMMSSH" | "DDMMSSH" | NA | 51.4775 | longitude below -180: the latitude is kept |
"0002741W" | "517539N" | "DDDMMSSH" | "DDMMSSH" | -0.46139 | NA | 75 minutes: the longitude is kept |
"512839N" | "0002741W" | "DDDMMSSH" | "DDMMSSH" | NA | NA | swapped: longitude comes first |
Examples
parse_lon_lat("-120.43", "49.12")
# with formats (see the Format section of ?parse_lat)
# ICAO => -0.46139 51.4775
parse_lon_lat(
"0002741W",
"512839N",
lon_format = "DDDMMSSH",
lat_format = "DDMMSSH"
)
# NMEA => -10.75206 45.94979
parse_lon_lat(
"01045.12345W",
"4556.98735N",
lon_format = "DDDMM.mH",
lat_format = "DDMM.mH"
)
# a format for the latitude only => -120.43 49.12
parse_lon_lat("-120.43", "4907.2", lat_format = "DDMM.m")
# several formats, tried in order for each value
parse_lon_lat(
c("0743056W", "074 30 56W"),
c("453015N", "45 30 15N"),
lon_format = c("DDDMMSSH", "DDD MM SSH"),
lat_format = c("DDMMSSH", "DD MM SSH")
)
# NA and a warning: longitude below -180, the latitude is kept
parse_lon_lat(
"1800001W",
"512839N",
lon_format = "DDDMMSSH",
lat_format = "DDMMSSH"
)
## Not run:
parse_lon_lat("-120.43", "93")
parse_lon_lat("-190", "49.12")
parse_lon_lat("240", "49.12")
parse_lon_lat("-190", "92")
# many
lons <- c("45W54.2356", "181", 45, 45.234234, "-45.98739874")
lats <- c("40.123°", "40.123N74.123W", "191.89", 12, "N45 04.25764")
parse_lon_lat(lons, lats)
## End(Not run)
parse coordinates into degrees, minutes and seconds
Description
parse coordinates into degrees, minutes and seconds
Usage
parse_parts_lon(str)
parse_parts_lat(str)
Arguments
str |
(character) string including longitude or latitude |
Value
data.frame with columns for:
deg (integer)
min (integer)
sec (numeric)
For negative values the sign is carried by the first non-zero part: -45.5
gives deg -45, min 30, but -0.5 gives deg 0, min -30. Minutes are always
below 60 and seconds below 60, in absolute value.
NA given upon error, with a warning
Examples
parse_parts_lon("140.4183318")
## Not run:
parse_parts_lon("174.6411133")
parse_parts_lon("-45.98739874")
parse_parts_lon("40.123W")
parse_parts_lat("45N54.2356")
parse_parts_lat("40.4183318")
parse_parts_lat("-74.6411133")
parse_parts_lat("-45.98739874")
parse_parts_lat("40.123N")
parse_parts_lat("N40°25’5.994")
# multiple
x <- c("40.123°", "40.123N74.123W", "191.89", 12, "N45 04.25764")
parse_parts_lat(x)
system.time(parse_parts_lat(rep(x, 10^2)))
## End(Not run)