Class GeometryTools
ROI
objects and Java Topology Suite Geometry
objects.- Author:
- Pete Bankhead
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Geometry
attemptOperation
(Geometry input, Function<Geometry, Geometry> fun) Attempt to apply a function to a geometry, returning the input unchanged if there was an exception.static Geometry
constrainToBounds
(Geometry geometry, double x, double y, double width, double height) Compute the intersection of a Geometry and a specified bounding box.static AffineTransformation
convertTransform
(AffineTransform transform) Convert anAffineTransform
to anAffineTransformation
.static AffineTransform
convertTransform
(AffineTransformation transform) Convert anAffineTransformation
to anAffineTransform
.static Geometry
createRectangle
(double x, double y, double width, double height) Create a rectangular Geometry for the specified bounding box.static Geometry
ensurePolygonal
(Geometry geometry) Strip non-polygonal parts from a GeometryCollection (non-recursive).static ImageRegion
envelopToRegion
(Envelope env, int z, int t) Convert anEnvelope
to anImageRegion
.static Geometry
Fill all interior rings for the specified geometry.static ROI
geometryToROI
(Geometry geometry, ImagePlane plane) Convert a JTS Geometry to a QuPath ROI.static Shape
geometryToShape
(Geometry geometry) Convert a JTS Geometry to a java.awt.Shape.static GeometryFactory
Get the default GeometryFactory to construct Geometries within QuPath.static Geometry
homogenizeGeometryCollection
(Geometry geometry) Ensure a GeometryCollection contains only Geometries of the same type (Polygonal, Lineal or Puntal).static AffineTransformation
parseTransformMatrix
(String text) Parse the matrix (String) to create and return anAffineTransformation
.static Geometry
refineAreas
(Geometry geometry, double minSizePixels, double minHoleSizePixels) Remove small fragments and fill small interior rings within a Geometry.static Geometry
regionToGeometry
(ImageRegion region) Convert an ImageRegion to a rectangular Geometry.static Geometry
removeFragments
(Geometry geometry, double minArea) Remove fragments smaller than the specified area from a Geometry, ignoring internal rings.static Geometry
removeInteriorRings
(Geometry geometry, double minRingArea) Fill all interior rings for the specified geometry that have an area < a specified threshold.static Geometry
roiToGeometry
(ROI roi) Convert to QuPath ROI to a JTS Geometry.static Geometry
roundCoordinates
(Geometry geometry) Round coordinates in a Geometry to integer values.static Geometry
shapeToGeometry
(Shape shape) Convert a java.awt.Shape to a JTS Geometry.splitGeometryByLineStrings
(Geometry polygon, Collection<? extends Geometry> splitLines) Split an input polygonal geometry using a collection of split lines.static Geometry
tryToFixPolygon
(Polygon polygon) Test a polygon for validity, attempting to fix TopologyValidationErrors if possible.static Geometry
union
(Collection<? extends Geometry> geometries) Calculate the union of multiple Geometry objects.
-
Constructor Details
-
GeometryTools
public GeometryTools()
-
-
Method Details
-
getDefaultFactory
Get the default GeometryFactory to construct Geometries within QuPath.- Returns:
-
parseTransformMatrix
Parse the matrix (String) to create and return anAffineTransformation
.The order of the matrix elements should be the following:
m00 m01 m02
m10 m11 m12
- Parameters:
text
-- Returns:
- affineTransformation
- Throws:
ParseException
-
convertTransform
Convert anAffineTransformation
to anAffineTransform
.- Parameters:
transform
-- Returns:
-
convertTransform
Convert anAffineTransform
to anAffineTransformation
.- Parameters:
transform
-- Returns:
-
shapeToGeometry
Convert a java.awt.Shape to a JTS Geometry.- Parameters:
shape
-- Returns:
-
envelopToRegion
Convert anEnvelope
to anImageRegion
.- Parameters:
env
- envelopz
- z index for the region (default is 0)t
- timepoint for the region (default is 0)- Returns:
- the smallest
ImageRegion
that contains the specified envelop
-
attemptOperation
Attempt to apply a function to a geometry, returning the input unchanged if there was an exception.The purpose of this is to make it easier to apply non-essential functions that might fail (e.g. with a
TopologyException
and to recover easily.- Parameters:
input
- the input geometryfun
- the function to (attempt) to apply- Returns:
- the new geometry if the function succeeded, otherwise the original geometry
-
roundCoordinates
Round coordinates in a Geometry to integer values.- Parameters:
geometry
- the updated Geometry- Returns:
-
constrainToBounds
public static Geometry constrainToBounds(Geometry geometry, double x, double y, double width, double height) Compute the intersection of a Geometry and a specified bounding box. The original Geometry may be returned unchanged if no changes are required to fit within the bounds.- Parameters:
geometry
- the updated Geometryx
-y
-width
-height
-- Returns:
-
createRectangle
Create a rectangular Geometry for the specified bounding box.- Parameters:
x
-y
-width
-height
-- Returns:
-
geometryToROI
Convert a JTS Geometry to a QuPath ROI.- Parameters:
geometry
-plane
-- Returns:
-
roiToGeometry
Convert to QuPath ROI to a JTS Geometry.- Parameters:
roi
-- Returns:
-
geometryToShape
Convert a JTS Geometry to a java.awt.Shape.- Parameters:
geometry
-- Returns:
-
regionToGeometry
Convert an ImageRegion to a rectangular Geometry. The z-position is retained, but timepoint is lost.- Parameters:
region
-- Returns:
-
union
Calculate the union of multiple Geometry objects.- Parameters:
geometries
-- Returns:
-
ensurePolygonal
Strip non-polygonal parts from a GeometryCollection (non-recursive).- Parameters:
geometry
-- Returns:
- a Geometry containing only Polygons, which may be the same as the input Geometry or empty
-
homogenizeGeometryCollection
Ensure a GeometryCollection contains only Geometries of the same type (Polygonal, Lineal or Puntal). Other geometries (with lower dimension) are discarded.- Parameters:
geometry
-- Returns:
-
removeInteriorRings
Fill all interior rings for the specified geometry that have an area < a specified threshold.Note that this assumes that the geometry is valid, and does not contain self-intersections or overlapping pieces. No checks are made to confirm this (for performance reasons).
- Parameters:
geometry
-minRingArea
-- Returns:
-
fillHoles
Fill all interior rings for the specified geometry.- Parameters:
geometry
-- Returns:
-
removeFragments
Remove fragments smaller than the specified area from a Geometry, ignoring internal rings.- Parameters:
geometry
- the geometry to refineminArea
- the minimum area of a fragment to retain. If ≤ 0, the geometry is returned unchanged. Otherwise, polygons will be extracted from the geometry and all that have an external ring area smaller than minArea will be removed.- Returns:
- the refined Geometry, or an empty polygon if all pieces of the geometry were removed.
-
tryToFixPolygon
Test a polygon for validity, attempting to fix TopologyValidationErrors if possible. This attempts a range of tricks (starting with Geometry.buffer(0)), although none are guaranteed to work. The first that largely preserves the polygon's area is returned.The result is guaranteed to be valid, but not necessarily to be a close match to the original polygon; in particular, if everything failed the result will be empty.
Code that calls this method can test if the output is equal to the input to determine if any changes were made.
- Parameters:
polygon
- input (possibly-invalid) polygon- Returns:
- the input polygon (if valid), an adjusted polygon (if attempted fixes helped), or an empty polygon if the situation could not be resolved
-
refineAreas
public static Geometry refineAreas(Geometry geometry, double minSizePixels, double minHoleSizePixels) Remove small fragments and fill small interior rings within a Geometry.Note that any modifications to the geometry will result in points and lines being stripped away, leaving only polygons.
- Parameters:
geometry
- input geometry to refineminSizePixels
- minimum area of a fragment to keep (the area of interior rings for polygons will be ignored)minHoleSizePixels
- minimum size of an interior hole to keep- Returns:
- the refined geometry (possibly the original unchanged), or empty geometry if the changes resulted in the Geometry disappearing
- See Also:
-
splitGeometryByLineStrings
public static List<Geometry> splitGeometryByLineStrings(Geometry polygon, Collection<? extends Geometry> splitLines) throws IllegalArgumentException Split an input polygonal geometry using a collection of split lines.The main input must be polygonal, but the split lines can be any geometry type; their linestrings will be extracted and used for splitting.
Important! This will also split a
MultiPolygon
into its constituentPolygon
objects as a side effect. This is to ensure consistency and avoid cases where linestrings may span multiple polygons within the same multipolygon. The output may be combined to form a new multipolygon later if required.- Parameters:
polygon
- the polygonal geometry to splitsplitLines
- a collection of geometries, whose union will be used to split the input geometry- Returns:
- a list of polygons formed by the splitting. This may return the original geometry, or geometries within an original collection, if these do not need to be split.
- Throws:
IllegalArgumentException
- if the input geometry is not polygonal- Since:
- v0.5.0
-