TypeCoordinateService implements GeocoderService { @Override public GetLocationResult[] getLocation(List<String> location, int maxAlternatives, Locale locale) { if (null == locale) { locale = Locale.US; } GetLocationResult result = new GetLocationResult(); if (location.size() > 0) { String[] parts = location.get(0).split("\\s+"); if (parts.length >= 2) { NumberFormat format = NumberFormat.getInstance(locale); try { Double x = format.parse(parts[0]).doubleValue(); Double y = format.parse(parts[1]).doubleValue(); Coordinate coordinate = new Coordinate(x, y); String canonicalCrs = ""; if (parts.length > 2) { String crsPart = parts[2]; if (crsPart.startsWith(CRS_PREFIX)) { crsPart = crsPart.substring(CRS_PREFIX.length()); try { coordinate = geoService.transform(coordinate, crsPart, defaultCrs); canonicalCrs = " " + CRS_PREFIX + crsPart; } catch (GeomajasException le) { log.debug("Crs conversion from " + crsPart + " tot " + defaultCrs + " failed, " + le.getMessage()); } } } result.setCoordinate(coordinate); List<String> canonical = new ArrayList<String>(); canonical.add(parts[0] + " " + parts[1] + canonicalCrs); result.setCanonicalStrings(canonical); return new GetLocationResult[] { result }; } catch (ParseException pe) { log.debug("Could not parse " + location.get(0) + ", " + pe.getMessage()); } } } return new GetLocationResult[0]; }  void setDefaultCrs(String defaultCrs); @Override String getName(); @Api void setName(String name); @Override CoordinateReferenceSystem getCrs(); @Override GetLocationResult[] getLocation(List<String> location, int maxAlternatives, Locale locale);  }
@Test public void testGeocoderSimple() { List<String> list = new ArrayList<String>(); GetLocationResult[] result; list.clear(); list.add("4.77397 51.05125 "); result = geocoder.getLocation(list, 50, null); Assert.assertNotNull(result); Assert.assertEquals(1,result.length); Assert.assertNotNull(result[0].getCoordinate()); Assert.assertEquals(4.77397, result[0].getCoordinate().x, DELTA); Assert.assertEquals(51.05125, result[0].getCoordinate().y, DELTA); Assert.assertEquals(1, result[0].getCanonicalStrings().size()); Assert.assertEquals("4.77397 51.05125", result[0].getCanonicalStrings().get(0)); Assert.assertNull(result[0].getEnvelope()); }
@Test public void testGeocoderSkipExtra() { List<String> list = new ArrayList<String>(); GetLocationResult[] result; list.clear(); list.add("4.77397 51.05125 bla "); result = geocoder.getLocation(list, 50, null); Assert.assertNotNull(result); Assert.assertEquals(1,result.length); Assert.assertNotNull(result[0].getCoordinate()); Assert.assertEquals(4.77397, result[0].getCoordinate().x, DELTA); Assert.assertEquals(51.05125, result[0].getCoordinate().y, DELTA); Assert.assertEquals(1, result[0].getCanonicalStrings().size()); Assert.assertEquals("4.77397 51.05125", result[0].getCanonicalStrings().get(0)); Assert.assertNull(result[0].getEnvelope()); }
@Test public void testGeocoderTransform() { List<String> list = new ArrayList<String>(); GetLocationResult[] result; list.clear(); list.add("4.77397 \t 51.05125 crs:EPSG:4326"); result = geocoder.getLocation(list, 50, null); Assert.assertNotNull(result); Assert.assertEquals(1,result.length); Assert.assertNotNull(result[0].getCoordinate()); Assert.assertEquals(531435.9094623642, result[0].getCoordinate().x, DELTA); Assert.assertEquals(6630364.2661175905, result[0].getCoordinate().y, DELTA); Assert.assertEquals(1, result[0].getCanonicalStrings().size()); Assert.assertEquals("4.77397 51.05125 crs:EPSG:4326", result[0].getCanonicalStrings().get(0)); Assert.assertNull(result[0].getEnvelope()); }
@Test public void testGeocoderInvalidTransform() { List<String> list = new ArrayList<String>(); GetLocationResult[] result; list.clear(); list.add("4.77397 51.05125 crs:bla"); result = geocoder.getLocation(list, 50, null); Assert.assertNotNull(result); Assert.assertEquals(1,result.length); Assert.assertNotNull(result[0].getCoordinate()); Assert.assertEquals(4.77397, result[0].getCoordinate().x, DELTA); Assert.assertEquals(51.05125, result[0].getCoordinate().y, DELTA); Assert.assertEquals(1, result[0].getCanonicalStrings().size()); Assert.assertEquals("4.77397 51.05125", result[0].getCanonicalStrings().get(0)); Assert.assertNull(result[0].getEnvelope()); }