-
Notifications
You must be signed in to change notification settings - Fork 448
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
Allow GeoJSONWriter to force polygons to be CCW #694
Merged
dr-jts
merged 16 commits into
locationtech:master
from
ruibritopt:feat/RFC-7946-geojson-spec-right-hand-rule
Feb 25, 2021
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
49284e7
correct parameter order on assertEquals, avoiding confusion when test…
ruibritopt 3cdc88a
fix tests that would fail the new right hand rule
ruibritopt 0054db6
right hand rules tests for polygon with exterior and polygon with hole
ruibritopt f6fd350
enforce right hand rule when writing the geojson string
ruibritopt 934bac8
Merge branch 'master' into feat/RFC-7946-geojson-spec-right-hand-rule
ruibritopt 9ab1f57
Merge branch 'master' into feat/RFC-7946-geojson-spec-right-hand-rule
ruibritopt e4f26a1
creation of a TransformationUtils class that holds the right hand rul…
ruibritopt aa689ba
added isRHREnforced option to GeoJsonWriter class which invokes the t…
ruibritopt 63b4708
i was getting index out of bounds with an implementation that uses ar…
ruibritopt 4650572
resolved potential aliases errors && renamed right hand rule methods …
ruibritopt 6aa05b4
renamed right hand rule method args to CCW
ruibritopt 666a775
extended javadoc information
ruibritopt 8bca609
Merge branch 'master' into feat/RFC-7946-geojson-spec-right-hand-rule
ruibritopt d027011
renamed arguments for the sake of brevity
ruibritopt 77300ae
Merge remote-tracking branch 'origin/feat/RFC-7946-geojson-spec-right…
ruibritopt 4a2e129
Merge branch 'master' into feat/RFC-7946-geojson-spec-right-hand-rule
ruibritopt 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 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
88 changes: 88 additions & 0 deletions
88
modules/io/common/src/main/java/org/locationtech/jts/io/geojson/OrientationTransformer.java
This file contains 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,88 @@ | ||
package org.locationtech.jts.io.geojson; | ||
|
||
import org.locationtech.jts.algorithm.Orientation; | ||
import org.locationtech.jts.geom.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Utilities to modify the ring orientation of polygonal geometries. | ||
*/ | ||
public class OrientationTransformer { | ||
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. Add class Javadoc. Can be something simple like
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. Took your recommendation verbatim |
||
|
||
/** | ||
* Transforms a geometry using the Right Hand Rule specifications defined | ||
* in the latest GeoJSON specification. | ||
* See <a href="https://tools.ietf.org/html/rfc7946#section-3.1.6">RFC-7946 Specification</a> for more context. | ||
* | ||
* @param geometry to be transformed | ||
* @return Geometry under the Right Hand Rule specifications | ||
*/ | ||
public static Geometry transformCCW(final Geometry geometry) { | ||
|
||
if (geometry instanceof MultiPolygon) { | ||
MultiPolygon multiPolygon = (MultiPolygon) geometry; | ||
|
||
List<Polygon> polygons = new ArrayList<>(); | ||
for (int i = 0; i < multiPolygon.getNumGeometries(); i++) { | ||
final Geometry polygon = multiPolygon.getGeometryN(i); | ||
polygons.add((Polygon) transformCCW(polygon)); | ||
} | ||
|
||
return new GeometryFactory().createMultiPolygon(polygons.toArray(new Polygon[0])); | ||
|
||
} else if (geometry instanceof Polygon) { | ||
return transformCCW((Polygon) geometry); | ||
|
||
} else { | ||
return geometry; | ||
} | ||
} | ||
|
||
/** | ||
* Transforms a polygon using the Right Hand Rule specifications defined | ||
* in the latest GeoJSON specification. | ||
* See <a href="https://tools.ietf.org/html/rfc7946#section-3.1.6">RFC-7946 Specification</a> for more context. | ||
* | ||
* @param polygon to be transformed | ||
* @return Polygon under the Right Hand Rule specifications | ||
*/ | ||
public static Polygon transformCCW(Polygon polygon) { | ||
LinearRing exteriorRing = polygon.getExteriorRing(); | ||
LinearRing exteriorRingEnforced = transformCCW(exteriorRing, true); | ||
|
||
List<LinearRing> interiorRings = new ArrayList<>(); | ||
for (int i = 0; i < polygon.getNumInteriorRing(); i++) { | ||
interiorRings.add(transformCCW(polygon.getInteriorRingN(i), false)); | ||
} | ||
|
||
return new GeometryFactory(polygon.getPrecisionModel(), polygon.getSRID()) | ||
.createPolygon(exteriorRingEnforced, interiorRings.toArray(new LinearRing[0])); | ||
} | ||
|
||
/** | ||
* Transforms a polygon using the Right Hand Rule specifications defined | ||
* in the latest GeoJSON specification. | ||
* A linear ring MUST follow the right-hand rule with respect to the | ||
* area it bounds, i.e., exterior rings are counterclockwise, and | ||
* holes are clockwise. | ||
* | ||
* See <a href="https://tools.ietf.org/html/rfc7946#section-3.1.6">RFC 7946 Specification</a> for more context. | ||
* | ||
* @param ring the LinearRing, a constraint specific to Polygons | ||
* @param isExteriorRing true if the LinearRing is the exterior polygon ring, the one that defines the boundary | ||
* @return LinearRing under the Right Hand Rule specifications | ||
*/ | ||
public static LinearRing transformCCW(LinearRing ring, boolean isExteriorRing) { | ||
final boolean isRingClockWise = !Orientation.isCCW(ring.getCoordinateSequence()); | ||
|
||
final LinearRing rightHandRuleRing; | ||
if (isExteriorRing) { | ||
rightHandRuleRing = isRingClockWise? ring.reverse() : (LinearRing) ring.copy(); | ||
} else { | ||
rightHandRuleRing = isRingClockWise? (LinearRing) ring.copy() : ring.reverse(); | ||
} | ||
return rightHandRuleRing; | ||
} | ||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One final request. In the interest of brevity, can this be changed to
setForceCCW
andisForceCCW
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done and done. Don't hold back on the requests, they have all been on the money and I'm a big fan of things done right 😄👍👍