From 6a0d315f2684d36731a98fc1044fb69307c60422 Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Sun, 21 Sep 2014 15:37:02 +0200 Subject: [PATCH] DATAGRAPH-497 GeoConverter negative coordinates --- .../data/neo4j/repository/GeoConverter.java | 4 +- .../neo4j/repository/GeoConverterTest.java | 62 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GeoConverterTest.java diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/GeoConverter.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/GeoConverter.java index 5409a4a25..35a6ffd19 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/GeoConverter.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/GeoConverter.java @@ -30,8 +30,8 @@ import java.util.regex.Pattern; */ public class GeoConverter { - public static final Pattern WKT_POINT = Pattern.compile("^POINT *\\( *([\\d.]+) *([\\d.]+) *\\) *$",Pattern.CASE_INSENSITIVE); - private static final String POINT = " *[\\d.]+ *[\\d.]+ *"; + public static final Pattern WKT_POINT = Pattern.compile("^POINT *\\( *([+-]?[\\d.]+) *([+-]?[\\d.]+) *\\) *$",Pattern.CASE_INSENSITIVE); + private static final String POINT = " *[+-]?[\\d.]+ *[+-]?[\\d.]+ *"; public static final Pattern WKT_POLYGON = Pattern.compile("^POLYGON *\\( *\\(((?:" + POINT + ",)*" + POINT + ") *\\) *\\) *$", Pattern.CASE_INSENSITIVE); public static String toWktCoords(Point point) { diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GeoConverterTest.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GeoConverterTest.java new file mode 100644 index 000000000..789668714 --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/GeoConverterTest.java @@ -0,0 +1,62 @@ +package org.springframework.data.neo4j.repository; + +import org.junit.Test; +import org.springframework.data.geo.Box; +import org.springframework.data.geo.Point; +import org.springframework.data.geo.Polygon; + +import static org.junit.Assert.*; + +public class GeoConverterTest { + + /* + 2| + 3 + ---|---4- + | + |1 + */ + public static final Point POINT_1 = new Point(1.23d, -4.56d); + public static final Point POINT_2 = new Point(-1.23d, 4.56d); + public static final Point POINT_3 = new Point(0d, 2d); + public static final Point POINT_4 = new Point(4d, 0d); + public static final Point POINT_5 = new Point(1.23d, 4.56d); + public static final Point POINT_6 = new Point(-1.23d, -4.56d); + + @Test + public void testToWktCoords() throws Exception { + assertEquals("1.23 -4.56", GeoConverter.toWktCoords(POINT_1)); + } + + @Test + public void testToWkt() throws Exception { + assertEquals("POLYGON((1.23 -4.56,-1.23 4.56,0.0 2.0,1.23 -4.56))",GeoConverter.toWkt(new Polygon(POINT_1,POINT_2,POINT_3))); + } + + @Test + public void testToWellKnownText() throws Exception { + assertEquals("POLYGON((1.23 -4.56,-1.23 4.56,0.0 2.0,1.23 -4.56))",GeoConverter.toWellKnownText(new Polygon(POINT_1,POINT_2,POINT_3))); + assertEquals("POINT(1.23 -4.56)",GeoConverter.toWellKnownText(POINT_1)); + assertEquals("POINT(-1.23 4.56)",GeoConverter.toWellKnownText(POINT_2)); + assertEquals("POINT(0.0 2.0)",GeoConverter.toWellKnownText(POINT_3)); + + } + + @Test + public void testToPolygon() throws Exception { + assertEquals(new Polygon(POINT_1,POINT_6,POINT_2, POINT_5),GeoConverter.toPolygon(new Box(POINT_1, POINT_2))); + } + + @Test + public void testFromWellKnownText() throws Exception { + assertEquals(new Polygon(POINT_1,POINT_2,POINT_3),GeoConverter.fromWellKnownText("POLYGON((1.23 -4.56,-1.23 4.56,0.0 2.0,1.23 -4.56))")); + } + + @Test + public void testPointFromWellKnownText() throws Exception { + assertEquals(POINT_1,GeoConverter.pointFromWellKnownText("POINT(1.23 -4.56)")); + assertEquals(POINT_2,GeoConverter.pointFromWellKnownText("POINT(-1.23 4.56)")); + assertEquals(POINT_3,GeoConverter.pointFromWellKnownText("POINT(0 2)")); + assertEquals(POINT_4,GeoConverter.pointFromWellKnownText("POINT(4 0)")); + } +}