Class PlanitOpenGisUtils


  • public class PlanitOpenGisUtils
    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
      PlanitOpenGisUtils()
      Constructor Uses default coordinate reference system
      PlanitOpenGisUtils​(org.opengis.referencing.crs.CoordinateReferenceSystem coordinateReferenceSystem)
      Constructor
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      List<org.opengis.geometry.DirectPosition> convertToDirectPositions​(org.locationtech.jts.geom.Coordinate[] coordinates)
      Convert JTS coordinates to OpenGIS directPositions
      org.opengis.geometry.coordinate.LineString convertToOpenGisLineString​(org.locationtech.jts.geom.LineString jtsLineString)
      Convert a JTS line string object to an OpenGis LineString instance by transferring the internal coordinates
      org.opengis.geometry.coordinate.LineString convertToOpenGisLineString​(org.locationtech.jts.geom.MultiLineString jtsMultiLineString)
      Converts a JTS MultiLineString with a single entry into an OpenGIS LineString instance
      static org.opengis.referencing.crs.CoordinateReferenceSystem createCoordinateReferenceSystem​(String code)
      create a coordinate reference system instance based on String representation, e.g.
      org.opengis.geometry.DirectPosition createDirectPosition​(double xCoordinate, double yCoordinate)
      Create DirectPosition object from X- and Y-coordinates
      org.opengis.geometry.coordinate.LineString createLineString​(String value, char ts, char cs)
      Based on the csv string construct a line string
      org.opengis.geometry.coordinate.LineString createLineString​(List<Double> coordinateList)
      Create a line string from the doubles passed in (list of doubles containing x1,y1,x2,y2,etc.
      org.opengis.geometry.coordinate.LineString createLineStringFromCsvString​(String value, String ts, String cs)
      Based on the csv string construct a line string
      org.opengis.geometry.coordinate.LineString createLineStringFromPositions​(List<org.opengis.geometry.coordinate.Position> positionList)
      Create a line string from the passed in positions
      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
      org.opengis.geometry.coordinate.Position getClosestSamplePointOnLineString​(org.opengis.geometry.coordinate.Position toMatch, org.opengis.geometry.coordinate.LineString lineString)
      Find the closest explicit sample point registered on the line string compared to the passed in position
      double getDistanceInKilometres​(org.opengis.geometry.coordinate.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.opengis.geometry.coordinate.Position startPosition, org.opengis.geometry.coordinate.Position 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 vertexA, Vertex vertexB)
      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.opengis.geometry.coordinate.Position startPosition, org.opengis.geometry.coordinate.Position endPosition)
      Compute the distance in metres between two positions assuming the positions are provided in the same crs as registered on this class instance
    • 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

      • PlanitOpenGisUtils

        public PlanitOpenGisUtils()
        Constructor Uses default coordinate reference system
      • PlanitOpenGisUtils

        public PlanitOpenGisUtils​(org.opengis.referencing.crs.CoordinateReferenceSystem coordinateReferenceSystem)
        Constructor
        Parameters:
        coordinateReferenceSystem - OpenGIS CoordinateReferenceSystem object containing geometry
    • Method Detail

      • getDistanceInMetres

        public double getDistanceInMetres​(org.opengis.geometry.coordinate.Position startPosition,
                                          org.opengis.geometry.coordinate.Position endPosition)
                                   throws PlanItException
        Compute the distance in metres 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 metres between the points
        Throws:
        PlanItException - thrown if there is an error
      • getDistanceInKilometres

        public double getDistanceInKilometres​(org.opengis.geometry.coordinate.Position startPosition,
                                              org.opengis.geometry.coordinate.Position 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 vertexA,
                                              Vertex vertexB)
                                       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:
        vertexA - vertex with location
        vertexB - vertex with location
        Returns:
        distance in kilometres between the points
        Throws:
        PlanItException - thrown if there is an error
      • createDirectPosition

        public org.opengis.geometry.DirectPosition createDirectPosition​(double xCoordinate,
                                                                        double yCoordinate)
                                                                 throws PlanItException
        Create DirectPosition object from X- and Y-coordinates
        Parameters:
        xCoordinate - X-coordinate (longitude assumed)
        yCoordinate - Y-coordinate (latitude assumed)
        Returns:
        DirectPosition object representing the location
        Throws:
        PlanItException - thrown if there is an error during processing
      • convertToOpenGisLineString

        public org.opengis.geometry.coordinate.LineString convertToOpenGisLineString​(org.locationtech.jts.geom.LineString jtsLineString)
                                                                              throws PlanItException
        Convert a JTS line string object to an OpenGis LineString instance by transferring the internal coordinates
        Parameters:
        jtsLineString - JTS line string input
        Returns:
        LineString GeoTools line string output object
        Throws:
        PlanItException - thrown if there is an error
      • convertToOpenGisLineString

        public org.opengis.geometry.coordinate.LineString convertToOpenGisLineString​(org.locationtech.jts.geom.MultiLineString jtsMultiLineString)
                                                                              throws PlanItException
        Converts a JTS MultiLineString with a single entry into an OpenGIS LineString instance
        Parameters:
        jtsMultiLineString - JTS MultiLineString input object
        Returns:
        LineString GeoTools MultiLineString output object
        Throws:
        PlanItException - thrown if there is an error
      • createLineString

        public org.opengis.geometry.coordinate.LineString createLineString​(List<Double> coordinateList)
                                                                    throws PlanItException
        Create a 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 org.opengis.geometry.coordinate.LineString createLineString​(String value,
                                                                           char ts,
                                                                           char 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 character
        cs - comma separating character
        Returns:
        the LineString created from the String
        Throws:
        PlanItException - thrown if error
      • createLineStringFromPositions

        public org.opengis.geometry.coordinate.LineString createLineStringFromPositions​(List<org.opengis.geometry.coordinate.Position> positionList)
                                                                                 throws PlanItException
        Create a line string from the passed in positions
        Parameters:
        positionList - source
        Returns:
        created line string
        Throws:
        PlanItException - thrown if error
      • createLineStringFromCsvString

        public org.opengis.geometry.coordinate.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
      • convertToDirectPositions

        public List<org.opengis.geometry.DirectPosition> convertToDirectPositions​(org.locationtech.jts.geom.Coordinate[] coordinates)
                                                                           throws PlanItException
        Convert JTS coordinates to OpenGIS directPositions
        Parameters:
        coordinates - array of JTS Coordinate objects
        Returns:
        List of GeoTools Position objects
        Throws:
        PlanItException - thrown if there is an error
      • getDistanceInKilometres

        public double getDistanceInKilometres​(org.opengis.geometry.coordinate.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
      • getClosestSamplePointOnLineString

        public org.opengis.geometry.coordinate.Position getClosestSamplePointOnLineString​(org.opengis.geometry.coordinate.Position toMatch,
                                                                                          org.opengis.geometry.coordinate.LineString lineString)
                                                                                   throws PlanItException
        Find the closest explicit sample point registered on the line string compared to the passed in position
        Parameters:
        toMatch - position to egt closest to
        lineString - to sample ordinates from to check
        Returns:
        closest ordinate (position) on line string to passed in toMatch position
        Throws:
        PlanItException - thrown if error
      • findMathTransform

        public static org.opengis.referencing.operation.MathTransform findMathTransform​(org.opengis.referencing.crs.CoordinateReferenceSystem sourceCRS,
                                                                                        org.opengis.referencing.crs.CoordinateReferenceSystem destinationCRS)
                                                                                 throws PlanItException
        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 source
        destinationCRS - the destination
        Returns:
        transformer
        Throws:
        PlanItException - thrown if error
      • createCoordinateReferenceSystem

        public static org.opengis.referencing.crs.CoordinateReferenceSystem createCoordinateReferenceSystem​(String code)
        create a coordinate reference system instance based on String representation, e.g. "EPSG:4326" for WGS84", using the underlying geotools hsql authority factory. see also https://docs.geotools.org/latest/userguide/library/referencing/crs.html on some context on why we include the hsql dependency in the planit build to ensure that the provided crs codes here can actually be transformed into a viable CRS and why it makes sense to provide this simple wrapper method in this utility class

        always make sure you lookup the CRS via this method as it ensures the logging of PLANit is not messed up by the geotools-HSQL dependency since we programmatically disallow it to overwrite our logging configuration in the static initialiser of this class.

        Parameters:
        code - for the CRS
        Returns:
        the created coordinate reference system