Skip to content

Commit

Permalink
Fix bbox bug
Browse files Browse the repository at this point in the history
  • Loading branch information
utas-raymondng committed Dec 6, 2024
1 parent 110ccb1 commit 6ef147a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,27 @@
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LinearRing;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.spatial4j.context.jts.JtsSpatialContext;
import org.locationtech.spatial4j.shape.jts.JtsGeometry;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* Utility for BBox operation only, it assumes the incoming is a bounding box
*/
public class BboxUtils {
/**
* Normalize a bbox by adjusting longitudes to the range [-180, 180], and then split it into two if it
* is cross meridian.
* is cross meridian. We need this because Bbox is assumed to work within this range only.
* @param minx - left
* @param maxx - right
* @param miny - top
* @param maxy - bottom
* @return
* @return - Geometry which is bounded [-180, 180]
*/
public static Geometry normalizeBbox(double minx, double maxx, double miny, double maxy) {
// Bounding check, if greater than 360 already cover whole world, so adjust the maxx to something
// meaningful, noted that minx and maxx is not normalized yet so can be anything even beyond 180
if((maxx - minx) >= 360) {
// Value does not matter, it cover whole world which is equal to
// Value does not matter, it covered whole world which is equal to
minx = -180;
maxx = 180;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,6 @@ public void verifyBBoxWorks1() throws CQLException, IOException, FactoryExceptio
*
* @throws CQLException - Will not throw
* @throws IOException - Will not throw
* @throws FactoryException - Will not throw
* @throws TransformException - Will not throw
* @throws ParseException - Will not throw
*/
@Test
public void verifyIntersectionWorks2() throws CQLException, IOException {
Expand All @@ -162,20 +159,17 @@ public void verifyIntersectionWorks2() throws CQLException, IOException {
Geometry g = (Geometry)filter.accept(visitor, geo.get());

Assertions.assertFalse(g.isEmpty());
Assertions.assertTrue(g instanceof Polygon);
Assertions.assertInstanceOf(Polygon.class, g);

Assertions.assertEquals(g.getCentroid().getX(), 168.30090846621448, "getX()");
Assertions.assertEquals(g.getCentroid().getY(), -33.95984804960966, "getY()");
Assertions.assertEquals(168.30090846621448, g.getCentroid().getX(), "getX()");
Assertions.assertEquals(-33.95984804960966, g.getCentroid().getY(), "getY()");
}
/**
* Test almost the same as the verifyIntersectionWorks2, since verifyIntersectionWorks1 create a polygon same as box
* so use Intersect or BBox will result in same result
*
* @throws CQLException - Will not throw
* @throws IOException - Will not throw
* @throws FactoryException - Will not throw
* @throws TransformException - Will not throw
* @throws ParseException - Will not throw
*/
@Test
public void verifyBBoxWorks2() throws CQLException, IOException {
Expand All @@ -199,20 +193,17 @@ public void verifyBBoxWorks2() throws CQLException, IOException {
Geometry g = (Geometry)filter.accept(visitor, geo.get());

Assertions.assertFalse(g.isEmpty());
Assertions.assertTrue(g instanceof Polygon);
Assertions.assertInstanceOf(Polygon.class, g);

Assertions.assertEquals(g.getCentroid().getX(), 168.30090846621448, 0.0000001, "getX()");
Assertions.assertEquals(g.getCentroid().getY(), -33.95984804960966, 0.0000001, "getY()");
Assertions.assertEquals(168.30090846621448, g.getCentroid().getX(), 0.0000001, "getX()");
Assertions.assertEquals(-33.95984804960966, g.getCentroid().getY(), 0.0000001, "getY()");
}
/**
* Similar test as verifyBBoxWorks2, the BBOX not only cross meridian but the sample json have spatial extents
* near equator and span across the whole world
*
* @throws CQLException - Will not throw
* @throws IOException - Will not throw
* @throws FactoryException - Will not throw
* @throws TransformException - Will not throw
* @throws ParseException - Will not throw
*/
@Test
public void verifyBBoxWorks3() throws CQLException, IOException {
Expand All @@ -235,15 +226,15 @@ public void verifyBBoxWorks3() throws CQLException, IOException {
// return value are geo applied the CQL, and in this case only BBOX intersected
Geometry g = (Geometry)filter.accept(visitor, geo.get());

Assertions.assertTrue(g instanceof MultiPolygon);
Assertions.assertInstanceOf(MultiPolygon.class, g);

MultiPolygon mp = (MultiPolygon)g;
Assertions.assertEquals(mp.getNumGeometries(), 2, "Geometries correct");
Assertions.assertEquals(2, mp.getNumGeometries(), "Geometries correct");

Assertions.assertEquals(mp.getGeometryN(0).getCentroid().getX(), -159.53241830835444, 0.0000001, "getX() for 0");
Assertions.assertEquals(mp.getGeometryN(0).getCentroid().getY(), -19.5, 0.0000001, "getY() for 0");
Assertions.assertEquals(-159.53241830835444, mp.getGeometryN(1).getCentroid().getX(), 0.0000001, "getX() for 0");
Assertions.assertEquals(-19.5, mp.getGeometryN(1).getCentroid().getY(), 0.0000001, "getY() for 0");

Assertions.assertEquals(mp.getGeometryN(1).getCentroid().getX(), 151.62121416760516, 0.0000001, "getX() for 1");
Assertions.assertEquals(mp.getGeometryN(1).getCentroid().getY(), -18.000822620336752, 0.0000001, "getY() for 1");
Assertions.assertEquals(151.62121416760516, mp.getGeometryN(0).getCentroid().getX(), 0.0000001, "getX() for 1");
Assertions.assertEquals(-18.000822620336752, mp.getGeometryN(0).getCentroid().getY(), 0.0000001, "getY() for 1");
}
}

0 comments on commit 6ef147a

Please sign in to comment.