Package mil.nga.sf.util
Class GeometryUtils
- java.lang.Object
-
- mil.nga.sf.util.GeometryUtils
-
public class GeometryUtils extends Object
Utilities for Geometry objects- Since:
- 1.0.3
- Author:
- osbornb
-
-
Field Summary
Fields Modifier and Type Field Description static doubleDEFAULT_EPSILONDefault epsilon for line tolerancestatic doubleWEB_MERCATOR_HALF_WORLD_WIDTHHalf the world distance in either direction
-
Constructor Summary
Constructors Constructor Description GeometryUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Map<GeometryType,Map<GeometryType,?>>childHierarchy(GeometryType geometryType)Get the child type hierarchy of the provided geometry type.static List<GeometryType>childTypes(GeometryType geometryType)Get the immediate child Geometry Types of the provided geometry typestatic booleanclosedPolygon(List<Point> points)Check if the polygon ring points are explicitly closed, where the first and last point are the samestatic booleanclosedPolygon(LineString ring)Check if the polygon ring is explicitly closed, where the first and last point are the samestatic booleanclosedPolygon(Polygon polygon)Check if the polygon outer ring is explicitly closed, where the first and last point are the samestatic PointdegreesToMeters(double x, double y)Convert a coordinate in degrees to a point in metersstatic PointdegreesToMeters(Point point)Convert a point in degrees to a point in metersstatic Geometrydeserialize(byte[] bytes)Deserialize the bytes into a geometrystatic doubledistance(Point point1, Point point2)Get the Pythagorean theorem distance between two pointsstatic PointgetCentroid(Geometry geometry)Get the centroid point of a 2 dimensional representation of the Geometry (balancing point of a 2d cutout of the geometry).static PointgetDegreesCentroid(Geometry geometry)Get the geographic centroid point of a 2 dimensional representation of the degree unit Geometry.static intgetDimension(Geometry geometry)Get the dimension of the Geometry, 0 for points, 1 for curves, 2 for surfaces.static <T extends Geometry>
booleanhasM(List<T> geometries)Determine if the geometries contain a M valuestatic <T extends Geometry>
booleanhasZ(List<T> geometries)Determine if the geometries contain a Z valuestatic Pointintersection(Line line1, Line line2)Get the point intersection between two linesstatic Pointintersection(Point line1Point1, Point line1Point2, Point line2Point1, Point line2Point2)Get the point intersection between end points of two linesstatic PointmetersToDegrees(double x, double y)Convert a coordinate in meters to a point in degreesstatic PointmetersToDegrees(Point point)Convert a point in meters to a point in degreesstatic voidminimizeGeometry(Geometry geometry, double maxX)Minimize the geometry using the shortest x distance between each connected set of points.static voidnormalizeGeometry(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.static List<GeometryType>parentHierarchy(GeometryType geometryType)Get the parent type hierarchy of the provided geometry type starting with the immediate parent.static GeometryTypeparentType(GeometryType geometryType)Get the parent Geometry Type of the provided geometry typestatic doubleperpendicularDistance(Point point, Point lineStart, Point lineEnd)Calculate the perpendicular distance between the point and the line represented by the start and end points.static booleanpointInPolygon(Point point, List<Point> points)Check if the point is in the polygon pointsstatic booleanpointInPolygon(Point point, List<Point> points, double epsilon)Check if the point is in the polygon pointsstatic booleanpointInPolygon(Point point, LineString ring)Check if the point is in the polygon ringstatic booleanpointInPolygon(Point point, LineString ring, double epsilon)Check if the point is in the polygon ringstatic booleanpointInPolygon(Point point, Polygon polygon)Check if the point is in the polygonstatic booleanpointInPolygon(Point point, Polygon polygon, double epsilon)Check if the point is in the polygonstatic booleanpointOnLine(Point point, List<Point> points)Check if the point is on the line represented by the pointsstatic booleanpointOnLine(Point point, List<Point> points, double epsilon)Check if the point is on the line represented by the pointsstatic booleanpointOnLine(Point point, LineString line)Check if the point is on the linestatic booleanpointOnLine(Point point, LineString line, double epsilon)Check if the point is on the linestatic booleanpointOnPath(Point point, Point point1, Point point2)Check if the point is on the path between point 1 and point 2static booleanpointOnPath(Point point, Point point1, Point point2, double epsilon)Check if the point is on the path between point 1 and point 2static booleanpointOnPolygonEdge(Point point, List<Point> points)Check if the point is on the polygon ring edge pointsstatic booleanpointOnPolygonEdge(Point point, List<Point> points, double epsilon)Check if the point is on the polygon ring edge pointsstatic booleanpointOnPolygonEdge(Point point, LineString ring)Check if the point is on the polygon ring edgestatic booleanpointOnPolygonEdge(Point point, LineString ring, double epsilon)Check if the point is on the polygon ring edgestatic booleanpointOnPolygonEdge(Point point, Polygon polygon)Check if the point is on the polygon edgestatic booleanpointOnPolygonEdge(Point point, Polygon polygon, double epsilon)Check if the point is on the polygon edgestatic byte[]serialize(Geometry geometry)Serialize the geometry to bytesstatic 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.
-
-
-
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
-
-
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 1point2- 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 resultingPoint.getZ()andPoint.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 resultingPoint.getZ()andPoint.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 ofWEB_MERCATOR_HALF_WORLD_WIDTH. Resulting x values will be in the range: -60112525.028367732 <= x <= 60112525.028367732- Parameters:
geometry- geometrymaxX- 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 ofWEB_MERCATOR_HALF_WORLD_WIDTH. Resulting x values will be in the range: -20037508.342789244 <= x <= 20037508.342789244.- Parameters:
geometry- geometrymaxX- 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 pointstolerance- 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- pointlineStart- point representing the line startlineEnd- 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- pointpolygon- 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- pointpolygon- polygonepsilon- 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- pointring- 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- pointring- polygon ringepsilon- 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- pointpoints- 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- pointpoints- polygon pointsepsilon- 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- pointpolygon- 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- pointpolygon- polygonepsilon- 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- pointring- 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- pointring- polygon ringepsilon- 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- pointpoints- 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- pointpoints- polygon pointsepsilon- 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- pointline- 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- pointline- lineepsilon- 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- pointpoints- 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- pointpoints- line pointsepsilon- 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- pointpoint1- path point 1point2- 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- pointpoint1- path point 1point2- path point 2epsilon- 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 lineline2- 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 lineline1Point2- second point of the first lineline2Point1- first point of the second lineline2Point2- 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 degreesy- 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 metersy- 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
-
-