← Back to team overview

dhis2-devs team mailing list archive

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

 

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@xxxxxxxxx>:
> 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@xxxxxxxxxxxxx> wrote:
>> ------------------------------------------------------------
>> 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 ) );
>>     }
>>  }
>>
>>
>> _______________________________________________
>> Mailing list: https://launchpad.net/~dhis2-devs
>> Post to     : dhis2-devs@xxxxxxxxxxxxxxxxxxx
>> Unsubscribe : https://launchpad.net/~dhis2-devs
>> More help   : https://help.launchpad.net/ListHelp
>>
>>
>


Follow ups

References