Wolfram Research

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 Automatic forms 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" Automatic symbols to use for degree, minute and seconds
"CardinalSymbols" “NESW” cardinal symbols to use
"CardinalPosition" After location of the cardinal symbols with respect to the numbers
"LeadingDigits" None number of digits before the period, padded with zeros on the left
"Precision" Automatic number of digits after the period for the last number
"Separators" Automatic separators to use between degrees, minutes and seconds parts and between the latitude and longitude parts
Possible forms for the options "DMSSymbols" are:
Automatic or True uses ° for degrees, ' for minute and " for seconds
s uses 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 False uses 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 False uses no cardinal symbols; signs are used to indicate the hemispheres
Possible forms for the option "CardinalPosition" are:
Before cardinal symbols are placed before the degrees, minutes and seconds
After cardinal symbols are placed after the degrees, minutes and seconds
Possible forms for the option "LeadingDigits" are:
n uses 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 None uses no leading zeros
True or Automatic uses two digits for the degrees 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 True uses six digits for the form "Degree", four digits for the form "DegreeMinute" and three for the form "DegreeMinuteSecond"
n uses n digits for the last number of both the latitude and the longitude
{ n lat , n lon } 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 True uses no separator for degree, minute, and seconds and a space to separate the latitude and longitude
s uses 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
{ s 1 , s 2 } uses s1to separate the degree, minute, and seconds and s2 for the latitude and longitude parts
None or False uses no separators
Angles in radians can be converted to degrees for use in ResourceFunction["GeneralizedDMSString"] by dividing by Degree.

Examples

Basic Examples

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

Use a GeoPosition instead:

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

DMSSymbols

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

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

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

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

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

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

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

Use underscores between degrees, minutes, and seconds and a comma 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

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

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

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]=

Resource History

License Information