dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #14371
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4866: Impl methods for getting latitude and longitude from coordinate string
------------------------------------------------------------
revno: 4866
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2011-10-06 19:27:11 +0200
message:
Impl methods for getting latitude and longitude from coordinate string
modified:
dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java
dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java 2011-10-06 17:00:43 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java 2011-10-06 17:27:11 +0000
@@ -28,6 +28,7 @@
*/
import java.util.Locale;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.validator.DateValidator;
@@ -40,7 +41,7 @@
*/
public class ValidationUtils
{
- private static Pattern REGEX_PATTERN = Pattern.compile( "\\[[\\+\\-]*\\d+\\.*\\d*,[\\+\\-]*\\d+\\.*\\d*\\]" );
+ private static Pattern COORDINATE_PATTERN = Pattern.compile( "\\[([\\+\\-]*\\d+\\.*\\d*),([\\+\\-]*\\d+\\.*\\d*)\\]" );
/**
* Validates whether an email string is valid.
@@ -105,6 +106,44 @@
*/
public static boolean coordinateIsValid( String coordinate )
{
- return coordinate != null ? REGEX_PATTERN.matcher( coordinate ).matches() : false;
+ return coordinate != null ? COORDINATE_PATTERN.matcher( coordinate ).matches() : false;
+ }
+
+ /**
+ * Returns the latitude from the given coordinate. Returns null if the
+ * coordinate string is not valid.
+ *
+ * @param coordinate the coordinate string.
+ * @return the latitude.
+ */
+ public static String getLatitude( String coordinate )
+ {
+ if ( coordinate == null )
+ {
+ return null;
+ }
+
+ Matcher matcher = COORDINATE_PATTERN.matcher( coordinate );
+
+ return matcher.find() ? matcher.group( 1 ) : null;
+ }
+
+ /**
+ * Returns the longitude from the given coordinate. Returns null if the
+ * coordinate string is not valid.
+ *
+ * @param coordinate the coordinate string.
+ * @return the longitude.
+ */
+ public static String getLongitude( String coordinate )
+ {
+ if ( coordinate == null )
+ {
+ return null;
+ }
+
+ Matcher matcher = COORDINATE_PATTERN.matcher( coordinate );
+
+ return matcher.find() ? matcher.group( 2 ) : null;
}
}
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java 2011-10-06 17:00:43 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java 2011-10-06 17:27:11 +0000
@@ -51,7 +51,25 @@
assertFalse( coordinateIsValid( "23.34343 56.3232" ) );
assertFalse( coordinateIsValid( "[23.34f43,56.3232]" ) );
assertFalse( coordinateIsValid( "23.34343,56.323.2" ) );
- assertFalse( coordinateIsValid( "S-0.27726 E37.08472" ) );
- assertFalse( coordinateIsValid( "17298" ) );
+ assertFalse( coordinateIsValid( "S-0.27726 E37.08472" ) );
+ assertFalse( coordinateIsValid( null ) );
+ }
+
+ @Test
+ public void testGetLatitude()
+ {
+ assertEquals( "+37.99034", getLatitude( "[+37.99034,-28.94221]" ) );
+ assertEquals( "37.99034", getLatitude( "[37.99034,28.94221]" ) );
+ assertNull( getLatitude( "23.34343,56.3232" ) );
+ assertNull( getLatitude( null ) );
+ }
+
+ @Test
+ public void testGetLongitude()
+ {
+ assertEquals( "-28.94221", getLongitude( "[+37.99034,-28.94221]" ) );
+ assertEquals( "28.94221", getLongitude( "[37.99034,28.94221]" ) );
+ assertNull( getLongitude( "23.34343,56.3232" ) );
+ assertNull( getLongitude( null ) );
}
}
Follow ups