Package org.planit.geo
Class PlanitJtsUtils
- java.lang.Object
-
- org.planit.geo.PlanitJtsUtils
-
public class PlanitJtsUtils extends Object
General geotools related utils. Uses geodetic distance when possible. In case the CRS is not based on an ellipsoid (2d plane) it will simply compute the distance between coordinates using Pythagoras with the unit distance in meters, consistent with theCartesianAuthorityFactory.GENERIC_2DIt is assumed that x coordinate refers to latitude and y coordinate refers to longitude- Author:
- markr
-
-
Field Summary
Fields Modifier and Type Field Description static org.opengis.referencing.crs.CoordinateReferenceSystemCARTESIANCRSIn absence of a geographic crs we can also use cartesian: GENERIC_2Dstatic org.geotools.referencing.crs.DefaultGeographicCRSDEFAULT_GEOGRAPHIC_CRSDefault Coordinate Reference System: WGS84
-
Constructor Summary
Constructors Constructor Description PlanitJtsUtils()Constructor Uses default coordinate reference systemPlanitJtsUtils(org.opengis.referencing.crs.CoordinateReferenceSystem coordinateReferenceSystem)Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static org.locationtech.jts.geom.LineStringconcatenate(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.LineStringconvertToJtsLineString(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.LineStringconvertToLineString(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.Polygoncreate2DPolygon(List<Double> coordinateList2D)create a polygon based on the passed in 2d list of doublesstatic org.locationtech.jts.geom.CoordinatecreateCoordinate(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.LineStringcreateCopyWithoutAdjacentDuplicateCoordinates(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.LineStringcreateCopyWithoutCoordinatesAfter(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.LineStringcreateCopyWithoutCoordinatesAfter(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.LineStringcreateCopyWithoutCoordinatesBefore(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.LineStringcreateCopyWithoutCoordinatesBefore(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 StringcreateCsvStringFromLineString(org.locationtech.jts.geom.LineString geometry, Character ts, Character cs, DecimalFormat df)Based on the line string construct a csv stringstatic org.locationtech.jts.geom.LineStringcreateLineString(String value, char ts, char cs)Based on the csv string construct a JTS line stringstatic org.locationtech.jts.geom.LineStringcreateLineString(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.LineStringcreateLineStringFromCoordinates(org.locationtech.jts.geom.Coordinate[] coordinates)Create a line string from the passed in coordinatesstatic org.locationtech.jts.geom.LineStringcreateLineStringFromCsvString(String value, String ts, String cs)Based on the csv string construct a line stringstatic org.locationtech.jts.geom.PointcreatePoint(double xCoordinate, double yCoordinate)Create JTS point object from X- and Y-coordinatesstatic org.locationtech.jts.geom.PolygoncreatePolygon()Create an empty polygon geometrystatic org.locationtech.jts.geom.PolygoncreatePolygon(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)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)find first position where the coordinate resides on the geometry.org.opengis.referencing.crs.CoordinateReferenceSystemgetCoordinateReferenceSystem()collect the crs used by this instance of the utils classdoublegetDistanceInKilometres(org.locationtech.jts.geom.LineString geometry)Compute the length of the line string by traversing all nodes and computing the segment by segment distances TODO: find out if a faster way is possibledoublegetDistanceInKilometres(org.locationtech.jts.geom.Point startPosition, org.locationtech.jts.geom.Point endPosition)Compute the distance in kilometres between two positions assuming the positions are provided in the same crs as registered on this class instancedoublegetDistanceInKilometres(Vertex vertex1, Vertex vertex2)Compute the distance in kilometres between two vertices assuming the positions are set and based on the same crs as registered on this class instancedoublegetDistanceInMetres(org.locationtech.jts.geom.Coordinate startCoordinate, org.locationtech.jts.geom.Coordinate endCoordinate)Compute the distance in metres between two (JTS) coordinates assuming the positions are provided in the same crs as registered on this class instancedoublegetDistanceInMetres(org.locationtech.jts.geom.Point startPosition, org.locationtech.jts.geom.Point endPosition)Compute the distance in metres between two (JTS) points assuming the positions are provided in the same crs as registered on this class instancestatic 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.
-
-
-
Field Detail
-
DEFAULT_GEOGRAPHIC_CRS
public static final org.geotools.referencing.crs.DefaultGeographicCRS DEFAULT_GEOGRAPHIC_CRS
Default Coordinate Reference System: WGS84
-
CARTESIANCRS
public static final org.opengis.referencing.crs.CoordinateReferenceSystem CARTESIANCRS
In absence of a geographic crs we can also use cartesian: GENERIC_2D
-
-
Constructor Detail
-
PlanitJtsUtils
public PlanitJtsUtils()
Constructor Uses default coordinate reference system
-
PlanitJtsUtils
public PlanitJtsUtils(org.opengis.referencing.crs.CoordinateReferenceSystem coordinateReferenceSystem)
Constructor- Parameters:
coordinateReferenceSystem- OpenGIS CoordinateReferenceSystem object containing geometry
-
-
Method Detail
-
getDistanceInMetres
public double getDistanceInMetres(org.locationtech.jts.geom.Point startPosition, org.locationtech.jts.geom.Point endPosition) throws PlanItExceptionCompute the distance in metres between two (JTS) points assuming the positions are provided in the same crs as registered on this class instance- Parameters:
startPosition- location of the start pointendPosition- location of the end point- Returns:
- distance in metres between the points
- Throws:
PlanItException- thrown if there is an error
-
getDistanceInMetres
public double getDistanceInMetres(org.locationtech.jts.geom.Coordinate startCoordinate, org.locationtech.jts.geom.Coordinate endCoordinate) throws PlanItExceptionCompute the distance in metres between two (JTS) coordinates assuming the positions are provided in the same crs as registered on this class instance- Parameters:
startCoordinate- location of the start pointendCoordinate- location of the end point- Returns:
- distance in metres between the points
- Throws:
PlanItException- thrown if there is an error
-
getDistanceInKilometres
public double getDistanceInKilometres(org.locationtech.jts.geom.Point startPosition, org.locationtech.jts.geom.Point endPosition) throws PlanItExceptionCompute the distance in kilometres between two positions assuming the positions are provided in the same crs as registered on this class instance- Parameters:
startPosition- location of the start pointendPosition- location of the end point- Returns:
- distance in kilometres between the points
- Throws:
PlanItException- thrown if there is an error
-
getDistanceInKilometres
public double getDistanceInKilometres(Vertex vertex1, Vertex vertex2) throws PlanItException
Compute the distance in kilometres between two vertices assuming the positions are set and based on the same crs as registered on this class instance- Parameters:
vertex1- vertex with locationvertex2- vertex with location- Returns:
- distance in kilometres between the points
- Throws:
PlanItException- thrown if there is an error
-
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(double xCoordinate, double yCoordinate) throws PlanItExceptionCreate 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
- Throws:
PlanItException- thrown if there is an error during processing
-
convertToJtsLineString
public static org.locationtech.jts.geom.LineString convertToJtsLineString(org.opengis.geometry.coordinate.LineString openGisLineString) throws PlanItExceptionConvert 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 PlanItExceptionCast 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
-
createLineString
public static org.locationtech.jts.geom.LineString createLineString(List<Double> coordinateList) throws PlanItException
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
- Throws:
PlanItException- thrown if error
-
createLineString
public static org.locationtech.jts.geom.LineString createLineString(String value, char ts, char cs) throws PlanItException
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
- Throws:
PlanItException- thrown if error
-
createLineStringFromCoordinates
public static org.locationtech.jts.geom.LineString createLineStringFromCoordinates(org.locationtech.jts.geom.Coordinate[] coordinates) throws PlanItExceptionCreate a line string from the passed in coordinates- Parameters:
coordinates- source- Returns:
- created line string
- Throws:
PlanItException- thrown if error
-
createLineStringFromCsvString
public static org.locationtech.jts.geom.LineString createLineStringFromCsvString(String value, String ts, String cs) throws PlanItException
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
- Throws:
PlanItException- thrown if error
-
createCsvStringFromLineString
public static String createCsvStringFromLineString(org.locationtech.jts.geom.LineString geometry, Character ts, Character cs, DecimalFormat df)
Based on the line string construct a csv string- Parameters:
geometry- 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
-
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
-
directPositionsToCoordinates
public static org.locationtech.jts.geom.Coordinate[] directPositionsToCoordinates(List<org.opengis.geometry.DirectPosition> positions) throws PlanItException
Convert OpenGIS directPosition to JTS coordinates- Parameters:
positions- List of GeoTools Position objects- Returns:
- coordinates array of JTS Coordinate objects
- Throws:
PlanItException- thrown if there is an error
-
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
-
getDistanceInKilometres
public double getDistanceInKilometres(org.locationtech.jts.geom.LineString geometry) throws PlanItExceptionCompute the length of the line string by traversing all nodes and computing the segment by segment distances TODO: find out if a faster way is possible- Parameters:
geometry- to extract length from- Returns:
- length in km
- 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) throws PlanItExceptionRemove 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
- Throws:
PlanItException- thrown if position could not be located
-
createCopyWithoutCoordinatesBefore
public static org.locationtech.jts.geom.LineString createCopyWithoutCoordinatesBefore(int startIndex, org.locationtech.jts.geom.LineString geometry) throws PlanItExceptionRemove 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) throws PlanItExceptionRemove 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- linestring- Returns:
- copy of the line string without indicated coordinates
- Throws:
PlanItException- thrown if position could not be located
-
createCopyWithoutCoordinatesAfter
public static org.locationtech.jts.geom.LineString createCopyWithoutCoordinatesAfter(int endIndex, org.locationtech.jts.geom.LineString geometry) throws PlanItExceptionRemove 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)
find first position where the coordinate resides on the geometry.- Parameters:
coordinateToLocate- the one to locateoffset- start searching from offset positiongeometry- to locate from- Returns:
- the position if present
-
findFirstCoordinatePosition
public static Optional<Integer> findFirstCoordinatePosition(org.locationtech.jts.geom.Coordinate coordinateToLocate, org.locationtech.jts.geom.LineString geometry)
find first position where the coordinate resides on the geometry.- Parameters:
coordinateToLocate- the one to locategeometry- to locate from- Returns:
- the position if present
-
copyCoordinatesFrom
public static org.locationtech.jts.geom.Coordinate[] copyCoordinatesFrom(int offset, org.locationtech.jts.geom.LineString geometry) throws PlanItExceptioncopy 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
- Throws:
PlanItException- thrown if error
-
copyCoordinatesUpToNotIncluding
public static org.locationtech.jts.geom.Coordinate[] copyCoordinatesUpToNotIncluding(int untilPoint, org.locationtech.jts.geom.LineString geometry) throws PlanItExceptioncopy 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
- Throws:
PlanItException- thrown if error
-
copyCoordinatesFromUpToNotIncluding
public static org.locationtech.jts.geom.Coordinate[] copyCoordinatesFromUpToNotIncluding(int offset, int untilPoint, org.locationtech.jts.geom.LineString geometry) throws PlanItExceptioncopy 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
- Throws:
PlanItException- thrown if error
-
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
-
getCoordinateReferenceSystem
public org.opengis.referencing.crs.CoordinateReferenceSystem getCoordinateReferenceSystem()
collect the crs used by this instance of the utils class- Returns:
- crs used by instance
-
-