Class 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 the CartesianAuthorityFactory.GENERIC_2D It 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.CoordinateReferenceSystem CARTESIANCRS
      In absence of a geographic crs we can also use cartesian: GENERIC_2D
      static org.geotools.referencing.crs.DefaultGeographicCRS DEFAULT_GEOGRAPHIC_CRS
      Default Coordinate Reference System: WGS84
    • Constructor Summary

      Constructors 
      Constructor Description
      PlanitJtsUtils()
      Constructor Uses default coordinate reference system
      PlanitJtsUtils​(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.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
      static 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 coordinates
      static 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 valid
      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)
      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
      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
      static org.locationtech.jts.geom.Polygon create2DPolygon​(List<Double> coordinateList2D)
      create a polygon based on the passed in 2d list of doubles
      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
      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
      static 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 createCsvStringFromLineString​(org.locationtech.jts.geom.LineString geometry, Character ts, Character cs, DecimalFormat df)
      Based on the line string construct a csv string
      static org.locationtech.jts.geom.LineString createLineString​(String value, char ts, char cs)
      Based on the csv string construct a JTS line string
      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.
      static org.locationtech.jts.geom.LineString createLineStringFromCoordinates​(org.locationtech.jts.geom.Coordinate[] coordinates)
      Create a line string from the passed in coordinates
      static org.locationtech.jts.geom.LineString createLineStringFromCsvString​(String value, String ts, String cs)
      Based on the csv string construct a line string
      static org.locationtech.jts.geom.Point createPoint​(double xCoordinate, double yCoordinate)
      Create JTS point object from X- and Y-coordinates
      static org.locationtech.jts.geom.Polygon createPolygon()
      Create an empty polygon geometry
      static org.locationtech.jts.geom.Polygon createPolygon​(org.locationtech.jts.geom.Coordinate[] coords)
      create a polygon based on the passed coordinate array
      static org.locationtech.jts.geom.Coordinate[] directPositionsToCoordinates​(List<org.opengis.geometry.DirectPosition> positions)
      Convert OpenGIS directPosition to JTS coordinates
      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.
      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.CoordinateReferenceSystem getCoordinateReferenceSystem()
      collect the crs used by this instance of the utils class
      double getDistanceInKilometres​(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 possible
      double getDistanceInKilometres​(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 instance
      double getDistanceInKilometres​(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 instance
      double getDistanceInMetres​(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 instance
      double getDistanceInMetres​(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 instance
      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.
    • 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 PlanItException
        Compute 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 point
        endPosition - 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 PlanItException
        Compute 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 point
        endCoordinate - 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 PlanItException
        Compute 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 point
        endPosition - 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 location
        vertex2 - 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 PlanItException
        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
        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 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
      • 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 instance
        ts - tuple separating character
        cs - 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 PlanItException
        Create 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 instance
        ts - 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 instance
        ts - tuple separating string to use
        cs - comma separating string to use
        df - 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 PlanItException
        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 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 PlanItException
        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 use
        geometry - 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 PlanItException
        Remove all coordinates in the line string up to but not including the passed in index.
        Parameters:
        startIndex - start index
        geometry - 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 PlanItException
        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 geometry
        geometry - 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 PlanItException
        Remove all coordinates in the line string after but not including the passed in index.
        Parameters:
        endIndex - last index to keep, after is removed
        geometry - 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 locate
        offset - start searching from offset position
        geometry - 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 locate
        geometry - 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 PlanItException
        copy the coordinates in the line string starting at the given offset (included)
        Parameters:
        offset - to start at
        geometry - 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 PlanItException
        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
        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 PlanItException
        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
        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