-
Notifications
You must be signed in to change notification settings - Fork 1
Bugs/5953 bug bbox #100
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
Merged
Merged
Bugs/5953 bug bbox #100
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
server/src/main/java/au/org/aodn/ogcapi/server/core/util/BboxUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package au.org.aodn.ogcapi.server.core.util; | ||
|
|
||
| import org.locationtech.jts.geom.*; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * 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. 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 - Geometry which is bounded [-180, 180] | ||
| */ | ||
| public static MultiPolygon 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 covered whole world which is equal to | ||
| minx = -180; | ||
| maxx = 180; | ||
| } | ||
| minx = (minx < -180) ? 180 - Math.abs(180 + minx) : minx; | ||
| maxx = (maxx > 180) ? -(maxx - 180) : maxx; | ||
| // Normalized the box, so it is within [-180, 180] | ||
| List<Polygon> polygons = new ArrayList<>(); | ||
| if(maxx >= 0 && maxx <= 180) { | ||
| // Normal case | ||
| polygons.add((Polygon)createBoxPolygon(minx, maxx, miny, maxy)); | ||
| } | ||
| else { | ||
| polygons.add((Polygon)createBoxPolygon(minx, 180, miny, maxy)); | ||
| polygons.add((Polygon)createBoxPolygon(-180, maxx, miny, maxy)); | ||
| } | ||
|
Comment on lines
+33
to
+40
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so you call
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can |
||
| return GeometryUtils.getFactory().createMultiPolygon(polygons.toArray(new Polygon[0])); | ||
| } | ||
|
|
||
| protected static Geometry createBoxPolygon(double startX, double endX, double startY, double endY) { | ||
| // If the longitude range crosses the anti-meridian (e.g., maxx > 180) | ||
| // Normal case have not cross dateline | ||
| Coordinate[] coordinates = new Coordinate[] { | ||
| new Coordinate(startX, startY), // Bottom-left corner | ||
| new Coordinate(endX, startY), // Bottom-right corner | ||
| new Coordinate(endX, endY), // Top-right corner | ||
| new Coordinate(startX, endY), // Top-left corner | ||
| new Coordinate(startX, startY) // Closing the loop (bottom-left corner) | ||
| }; | ||
|
|
||
| // Create a LinearRing for the boundary of the Polygon | ||
| LinearRing ring = GeometryUtils.getFactory().createLinearRing(coordinates); | ||
|
|
||
| // Create the Polygon using the LinearRing (no holes for simplicity) | ||
| return GeometryUtils.getFactory().createPolygon(ring, null); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
server/src/test/java/au/org/aodn/ogcapi/server/core/util/BboxUtilsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package au.org.aodn.ogcapi.server.core.util; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.locationtech.jts.geom.Geometry; | ||
| import org.locationtech.jts.geom.Polygon; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| public class BboxUtilsTest { | ||
| /** | ||
| * This test have bbox start from -4.6 -> 0 -> 180 -> -180 -> -76.5 | ||
| */ | ||
| @Test | ||
| public void verifyNormalizeBbox1() { | ||
| Geometry n = BboxUtils.normalizeBbox(-4.614697916267209,314.17320304524225,-76.58044236400484,60.83962132913365); | ||
| assertEquals(2, n.getNumGeometries(), "Size correct"); | ||
|
|
||
| Polygon expect1 = (Polygon)BboxUtils.createBoxPolygon(-4.614697916267209, 180, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect1, n.getGeometryN(0), "First polygon"); | ||
|
|
||
| Polygon expect3 = (Polygon)BboxUtils.createBoxPolygon(-180, -134.17320304524225, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect3, n.getGeometryN(1), "Second polygon"); | ||
| } | ||
| /** | ||
| * This test have bbox start from 10 -> 180 -> -180 -> -76.5 | ||
| */ | ||
| @Test | ||
| public void verifyNormalizeBbox2() { | ||
| Geometry n = BboxUtils.normalizeBbox(10,314.17320304524225,-76.58044236400484,60.83962132913365); | ||
| assertEquals(2, n.getNumGeometries(), "Size correct"); | ||
|
|
||
| Polygon expect1 = (Polygon)BboxUtils.createBoxPolygon(10, 180, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect1, n.getGeometryN(0), "First polygon"); | ||
|
|
||
| Polygon expect2 = (Polygon)BboxUtils.createBoxPolygon(-180, -134.17320304524225, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect2, n.getGeometryN(1), "Second polygon"); | ||
| } | ||
| /** | ||
| * The maxx is so big that it cover whole world a few times | ||
| */ | ||
| @Test | ||
| public void verifyNormalizeBboxVeryBigMaxX() { | ||
| Geometry n = BboxUtils.normalizeBbox(10,600,-76.58044236400484,60.83962132913365); | ||
| assertEquals(1, n.getNumGeometries(), "Size correct"); | ||
|
|
||
| Polygon expect1 = (Polygon)BboxUtils.createBoxPolygon(-180, 180, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect1, n.getGeometryN(0), "First polygon min 10"); | ||
|
|
||
| // Same no matter how small the minx is | ||
| n = BboxUtils.normalizeBbox(-200,600,-76.58044236400484,60.83962132913365); | ||
| assertEquals(1, n.getNumGeometries(), "Size correct"); | ||
|
|
||
| expect1 = (Polygon)BboxUtils.createBoxPolygon(-180, 180, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect1, n.getGeometryN(0), "First polygon min -200"); | ||
| } | ||
| /** | ||
| * The minx is so small | ||
| */ | ||
| @Test | ||
| public void verifyNormalizeBboxSmallMinX() { | ||
| Geometry n = BboxUtils.normalizeBbox(-250,-60,-76.58044236400484,60.83962132913365); | ||
| assertEquals(2, n.getNumGeometries(), "Size correct"); | ||
|
|
||
| Polygon expect1 = (Polygon)BboxUtils.createBoxPolygon(110, 180, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect1, n.getGeometryN(0), "First polygon"); | ||
|
|
||
| Polygon expect2 = (Polygon)BboxUtils.createBoxPolygon(-180, -60, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect2, n.getGeometryN(1), "Second polygon"); | ||
| // -351 = 9 after minus 180 | ||
| n = BboxUtils.normalizeBbox(-351,-60,-76.58044236400484,60.83962132913365); | ||
| assertEquals(2, n.getNumGeometries(), "Size correct"); | ||
|
|
||
| expect1 = (Polygon)BboxUtils.createBoxPolygon(9, 180, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect1, n.getGeometryN(0), "First polygon"); | ||
|
|
||
| expect2 = (Polygon)BboxUtils.createBoxPolygon(-180, -60, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect2, n.getGeometryN(1), "Second polygon"); | ||
| // -361 = -1 after minus 180, and -2 for maxx will not cover the whole world | ||
| n = BboxUtils.normalizeBbox(-361,-2, -76.58044236400484,60.83962132913365); | ||
| assertEquals(2, n.getNumGeometries(), "Size correct"); | ||
|
|
||
| expect1 = (Polygon)BboxUtils.createBoxPolygon(-1, 180, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect1, n.getGeometryN(0), "First polygon"); | ||
|
|
||
| expect2 = (Polygon)BboxUtils.createBoxPolygon(-180, -2, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect2, n.getGeometryN(1), "Second polygon"); | ||
| // -361 = -1 after minus 180, and -1 for maxx will cover the whole world | ||
| n = BboxUtils.normalizeBbox(-361,-1, -76.58044236400484,60.83962132913365); | ||
| assertEquals(1, n.getNumGeometries(), "Size correct"); | ||
|
|
||
| expect1 = (Polygon)BboxUtils.createBoxPolygon(-180, 180, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect1, n.getGeometryN(0), "First polygon"); | ||
| } | ||
| /** | ||
| * Very small minx so it cover whole world | ||
| */ | ||
| @Test | ||
| public void verifyNormalizeBboxVerySmallMinX() { | ||
| Geometry n = BboxUtils.normalizeBbox(-650,-60,-76.58044236400484,60.83962132913365); | ||
| assertEquals(1, n.getNumGeometries(), "Size correct"); | ||
|
|
||
| Polygon expect1 = (Polygon)BboxUtils.createBoxPolygon(-180, 180, -76.58044236400484, 60.83962132913365); | ||
| assertEquals(expect1, n.getGeometryN(0), "First polygon min 10"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.