Class GISUtils

  • All Implemented Interfaces:
    ObjectFactory

    public final class GISUtils
    extends Object
    implements ObjectFactory
    A class containing static methods which are useful for GIS operations. Also implements ObjectFactory so that it can be used to provide the JdbcDataSource for the EPSG database to JNDI.
    Author:
    Guy
    • Constructor Detail

      • GISUtils

        public GISUtils()
    • Method Detail

      • defaultGeographicCRS

        public static org.opengis.referencing.crs.CoordinateReferenceSystem defaultGeographicCRS()
        Returns the default Lon-Lat geographic CRS. This method guarantees that the returned CRS complies to the following conditions:
        • is geographic
        • has axes in longitude, latitude order
        • has longitude in degrees increasing toward East
        • has latitude in degrees increasing toward North
        • use the Greenwich prime meridian.
        This method makes no guarantees about the geodetic datum. The current implementation delegates to CommonCRS.defaultGeographic(), which itself delegates to a geographic CRS based on the WGS84 datum. However the default may change in future versions, for example using a spherical CRS instead of WGS84.
        Returns:
        the default geographic CRS with Lon-Lat axes in degrees.
      • isDefaultGeographicCRS

        public static boolean isDefaultGeographicCRS​(org.opengis.referencing.crs.CoordinateReferenceSystem coordinateReferenceSystem)
        Tests if a coordinate reference system is equivalent to the default geographic CRS.
        Parameters:
        coordinateReferenceSystem - The CoordinateReferenceSystem to test
        Returns:
        true if the supplied CoordinateReferenceSystem is equivalent to defaultGeographicCRS()
      • isWgs84LonLat

        public static boolean isWgs84LonLat​(org.opengis.referencing.crs.CoordinateReferenceSystem coordinateReferenceSystem)
        Tests if a coordinate reference system is equivalent to WGS84 Lon-Lat
        Parameters:
        coordinateReferenceSystem - The CoordinateReferenceSystem to test
        Returns:
        true if the supplied CoordinateReferenceSystem is equivalent to WGS84
      • getNextEquivalentLongitude

        public static double getNextEquivalentLongitude​(double reference,
                                                        double target)
        Finds the next longitude which is greater than the reference longitude and equivalent to the target longitude
        Parameters:
        reference - The reference longitude
        target - The target longitude
        Returns:
        A longitude which is equivalent to the target and greater than the reference
      • constrainLongitude180

        public static double constrainLongitude180​(double value)
        Returns a longitude value in degrees that is equal to the given value but in the range (-180:180]
      • constrainLongitude360

        public static double constrainLongitude360​(double value)
        Returns a longitude value in degrees that is equal to the given value but in the range [0:360)
      • getNearestEquivalentLongitude

        public static double getNearestEquivalentLongitude​(double target,
                                                           double longitude)
        Given a target longitude and a longitude, this returns the longitude value which is nearest to the target, taking wrapping into account
        Parameters:
        target - The longitude which we are aiming to be nearest to
        longitude - The longitude which we want to be nearest to the target
        Returns:
        A longitude value which is equivalent to longitude
      • constrainBoundingBox

        public static BoundingBox constrainBoundingBox​(BoundingBox bbox)
        Constrains a lat-lon bounding box to have all longitude values in the range (-180:180]
        Parameters:
        bbox - The BoundingBox to constrain
        Returns:
        The constrained BoundingBox. If the BoundingBox crosses the date line, a BoundingBox which spans the entire range (-180:180] will be returned
      • transformPosition

        public static HorizontalPosition transformPosition​(HorizontalPosition pos,
                                                           org.opengis.referencing.crs.CoordinateReferenceSystem targetCrs)
        Transforms the given HorizontalPosition to a new position in the given coordinate reference system.
        Parameters:
        pos - The position to translate.
        targetCrs - The CRS to translate into
        Returns:
        a new position in the given CRS, or the same position if the new CRS is the same as the point's CRS. The returned point's CRS will be set to targetCrs. If the CRS of the position is null, the CRS will simply be set to the targetCrs.
        Throws:
        NullPointerException - if targetCrs is null.
      • transformWgs84Heading

        public static Double transformWgs84Heading​(Number heading,
                                                   HorizontalPosition position)
        Transforms the given lat-lon heading to a different CoordinateReferenceSystem
        Parameters:
        heading - The heading in degrees
        position - The HorizontalPosition at which to transform the heading. The CoordinateReferenceSystem returned by HorizontalPosition.getCoordinateReferenceSystem() will be the CoordinateReferenceSystem in which the heading is valid.
        Returns:
        The heading, in degrees clockwise from "upwards" (i.e. y-positive in the target CRS)
      • crsMatch

        public static boolean crsMatch​(org.opengis.referencing.crs.CoordinateReferenceSystem sourceCrs,
                                       org.opengis.referencing.crs.CoordinateReferenceSystem targetCrs)
        Tests whether 2 CoordinateReferenceSystems are equivalent
        Parameters:
        sourceCrs - The first CoordinateReferenceSystem to test
        targetCrs - The second CoordinateReferenceSystem to test
      • getCrs

        public static org.opengis.referencing.crs.CoordinateReferenceSystem getCrs​(String crsCode)
                                                                            throws InvalidCrsException
        Finds a CoordinateReferenceSystem with the given code, forcing longitude-first axis order.
        Parameters:
        crsCode - The code for the CRS
        Returns:
        a coordinate reference system with the longitude axis first
        Throws:
        InvalidCrsException - if a CRS matching the code cannot be found
        NullPointerException - if crsCode is null
      • parseBbox

        public static BoundingBox parseBbox​(String bboxStr,
                                            boolean xFirst,
                                            String crs)
                                     throws EdalException
        Converts a string of the form "a1,b1,a2,b2" into a BoundingBox
        Parameters:
        bboxStr - A string of the form "a1,b1,a2,b2". If xFirst is true, then a1, a2 represent the x-coordinates and b1,b2 represent the y-coordinates, otherwise it is the other way around
        xFirst - Whether the x-coordinates are first or second in the list
        crs - A string representing the CoordinateReferenceSystem of the BoundingBox
        Throws:
        EdalException - if the format of the bounding box is invalid
      • isTemporalDomainTimeAxis

        public static boolean isTemporalDomainTimeAxis​(TemporalDomain domain)
        Tests whether a TemporalDomain is discrete. Used when generating Capabilities document in WMS
      • isVerticalDomainVerticalAxis

        public static boolean isVerticalDomainVerticalAxis​(VerticalDomain domain)
        Tests whether a VerticalDomain is discrete. Used when generating Capabilities document in WMS
      • getClosestToCurrentTime

        public static org.joda.time.DateTime getClosestToCurrentTime​(TemporalDomain tDomain)
        Returns the closest time to the current time from a list of values
        Parameters:
        tDomain - The list of times to check
        Returns:
        The closest from the list to the current time.
      • getClosestTimeTo

        public static org.joda.time.DateTime getClosestTimeTo​(org.joda.time.DateTime targetTime,
                                                              TemporalDomain tDomain)
        Returns the closest time within a temporal domain to the given time.
        Parameters:
        targetTime - The target time
        tDomain - The TemporalDomain to check
        Returns:
        Either the closest time within that axis, or the closest to the current time if the target is null, or null if the list of times is null
      • getIndexOfClosestTimeTo

        public static int getIndexOfClosestTimeTo​(org.joda.time.DateTime targetTime,
                                                  TimeAxis timeAxis)
      • getClosestElevationTo

        public static Double getClosestElevationTo​(Double targetZ,
                                                   VerticalDomain zDomain)
        Returns the closest elevation within a vertical domain to the given elevation.
        Parameters:
        targetZ - The target elevation
        zDomain - The VerticalDomain to check
        Returns:
        Either the closest elevation within that axis, or the closest to the surface if the target is null, or null if the VerticalDomain is null
      • getIndexOfClosestElevationTo

        public static int getIndexOfClosestElevationTo​(Double target,
                                                       VerticalAxis zAxis)
      • getOptimalTransectPoints

        public static List<HorizontalPosition> getOptimalTransectPoints​(HorizontalGrid hGrid,
                                                                        LineString transect)
        Gets a HorizontalDomain that contains (near) the minimum necessary number of points to sample a layer's source grid of data. That is to say, creating a HorizontalDomain at higher resolution would not result in sampling significantly more points in the layer's source grid.
        Parameters:
        hGrid - The HorizontalGrid to find transect point on
        transect - The transect as specified in the request
        Returns:
        a HorizontalDomain that contains (near) the minimum necessary number of points to sample a layer's source grid of data.
      • toGeographicBoundingBox

        public static org.opengis.metadata.extent.GeographicBoundingBox toGeographicBoundingBox​(BoundingBox bbox)
        Converts a BoundingBox into a GeographicBoundingBox (i.e. one which is in lat/lon WGS84). This method is not guaranteed to be exact. Its aim is to choose bounding boxes which contain all of the data - there is no requirement that the bounding box is a tight fit.
        Parameters:
        bbox - The bounding box
        Returns:
        A GeographicBoundingBox which contains the supplied BoundingBox
      • getLargeBoundingBox

        public static BoundingBox getLargeBoundingBox​(BoundingBox bbox,
                                                      double percentageIncrease)
        Increases the size of a BoundingBox by a given factor
        Parameters:
        bbox - The BoundingBox to increase the size of
        percentageIncrease - The percentage increase
        Returns:
        A larger BoundingBox with the same centre
      • getDistSquared

        public static double getDistSquared​(HorizontalPosition pos1,
                                            HorizontalPosition pos2)
        Performs Pythagoras on two distances to calculate the distance squared. Useful for sorting lists according to distance from a point. The result shouldn't be used for anything critical, since two results may not accurately reflect the true distance (e.g. two points near the pole may incorrectly report a smaller distance apart than two near the equator). No check that the positions have the same CRS is performed (for speed).
        Parameters:
        pos1 - The first position
        pos2 - The second position
        Returns:
        A number related to how far apart the positions are, or Double.MAX_VALUE if either is null
      • limitZAxis

        public static VerticalAxis limitZAxis​(VerticalAxis axis,
                                              Extent<Double> limits)
        Limits a z-axis to include a range as tightly as possible
        Parameters:
        axis - The axis to limit
        limits - The range to limit to
        Returns:
        A new VerticalAxis which will extend by at most one point over each of the bounds provided by limits, or the original axis if limits is null
      • limitTAxis

        public static TimeAxis limitTAxis​(TimeAxis axis,
                                          Extent<org.joda.time.DateTime> limits)
        Limits a t-axis to include a range as tightly as possible
        Parameters:
        axis - The axis to limit
        limits - The range to limit to
        Returns:
        A new TimeAxis which will extend by at most one point over each of the bounds provided by limits, or the original axis if limits is null
      • releaseEpsgDatabase

        public static void releaseEpsgDatabase()
      • isPressureUnits

        public static boolean isPressureUnits​(String units)
      • isLatitudeUnits

        public static boolean isLatitudeUnits​(String units)
      • isLongitudeUnits

        public static boolean isLongitudeUnits​(String units)