[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4866: Impl methods for getting latitude and longitude from coordinate string

revision-diff.txt (3.77 KB)

Hi Lars,

Small suggested change. Not really sure if the square brackets []
should be part of the validation or not. You may need to double the
slashes as well.

\[[+-]?\d{1,3}(\.\d*)?,[+-]?\d{1,3}(\.\d*)?\]

Decimal points should be optional. Only single +- signs should be
allowed. Not sure about spaces after then comma. Perhaps they are
valid?

This is still not quite tight enough, as latitude values greater than
90 and longitude values greater than 180 should also not be
allowed.Also, double zeroes would be allowed before the decimal. We
could probably lock it down with a bit more work.

I suppose this is only for point coordinates?

Regards,
Jason

···

On Thu, Oct 6, 2011 at 7:29 PM, <noreply@launchpad.net> wrote:

------------------------------------------------------------
revno: 4866
committer: Lars Helge Overland <larshelge@gmail.com>
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 OpenID transaction in progress

=== 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 ) );
}
}

_______________________________________________
Mailing list: DHIS 2 developers in Launchpad
Post to : dhis2-devs@lists.launchpad.net
Unsubscribe : DHIS 2 developers in Launchpad
More help : ListHelp - Launchpad Help

Hi Lars,

Again, another suggestion for the coordinate format, which is slightly
more restrictive (perhaps overly so).

^\[[+-]?([0-9]|[1-9][0-9]|1[0-7][0-9])(\.\d{1,})?,(\s)?[+-]?([0-9]|[1-8][0-9])(\.\d{1,})?\]$

Not perfect, as [180, 90] is not allowed, but seems that the need to
have the north/south poles is not particular compelling unless we
deploy to Antarctica any time soon.

Longitude is restricted to +/- 0-179.9 and lat is restricted to +/-
0-89.9. Not sure if -0 is allowable in GeoJson or not, but it would be
possible. No trailing or leading spaces. An optional single space
after the comma is allowed. Signs are optional. Decimals are optional,
but require at least a single digit if the decimal exists.

Hope this helps.

Regards,
Jason

···

2011/10/6 Jason Pickering <jason.p.pickering@gmail.com>:

Hi Lars,

Small suggested change. Not really sure if the square brackets []
should be part of the validation or not. You may need to double the
slashes as well.

\[[+-]?\d{1,3}(\.\d*)?,[+-]?\d{1,3}(\.\d*)?\]

Decimal points should be optional. Only single +- signs should be
allowed. Not sure about spaces after then comma. Perhaps they are
valid?

This is still not quite tight enough, as latitude values greater than
90 and longitude values greater than 180 should also not be
allowed.Also, double zeroes would be allowed before the decimal. We
could probably lock it down with a bit more work.

I suppose this is only for point coordinates?

Regards,
Jason

On Thu, Oct 6, 2011 at 7:29 PM, <noreply@launchpad.net> wrote:

------------------------------------------------------------
revno: 4866
committer: Lars Helge Overland <larshelge@gmail.com>
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 OpenID transaction in progress

=== 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 ) );
}
}

_______________________________________________
Mailing list: DHIS 2 developers in Launchpad
Post to : dhis2-devs@lists.launchpad.net
Unsubscribe : DHIS 2 developers in Launchpad
More help : ListHelp - Launchpad Help

Hi Lars,

Again, another suggestion for the coordinate format, which is slightly
more restrictive (perhaps overly so).

^\[[+-]?([0-9]|[1-9][0-9]|1[0-7][0-9])(\.\d{1,})?,(\s)?[+-]?([0-9]|[1-8][0-9])(\.\d{1,})?\]$

I guess it's an age old principle that if it was hard to write it
should be hard to read :slight_smile:

Seriously though, I love regex's to death, particulalrly for difficult
string matching, but where we are simply looking at parsing numbers
and checking their values are in range, I think there are more direct
approaches. Sad thing that the java fathers neglected to include
scanf from their C grandfathers, otherwise:

res = sscanf(coord,"[%f, %f]", &lat, &lon);
if (res!=3) return false;
if (lat>180 || lat < -80) return false;
if (lon>180 || lon < -80) return false;
return true;

would have been our friend.

Nevertheless I would have probably opted for the pedestrian route of
doing a String.split() followed by testing parseFloat. But it would
burn an extra cycle or two and would could contribute to those poles
getting melted. In which case you might well end up having a facility
- in the South at least :slight_smile:

Other than all that, I the regex looks quite ok to me.

Cheers
Bob

···

2011/10/7 Jason Pickering <jason.p.pickering@gmail.com>:

Not perfect, as [180, 90] is not allowed, but seems that the need to
have the north/south poles is not particular compelling unless we
deploy to Antarctica any time soon.

Longitude is restricted to +/- 0-179.9 and lat is restricted to +/-
0-89.9. Not sure if -0 is allowable in GeoJson or not, but it would be
possible. No trailing or leading spaces. An optional single space
after the comma is allowed. Signs are optional. Decimals are optional,
but require at least a single digit if the decimal exists.

Hope this helps.

Regards,
Jason

2011/10/6 Jason Pickering <jason.p.pickering@gmail.com>:

Hi Lars,

Small suggested change. Not really sure if the square brackets []
should be part of the validation or not. You may need to double the
slashes as well.

\[[+-]?\d{1,3}(\.\d*)?,[+-]?\d{1,3}(\.\d*)?\]

Decimal points should be optional. Only single +- signs should be
allowed. Not sure about spaces after then comma. Perhaps they are
valid?

This is still not quite tight enough, as latitude values greater than
90 and longitude values greater than 180 should also not be
allowed.Also, double zeroes would be allowed before the decimal. We
could probably lock it down with a bit more work.

I suppose this is only for point coordinates?

Regards,
Jason

On Thu, Oct 6, 2011 at 7:29 PM, <noreply@launchpad.net> wrote:

------------------------------------------------------------
revno: 4866
committer: Lars Helge Overland <larshelge@gmail.com>
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 ) );
}
}

_______________________________________________
Mailing list: https://launchpad.net/~dhis2-devs
Post to : dhis2-devs@lists.launchpad.net
Unsubscribe : https://launchpad.net/~dhis2-devs
More help : https://help.launchpad.net/ListHelp

_______________________________________________
Mailing list: https://launchpad.net/~dhis2-devs
Post to : dhis2-devs@lists.launchpad.net
Unsubscribe : https://launchpad.net/~dhis2-devs
More help : https://help.launchpad.net/ListHelp

Wasn't hard really, just need some fresh African air, a sunrise, and a
cup of coffee. Voila.

I agree that doing it this way may not be really ideal and it may not
perform particularly well, but it should do the job.

Regards,
Jason

···

On Fri, Oct 7, 2011 at 11:05 AM, Bob Jolliffe <bobjolliffe@gmail.com> wrote:

2011/10/7 Jason Pickering <jason.p.pickering@gmail.com>:

Hi Lars,

Again, another suggestion for the coordinate format, which is slightly
more restrictive (perhaps overly so).

^\[[+-]?([0-9]|[1-9][0-9]|1[0-7][0-9])(\.\d{1,})?,(\s)?[+-]?([0-9]|[1-8][0-9])(\.\d{1,})?\]$

I guess it's an age old principle that if it was hard to write it
should be hard to read :slight_smile:

Seriously though, I love regex's to death, particulalrly for difficult
string matching, but where we are simply looking at parsing numbers
and checking their values are in range, I think there are more direct
approaches. Sad thing that the java fathers neglected to include
scanf from their C grandfathers, otherwise:

res = sscanf(coord,"[%f, %f]", &lat, &lon);
if (res!=3) return false;
if (lat>180 || lat < -80) return false;
if (lon>180 || lon < -80) return false;
return true;

would have been our friend.

Nevertheless I would have probably opted for the pedestrian route of
doing a String.split() followed by testing parseFloat. But it would
burn an extra cycle or two and would could contribute to those poles
getting melted. In which case you might well end up having a facility
- in the South at least :slight_smile:

Other than all that, I the regex looks quite ok to me.

Cheers
Bob

Not perfect, as [180, 90] is not allowed, but seems that the need to
have the north/south poles is not particular compelling unless we
deploy to Antarctica any time soon.

Longitude is restricted to +/- 0-179.9 and lat is restricted to +/-
0-89.9. Not sure if -0 is allowable in GeoJson or not, but it would be
possible. No trailing or leading spaces. An optional single space
after the comma is allowed. Signs are optional. Decimals are optional,
but require at least a single digit if the decimal exists.

Hope this helps.

Regards,
Jason

2011/10/6 Jason Pickering <jason.p.pickering@gmail.com>:

Hi Lars,

Small suggested change. Not really sure if the square brackets []
should be part of the validation or not. You may need to double the
slashes as well.

\[[+-]?\d{1,3}(\.\d*)?,[+-]?\d{1,3}(\.\d*)?\]

Decimal points should be optional. Only single +- signs should be
allowed. Not sure about spaces after then comma. Perhaps they are
valid?

This is still not quite tight enough, as latitude values greater than
90 and longitude values greater than 180 should also not be
allowed.Also, double zeroes would be allowed before the decimal. We
could probably lock it down with a bit more work.

I suppose this is only for point coordinates?

Regards,
Jason

On Thu, Oct 6, 2011 at 7:29 PM, <noreply@launchpad.net> wrote:

------------------------------------------------------------
revno: 4866
committer: Lars Helge Overland <larshelge@gmail.com>
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 OpenID transaction in progress

=== 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 ) );
}
}

_______________________________________________
Mailing list: DHIS 2 developers in Launchpad
Post to : dhis2-devs@lists.launchpad.net
Unsubscribe : DHIS 2 developers in Launchpad
More help : ListHelp - Launchpad Help

_______________________________________________
Mailing list: DHIS 2 developers in Launchpad
Post to : dhis2-devs@lists.launchpad.net
Unsubscribe : DHIS 2 developers in Launchpad
More help : ListHelp - Launchpad Help

Hi Jason, thanks for the tip, will look closer at this asap.

regards, Lars