Permalink
Browse files

Fix JtsSatialContext to normalize x & y in createPoint(). Enhanced te…

…sts to test JtsSpatialContext now (finally!)
  • Loading branch information...
1 parent a6fb41d commit ea5e2ed1093335c18a8c32971565d6bd7eb57967 @dsmiley dsmiley committed May 22, 2012
@@ -17,12 +17,6 @@
package com.spatial4j.core.context.jts;
-import com.vividsolutions.jts.geom.Coordinate;
-import com.vividsolutions.jts.geom.Envelope;
-import com.vividsolutions.jts.geom.Geometry;
-import com.vividsolutions.jts.geom.GeometryFactory;
-import com.vividsolutions.jts.io.*;
-import com.vividsolutions.jts.util.GeometricShapeFactory;
import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.distance.DistanceCalculator;
import com.spatial4j.core.distance.DistanceUnits;
@@ -32,12 +26,17 @@
import com.spatial4j.core.shape.Rectangle;
import com.spatial4j.core.shape.Shape;
import com.spatial4j.core.shape.impl.RectangleImpl;
-import com.spatial4j.core.shape.jts.*;
+import com.spatial4j.core.shape.jts.JtsGeometry;
+import com.spatial4j.core.shape.jts.JtsPoint;
+import com.vividsolutions.jts.geom.Coordinate;
+import com.vividsolutions.jts.geom.Envelope;
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.geom.GeometryFactory;
+import com.vividsolutions.jts.io.*;
+import com.vividsolutions.jts.util.GeometricShapeFactory;
import java.io.IOException;
import java.nio.ByteBuffer;
-import java.text.NumberFormat;
-import java.util.Locale;
public class JtsSpatialContext extends SpatialContext {
@@ -187,6 +186,9 @@ public Geometry getGeometryFrom(Shape shape) {
@Override
public Point makePoint(double x, double y) {
+ //A Jts Point is fairly heavyweight! TODO could/should we optimize this?
+ x = normX(x);
+ y = normY(y);
return new JtsPoint(factory.createPoint(new Coordinate(x, y)));
}
}
@@ -109,7 +109,7 @@ protected void testRectangle(double minX, double width, double minY, double heig
protected void testRectIntersect() {
final double INCR = 45;
- final double Y = 10;
+ final double Y = 20;
for(double left = -180; left <= 180; left += INCR) {
for(double right = left; right - left <= 360; right += INCR) {
Rectangle r = ctx.makeRect(left,right,-Y,Y);
@@ -119,19 +119,20 @@ protected void testRectIntersect() {
for(double right2 = left2; right2 <= right; right2 += INCR) {
Rectangle r2 = ctx.makeRect(left2,right2,-Y,Y);
assertRelation(null, SpatialRelation.CONTAINS, r, r2);
+
+ //test point contains
+ assertRelation(null, SpatialRelation.CONTAINS, r, r2.getCenter());
}
}
- //test point contains
- assertRelation(null, SpatialRelation.CONTAINS, r, ctx.makePoint(left, Y));
//test disjoint
for(double left2 = right+INCR; left2 - left < 360; left2 += INCR) {
+ //test point disjoint
+ assertRelation(null, SpatialRelation.DISJOINT, r, ctx.makePoint(left2, randomIntBetween(-90,90)));
+
for(double right2 = left2; right2 - left < 360; right2 += INCR) {
Rectangle r2 = ctx.makeRect(left2,right2,-Y,Y);
assertRelation(null, SpatialRelation.DISJOINT, r, r2);
-
- //test point disjoint
- assertRelation(null, SpatialRelation.DISJOINT, r, ctx.makePoint(left2, Y));
}
}
//test intersect
@@ -19,6 +19,7 @@
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
import com.spatial4j.core.context.SpatialContext;
+import com.spatial4j.core.context.jts.JtsSpatialContext;
import com.spatial4j.core.distance.DistanceCalculator;
import com.spatial4j.core.distance.DistanceUnits;
import com.spatial4j.core.distance.GeodesicSphereDistCalc;
@@ -143,8 +144,9 @@ private double degToDist(int deg) {
DistanceCalculator distCalcV = new GeodesicSphereDistCalc.Vincenty(units.earthRadius());//default
return Arrays.asList($$(
$(new SpatialContext(units,distCalcH,SpatialContext.GEO_WORLDBOUNDS)),
- $(new SpatialContext(units,distCalcV,SpatialContext.GEO_WORLDBOUNDS))
- ));
+ $(new SpatialContext(units,distCalcV,SpatialContext.GEO_WORLDBOUNDS)),
+ $(JtsSpatialContext.GEO_KM))
+ );
}
}

0 comments on commit ea5e2ed

Please sign in to comment.