Skip to content

Instantly share code, notes, and snippets.

@bernoneill
Created February 28, 2015 16:05
Show Gist options
  • Save bernoneill/0eb73ae3f7a3e00d4b05 to your computer and use it in GitHub Desktop.
Save bernoneill/0eb73ae3f7a3e00d4b05 to your computer and use it in GitHub Desktop.
This is a simple Groovy port (used Groovy v2.3.8) of a Gist from Chris Gudea Dustin Lambright (Thanks!)
/**
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Authors: Chris Gudea
// Dustin Lambright
// Bernie O'Neill
// Contact: pulala@gmail.com
// dustin.lambright@gmail.com
// bern.oneill@gmail.com
// Version: 1
// Info: This is a simple Groovy port (used Groovy v2.3.8) of a Gist from Authors noted above (Thanks!)
// and found here: https://gist.github.com/cgudea/7c558138cb48b36e785b
// This program uses regular expressions to test single lines of text
// to test for compliance of the expression against coordinate system
// standards.
// Standards:
// DDMS (Degrees Minutes Seconds)
// UTM (Universal Transverse Mercator coordinate system)
// Decimal (-180.0 to 180.0)
// MGRS (Military Grid Reference System)
*/
class Geodesy_regex {
//This is a simple port of code from a Gist kindly provided by Chris Gudea and Dustin Lambright (see above)
public static void main(String[] args) {
run()
}
//used to evaluate if the test is successful
static unitTester(item, expected, actual) {
assert item
println "expected: ${expected} | actual: ${actual}"
assert expected == actual
}
//check 'item' string using 'regex' string
static boolean regexCheck(regexPattern, item){
return regexPattern.matcher(item).matches()
}
static run(){
//DMS test data
def dmsLatFails = ['20/60/0', '80/60/1.11', '80/59/70', '90/59/0', '86/60/1']
def dmsLatMatches = ['-12/23/59.999999999999999', '-1/2/1.000234', '-1:2:12.000234', '0 59 0.22', '60/34/34']
def dmsLonFails = ['123:60:59.99999999', '-180:12:0', '180:60:0',
'700 50 50.11', '23/79/22.22', '0 60 2.2', '0 60 0']
def dmsLonMatches = ['-179/59/0.23', '127/12/12.223', '-180/0/0',
'179:59:59.99999999', '-59:13:58.22212345',
'0:0:2.3', '127 12 12.333', '180 0 0', '-0 0 0.00001']
//decimal test data
def decimalFails = ["323.312.0", "ABC", "abc", "2.a23", "e2.31", "123-213", "-123e", "180.1", "-190", "-200.1"]
def decimalMatches = ["-22.311", "-12", "-170", "28.324125", "179.0111111111111", "13", "-100.0"]
//mgrs test data
def mgrsMatches = ["4QFJ1093763778", "2QFK3093763778", "2QaK3093763778", "1fgh23456789"]
def mgrsFails = ["QFJ1093763778", "4QFJ109373778", "123a", "12.4.41"]
//utm test data
def utmMatches = ['4Q6109372363778', '1f21', '2e01928391087509127405123521353526798',
'4:Q6109372363778', '4/Q/6109372363778', '4 Q 6109372363778', '1 e 231']
def utmFails = ['4a6109372363778', 'asljkd', '123f', '1a21', '1234', '1/2:123', '1:./21', '1 a 1234']
//regular expression Groovy Patterns (all the magic)
def dmsLatRegExPattern = ~/^-?((90\/[0]{0,}\/[0]{0,}$)|([1-8]?\d))(\/|\:|\ )(([1-5]?\d))(\/|\:|\ )[1-5]?\d(\.\d{0,})?$/
def dmsLonRegExPattern = ~/^-?((180(\/|\:| )0(\/|\:| )0((\.0{0,})?))|(([1]?[1-7]\d)|\d?\d)(\/|\:| )([1-5]?\d)(\/|\:| )[1-5]?\d(\.\d{0,})?$)/
def decimalRegExPattern = ~/^-?(180((\.0{0,})?)|([1]?[0-7]?\d(\.\d{0,})?))$/
def mgrsRegExPattern = ~/^\d{1,2}[^ABIOYZabioyz][A-Za-z]{2}([0-9][0-9])+$/
def utmRegExPattern = ~/^\d(\/|\:| |)[^aboiyzABOIYZ\d\[-\` -@](\/|\:| |)\d{2,}$/
println "----Degrees Minutes Seconds regular expression----"
println "\nTesting all examples that should show false:"
dmsLatFails.each { String value ->
unitTester(value, false, regexCheck(dmsLatRegExPattern, value))
}
println "\nTesting all examples that should show true:"
dmsLatMatches.each { String value ->
unitTester(value, true, regexCheck(dmsLatRegExPattern, value))
}
println "\nTesting all examples that should show false:"
dmsLonFails.each { String value ->
unitTester(value, false, regexCheck(dmsLonRegExPattern, value))
}
println "Testing all examples that should show true:"
dmsLonMatches.each { String value ->
unitTester(value, true, regexCheck(dmsLonRegExPattern, value))
}
//////////////
println "\n\n----Testing Decimal coordinates----"
println "Testing all examples that should show false:"
decimalFails.each { String value ->
unitTester(value, false, regexCheck(decimalRegExPattern, value))
}
println "Testing all examples that should show true:"
decimalMatches.each { String value ->
unitTester(value, true, regexCheck(decimalRegExPattern, value))
}
//////////////
println "\n\n----Testing MGRS coordinates----"
println "Testing all examples that should show false:"
mgrsFails.each { String value ->
unitTester(value, false, regexCheck(mgrsRegExPattern, value))
}
println "Testing all examples that should show true:"
mgrsMatches.each { String value ->
unitTester(value, true, regexCheck(mgrsRegExPattern, value))
}
//////////////
println "\n\n----Testing UTM coordinates----"
println "Testing all examples that should show false:"
utmFails.each { String value ->
unitTester(value, false, regexCheck(utmRegExPattern, value))
}
println "Testing all examples that should show true:"
utmMatches.each { String value ->
unitTester(value, true, regexCheck(utmRegExPattern, value))
}
}
}
@cgudea
Copy link

cgudea commented Jul 10, 2015

Nice! Glad this was of use to someone else.

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