Class GeometryUtilities


  • public class GeometryUtilities
    extends Object
    Utilities related to Geometry.
    Author:
    Andrea Antonello (www.hydrologis.com)
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double angleBetween​(org.locationtech.jts.geom.LineSegment l1, org.locationtech.jts.geom.LineSegment l2)
      Calculates the angle between two LineSegments.
      static double angleBetween3D​(org.locationtech.jts.geom.Coordinate c1, org.locationtech.jts.geom.Coordinate c2, org.locationtech.jts.geom.Coordinate c3)
      Calculates the angle in degrees between 3 3D coordinates.
      static double azimuth​(org.locationtech.jts.geom.Coordinate c1, org.locationtech.jts.geom.Coordinate c2)
      Calculates the azimuth in degrees given two Coordinate composing a line.
      static org.locationtech.jts.geom.PrecisionModel basicPrecisionModel()  
      static org.locationtech.jts.geom.LineString createDummyLine()
      Creates a line that may help out as placeholder.
      static org.locationtech.jts.geom.Point createDummyPoint()
      Creates a point that may help out as placeholder.
      static org.locationtech.jts.geom.Polygon createDummyPolygon()
      Creates a polygon that may help out as placeholder.
      static org.locationtech.jts.geom.Polygon createPolygonFromEnvelope​(org.locationtech.jts.geom.Envelope env)  
      static org.locationtech.jts.geom.Geometry createPolygonsFromRanges​(double[] xRanges, double[] yRanges)  
      static List<org.locationtech.jts.geom.Polygon> createSimpleDirectionArrow​(org.locationtech.jts.geom.Geometry... geometries)
      Creates simple arrow polygons in the direction of the coordinates.
      static org.locationtech.jts.geom.Polygon createSimplePolygon​(org.locationtech.jts.geom.Coordinate[] coords)
      Create a simple polygon (no holes).
      static double distance3d​(org.locationtech.jts.geom.Coordinate c1, org.locationtech.jts.geom.Coordinate c2, org.geotools.referencing.GeodeticCalculator geodeticCalculator)
      Calculates the 3d distance between two coordinates that have an elevation information.
      static List<org.locationtech.jts.geom.Geometry> extractSubGeometries​(org.locationtech.jts.geom.Geometry geometry)  
      static org.locationtech.jts.index.quadtree.Quadtree geometriesToQuadTree​(List<? extends org.locationtech.jts.geom.Geometry> geometries)  
      static org.locationtech.jts.index.strtree.STRtree geometriesToSRTree​(List<? extends org.locationtech.jts.geom.Geometry> geometries)
      Pack a list of geometries in a STRtree.
      static double getAngleBetweenLinePlane​(org.locationtech.jts.geom.Coordinate a, org.locationtech.jts.geom.Coordinate d, org.locationtech.jts.geom.Coordinate b, org.locationtech.jts.geom.Coordinate c)
      Calculates the angle between line and plane.
      static double getAngleInTriangle​(double a, double b, double c)
      Uses the cosine rule to find an angle in radiants of a triangle defined by the length of its sides.
      static org.locationtech.jts.geom.Coordinate getCoordinateAtAzimuthDistance​(org.locationtech.jts.geom.Coordinate startPoint, double azimuthDeg, double distance)  
      static List<org.locationtech.jts.geom.Coordinate> getCoordinatesAtInterval​(org.locationtech.jts.geom.LineString line, double interval, boolean keepExisting, double startFrom, double endAt)
      Returns the coordinates at a given interval along the line.
      static org.locationtech.jts.geom.Coordinate getLineWithPlaneIntersection​(org.locationtech.jts.geom.Coordinate lC1, org.locationtech.jts.geom.Coordinate lC2, org.locationtech.jts.geom.Coordinate pC1, org.locationtech.jts.geom.Coordinate pC2, org.locationtech.jts.geom.Coordinate pC3)
      Get the intersection coordinate between a line and plane.
      static double[] getPlaneCoefficientsFrom3Points​(org.locationtech.jts.geom.Coordinate c1, org.locationtech.jts.geom.Coordinate c2, org.locationtech.jts.geom.Coordinate c3)
      Calculates the coeffs of the plane equation: ax+by+cz+d=0 given 3 coordinates.
      static int getPointPositionAgainstLine​(org.locationtech.jts.geom.Coordinate point, org.locationtech.jts.geom.Coordinate lineStart, org.locationtech.jts.geom.Coordinate lineEnd)
      Get the position of a point (left, right, on line) for a given line.
      static double getPolygonArea​(int[] x, int[] y, int N)
      Calculates the area of a polygon from its vertices.
      static List<org.locationtech.jts.geom.LineString> getSectionsAtInterval​(org.locationtech.jts.geom.LineString line, double interval, double width, double startFrom, double endAt)
      Returns the section line at a given interval along the line.
      static List<org.locationtech.jts.geom.LineString> getSectionsFromCoordinates​(List<org.locationtech.jts.geom.Coordinate> coordinates, double width)
      Extracts traversal sections of a given with from the supplied Coordinates.
      static double getShortestDistanceFromTriangle​(org.locationtech.jts.geom.Coordinate c, org.locationtech.jts.geom.Coordinate pC1, org.locationtech.jts.geom.Coordinate pC2, org.locationtech.jts.geom.Coordinate pC3)
      Get shortest distance from a point in 3d to a plane defined by 3 coordinates.
      static org.locationtech.jts.geom.Coordinate getTriangleCentroid​(org.locationtech.jts.geom.Coordinate A, org.locationtech.jts.geom.Coordinate B, org.locationtech.jts.geom.Coordinate C)
      Get the 3d centroid Coordinate of a triangle.
      static int getTriangleWindingRule​(org.locationtech.jts.geom.Coordinate A, org.locationtech.jts.geom.Coordinate B, org.locationtech.jts.geom.Coordinate C)
      Get the winding rule of a triangle by their coordinates (given in digitized order).
      static org.locationtech.jts.geom.GeometryFactory gf()  
      static org.locationtech.jts.geom.Polygon lines2Polygon​(boolean checkValid, org.locationtech.jts.geom.LineString... lines)
      Joins two lines to a polygon.
      static List<org.locationtech.jts.geom.LineString> mergeLinestrings​(List<org.locationtech.jts.geom.LineString> multiLines)
      Tries to merge multilines when they are snapped properly.
      static org.locationtech.jts.geom.PrecisionModel pm()  
      static List<org.locationtech.jts.geom.Geometry> queryAndIntersectGeometryTree​(org.locationtech.jts.index.strtree.STRtree tree, org.locationtech.jts.geom.Geometry intersectionGeometry)
      Query and test intersection on the result of an STRtree index containing geometries.
      static void scaleDownToFit​(Rectangle2D rectToFitIn, Rectangle2D toScale)
      Scales a rectangle down to fit inside the given one, keeping the ratio.
      static void scaleToRatio​(Rectangle2D fixed, Rectangle2D toScale, boolean doShrink)
      Extends or shrinks a rectangle following the ration of a fixed one.
      static org.locationtech.jts.geom.Geometry scaleToUnitaryArea​(org.locationtech.jts.geom.Geometry polygon)
      Scales a Polygon to have an unitary area.
      static void sortHorizontal​(org.locationtech.jts.geom.Coordinate[] coordinates)  
      static List<org.locationtech.jts.geom.Polygon> splitPolygon​(org.locationtech.jts.geom.Polygon polygon, org.locationtech.jts.geom.LineString line)
      Polygon by LineString split.
    • Field Detail

      • TYPE_GEOMETRY

        public static org.locationtech.jts.geom.Geometry[] TYPE_GEOMETRY
      • TYPE_POLYGON

        public static org.locationtech.jts.geom.Polygon[] TYPE_POLYGON
      • TYPE_MULTIPOLYGON

        public static org.locationtech.jts.geom.MultiPolygon[] TYPE_MULTIPOLYGON
      • TYPE_LINESTRING

        public static org.locationtech.jts.geom.LineString[] TYPE_LINESTRING
      • TYPE_MULTILINESTRING

        public static org.locationtech.jts.geom.MultiLineString[] TYPE_MULTILINESTRING
      • TYPE_POINT

        public static org.locationtech.jts.geom.Point[] TYPE_POINT
      • TYPE_MULTIPOINT

        public static org.locationtech.jts.geom.MultiPoint[] TYPE_MULTIPOINT
    • Constructor Detail

      • GeometryUtilities

        public GeometryUtilities()
    • Method Detail

      • basicPrecisionModel

        public static org.locationtech.jts.geom.PrecisionModel basicPrecisionModel()
      • gf

        public static org.locationtech.jts.geom.GeometryFactory gf()
      • pm

        public static org.locationtech.jts.geom.PrecisionModel pm()
      • createSimplePolygon

        public static org.locationtech.jts.geom.Polygon createSimplePolygon​(org.locationtech.jts.geom.Coordinate[] coords)
        Create a simple polygon (no holes).
        Parameters:
        coords - the coords of the polygon.
        Returns:
        the Polygon.
      • createDummyPolygon

        public static org.locationtech.jts.geom.Polygon createDummyPolygon()
        Creates a polygon that may help out as placeholder.
        Returns:
        a dummy Polygon.
      • createDummyLine

        public static org.locationtech.jts.geom.LineString createDummyLine()
        Creates a line that may help out as placeholder.
        Returns:
        a dummy LineString.
      • createDummyPoint

        public static org.locationtech.jts.geom.Point createDummyPoint()
        Creates a point that may help out as placeholder.
        Returns:
        a dummy Point.
      • createPolygonFromEnvelope

        public static org.locationtech.jts.geom.Polygon createPolygonFromEnvelope​(org.locationtech.jts.geom.Envelope env)
      • createPolygonsFromRanges

        public static org.locationtech.jts.geom.Geometry createPolygonsFromRanges​(double[] xRanges,
                                                                                  double[] yRanges)
      • extractSubGeometries

        public static List<org.locationtech.jts.geom.Geometry> extractSubGeometries​(org.locationtech.jts.geom.Geometry geometry)
      • angleBetween

        public static double angleBetween​(org.locationtech.jts.geom.LineSegment l1,
                                          org.locationtech.jts.geom.LineSegment l2)
        Calculates the angle between two LineSegments.
        Parameters:
        l1 - the first segment.
        l2 - the second segment.
        Returns:
        the angle between the two segments, starting from the first segment moving clockwise.
      • azimuth

        public static double azimuth​(org.locationtech.jts.geom.Coordinate c1,
                                     org.locationtech.jts.geom.Coordinate c2)
        Calculates the azimuth in degrees given two Coordinate composing a line. Note that the coords order is important and will differ of 180.
        Parameters:
        c1 - first coordinate (used as origin).
        c2 - second coordinate.
        Returns:
        the azimuth angle.
      • getCoordinateAtAzimuthDistance

        public static org.locationtech.jts.geom.Coordinate getCoordinateAtAzimuthDistance​(org.locationtech.jts.geom.Coordinate startPoint,
                                                                                          double azimuthDeg,
                                                                                          double distance)
      • getPolygonArea

        public static double getPolygonArea​(int[] x,
                                            int[] y,
                                            int N)
        Calculates the area of a polygon from its vertices.
        Parameters:
        x - the array of x coordinates.
        y - the array of y coordinates.
        N - the number of sides of the polygon.
        Returns:
        the area of the polygon.
      • distance3d

        public static double distance3d​(org.locationtech.jts.geom.Coordinate c1,
                                        org.locationtech.jts.geom.Coordinate c2,
                                        org.geotools.referencing.GeodeticCalculator geodeticCalculator)
        Calculates the 3d distance between two coordinates that have an elevation information.

        Note that the Coordinate.distance(Coordinate) method does only 2d.

        Parameters:
        c1 - coordinate 1.
        c2 - coordinate 2.
        geodeticCalculator - If supplied it will be used for planar distance calculation.
        Returns:
        the distance considering also elevation.
      • sortHorizontal

        public static void sortHorizontal​(org.locationtech.jts.geom.Coordinate[] coordinates)
      • lines2Polygon

        public static org.locationtech.jts.geom.Polygon lines2Polygon​(boolean checkValid,
                                                                      org.locationtech.jts.geom.LineString... lines)
        Joins two lines to a polygon.
        Parameters:
        checkValid - checks if the resulting polygon is valid.
        lines - the lines to use.
        Returns:
        the joined polygon or null if something ugly happened.
      • getCoordinatesAtInterval

        public static List<org.locationtech.jts.geom.Coordinate> getCoordinatesAtInterval​(org.locationtech.jts.geom.LineString line,
                                                                                          double interval,
                                                                                          boolean keepExisting,
                                                                                          double startFrom,
                                                                                          double endAt)
        Returns the coordinates at a given interval along the line.

        Note that first and last coordinate are also added, making it likely that the interval between the last two coordinates is less than the supplied interval.

        Parameters:
        line - the line to use.
        interval - the interval to use as distance between coordinates. Has to be > 0.
        keepExisting - if true, keeps the existing coordinates in the generated list. Aslo in this case the interval around the existing points will not reflect the asked interval.
        startFrom - if > 0, it defines the initial distance to jump.
        endAt - if > 0, it defines where to end, even if the line is longer.
        Returns:
        the list of extracted coordinates.
      • getSectionsFromCoordinates

        public static List<org.locationtech.jts.geom.LineString> getSectionsFromCoordinates​(List<org.locationtech.jts.geom.Coordinate> coordinates,
                                                                                            double width)
        Extracts traversal sections of a given with from the supplied Coordinates.
        Parameters:
        coordinates - the list of coordinates.
        width - the total with of the sections.
        Returns:
        the list of sections.
      • getSectionsAtInterval

        public static List<org.locationtech.jts.geom.LineString> getSectionsAtInterval​(org.locationtech.jts.geom.LineString line,
                                                                                       double interval,
                                                                                       double width,
                                                                                       double startFrom,
                                                                                       double endAt)
        Returns the section line at a given interval along the line.

        The returned lines are digitized from left to right and contain also the center point.

        Note that first and last coordinate's section are also added, making it likely that the interval between the last two coordinates is less than the supplied interval.

        Parameters:
        line - the line to use.
        interval - the interval to use as distance between coordinates. Has to be > 0.
        width - the total width of the section.
        startFrom - if > 0, it defines the initial distance to jump.
        endAt - if > 0, it defines where to end, even if the line is longer.
        Returns:
        the list of coordinates.
      • geometriesToSRTree

        public static org.locationtech.jts.index.strtree.STRtree geometriesToSRTree​(List<? extends org.locationtech.jts.geom.Geometry> geometries)
        Pack a list of geometries in a STRtree.

        Note that the tree can't be modified once the query method has been called first.

        Parameters:
        geometries - the list of geometries.
        Returns:
        the STRtree.
      • queryAndIntersectGeometryTree

        public static List<org.locationtech.jts.geom.Geometry> queryAndIntersectGeometryTree​(org.locationtech.jts.index.strtree.STRtree tree,
                                                                                             org.locationtech.jts.geom.Geometry intersectionGeometry)
        Query and test intersection on the result of an STRtree index containing geometries.
        Parameters:
        tree - the index.
        intersectionGeometry - the geometry to check;
        Returns:
        the intersecting geometries.
      • geometriesToQuadTree

        public static org.locationtech.jts.index.quadtree.Quadtree geometriesToQuadTree​(List<? extends org.locationtech.jts.geom.Geometry> geometries)
      • splitPolygon

        public static List<org.locationtech.jts.geom.Polygon> splitPolygon​(org.locationtech.jts.geom.Polygon polygon,
                                                                           org.locationtech.jts.geom.LineString line)
        Polygon by LineString split.

        From JTS ml: http://lists.refractions.net/pipermail/jts-devel/2008-September/002666.html

        Parameters:
        polygon - the input polygon.
        line - the input line to use to split.
        Returns:
        the list of split polygons.
      • scaleToRatio

        public static void scaleToRatio​(Rectangle2D fixed,
                                        Rectangle2D toScale,
                                        boolean doShrink)
        Extends or shrinks a rectangle following the ration of a fixed one.

        This keeps the center point of the rectangle fixed.

        Parameters:
        fixed - the fixed Rectangle2D to use for the ratio.
        toScale - the Rectangle2D to adapt to the ratio of the fixed one.
        doShrink - if true, the adapted rectangle is shrinked as opposed to extended.
      • scaleDownToFit

        public static void scaleDownToFit​(Rectangle2D rectToFitIn,
                                          Rectangle2D toScale)
        Scales a rectangle down to fit inside the given one, keeping the ratio.
        Parameters:
        rectToFitIn - the fixed rectangle to fit in.
        toScale - the rectangle to scale.
      • getPlaneCoefficientsFrom3Points

        public static double[] getPlaneCoefficientsFrom3Points​(org.locationtech.jts.geom.Coordinate c1,
                                                               org.locationtech.jts.geom.Coordinate c2,
                                                               org.locationtech.jts.geom.Coordinate c3)
        Calculates the coeffs of the plane equation: ax+by+cz+d=0 given 3 coordinates.
        Parameters:
        c1 - coordinate 1.
        c2 - coordinate 2.
        c3 - coordinate 3.
        Returns:
        the array of the coeffs [a, b, c, d]
      • getLineWithPlaneIntersection

        public static org.locationtech.jts.geom.Coordinate getLineWithPlaneIntersection​(org.locationtech.jts.geom.Coordinate lC1,
                                                                                        org.locationtech.jts.geom.Coordinate lC2,
                                                                                        org.locationtech.jts.geom.Coordinate pC1,
                                                                                        org.locationtech.jts.geom.Coordinate pC2,
                                                                                        org.locationtech.jts.geom.Coordinate pC3)
        Get the intersection coordinate between a line and plane.

        The line is defined by 2 3d coordinates and the plane by 3 3d coordinates.

        from http://paulbourke.net/geometry/pointlineplane/

        Parameters:
        lC1 - line coordinate 1.
        lC2 - line coordinate 2.
        pC1 - plane coordinate 1.
        pC2 - plane coordinate 2.
        pC3 - plane coordinate 3.
        Returns:
        the intersection coordinate or null if the line is parallel to the plane.
      • getAngleBetweenLinePlane

        public static double getAngleBetweenLinePlane​(org.locationtech.jts.geom.Coordinate a,
                                                      org.locationtech.jts.geom.Coordinate d,
                                                      org.locationtech.jts.geom.Coordinate b,
                                                      org.locationtech.jts.geom.Coordinate c)
        Calculates the angle between line and plane. http://geogebrawiki.wikispaces.com/3D+Geometry
        Parameters:
        a - the 3d point.
        d - the point of intersection between line and plane.
        b - the second plane coordinate.
        c - the third plane coordinate.
        Returns:
        the angle in degrees between line and plane.
      • getShortestDistanceFromTriangle

        public static double getShortestDistanceFromTriangle​(org.locationtech.jts.geom.Coordinate c,
                                                             org.locationtech.jts.geom.Coordinate pC1,
                                                             org.locationtech.jts.geom.Coordinate pC2,
                                                             org.locationtech.jts.geom.Coordinate pC3)
        Get shortest distance from a point in 3d to a plane defined by 3 coordinates.
        Parameters:
        c - the point in 3d.
        pC1 - plane coordinate 1.
        pC2 - plane coordinate 2.
        pC3 - plane coordinate 3.
        Returns:
        the shortest distance from the point to the plane.
      • getAngleInTriangle

        public static double getAngleInTriangle​(double a,
                                                double b,
                                                double c)
        Uses the cosine rule to find an angle in radiants of a triangle defined by the length of its sides.

        The calculated angle is the one between the two adjacent sides a and b.

        Parameters:
        a - adjacent side 1 length.
        b - adjacent side 2 length.
        c - opposite side length.
        Returns:
        the angle in radiants.
      • angleBetween3D

        public static double angleBetween3D​(org.locationtech.jts.geom.Coordinate c1,
                                            org.locationtech.jts.geom.Coordinate c2,
                                            org.locationtech.jts.geom.Coordinate c3)
        Calculates the angle in degrees between 3 3D coordinates.

        The calculated angle is the one placed in vertex c2.

        Parameters:
        c1 - first 3D point.
        c2 - central 3D point.
        c3 - last 3D point.
        Returns:
        the angle between the coordinates in degrees.
      • getTriangleWindingRule

        public static int getTriangleWindingRule​(org.locationtech.jts.geom.Coordinate A,
                                                 org.locationtech.jts.geom.Coordinate B,
                                                 org.locationtech.jts.geom.Coordinate C)
        Get the winding rule of a triangle by their coordinates (given in digitized order).
        Parameters:
        A - coordinate 1.
        B - coordinate 2.
        C - coordinate 3.
        Returns:
        -1 if the digitalization is clock wise, else 1.
      • getTriangleCentroid

        public static org.locationtech.jts.geom.Coordinate getTriangleCentroid​(org.locationtech.jts.geom.Coordinate A,
                                                                               org.locationtech.jts.geom.Coordinate B,
                                                                               org.locationtech.jts.geom.Coordinate C)
        Get the 3d centroid Coordinate of a triangle.
        Parameters:
        A - coordinate 1.
        B - coordinate 2.
        C - coordinate 3.
        Returns:
        the centroid coordinate.
      • scaleToUnitaryArea

        public static org.locationtech.jts.geom.Geometry scaleToUnitaryArea​(org.locationtech.jts.geom.Geometry polygon)
                                                                     throws Exception
        Scales a Polygon to have an unitary area.
        Parameters:
        polygon - the geometry to scale.
        Returns:
        a copy of the scaled geometry.
        Throws:
        Exception
      • mergeLinestrings

        public static List<org.locationtech.jts.geom.LineString> mergeLinestrings​(List<org.locationtech.jts.geom.LineString> multiLines)
        Tries to merge multilines when they are snapped properly.
        Parameters:
        multiLines - the lines to merge.
        Returns:
        the list of lines, ideally containing a single line,merged.
      • getPointPositionAgainstLine

        public static int getPointPositionAgainstLine​(org.locationtech.jts.geom.Coordinate point,
                                                      org.locationtech.jts.geom.Coordinate lineStart,
                                                      org.locationtech.jts.geom.Coordinate lineEnd)
        Get the position of a point (left, right, on line) for a given line.
        Parameters:
        point - the point to check.
        lineStart - the start coordinate of the line.
        lineEnd - the end coordinate of the line.
        Returns:
        1 if the point is left of the line, -1 if it is right, 0 if it is on the line.
      • createSimpleDirectionArrow

        public static List<org.locationtech.jts.geom.Polygon> createSimpleDirectionArrow​(org.locationtech.jts.geom.Geometry... geometries)
        Creates simple arrow polygons in the direction of the coordinates.
        Parameters:
        geometries - the geometries (lines and polygons) for which to create the arrows.
        Returns:
        the list of polygon arrows.