Package org.goplanit.utils.geo
Class PlanitJtsUtils
- java.lang.Object
-
- org.goplanit.utils.geo.PlanitJtsUtils
-
public class PlanitJtsUtils extends Object
General geographic related utils utilising the JTS API.- Author:
- markr
-
-
Field Summary
Fields Modifier and Type Field Description protected static org.locationtech.jts.geom.GeometryFactory
jtsGeometryFactory
jts geometry factory, jts geometry differs from opengis implementation by not carrying the crs and being more lightweight
-
Constructor Summary
Constructors Constructor Description PlanitJtsUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.locationtech.jts.geom.LineString
concatenate(org.locationtech.jts.geom.LineString... geometries)
concatenate the passed in gemoetries (lines strings) by simply copying all the coorcinates in order and create a new line string from these pointsstatic org.locationtech.jts.geom.LineString
convertToJtsLineString(org.opengis.geometry.coordinate.LineString openGisLineString)
Convert an open gis line string object to a JTS Gis LineString instance by copying the internal coordinatesstatic org.locationtech.jts.geom.LineString
convertToLineString(org.locationtech.jts.geom.MultiLineString jtsMultiLineString)
Cast a JTS MultiLineString with a single entry into a JTS LineString instance if validstatic org.locationtech.jts.geom.Coordinate[]
copyCoordinatesFrom(int offset, org.locationtech.jts.geom.LineString geometry)
copy the coordinates in the line string starting at the given offset (included)static org.locationtech.jts.geom.Coordinate[]
copyCoordinatesFromUpToNotIncluding(int offset, int untilPoint, org.locationtech.jts.geom.LineString geometry)
copy the coordinates in the line string from-to the given locations, the locations are includedstatic org.locationtech.jts.geom.Coordinate[]
copyCoordinatesUpToNotIncluding(int untilPoint, org.locationtech.jts.geom.LineString geometry)
copy the coordinates in the line string until the given location, the location is includedstatic org.locationtech.jts.geom.Coordinate[]
copyWithoutNullEntries(org.locationtech.jts.geom.Coordinate[] coordArray)
copy the array and remove any null entriesstatic org.locationtech.jts.geom.Polygon
create2DPolygon(List<Double> coordinateList2D)
create a polygon based on the passed in 2d list of doublesstatic org.locationtech.jts.geom.Polygon
create2DPolygon(org.locationtech.jts.geom.Envelope envelope)
create a polygon based on the bounding boxstatic org.locationtech.jts.geom.Coordinate
createCoordinate(org.opengis.geometry.DirectPosition position)
create a coordinate by mapping ordinate 0 to x and ordinate 1 to y on the open gis DirecPositionstatic org.locationtech.jts.geom.LineString
createCopyWithoutAdjacentDuplicateCoordinates(org.locationtech.jts.geom.LineString geometry)
create an identical copy, except that any adjacent duplicate coordinates in the line string are removedstatic org.locationtech.jts.geom.LineString
createCopyWithoutCoordinatesAfter(int endIndex, org.locationtech.jts.geom.LineString geometry)
Remove all coordinates in the line string after but not including the passed in index.static org.locationtech.jts.geom.LineString
createCopyWithoutCoordinatesAfter(org.locationtech.jts.geom.Point position, org.locationtech.jts.geom.LineString geometry)
Remove all coordinates in the line string after but not including the passed in position.static org.locationtech.jts.geom.LineString
createCopyWithoutCoordinatesBefore(int startIndex, org.locationtech.jts.geom.LineString geometry)
Remove all coordinates in the line string up to but not including the passed in index.static org.locationtech.jts.geom.LineString
createCopyWithoutCoordinatesBefore(org.locationtech.jts.geom.Point position, org.locationtech.jts.geom.LineString geometry)
Remove all coordinates in the line string up to but not including the first occurrence of the passed in position.static String
createCsvStringFromCoordinates(org.locationtech.jts.geom.Coordinate[] coordinates, Character ts, Character cs, DecimalFormat df)
Based on the line string construct a csv stringstatic org.locationtech.jts.geom.LineSegment
createLineSegment(org.locationtech.jts.geom.Coordinate coordinate1, org.locationtech.jts.geom.Coordinate coordinate2)
create a line segmentstatic org.locationtech.jts.geom.LineString
createLineString(String value, char ts, char cs)
Based on the csv string construct a JTS line stringstatic org.locationtech.jts.geom.LineString
createLineString(List<Double> coordinateList)
Create a JTS line string from the doubles passed in (list of doubles containing x1,y1,x2,y2,etc.static org.locationtech.jts.geom.LineString
createLineString(org.locationtech.jts.geom.Coordinate... coordinates)
Create a line string from the passed in coordinatesstatic org.locationtech.jts.geom.LineString
createLineStringFromCsvString(String value, String ts, String cs)
Based on the csv string construct a line stringstatic org.locationtech.jts.geom.MultiLineString
createMultiLineString(org.locationtech.jts.geom.LineString... lineStrings)
Create a multi line string from the passed in line stringsstatic org.locationtech.jts.geom.Point
createPoint(Number xCoordinate, Number yCoordinate)
Create JTS point object from X- and Y-coordinatesstatic org.locationtech.jts.geom.Point
createPoint(org.locationtech.jts.geom.Coordinate coordinate)
Create JTS point object from coordinatestatic org.locationtech.jts.geom.Polygon
createPolygon()
Create an empty polygon geometrystatic org.locationtech.jts.geom.Polygon
createPolygon(org.locationtech.jts.geom.Coordinate[] coords)
create a polygon based on the passed coordinate arraystatic org.locationtech.jts.geom.Coordinate[]
directPositionsToCoordinates(List<org.opengis.geometry.DirectPosition> positions)
Convert OpenGIS directPosition to JTS coordinatesstatic Optional<Integer>
findFirstCoordinatePosition(org.locationtech.jts.geom.Coordinate coordinateToLocate, int offset, org.locationtech.jts.geom.LineString geometry, double tolerance)
find first position where the coordinate resides on the geometry.static Optional<Integer>
findFirstCoordinatePosition(org.locationtech.jts.geom.Coordinate coordinateToLocate, org.locationtech.jts.geom.LineString geometry, double tolerance)
find first position where the coordinate resides on the geometry.static org.opengis.referencing.operation.MathTransform
findMathTransform(org.opengis.referencing.crs.CoordinateReferenceSystem sourceCRS, org.opengis.referencing.crs.CoordinateReferenceSystem destinationCRS)
Convenience method that wraps the CRS.findMathTransform by catching exceptions and producing a planit excepion only as well as allowing for lenient transformerstatic int
getCoordinateIndexOf(org.locationtech.jts.geom.Coordinate coordinate, org.locationtech.jts.geom.Coordinate[] coordinates)
collect the index of the given coordinate from the arraystatic boolean
isClosed2D(org.locationtech.jts.geom.Coordinate[] coordArray)
check if coord array is closed, i.e., first coordinate is the same as the last in 2Dstatic boolean
isCoordinateLeftOf(org.locationtech.jts.geom.Coordinate coordM, org.locationtech.jts.geom.Coordinate coordA, org.locationtech.jts.geom.Coordinate coordB)
Using the normalised sign of the determinant of line AB and AM we determine if coordM resides left of the line segment ABstatic boolean
isCoordinateRightOf(org.locationtech.jts.geom.Coordinate coordM, org.locationtech.jts.geom.Coordinate coordA, org.locationtech.jts.geom.Coordinate coordB)
Using the normalised sign of the determinant of line AB and AM we determine if coordM resides right of the line segment ABstatic org.locationtech.jts.geom.Coordinate[]
listTo2DCoordinates(List<?> posList)
Create an array of coordinates based on a list of some type that can be interpreted as strings and converted to doubles.static org.locationtech.jts.geom.Coordinate[]
makeClosed2D(org.locationtech.jts.geom.Coordinate[] coordArray)
create a copy of the passed in coord array and close it by adding a new coordinate at the end that matches the first.static org.locationtech.jts.geom.LineString
mergeLineStrings(org.locationtech.jts.geom.LineString first, org.locationtech.jts.geom.LineString second)
Merge two line strings that are expected to have at least one point in common, in case any input is null or there is no overlap between the two null is returnedstatic double
minDiffAngleInDegrees(double angleDegrees1, double angleDegrees2)
Compute the minimum difference t get to one angle to another in either direction between the given two angles (0-360)static Pair<org.locationtech.jts.geom.LineString,org.locationtech.jts.geom.LineString>
splitLineString(org.locationtech.jts.geom.LineString geometry, org.locationtech.jts.linearref.LinearLocation splitLocation)
split a line string into two line strings at a given location along the original geometry.static org.locationtech.jts.geom.Envelope
transformEnvelope(org.locationtech.jts.geom.Envelope envelope, org.opengis.referencing.operation.MathTransform crsTransform)
Transform the spanning coordinates of envelope to difference CRSstatic org.locationtech.jts.geom.Geometry
transformGeometry(org.locationtech.jts.geom.Geometry geometry, org.opengis.referencing.operation.MathTransform transformer)
Transform given geometry based on provided transformer, checkedd exceptions are converted to PLANitRunTimeException instead
-
-
-
Method Detail
-
findMathTransform
public static org.opengis.referencing.operation.MathTransform findMathTransform(org.opengis.referencing.crs.CoordinateReferenceSystem sourceCRS, org.opengis.referencing.crs.CoordinateReferenceSystem destinationCRS)
Convenience method that wraps the CRS.findMathTransform by catching exceptions and producing a planit excepion only as well as allowing for lenient transformer- Parameters:
sourceCRS
- the sourcedestinationCRS
- the destination- Returns:
- transformer
-
transformGeometry
public static org.locationtech.jts.geom.Geometry transformGeometry(org.locationtech.jts.geom.Geometry geometry, org.opengis.referencing.operation.MathTransform transformer)
Transform given geometry based on provided transformer, checkedd exceptions are converted to PLANitRunTimeException instead- Parameters:
geometry
- to transformtransformer
- to apply transformation- Returns:
- transformed geometry
-
transformEnvelope
public static org.locationtech.jts.geom.Envelope transformEnvelope(org.locationtech.jts.geom.Envelope envelope, org.opengis.referencing.operation.MathTransform crsTransform)
Transform the spanning coordinates of envelope to difference CRS- Parameters:
envelope
- to transformcrsTransform
- to apply- Returns:
- transformed envelope
-
createCoordinate
public static org.locationtech.jts.geom.Coordinate createCoordinate(org.opengis.geometry.DirectPosition position)
create a coordinate by mapping ordinate 0 to x and ordinate 1 to y on the open gis DirecPosition- Parameters:
position
- in opengis format- Returns:
- JTS coordinate created
-
createPoint
public static org.locationtech.jts.geom.Point createPoint(org.locationtech.jts.geom.Coordinate coordinate)
Create JTS point object from coordinate- Parameters:
coordinate
- to use- Returns:
- point object representing the location
-
createPoint
public static org.locationtech.jts.geom.Point createPoint(Number xCoordinate, Number yCoordinate)
Create JTS point object from X- and Y-coordinates- Parameters:
xCoordinate
- X-coordinate (longitude assumed)yCoordinate
- Y-coordinate (latitude assumed)- Returns:
- point object representing the location
-
createLineSegment
public static org.locationtech.jts.geom.LineSegment createLineSegment(org.locationtech.jts.geom.Coordinate coordinate1, org.locationtech.jts.geom.Coordinate coordinate2)
create a line segment- Parameters:
coordinate1
- first coordinatecoordinate2
- second coordinate- Returns:
- created line segment
-
createLineString
public static org.locationtech.jts.geom.LineString createLineString(List<Double> coordinateList)
Create a JTS line string from the doubles passed in (list of doubles containing x1,y1,x2,y2,etc. coordinates- Parameters:
coordinateList
- source- Returns:
- created line string
-
createLineString
public static org.locationtech.jts.geom.LineString createLineString(String value, char ts, char cs)
Based on the csv string construct a JTS line string- Parameters:
value
- the values containing the x,y coordinates in the crs of this instancets
- tuple separating charactercs
- comma separating character- Returns:
- the LineString created from the String
-
createLineString
public static org.locationtech.jts.geom.LineString createLineString(org.locationtech.jts.geom.Coordinate... coordinates)
Create a line string from the passed in coordinates- Parameters:
coordinates
- source- Returns:
- created line string
-
createLineStringFromCsvString
public static org.locationtech.jts.geom.LineString createLineStringFromCsvString(String value, String ts, String cs)
Based on the csv string construct a line string- Parameters:
value
- the values containing the x,y coordinates in the crs of this instancets
- tuple separating string (which must be a a character)cs
- comma separating string (which must be a a character)- Returns:
- the LineString created from the String
-
createCsvStringFromCoordinates
public static String createCsvStringFromCoordinates(org.locationtech.jts.geom.Coordinate[] coordinates, Character ts, Character cs, DecimalFormat df)
Based on the line string construct a csv string- Parameters:
coordinates
- the values containing the x,y coordinates in the crs of this instancets
- tuple separating string to usecs
- comma separating string to usedf
- decinal formatter to format the decimals of the coordinates- Returns:
- the LineString created from the String
-
createMultiLineString
public static org.locationtech.jts.geom.MultiLineString createMultiLineString(org.locationtech.jts.geom.LineString... lineStrings)
Create a multi line string from the passed in line strings- Parameters:
lineStrings
- source- Returns:
- created multi line string
-
createPolygon
public static org.locationtech.jts.geom.Polygon createPolygon()
Create an empty polygon geometry- Returns:
- polygon
-
create2DPolygon
public static org.locationtech.jts.geom.Polygon create2DPolygon(List<Double> coordinateList2D)
create a polygon based on the passed in 2d list of doubles- Parameters:
coordinateList2D
- to use- Returns:
- created polygon
-
createPolygon
public static org.locationtech.jts.geom.Polygon createPolygon(org.locationtech.jts.geom.Coordinate[] coords)
create a polygon based on the passed coordinate array- Parameters:
coords
- to use- Returns:
- created polygon
-
create2DPolygon
public static org.locationtech.jts.geom.Polygon create2DPolygon(org.locationtech.jts.geom.Envelope envelope)
create a polygon based on the bounding box- Parameters:
envelope
- to use- Returns:
- polygon geometry created
-
directPositionsToCoordinates
public static org.locationtech.jts.geom.Coordinate[] directPositionsToCoordinates(List<org.opengis.geometry.DirectPosition> positions)
Convert OpenGIS directPosition to JTS coordinates- Parameters:
positions
- List of GeoTools Position objects- Returns:
- coordinates array of JTS Coordinate objects
-
listTo2DCoordinates
public static org.locationtech.jts.geom.Coordinate[] listTo2DCoordinates(List<?> posList)
Create an array of coordinates based on a list of some type that can be interpreted as strings and converted to doubles.- Parameters:
posList
- to extract coordinates from- Returns:
- coordinates array
-
copyWithoutNullEntries
public static org.locationtech.jts.geom.Coordinate[] copyWithoutNullEntries(org.locationtech.jts.geom.Coordinate[] coordArray)
copy the array and remove any null entries- Parameters:
coordArray
- to trim- Returns:
- copy without null entries
-
isClosed2D
public static boolean isClosed2D(org.locationtech.jts.geom.Coordinate[] coordArray)
check if coord array is closed, i.e., first coordinate is the same as the last in 2D- Parameters:
coordArray
- to check- Returns:
- true when closed, false otherwise
-
makeClosed2D
public static org.locationtech.jts.geom.Coordinate[] makeClosed2D(org.locationtech.jts.geom.Coordinate[] coordArray) throws PlanItException
create a copy of the passed in coord array and close it by adding a new coordinate at the end that matches the first. If the array is already closed, it is returned as is. If not eligible for closing, exception is thrown.- Parameters:
coordArray
- to make closed if possible- Returns:
- closed array
- Throws:
PlanItException
- thrown if error
-
createCopyWithoutCoordinatesBefore
public static org.locationtech.jts.geom.LineString createCopyWithoutCoordinatesBefore(org.locationtech.jts.geom.Point position, org.locationtech.jts.geom.LineString geometry)
Remove all coordinates in the line string up to but not including the first occurrence of the passed in position. In case the position cannot be found, an exception will be thrown- Parameters:
position
- to usegeometry
- linestring- Returns:
- the line string created
-
createCopyWithoutCoordinatesBefore
public static org.locationtech.jts.geom.LineString createCopyWithoutCoordinatesBefore(int startIndex, org.locationtech.jts.geom.LineString geometry) throws PlanItException
Remove all coordinates in the line string up to but not including the passed in index.- Parameters:
startIndex
- start indexgeometry
- to apply to- Returns:
- the line string created
- Throws:
PlanItException
- thrown if error
-
createCopyWithoutCoordinatesAfter
public static org.locationtech.jts.geom.LineString createCopyWithoutCoordinatesAfter(org.locationtech.jts.geom.Point position, org.locationtech.jts.geom.LineString geometry)
Remove all coordinates in the line string after but not including the passed in position. In case the position cannot be found, an exception will be thrown- Parameters:
position
- first location of this position in geometry is the last entry in the copied geometrygeometry
- line string- Returns:
- copy of the line string without indicated coordinates
-
createCopyWithoutCoordinatesAfter
public static org.locationtech.jts.geom.LineString createCopyWithoutCoordinatesAfter(int endIndex, org.locationtech.jts.geom.LineString geometry) throws PlanItException
Remove all coordinates in the line string after but not including the passed in index.- Parameters:
endIndex
- last index to keep, after is removedgeometry
- to apply to- Returns:
- the line string created
- Throws:
PlanItException
- thrown if error
-
createCopyWithoutAdjacentDuplicateCoordinates
public static org.locationtech.jts.geom.LineString createCopyWithoutAdjacentDuplicateCoordinates(org.locationtech.jts.geom.LineString geometry)
create an identical copy, except that any adjacent duplicate coordinates in the line string are removed- Parameters:
geometry
- to remove duplicate coordinates from- Returns:
- geometry copy without duplicates
-
findFirstCoordinatePosition
public static Optional<Integer> findFirstCoordinatePosition(org.locationtech.jts.geom.Coordinate coordinateToLocate, int offset, org.locationtech.jts.geom.LineString geometry, double tolerance)
find first position where the coordinate resides on the geometry.- Parameters:
coordinateToLocate
- the one to locateoffset
- start searching from offset positiongeometry
- to locate fromtolerance
- the tolerance allowed- Returns:
- the position if present
-
findFirstCoordinatePosition
public static Optional<Integer> findFirstCoordinatePosition(org.locationtech.jts.geom.Coordinate coordinateToLocate, org.locationtech.jts.geom.LineString geometry, double tolerance)
find first position where the coordinate resides on the geometry.- Parameters:
coordinateToLocate
- the one to locategeometry
- to locate fromtolerance
- the tolerance allowed- Returns:
- the position if present
-
copyCoordinatesFrom
public static org.locationtech.jts.geom.Coordinate[] copyCoordinatesFrom(int offset, org.locationtech.jts.geom.LineString geometry)
copy the coordinates in the line string starting at the given offset (included)- Parameters:
offset
- to start atgeometry
- to copy from- Returns:
- coordinate array, when offset is out of bounds null is returned
-
copyCoordinatesUpToNotIncluding
public static org.locationtech.jts.geom.Coordinate[] copyCoordinatesUpToNotIncluding(int untilPoint, org.locationtech.jts.geom.LineString geometry)
copy the coordinates in the line string until the given location, the location is included- Parameters:
untilPoint
- to stop (not included)geometry
- to copy from- Returns:
- coordinate array, when offset is out of bounds null is returned
-
copyCoordinatesFromUpToNotIncluding
public static org.locationtech.jts.geom.Coordinate[] copyCoordinatesFromUpToNotIncluding(int offset, int untilPoint, org.locationtech.jts.geom.LineString geometry)
copy the coordinates in the line string from-to the given locations, the locations are included- Parameters:
offset
- to start at (included)untilPoint
- to end with (not included)geometry
- to copy from- Returns:
- coordinate array, when offset is out of bounds empty coordinate array is returned
-
concatenate
public static org.locationtech.jts.geom.LineString concatenate(org.locationtech.jts.geom.LineString... geometries)
concatenate the passed in gemoetries (lines strings) by simply copying all the coorcinates in order and create a new line string from these points- Parameters:
geometries
- to concatenate- Returns:
- created concatenated linesString
-
convertToJtsLineString
public static org.locationtech.jts.geom.LineString convertToJtsLineString(org.opengis.geometry.coordinate.LineString openGisLineString) throws PlanItException
Convert an open gis line string object to a JTS Gis LineString instance by copying the internal coordinates- Parameters:
openGisLineString
- to convert- Returns:
- jtsLineString created
- Throws:
PlanItException
- thrown if there is an error
-
convertToLineString
public static org.locationtech.jts.geom.LineString convertToLineString(org.locationtech.jts.geom.MultiLineString jtsMultiLineString) throws PlanItException
Cast a JTS MultiLineString with a single entry into a JTS LineString instance if valid- Parameters:
jtsMultiLineString
- JTS MultiLineString input object- Returns:
- jts LineString output object
- Throws:
PlanItException
- thrown if there is an error in casting
-
splitLineString
public static Pair<org.locationtech.jts.geom.LineString,org.locationtech.jts.geom.LineString> splitLineString(org.locationtech.jts.geom.LineString geometry, org.locationtech.jts.linearref.LinearLocation splitLocation)
split a line string into two line strings at a given location along the original geometry. the resulting linestrings have the split location in common- Parameters:
geometry
- to splitsplitLocation
- where to split- Returns:
- line string pair, first from start to split location, second from split location to end
-
mergeLineStrings
public static org.locationtech.jts.geom.LineString mergeLineStrings(org.locationtech.jts.geom.LineString first, org.locationtech.jts.geom.LineString second)
Merge two line strings that are expected to have at least one point in common, in case any input is null or there is no overlap between the two null is returned- Parameters:
first
- line stringsecond
- line string- Returns:
- merged line string, or null if failed or any input is null
-
isCoordinateLeftOf
public static boolean isCoordinateLeftOf(org.locationtech.jts.geom.Coordinate coordM, org.locationtech.jts.geom.Coordinate coordA, org.locationtech.jts.geom.Coordinate coordB)
Using the normalised sign of the determinant of line AB and AM we determine if coordM resides left of the line segment AB- Parameters:
coordM
- to check if left of ABcoordA
- A coord of ABcoordB
- B coord of AB- Returns:
- true when M is left of AB
-
isCoordinateRightOf
public static boolean isCoordinateRightOf(org.locationtech.jts.geom.Coordinate coordM, org.locationtech.jts.geom.Coordinate coordA, org.locationtech.jts.geom.Coordinate coordB)
Using the normalised sign of the determinant of line AB and AM we determine if coordM resides right of the line segment AB- Parameters:
coordM
- to check if right of ABcoordA
- A coord of ABcoordB
- B coord of AB- Returns:
- true when M is right of AB
-
getCoordinateIndexOf
public static int getCoordinateIndexOf(org.locationtech.jts.geom.Coordinate coordinate, org.locationtech.jts.geom.Coordinate[] coordinates)
collect the index of the given coordinate from the array- Parameters:
coordinate
- to collect index forcoordinates
- to collect from- Returns:
- index, -1 if nto found
-
minDiffAngleInDegrees
public static double minDiffAngleInDegrees(double angleDegrees1, double angleDegrees2)
Compute the minimum difference t get to one angle to another in either direction between the given two angles (0-360)- Parameters:
angleDegrees1
- first angle in degreesangleDegrees2
- second angle in degrees- Returns:
- the found angle
-
-