Skip to content

Instantly share code, notes, and snippets.

@pjobson
Last active January 27, 2024 15:28
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save pjobson/8f44ea79d1852900457bc257a4c9fcd5 to your computer and use it in GitHub Desktop.
Save pjobson/8f44ea79d1852900457bc257a4c9fcd5 to your computer and use it in GitHub Desktop.
Latitude / Longitude DMS, DDM, DD Regular Expressions
Degrees Minutes Seconds (DMS)
40° 26′ 46″ N 79° 58′ 56″ W
40° 26′ 46″ S 79° 58′ 56″ E
90° 0′ 0″ S 180° 0′ 0″ E
40° 26′ 45.9996″ N 79° 58′ 55.2″ E
Latitudes range from 0 to 90.
Longitudes range from 0 to 180.
Minutes & Seconds range from 0-60
Use N, S, E or W as either the last character,
which represents a compass direction North, South, East or West.
D & M must be intergers, S may be an interger or float.
DMS.latitude - /^[\+-]?(([1-8]?\d)\D+([1-5]?\d|60)\D+([1-5]?\d|60)(\.\d+)?|90\D+0\D+0)\D+[NSns]?$/
DMS.longitude - /^[\+-]?([1-7]?\d{1,2}\D+([1-5]?\d|60)\D+([1-5]?\d|60)(\.\d+)?|180\D+0\D+0)\D+[EWew]?$/
Degrees Decimal Minutes (DDM)
40° 26.767′ N 79° 58.933′ W
90° 0′ N 180° 0′ W
90° N 180° W
Latitudes range from 0 to 90.
Longitudes range from 0 to 180.
Use N, S, E or W as either the last character,
which represents a compass direction North, South, East or West.
The last degree, minute, or second of a latitude or longitude
may contain a decimal portion.
DDM.latitude - /^[\+-]?(([1-8]?\d)\D+[1-6]?\d(\.\d{1,})?|90(\D+0)?)\D+[NSns]?$/
DDM.longitude - /^[\+-]?((1[0-7]\d|[1-9]?\d)\D+[1-6]?\d(\.\d{1,})?|180(\D+0)?)\D+[EWew]?$/
Decimal Degrees (DD)
40.446° N 79.982° W
Precede South latitudes and West longitudes with a minus sign.
Latitudes range from -90 to 90
Longitudes range from -180 to 180
Up to 6 decimal places
DD.latitude - /^[\+-]?(([1-8]?\d)(\.\d{1,})?|90)\D*[NSns]?$/
DD.longitude - /^[\+-]?((1[0-7]\d|[1-9]?\d)(\.\d{1,})?|180)\D*[EWew]?$/
IN JS:
const patterns = {
lat: {
isDMS: /^(([1-8]?[0-9])\D+([1-5]?[0-9]|60)\D+([1-5]?[0-9]|60)(\.[0-9]+)?|90\D+0\D+0)\D+[NSns]$/,
isDDM: /^(([1-8]?[0-9])\D+[1-6]?[0-9](\.\d{1,3})?|90(\D+0)?)\D+([NSns])$/,
isDD: /^[\+-]?(([1-8]?[0-9])(\.\d{1,6})?|90)\D*[NSns]?$/
},
lng: {
isDMS: /^((1[0-7][0-9]|[1-9]?[0-9])\D+([1-5]?[0-9]|60)\D+([1-5]?[0-9]|60)(\.[0-9]+)?|180\D+0\D+0)\D+[EWew]$/,
isDDM: /^((1[0-7][0-9]|[1-9]?[0-9])\D+[1-6]?[0-9](\.\d{1,3})?|180(\D+0)?)\D+([EWew])$/,
isDD: /^[\+-]?((1[0-7][0-9]|[1-9]?[0-9])(\.\d{1,6})?|180)\D*[EWew]?$/
}
};
Combined:
patterns.lat.combined = new RegExp(`^(${patterns.lat.map(tt => `(${tt.toString().replace(/^\/\^(.+?)\$\//g,'$1')})`).join('|')})$`);
patterns.lng.combined = new RegExp(`^(${patterns.lng.map(tt => `(${tt.toString().replace(/^\/\^(.+?)\$\//g,'$1')})`).join('|')})$`)
@pjobson
Copy link
Author

pjobson commented Jul 8, 2021

Updated 8 July 2021 - Removed lodash stuff, converted to ES6 style map.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment