Function Repository Resource:

GeneralizedDMSString

Source Notebook

Convert an angle into a customizable degree-minute-second string

Contributed by: Sander Huisman

ResourceFunction["GeneralizedDMSString"][θ]

converts the angle θ in decimal degrees to a DMS string.

ResourceFunction["GeneralizedDMSString"][θ,form]

converts the angle θ in decimal degrees to a form string.

ResourceFunction["GeneralizedDMSString"][{lat,lon}]

converts latitude lat and longitude lon in decimal degrees to a DMS latitude-longitude string.

ResourceFunction["GeneralizedDMSString"][{lat,lon},form]

converts latitude lat and longitude lon in decimal degrees to a form latitude-longitude string.

ResourceFunction["GeneralizedDMSString"][GeoPosition[{lat,lon}],form]

converts a GeoPosition to a form latitude-longitude string.

Details and Options

ResourceFunction["GeneralizedDMSString"][{θ,"NS"}] formats the angle θ as a latitude.
ResourceFunction["GeneralizedDMSString"][{θ,"EW"}] formats the angle θ as a longitude.
form can be one of the following:
"DegreeMinuteSecond" or "DMS" or Automaticforms a string with degrees, minutes and seconds
"DegreeMinute" or "DM"forms a string with degrees and minutes
"Degree" or "D"forms a string with degrees
ResourceFunction["GeneralizedDMSString"] has the following options:
"DMSSymbols"Automaticsymbols to use for degree, minute and seconds
"CardinalSymbols""NESW"cardinal symbols to use
"CardinalPosition"Afterlocation of the cardinal symbols with respect to the numbers
"LeadingDigits"Nonenumber of digits before the period, padded with zeros on the left
"Precision"Automaticnumber of digits after the period for the last number
"Separators"Automaticseparators to use between degrees, minutes and seconds parts and between the latitude and longitude parts
Possible forms for the options "DMSSymbols" are:
Automatic or Trueuses ° for degrees, ' for minute and " for seconds
suses s for degrees, minutes and seconds
{d}uses d for degrees and nothing for minutes and seconds
{d,m}uses d for degrees, m for minutes and nothing for seconds
{d,m,s}uses d for degrees, m for minutes and s for seconds
None or Falseuses no symbols
Possible forms for the option "CardinalSymbols" are:
"NESW" or Automatic or True"N" for north, "E" for east, "S" for south, "W" for west
"NorthEastSouthWest""North" for north, "East" for east, "South" for south, "West" for west
"NE""N" for latitude, "E" for longitude; signs are used to indicate south and west
"NorthEast""North" for latitude, "East" for longitude; signs are used to indicate south and west
{n,e}uses n for latitude, and e for longitude; signs are used to indicate south and west
{{n,s},{e,w}}uses n for north, s for south, e for east, w for west
None or Falseuses no cardinal symbols; signs are used to indicate the hemispheres
Possible forms for the option "CardinalPosition" are:
Beforecardinal symbols are placed before the degrees, minutes and seconds
Aftercardinal symbols are placed after the degrees, minutes and seconds
Possible forms for the option "LeadingDigits" are:
nuses n digits for the degrees in the latitude and the longitude
{n}uses n digits for the degrees in the latitude and the longitude
{n,m}uses n digits for the degrees and m for the minutes
{n,m,o}uses n digits for the degrees, m for the minutes and o for the seconds
False or Noneuses no leading zeros
True or Automaticuses two digits for the degree part of the latitude and three for the degree part of the longitude
{{nlat,nlon},{mlat,mlon},{olat,olon}}specification for the general case for the degrees, minutes and seconds for both the latitude and the longitude
Possible forms for the option "Precision" are:
Automatic or Trueuses six digits for the form "Degree", four digits for the form "DegreeMinute" and three for the form "DegreeMinuteSecond"
nuses n digits for the last number of both the latitude and the longitude
{nlat,nlon}uses nlat digits for the last number of the latitude and nlon for the last number of the longitude
Possible forms for the option "Separators" are:
Automatic or Trueuses no separator for degree, minute and seconds and a space to separate the latitude and longitude
suses s to separate the degree, minute and seconds and the latitude and longitude parts
{s}uses s to separate the degree, minute and seconds and the latitude and longitude parts
{s1,s2}uses s1to separate the degree, minute and seconds and s2 for the latitude and longitude parts
None or Falseuses no separators
Angles in radians can be converted to degrees for use in ResourceFunction["GeneralizedDMSString"] by dividing by Degree.

Examples

Basic Examples (4) 

Convert an angle in a DMS string:

In[1]:=
ResourceFunction["GeneralizedDMSString"][52.22]
Out[1]=

Convert a latitude-longitude pair:

In[2]:=
ResourceFunction["GeneralizedDMSString"][{-52.22`, -6.89`}]
Out[2]=

Convert to a string with degrees and minutes:

In[3]:=
ResourceFunction[
 "GeneralizedDMSString"][{52.2233445566, 6.89012345678}, "DegreeMinute"]
Out[3]=

Convert to a string with only degrees:

In[4]:=
ResourceFunction[
 "GeneralizedDMSString"][{52.2233445566, 6.89012345678}, "Degree"]
Out[4]=

Scope (3) 

Use a GeoPosition:

In[5]:=
ResourceFunction["GeneralizedDMSString"][GeoPosition[{52.22`, 6.89`}]]
Out[5]=

Convert only a latitude into a DMS string:

In[6]:=
ResourceFunction["GeneralizedDMSString"][{-52.22, "NS"}]
Out[6]=

Convert only a longitude into a DMS string:

In[7]:=
ResourceFunction["GeneralizedDMSString"][{-6.89`, "EW"}, "CardinalSymbols" -> "NorthEastSouthWest", "CardinalPosition" -> Before]
Out[7]=

Options (20) 

DMSSymbols (3) 

Use different symbols for degrees, minutes and seconds:

In[8]:=
ResourceFunction["GeneralizedDMSString"][{52.22`, 6.89`}, "DMSSymbols" -> {" deg", " min", " sec"}, "Separators" -> {" ", ", "}]
Out[8]=

Use no symbols for degrees, minutes and seconds:

In[9]:=
ResourceFunction["GeneralizedDMSString"][{52.22`, 6.89`}, "DMSSymbols" -> None, "Separators" -> " "]
Out[9]=

Use the default symbols for degrees, minutes and seconds:

In[10]:=
ResourceFunction["GeneralizedDMSString"][{52.22`, 6.89`}, "DMSSymbols" -> Automatic]
Out[10]=

CardinalSymbols (6) 

Use no cardinal indicators:

In[11]:=
ResourceFunction["GeneralizedDMSString"][{-52.22`, 6.89`}, "CardinalSymbols" -> None]
Out[11]=

Use N for the latitude and E for the longitude; signs indicate the hemisphere:

In[12]:=
ResourceFunction["GeneralizedDMSString"][{-52.22`, 6.89`}, "CardinalSymbols" -> "NE"]
Out[12]=

Signs are removed and replaced by the proper cardinal direction:

In[13]:=
ResourceFunction["GeneralizedDMSString"][{-52.22`, 6.89`}, "CardinalSymbols" -> "NESW"]
Out[13]=

Use the standard four cardinal directions written fully out:

In[14]:=
ResourceFunction["GeneralizedDMSString"][{-52.22`, 6.89`}, "CardinalSymbols" -> "NorthEastSouthWest", "Separators" -> " "]
Out[14]=

Use custom cardinal directions:

In[15]:=
ResourceFunction["GeneralizedDMSString"][{-52.22`, 6.89`}, "CardinalSymbols" -> {{"Noord", "Zuid"}, {"Oost", "West"}}, "Separators" -> " "]
Out[15]=

Use a custom specification where the sign is retained:

In[16]:=
ResourceFunction["GeneralizedDMSString"][{-52.22`, 6.89`}, "CardinalSymbols" -> {"Noord", "Oost"}, "Separators" -> " "]
Out[16]=

CardinalPosition (2) 

Place the cardinal symbols before the degrees, minutes and seconds:

In[17]:=
ResourceFunction["GeneralizedDMSString"][{-52.22`, 6.89`}, "CardinalPosition" -> Before]
Out[17]=

Place the cardinal symbols after the degrees, minutes and seconds:

In[18]:=
ResourceFunction["GeneralizedDMSString"][{-52.22`, 6.89`}, "CardinalPosition" -> After]
Out[18]=

LeadingDigits (4) 

Use 3 digits for degrees:

In[19]:=
ResourceFunction["GeneralizedDMSString"][3.0341666666666667`, "LeadingDigits" -> 3]
Out[19]=

Use 3 digits for degrees and 2 for minutes:

In[20]:=
ResourceFunction["GeneralizedDMSString"][3.0341666666666667`, "LeadingDigits" -> {3, 2}]
Out[20]=

Use 3 digits for degrees, 2 for minutes and 2 for seconds:

In[21]:=
ResourceFunction["GeneralizedDMSString"][3.0341666666666667`, "LeadingDigits" -> {3, 2, 2}]
Out[21]=

Use different specifications for the latitude and longitude:

In[22]:=
ResourceFunction[
 "GeneralizedDMSString"][{3.0341666666666667`, 5.017222222222222`}, "LeadingDigits" -> {{2, 3}, {2, 2}, {2, 2}}]
Out[22]=

Precision (2) 

Use 5 digits for the last number (in this case, the seconds):

In[23]:=
ResourceFunction["GeneralizedDMSString"][{52.2231331`, 6.89424342`}, "Precision" -> 5]
Out[23]=

Use 3 digits for the last number (in this case, the minutes):

In[24]:=
ResourceFunction[
 "GeneralizedDMSString"][{52.2231331`, 6.89424342`}, "DegreeMinute", "Precision" -> 3]
Out[24]=

Separators (3) 

Use underscores between degrees, minutes, seconds, latitude and longitude:

In[25]:=
ResourceFunction["GeneralizedDMSString"][{52.22`, 6.89`}, "Separators" -> "_"]
Out[25]=

Use commas between degrees, minutes and seconds, and an underscore between the latitude and longitude:

In[26]:=
ResourceFunction["GeneralizedDMSString"][{52.22`, 6.89`}, "Separators" -> {",", "_"}]
Out[26]=

Use no separators:

In[27]:=
ResourceFunction["GeneralizedDMSString"][{52.22`, 6.89`}, "Separators" -> None]
Out[27]=

Applications (2) 

Make a function that produces a string generally used in geocaching:

In[28]:=
ClearAll[GeoCachingString]
GeoCachingString[gp : {lat_, lon_}] := ResourceFunction["GeneralizedDMSString"][gp, "DegreeMinute", "Separators" -> " ", "CardinalSymbols" -> {{"N", "S"}, {"E", "W"}}, "CardinalPosition" -> Before, "DMSSymbols" -> {"\[Degree]", "", ""},
   "LeadingDigits" -> {{2, 3}, {2, 2}, {0, 0}}, "Precision" -> 3]

Apply this function to a location:

In[29]:=
GeoCachingString[{52.23235`, 6.9404`}]
Out[29]=

Properties and Relations (2) 

For a latitude-longitude pair, and without any options, GeneralizedDMSString gives the same output as DMSString:

In[30]:=
location = {-52.22121`, 6.89121`};
DMSString[location]
Out[31]=
In[32]:=
ResourceFunction["GeneralizedDMSString"][location]
Out[32]=

GeneralizedDMSString keeps the sign, as opposed to DMSString:

In[33]:=
ResourceFunction["GeneralizedDMSString"][-6.89121]
Out[33]=
In[34]:=
DMSString[-6.89121]
Out[34]=

Possible Issues (2) 

Exact values are approximated using N:

In[35]:=
ResourceFunction["GeneralizedDMSString"][{-10, 6}]
Out[35]=

Do not show the zeros behind the period:

In[36]:=
ResourceFunction["GeneralizedDMSString"][{-10, 6}, "Precision" -> 0]
Out[36]=

Publisher

SHuisman

Version History

  • 3.0.0 – 28 June 2019
  • 2.0.0 – 28 June 2019
  • 1.0.0 – 26 June 2019

License Information