Class GeometryUtils


  • public class GeometryUtils
    extends Object
    Utilities for Geometry objects
    Since:
    1.0.3
    Author:
    osbornb
    • Field Detail

      • DEFAULT_EPSILON

        public static final double DEFAULT_EPSILON
        Default epsilon for line tolerance
        Since:
        1.0.5
        See Also:
        Constant Field Values
      • WEB_MERCATOR_HALF_WORLD_WIDTH

        public static final double WEB_MERCATOR_HALF_WORLD_WIDTH
        Half the world distance in either direction
        Since:
        2.1.0
        See Also:
        Constant Field Values
    • Constructor Detail

      • GeometryUtils

        public GeometryUtils()
    • Method Detail

      • getDimension

        public static int getDimension​(Geometry geometry)
        Get the dimension of the Geometry, 0 for points, 1 for curves, 2 for surfaces. If a collection, the largest dimension is returned.
        Parameters:
        geometry - geometry object
        Returns:
        dimension (0, 1, or 2)
      • distance

        public static double distance​(Point point1,
                                      Point point2)
        Get the Pythagorean theorem distance between two points
        Parameters:
        point1 - point 1
        point2 - point 2
        Returns:
        distance
      • getCentroid

        public static Point getCentroid​(Geometry geometry)
        Get the centroid point of a 2 dimensional representation of the Geometry (balancing point of a 2d cutout of the geometry). Only the x and y coordinate of the resulting point are calculated and populated. The resulting Point.getZ() and Point.getM() methods will always return null.
        Parameters:
        geometry - geometry object
        Returns:
        centroid point
      • getDegreesCentroid

        public static Point getDegreesCentroid​(Geometry geometry)
        Get the geographic centroid point of a 2 dimensional representation of the degree unit Geometry. Only the x and y coordinate of the resulting point are calculated and populated. The resulting Point.getZ() and Point.getM() methods will always return null.
        Parameters:
        geometry - geometry object
        Returns:
        centroid point
        Since:
        2.0.5
      • minimizeGeometry

        public static void minimizeGeometry​(Geometry geometry,
                                            double maxX)
        Minimize the geometry using the shortest x distance between each connected set of points. The resulting geometry point x values will be in the range: (3 * min value <= x <= 3 * max value Example: For WGS84 provide a max x of 180.0. Resulting x values will be in the range: -540.0 <= x <= 540.0 Example: For web mercator provide a world width of WEB_MERCATOR_HALF_WORLD_WIDTH. Resulting x values will be in the range: -60112525.028367732 <= x <= 60112525.028367732
        Parameters:
        geometry - geometry
        maxX - max positive x value in the geometry projection
      • normalizeGeometry

        public static void normalizeGeometry​(Geometry geometry,
                                             double maxX)
        Normalize the geometry so all points outside of the min and max value range are adjusted to fall within the range. Example: For WGS84 provide a max x of 180.0. Resulting x values will be in the range: -180.0 <= x <= 180.0. Example: For web mercator provide a world width of WEB_MERCATOR_HALF_WORLD_WIDTH. Resulting x values will be in the range: -20037508.342789244 <= x <= 20037508.342789244.
        Parameters:
        geometry - geometry
        maxX - max positive x value in the geometry projection
      • simplifyPoints

        public static List<Point> simplifyPoints​(List<Point> points,
                                                 double tolerance)
        Simplify the ordered points (representing a line, polygon, etc) using the Douglas Peucker algorithm to create a similar curve with fewer points. Points should be in a meters unit type projection. The tolerance is the minimum tolerated distance between consecutive points.
        Parameters:
        points - geometry points
        tolerance - minimum tolerance in meters for consecutive points
        Returns:
        simplified points
        Since:
        1.0.4
      • perpendicularDistance

        public static double perpendicularDistance​(Point point,
                                                   Point lineStart,
                                                   Point lineEnd)
        Calculate the perpendicular distance between the point and the line represented by the start and end points. Points should be in a meters unit type projection.
        Parameters:
        point - point
        lineStart - point representing the line start
        lineEnd - point representing the line end
        Returns:
        distance in meters
        Since:
        1.0.4
      • pointInPolygon

        public static boolean pointInPolygon​(Point point,
                                             Polygon polygon)
        Check if the point is in the polygon
        Parameters:
        point - point
        polygon - polygon
        Returns:
        true if in the polygon
        Since:
        1.0.5
      • pointInPolygon

        public static boolean pointInPolygon​(Point point,
                                             Polygon polygon,
                                             double epsilon)
        Check if the point is in the polygon
        Parameters:
        point - point
        polygon - polygon
        epsilon - epsilon line tolerance
        Returns:
        true if in the polygon
        Since:
        1.0.5
      • pointInPolygon

        public static boolean pointInPolygon​(Point point,
                                             LineString ring)
        Check if the point is in the polygon ring
        Parameters:
        point - point
        ring - polygon ring
        Returns:
        true if in the polygon
        Since:
        1.0.5
      • pointInPolygon

        public static boolean pointInPolygon​(Point point,
                                             LineString ring,
                                             double epsilon)
        Check if the point is in the polygon ring
        Parameters:
        point - point
        ring - polygon ring
        epsilon - epsilon line tolerance
        Returns:
        true if in the polygon
        Since:
        1.0.5
      • pointInPolygon

        public static boolean pointInPolygon​(Point point,
                                             List<Point> points)
        Check if the point is in the polygon points
        Parameters:
        point - point
        points - polygon points
        Returns:
        true if in the polygon
        Since:
        1.0.5
      • pointInPolygon

        public static boolean pointInPolygon​(Point point,
                                             List<Point> points,
                                             double epsilon)
        Check if the point is in the polygon points
        Parameters:
        point - point
        points - polygon points
        epsilon - epsilon line tolerance
        Returns:
        true if in the polygon
        Since:
        1.0.5
      • pointOnPolygonEdge

        public static boolean pointOnPolygonEdge​(Point point,
                                                 Polygon polygon)
        Check if the point is on the polygon edge
        Parameters:
        point - point
        polygon - polygon
        Returns:
        true if on the polygon edge
        Since:
        1.0.5
      • pointOnPolygonEdge

        public static boolean pointOnPolygonEdge​(Point point,
                                                 Polygon polygon,
                                                 double epsilon)
        Check if the point is on the polygon edge
        Parameters:
        point - point
        polygon - polygon
        epsilon - epsilon line tolerance
        Returns:
        true if on the polygon edge
        Since:
        1.0.5
      • pointOnPolygonEdge

        public static boolean pointOnPolygonEdge​(Point point,
                                                 LineString ring)
        Check if the point is on the polygon ring edge
        Parameters:
        point - point
        ring - polygon ring
        Returns:
        true if on the polygon edge
        Since:
        1.0.5
      • pointOnPolygonEdge

        public static boolean pointOnPolygonEdge​(Point point,
                                                 LineString ring,
                                                 double epsilon)
        Check if the point is on the polygon ring edge
        Parameters:
        point - point
        ring - polygon ring
        epsilon - epsilon line tolerance
        Returns:
        true if on the polygon edge
        Since:
        1.0.5
      • pointOnPolygonEdge

        public static boolean pointOnPolygonEdge​(Point point,
                                                 List<Point> points)
        Check if the point is on the polygon ring edge points
        Parameters:
        point - point
        points - polygon points
        Returns:
        true if on the polygon edge
        Since:
        1.0.5
      • pointOnPolygonEdge

        public static boolean pointOnPolygonEdge​(Point point,
                                                 List<Point> points,
                                                 double epsilon)
        Check if the point is on the polygon ring edge points
        Parameters:
        point - point
        points - polygon points
        epsilon - epsilon line tolerance
        Returns:
        true if on the polygon edge
        Since:
        1.0.5
      • closedPolygon

        public static boolean closedPolygon​(Polygon polygon)
        Check if the polygon outer ring is explicitly closed, where the first and last point are the same
        Parameters:
        polygon - polygon
        Returns:
        true if the first and last points are the same
        Since:
        1.0.5
      • closedPolygon

        public static boolean closedPolygon​(LineString ring)
        Check if the polygon ring is explicitly closed, where the first and last point are the same
        Parameters:
        ring - polygon ring
        Returns:
        true if the first and last points are the same
        Since:
        1.0.5
      • closedPolygon

        public static boolean closedPolygon​(List<Point> points)
        Check if the polygon ring points are explicitly closed, where the first and last point are the same
        Parameters:
        points - polygon ring points
        Returns:
        true if the first and last points are the same
        Since:
        1.0.5
      • pointOnLine

        public static boolean pointOnLine​(Point point,
                                          LineString line)
        Check if the point is on the line
        Parameters:
        point - point
        line - line
        Returns:
        true if on the line
        Since:
        1.0.5
      • pointOnLine

        public static boolean pointOnLine​(Point point,
                                          LineString line,
                                          double epsilon)
        Check if the point is on the line
        Parameters:
        point - point
        line - line
        epsilon - epsilon line tolerance
        Returns:
        true if on the line
        Since:
        1.0.5
      • pointOnLine

        public static boolean pointOnLine​(Point point,
                                          List<Point> points)
        Check if the point is on the line represented by the points
        Parameters:
        point - point
        points - line points
        Returns:
        true if on the line
        Since:
        1.0.5
      • pointOnLine

        public static boolean pointOnLine​(Point point,
                                          List<Point> points,
                                          double epsilon)
        Check if the point is on the line represented by the points
        Parameters:
        point - point
        points - line points
        epsilon - epsilon line tolerance
        Returns:
        true if on the line
        Since:
        1.0.5
      • pointOnPath

        public static boolean pointOnPath​(Point point,
                                          Point point1,
                                          Point point2)
        Check if the point is on the path between point 1 and point 2
        Parameters:
        point - point
        point1 - path point 1
        point2 - path point 2
        Returns:
        true if on the path
        Since:
        1.0.5
      • pointOnPath

        public static boolean pointOnPath​(Point point,
                                          Point point1,
                                          Point point2,
                                          double epsilon)
        Check if the point is on the path between point 1 and point 2
        Parameters:
        point - point
        point1 - path point 1
        point2 - path point 2
        epsilon - epsilon line tolerance
        Returns:
        true if on the path
        Since:
        1.0.5
      • intersection

        public static Point intersection​(Line line1,
                                         Line line2)
        Get the point intersection between two lines
        Parameters:
        line1 - first line
        line2 - second line
        Returns:
        intersection point or null if no intersection
        Since:
        2.1.0
      • intersection

        public static Point intersection​(Point line1Point1,
                                         Point line1Point2,
                                         Point line2Point1,
                                         Point line2Point2)
        Get the point intersection between end points of two lines
        Parameters:
        line1Point1 - first point of the first line
        line1Point2 - second point of the first line
        line2Point1 - first point of the second line
        line2Point2 - second point of the second line
        Returns:
        intersection point or null if no intersection
        Since:
        2.1.0
      • degreesToMeters

        public static Point degreesToMeters​(Point point)
        Convert a point in degrees to a point in meters
        Parameters:
        point - point in degrees
        Returns:
        point in meters
        Since:
        2.1.0
      • degreesToMeters

        public static Point degreesToMeters​(double x,
                                            double y)
        Convert a coordinate in degrees to a point in meters
        Parameters:
        x - x value in degrees
        y - y value in degrees
        Returns:
        point in meters
        Since:
        2.1.0
      • metersToDegrees

        public static Point metersToDegrees​(Point point)
        Convert a point in meters to a point in degrees
        Parameters:
        point - point in meters
        Returns:
        point in degrees
        Since:
        2.1.0
      • metersToDegrees

        public static Point metersToDegrees​(double x,
                                            double y)
        Convert a coordinate in meters to a point in degrees
        Parameters:
        x - x value in meters
        y - y value in meters
        Returns:
        point in degrees
        Since:
        2.1.0
      • hasZ

        public static <T extends Geometry> boolean hasZ​(List<T> geometries)
        Determine if the geometries contain a Z value
        Type Parameters:
        T - geometry type
        Parameters:
        geometries - list of geometries
        Returns:
        true if has z
      • hasM

        public static <T extends Geometry> boolean hasM​(List<T> geometries)
        Determine if the geometries contain a M value
        Type Parameters:
        T - geometry type
        Parameters:
        geometries - list of geometries
        Returns:
        true if has m
      • parentHierarchy

        public static List<GeometryType> parentHierarchy​(GeometryType geometryType)
        Get the parent type hierarchy of the provided geometry type starting with the immediate parent. If the argument is GEOMETRY, an empty list is returned, else the final type in the list will be GEOMETRY.
        Parameters:
        geometryType - geometry type
        Returns:
        list of increasing parent types
        Since:
        2.0.1
      • parentType

        public static GeometryType parentType​(GeometryType geometryType)
        Get the parent Geometry Type of the provided geometry type
        Parameters:
        geometryType - geometry type
        Returns:
        parent geometry type or null if argument is GEOMETRY (no parent type)
        Since:
        2.0.1
      • childHierarchy

        public static Map<GeometryType,​Map<GeometryType,​?>> childHierarchy​(GeometryType geometryType)
        Get the child type hierarchy of the provided geometry type.
        Parameters:
        geometryType - geometry type
        Returns:
        child type hierarchy, null if no children
        Since:
        2.0.1
      • childTypes

        public static List<GeometryType> childTypes​(GeometryType geometryType)
        Get the immediate child Geometry Types of the provided geometry type
        Parameters:
        geometryType - geometry type
        Returns:
        child geometry types, empty list if no child types
        Since:
        2.0.1
      • serialize

        public static byte[] serialize​(Geometry geometry)
        Serialize the geometry to bytes
        Parameters:
        geometry - geometry
        Returns:
        serialized bytes
        Since:
        2.0.1
      • deserialize

        public static Geometry deserialize​(byte[] bytes)
        Deserialize the bytes into a geometry
        Parameters:
        bytes - serialized bytes
        Returns:
        geometry
        Since:
        2.0.1