Skip to content

Commit

Permalink
Fix OffsetCurve to support mitre joins for polygons (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-jts authored Jan 7, 2025
1 parent dcb9f94 commit e83b7e4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,27 +208,37 @@ public Geometry getCurve() {
public Geometry map(Geometry geom) {
if (geom instanceof Point) return null;
if (geom instanceof Polygon ) {
return toLineString(geom.buffer(distance).getBoundary());
return computePolygonCurve((Polygon) geom, distance);
}
return computeCurve((LineString) geom, distance);
}

/**
* Force LinearRings to be LineStrings.
*
* @param geom a geometry which may be a LinearRing
* @return a geometry which will be a LineString or MultiLineString
*/
private Geometry toLineString(Geometry geom) {
if (geom instanceof LinearRing) {
LinearRing ring = (LinearRing) geom;
return geom.getFactory().createLineString(ring.getCoordinateSequence());
}
return geom;
}
});
}

private Geometry computePolygonCurve(Polygon poly, double distance) {
Geometry buffer;
if (bufferParams == null)
buffer = BufferOp.bufferOp(poly, distance);
else {
buffer = BufferOp.bufferOp(poly, distance, bufferParams);
}
return toLineString(buffer.getBoundary());
}

/**
* Force LinearRings to be LineStrings.
*
* @param geom a linear geometry which may be a LinearRing
* @return a geometry which if linear is a LineString or MultiLineString
*/
private static Geometry toLineString(Geometry geom) {
if (geom instanceof LinearRing) {
LinearRing ring = (LinearRing) geom;
return geom.getFactory().createLineString(ring.getCoordinateSequence());
}
return geom;
}

/**
* Gets the raw offset curve for a line at a given distance.
* The quadrant segments, join style and mitre limit can be specified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,21 @@ public void testMitreJoinNegDistance() {
);
}

public void testPolygonMitreJoin() {
checkOffsetCurve(
"POLYGON ((1 1, 1 7, 5 4, 8 8, 8 1, 1 1))",
1, 0, BufferParameters.JOIN_MITRE, 5,
"LINESTRING (0 0, 0 9, 4.8 5.4, 9 11, 9 0, 0 0)",
0.0001
);
checkOffsetCurve(
"POLYGON ((1 1, 1 7, 5 4, 8 8, 8 1, 1 1))",
-1, 0, BufferParameters.JOIN_MITRE, 5,
"LINESTRING (2 2, 2 5, 5.2 2.6, 7 5, 7 2, 2 2)",
0.0001
);

}
//=======================================

private static final double EQUALS_TOL = 0.05;
Expand Down

0 comments on commit e83b7e4

Please sign in to comment.