Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cursor position tool should consider locale when parsing coord string #330

Merged
merged 7 commits into from
Dec 7, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.util.Locale;

import org.locationtech.udig.tools.internal.CursorPosition;

import org.geotools.geometry.jts.JTS;
Expand All @@ -22,49 +25,72 @@

import com.vividsolutions.jts.geom.Coordinate;

public class CursorPositionTest {
public abstract class CursorPositionTest {

@Test
public void testParseString() throws Exception {
Coordinate coord=CursorPosition.parse("124,88", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse("(124,88)", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse("124 88", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse("(124 88)", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse("124 88LL", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse("(124 88)LL", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse(" (124 88 )", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse("( 124, 88 )", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse("(124, 88)LL", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse("[124 88]", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse("[124 88]LATLONG", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse("[124 88]LAT LONG", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse("124 88LAT LONG", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
coord=CursorPosition.parse("124 88ll", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124,88), coord);
CoordinateReferenceSystem albers = CRS.decode("EPSG:3005"); //$NON-NLS-1$
coord=CursorPosition.parse("124 88LAT LONG", albers); //$NON-NLS-1$
Coordinate expected=new Coordinate();
JTS.transform(new Coordinate(124,88), expected, CRS.findMathTransform(DefaultGeographicCRS.WGS84,albers));
assertEquals(expected.x, coord.x, 0.00001);
public void testParseStringCommon() throws Exception {

//System.out.println(Locale.getDefault());

Coordinate coord = CursorPosition.parse("124,88", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse("(124,88)", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse("124 88", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse("(124 88)", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse("124 88LL", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse("(124 88)LL", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse(" (124 88 )", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse("( 124, 88 )", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse("(124, 88)LL", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse("[124 88]", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse("[124 88]LATLONG", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse("[124 88]LAT LONG", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse("124 88LAT LONG", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
coord = CursorPosition.parse("124 88ll", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124, 88), coord);
CoordinateReferenceSystem albers = CRS.decode("EPSG:3005"); //$NON-NLS-1$
coord = CursorPosition.parse("124 88LAT LONG", albers); //$NON-NLS-1$
Coordinate expected = new Coordinate();
JTS.transform(new Coordinate(124, 88), expected,
CRS.findMathTransform(DefaultGeographicCRS.WGS84, albers));
assertEquals(expected.x, coord.x, 0.00001);

coord = CursorPosition.parse("aasdf asdf", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertNull(coord);

coord = CursorPosition.parse("13g4", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertNull(coord);

coord = CursorPosition.parse("124.88, 234.22", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124.88, 234.22), coord);

// parse with dot decimal and ' ' separator should work in both Locales
coord = CursorPosition.parse("124.88 234.22", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124.88, 234.22), coord);

// parse with dot decimal ',' separator should work in both Locales
coord = CursorPosition.parse(" 124.88,234.22", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124.88, 234.22), coord);

// check that multiple spaces between coords do not cause a problem
coord = CursorPosition.parse("124.88 234.22", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124.88, 234.22), coord);

// check that multiple spaces between coords do not cause a problem
coord = CursorPosition.parse("124.88, 234.22", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124.88, 234.22), coord);

coord=CursorPosition.parse("aasdf asdf", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertNull(coord);

coord=CursorPosition.parse("13g4", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertNull(coord);

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* uDig - User Friendly Desktop Internet GIS client
* http://udig.refractions.net
* (C) 2012, Refractions Research Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Refractions BSD
* License v1.0 (http://udig.refractions.net/files/bsd3-v10.html).
*/
package org.locationtech.udig.tool.tests;

import static org.junit.Assert.assertEquals;

import java.util.Locale;

import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.junit.Rule;
import org.junit.Test;
import org.locationtech.udig.tools.internal.CursorPosition;

import com.vividsolutions.jts.geom.Coordinate;

public class CursorPositionTest_FR extends CursorPositionTest {

@Rule public final DefaultLocaleRule defaultLocaleRule = new DefaultLocaleRule(Locale.FRENCH);
@Test
public void testParseString() throws Exception {

//System.out.println(Locale.getDefault());

// Locales with ',' as decimal operator should work correctly
Coordinate coord = CursorPosition.parse(" 124,88 234,22", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124.88, 234.22), coord);

// Locales with ',' as decimal operator should NOT work correctly
coord = CursorPosition.parse(" 124.88, 234.22", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124.88, 234.22), coord);

// Locales with ',' as decimal operator should NOT work correctly
coord = CursorPosition.parse(" 124,88, 234,22", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124.88, 234.22), coord);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* uDig - User Friendly Desktop Internet GIS client
* http://udig.refractions.net
* (C) 2012, Refractions Research Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Refractions BSD
* License v1.0 (http://udig.refractions.net/files/bsd3-v10.html).
*/
package org.locationtech.udig.tool.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import java.util.Locale;

import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.junit.Rule;
import org.junit.Test;
import org.locationtech.udig.tools.internal.CursorPosition;

import com.vividsolutions.jts.geom.Coordinate;

public class CursorPositionTest_US extends CursorPositionTest {

@Rule public final DefaultLocaleRule defaultLocaleRule = new DefaultLocaleRule(Locale.US);
@Test
public void testParseString() throws Exception {

//System.out.println(Locale.getDefault());

// Locales with '.' as decimal operator parsing of numbers with ','
// causes a problem
Coordinate coord = CursorPosition.parse(" 124,88 234,22", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
// wrong parsing due to wrongly used decimal seperator
assertEquals(new Coordinate(124, 88), coord);

// Locales with ',' as decimal operator should NOT work correctly
coord = CursorPosition.parse(" 124.88, 234.22", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertEquals(new Coordinate(124.88, 234.22), coord);

// Locales with ',' as decimal operator should NOT work correctly
coord = CursorPosition.parse(" 124,88, 234,22", DefaultGeographicCRS.WGS84); //$NON-NLS-1$
assertNotEquals(new Coordinate(124.88, 234.22), coord);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.locationtech.udig.tool.tests;

import java.util.Locale;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/** JUnit rule for taking control over the Locale. */
public final class DefaultLocaleRule implements TestRule {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess regarding Eclipse IP its not allowed to copy code from other projects. So it can inspire uDig but this class needs to be re-written or we add a dependency to the project to use it as a library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class is quite elementary in terms of behavior and applied logic. Would it be OK if I just rename it (for example ConfigureLocaleRule) and possibly alter a couple of Comments

final Locale preference;

/** Creates the rule and will restore the default locale for each test. */
public DefaultLocaleRule() {
preference = null;
}

/** Creates the rule and will set the preferred locale for each test. */
public DefaultLocaleRule(final Locale preference) {
this.preference = preference;
}

@Override public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override public void evaluate() throws Throwable {
final Locale defaultLocale = Locale.getDefault();

try {
if (preference != null) {
Locale.setDefault(preference);
}

base.evaluate();
} finally {
Locale.setDefault(defaultLocale);
}
}
};
}
}