java.lang.Object
org.apache.sis.util.Static
org.apache.sis.internal.feature.jts.JTS

public final class JTS extends Static
Utilities for Java Topology Suite (JTS) objects. We use this class for functionalities not supported by Apache SIS with other libraries. For library-agnostic functionalities, see Geometries instead.

This method may be modified or removed in any future version. For example, we may replace it by a more general mechanism working also on other geometry libraries.

Since:
1.0
Version:
1.2
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Key used in user data map for storing an instance of CoordinateReferenceSystem.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    JTS()
    Do not allow instantiation of this class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Shape
    asShape(org.locationtech.jts.geom.Geometry geometry)
    Returns a view of the given JTS geometry as a Java2D shape.
    (package private) static void
    copyMetadata(org.locationtech.jts.geom.Geometry source, org.locationtech.jts.geom.Geometry target)
    Copies coordinate reference system information from the given source geometry to the target geometry.
    private static org.opengis.referencing.operation.CoordinateOperation
    findOperation(org.opengis.referencing.crs.CoordinateReferenceSystem sourceCRS, org.opengis.referencing.crs.CoordinateReferenceSystem targetCRS, org.locationtech.jts.geom.Geometry areaOfInterest)
    Finds an operation between the given CRS valid in the given area of interest.
    static org.locationtech.jts.geom.Geometry
    fromAWT(org.locationtech.jts.geom.GeometryFactory factory, Shape shape, double flatness)
    Converts a Java2D shape to a JTS geometry.
    static org.opengis.referencing.crs.CoordinateReferenceSystem
    getCoordinateReferenceSystem(org.locationtech.jts.geom.Geometry source)
    Gets the Coordinate Reference System (CRS) from the given geometry.
    (package private) static boolean
    isSameCRS(org.locationtech.jts.geom.Geometry first, org.locationtech.jts.geom.Geometry second)
    Returns true if the two geometries use the same CRS, based on very cheap comparison.
    (package private) static void
    setCoordinateReferenceSystem(org.locationtech.jts.geom.Geometry target, org.opengis.referencing.crs.CoordinateReferenceSystem crs)
    Sets the Coordinate Reference System (CRS) in the specified geometry.
    static org.locationtech.jts.geom.Geometry
    transform(org.locationtech.jts.geom.Geometry geometry, org.opengis.referencing.crs.CoordinateReferenceSystem targetCRS)
    Transforms the given geometry to the specified Coordinate Reference System (CRS).
    static org.locationtech.jts.geom.Geometry
    transform(org.locationtech.jts.geom.Geometry geometry, org.opengis.referencing.operation.CoordinateOperation operation, boolean validate)
    Transforms the given geometry using the given coordinate operation.
    static org.locationtech.jts.geom.Geometry
    transform(org.locationtech.jts.geom.Geometry geometry, org.opengis.referencing.operation.MathTransform transform)
    Transforms the given geometry using the given math transform.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • JTS

      private JTS()
      Do not allow instantiation of this class.
  • Method Details

    • isSameCRS

      static boolean isSameCRS(org.locationtech.jts.geom.Geometry first, org.locationtech.jts.geom.Geometry second)
      Returns true if the two geometries use the same CRS, based on very cheap comparison. A value of false does not necessarily means that the CRS are different, but it means that a more expansive comparison is required. If CRS are specified by SRID codes, then this method assumes that the two SRID codes are defined by the same authority (e.g. EPSG).

      If both CRS are undefined (null), then they are considered the same.

      Parameters:
      first - the first geometry.
      second - the second geometry.
      Returns:
      true if the two geometries use equivalent CRS, or false in case of doubt.
    • getCoordinateReferenceSystem

      public static org.opengis.referencing.crs.CoordinateReferenceSystem getCoordinateReferenceSystem(org.locationtech.jts.geom.Geometry source) throws org.opengis.util.FactoryException
      Gets the Coordinate Reference System (CRS) from the given geometry. This method expects the CRS to be stored in one the following ways:
      • Geometry user data is an instance of CoordinateReferenceSystem.
      • user data is a (@link Map} with a value for the "CRS" key.
      • Geometry SRID is strictly positive, in which case it is interpreted as an EPSG code.
      If none of the above is valid, null is returned.
      Parameters:
      source - the geometry from which to get the CRS, or null.
      Returns:
      the coordinate reference system, or null if none.
      Throws:
      org.opengis.util.FactoryException - if the CRS cannot be created from the SRID code.
    • setCoordinateReferenceSystem

      static void setCoordinateReferenceSystem(org.locationtech.jts.geom.Geometry target, org.opengis.referencing.crs.CoordinateReferenceSystem crs)
      Sets the Coordinate Reference System (CRS) in the specified geometry. This method overwrite any previous user data; it should be invoked only when the geometry is known to not store any other information. In current Apache SIS usage, this method is invoked only for newly created geometries.

      This method also sets the JTS SRID to EPSG code if such code can be found. For performance reasons this method does not perform a full scan of EPSG database if the CRS does not provide an EPSG code.

      Parameters:
      target - the geometry where to store coordinate reference system information.
      crs - the CRS to store, or null.
    • copyMetadata

      static void copyMetadata(org.locationtech.jts.geom.Geometry source, org.locationtech.jts.geom.Geometry target)
      Copies coordinate reference system information from the given source geometry to the target geometry. Current implementation copies only CRS information, but future implementations could copy some other values if they may apply to the target geometry as well.
    • findOperation

      private static org.opengis.referencing.operation.CoordinateOperation findOperation(org.opengis.referencing.crs.CoordinateReferenceSystem sourceCRS, org.opengis.referencing.crs.CoordinateReferenceSystem targetCRS, org.locationtech.jts.geom.Geometry areaOfInterest) throws org.opengis.util.FactoryException
      Finds an operation between the given CRS valid in the given area of interest. This method does not verify the CRS of the given geometry.
      Parameters:
      sourceCRS - the CRS of source coordinates.
      targetCRS - the CRS of target coordinates.
      areaOfInterest - the area of interest.
      Returns:
      the mathematical operation from sourceCRS to targetCRS.
      Throws:
      org.opengis.util.FactoryException - if the operation cannot be created.
    • transform

      public static org.locationtech.jts.geom.Geometry transform(org.locationtech.jts.geom.Geometry geometry, org.opengis.referencing.crs.CoordinateReferenceSystem targetCRS) throws org.opengis.util.FactoryException, org.opengis.referencing.operation.TransformException
      Transforms the given geometry to the specified Coordinate Reference System (CRS). If the given CRS or the given geometry is null or is the same than current CRS, then the geometry is returned unchanged. If the geometry has no Coordinate Reference System, then the geometry is returned unchanged.

      This operation may be slow! If many geometries need to be transformed, it is better to fetch the CoordinateOperation only once, then invoke #transform(Geometry, CoordinateOperation) for each geometry. Alternatively, the geometries can be stored in a single geometry collection in order to invoke this method only once.

      Parameters:
      geometry - the geometry to transform, or null.
      targetCRS - the target coordinate reference system, or null.
      Returns:
      the transformed geometry, or the same geometry if it is already in target CRS.
      Throws:
      org.opengis.util.FactoryException - if transformation to the target CRS cannot be constructed.
      org.opengis.referencing.operation.TransformException - if the given geometry cannot be transformed.
    • transform

      public static org.locationtech.jts.geom.Geometry transform(org.locationtech.jts.geom.Geometry geometry, org.opengis.referencing.operation.CoordinateOperation operation, boolean validate) throws org.opengis.util.FactoryException, org.opengis.referencing.operation.TransformException
      Transforms the given geometry using the given coordinate operation. If the geometry or the operation is null, then the geometry is returned unchanged. If the source CRS is not equals to the geometry CRS and validate is true, then a new operation is inferred.
      Parameters:
      geometry - the geometry to transform, or null.
      operation - the coordinate operation to apply, or null.
      validate - whether to validate the operation source CRS.
      Returns:
      the transformed geometry, or the same geometry if it is already in target CRS.
      Throws:
      org.opengis.util.FactoryException - if transformation to the target CRS cannot be found.
      org.opengis.referencing.operation.TransformException - if the given geometry cannot be transformed.
    • transform

      public static org.locationtech.jts.geom.Geometry transform(org.locationtech.jts.geom.Geometry geometry, org.opengis.referencing.operation.MathTransform transform) throws org.opengis.referencing.operation.TransformException
      Transforms the given geometry using the given math transform. If the geometry or the transform is null or identity, then the geometry is returned unchanged.
      Parameters:
      geometry - the geometry to transform, or null.
      transform - the transform to apply, or null.
      Returns:
      the transformed geometry, or the same geometry if it is already in target CRS.
      Throws:
      org.opengis.referencing.operation.TransformException - if the given geometry cannot be transformed.
    • asShape

      public static Shape asShape(org.locationtech.jts.geom.Geometry geometry)
      Returns a view of the given JTS geometry as a Java2D shape.
      Parameters:
      geometry - the geometry to view as a shape, not null.
      Returns:
      the Java2D shape view.
    • fromAWT

      public static org.locationtech.jts.geom.Geometry fromAWT(org.locationtech.jts.geom.GeometryFactory factory, Shape shape, double flatness)
      Converts a Java2D shape to a JTS geometry. If the given shape is a view created by asShape(Geometry), then the original geometry is returned. Otherwise a new geometry is created with a copy (not a view) of the shape coordinates.
      Parameters:
      factory - factory to use for creating the geometry, or null for the default.
      shape - the Java2D shape to convert. Cannot be null.
      flatness - the maximum distance that line segments are allowed to deviate from curves.
      Returns:
      JTS geometry with shape coordinates. Never null but can be empty.