Class JtsGeometry

  • All Implemented Interfaces:
    Shape

    public class JtsGeometry
    extends BaseShape<JtsSpatialContext>
    Wraps a JTS Geometry (i.e. may be a polygon or basically anything). JTS does a great deal of the hard work, but there is work here in handling dateline (aka anti-meridian) wrap.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Rectangle bbox  
      private org.locationtech.jts.geom.Geometry geom  
      private boolean hasArea  
      protected org.locationtech.jts.geom.prep.PreparedGeometry preparedGeometry  
      static java.lang.String SYSPROP_ASSERT_VALIDATE
      System property boolean that can disable auto validation in an assert.
      protected boolean validated  
      • Fields inherited from class org.locationtech.spatial4j.shape.BaseShape

        ctx
    • Constructor Summary

      Constructors 
      Constructor Description
      JtsGeometry​(org.locationtech.jts.geom.Geometry geom, JtsSpatialContext ctx, boolean dateline180Check, boolean allowMultiOverlap)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private boolean assertValidate()
      called via assertion
      protected Rectangle computeGeoBBox​(org.locationtech.jts.geom.Geometry geoms)
      Given geoms which has already been checked for being in world bounds, return the minimal longitude range of the bounding box.
      private static org.locationtech.jts.geom.Geometry cutUnwrappedGeomInto360​(org.locationtech.jts.geom.Geometry geom)
      This "pages" through standard geo boundaries offset by multiples of 360 longitudinally that intersect geom, and the intersecting results of a page and the geom are shifted into the standard -180 to +180 and added to a new geometry that is returned.
      boolean equals​(java.lang.Object o)
      The sub-classes of Shape generally implement the same contract for Object.equals(Object) and Object.hashCode() amongst the same sub-interface type.
      double getArea​(SpatialContext ctx)
      Calculates the area of the shape, in square-degrees.
      Rectangle getBoundingBox()
      Get the bounding box for this Shape.
      JtsGeometry getBuffered​(double distance, SpatialContext ctx)
      Returns a buffered version of this shape.
      JtsPoint getCenter()
      Returns the center point of this shape.
      org.locationtech.jts.geom.Geometry getGeom()  
      boolean hasArea()
      Does the shape have area? This will be false for points and lines.
      int hashCode()  
      void index()
      Adds an index to this class internally to compute spatial relations faster.
      static SpatialRelation intersectionMatrixToSpatialRelation​(org.locationtech.jts.geom.IntersectionMatrix matrix)  
      boolean isEmpty()
      Shapes can be "empty", which is to say it exists nowhere.
      (package private) boolean isIndexed()
      Determines if the shape has been indexed.
      private org.locationtech.jts.geom.Geometry narrowCollectionIfPossible​(org.locationtech.jts.geom.GeometryCollection gc)
      Attempts to retype a geometry collection under the following circumstances, returning null if the collection can not be retyped.
      protected SpatialRelation relate​(org.locationtech.jts.geom.Geometry oGeom)  
      SpatialRelation relate​(Circle circle)  
      SpatialRelation relate​(JtsGeometry jtsGeometry)  
      SpatialRelation relate​(Point pt)  
      SpatialRelation relate​(Rectangle rectangle)  
      SpatialRelation relate​(Shape other)
      Describe the relationship between the two objects.
      private static void shiftGeomByX​(org.locationtech.jts.geom.Geometry geom, int xShift)  
      java.lang.String toString()  
      private static org.locationtech.jts.geom.Geometry unionGeometryCollection​(org.locationtech.jts.geom.Geometry geom)  
      private static org.locationtech.jts.geom.Geometry unwrapDateline​(org.locationtech.jts.geom.Geometry geom)
      If geom spans the dateline (aka anti-meridian), then this modifies it to be a valid JTS geometry that extends to the right of the standard -180 to +180 width such that some points are greater than +180 but some remain less.
      private static int unwrapDateline​(org.locationtech.jts.geom.LineString lineString)
      private static int unwrapDateline​(org.locationtech.jts.geom.Polygon poly)
      void validate()
      Validates the shape, throwing a descriptive error if it isn't valid.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • SYSPROP_ASSERT_VALIDATE

        public static final java.lang.String SYSPROP_ASSERT_VALIDATE
        System property boolean that can disable auto validation in an assert.
        See Also:
        Constant Field Values
      • geom

        private final org.locationtech.jts.geom.Geometry geom
      • hasArea

        private final boolean hasArea
      • preparedGeometry

        protected org.locationtech.jts.geom.prep.PreparedGeometry preparedGeometry
      • validated

        protected boolean validated
    • Constructor Detail

      • JtsGeometry

        public JtsGeometry​(org.locationtech.jts.geom.Geometry geom,
                           JtsSpatialContext ctx,
                           boolean dateline180Check,
                           boolean allowMultiOverlap)
    • Method Detail

      • narrowCollectionIfPossible

        private org.locationtech.jts.geom.Geometry narrowCollectionIfPossible​(org.locationtech.jts.geom.GeometryCollection gc)
        Attempts to retype a geometry collection under the following circumstances, returning null if the collection can not be retyped.
        • Single object collections are collapsed down to the object.
        • Homogenous collections are recast as the appropriate subclass.
        See Also:
        GeometryFactory.buildGeometry(Collection)
      • assertValidate

        private boolean assertValidate()
        called via assertion
      • validate

        public void validate()
                      throws InvalidShapeException
        Validates the shape, throwing a descriptive error if it isn't valid. Note that this is usually called automatically by default, but that can be disabled.
        Throws:
        InvalidShapeException - with descriptive error if the shape isn't valid
      • isIndexed

        boolean isIndexed()
        Determines if the shape has been indexed.
      • index

        public void index()
        Adds an index to this class internally to compute spatial relations faster. In JTS this is called a PreparedGeometry. This isn't done by default because it takes some time to do the optimization, and it uses more memory. Calling this method isn't thread-safe so be careful when this is done. If it was already indexed then nothing happens.
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: Shape
        Shapes can be "empty", which is to say it exists nowhere. The underlying coordinates are typically NaN.
      • computeGeoBBox

        protected Rectangle computeGeoBBox​(org.locationtech.jts.geom.Geometry geoms)
        Given geoms which has already been checked for being in world bounds, return the minimal longitude range of the bounding box.
      • getBuffered

        public JtsGeometry getBuffered​(double distance,
                                       SpatialContext ctx)
        Description copied from interface: Shape
        Returns a buffered version of this shape. The buffer is usually a rounded-corner buffer, although some shapes might buffer differently. This is an optional operation.
        Returns:
        Not null, and the returned shape should contain the current shape.
      • hasArea

        public boolean hasArea()
        Description copied from interface: Shape
        Does the shape have area? This will be false for points and lines. It will also be false for shapes that normally have area but are constructed in a degenerate case as to not have area (e.g. a circle with 0 radius or rectangle with no height or no width).
      • getArea

        public double getArea​(SpatialContext ctx)
        Description copied from interface: Shape
        Calculates the area of the shape, in square-degrees. If ctx is null then simple Euclidean calculations will be used. This figure can be an estimate.
      • getBoundingBox

        public Rectangle getBoundingBox()
        Description copied from interface: Shape
        Get the bounding box for this Shape. This means the shape is within the bounding box and that it touches each side of the rectangle.

        Postcondition: this.getBoundingBox().relate(this) == CONTAINS

      • getCenter

        public JtsPoint getCenter()
        Description copied from interface: Shape
        Returns the center point of this shape. This is usually the same as getBoundingBox().getCenter() but it doesn't have to be.

        Postcondition: this.relate(this.getCenter()) == CONTAINS

      • relate

        public SpatialRelation relate​(Shape other)
        Description copied from interface: Shape
        Describe the relationship between the two objects. For example
        • this is WITHIN other
        • this CONTAINS other
        • this is DISJOINT other
        • this INTERSECTS other
        Note that a Shape implementation may choose to return INTERSECTS when the true answer is WITHIN or CONTAINS for performance reasons. If a shape does this then it must document when it does. Ideally the shape will not do this approximation in all circumstances, just sometimes.

        If the shapes are equal then the result is CONTAINS (preferred) or WITHIN.

      • relate

        protected SpatialRelation relate​(org.locationtech.jts.geom.Geometry oGeom)
      • intersectionMatrixToSpatialRelation

        public static SpatialRelation intersectionMatrixToSpatialRelation​(org.locationtech.jts.geom.IntersectionMatrix matrix)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Description copied from interface: Shape
        The sub-classes of Shape generally implement the same contract for Object.equals(Object) and Object.hashCode() amongst the same sub-interface type. This means, for example, that multiple Point implementations of different classes are equal if they share the same x & y.
        Specified by:
        equals in interface Shape
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • getGeom

        public org.locationtech.jts.geom.Geometry getGeom()
      • unwrapDateline

        private static org.locationtech.jts.geom.Geometry unwrapDateline​(org.locationtech.jts.geom.Geometry geom)
        If geom spans the dateline (aka anti-meridian), then this modifies it to be a valid JTS geometry that extends to the right of the standard -180 to +180 width such that some points are greater than +180 but some remain less.
        Returns:
        The same geometry or a new one if it was unwrapped
      • unwrapDateline

        private static int unwrapDateline​(org.locationtech.jts.geom.Polygon poly)
      • unwrapDateline

        private static int unwrapDateline​(org.locationtech.jts.geom.LineString lineString)
      • shiftGeomByX

        private static void shiftGeomByX​(org.locationtech.jts.geom.Geometry geom,
                                         int xShift)
      • unionGeometryCollection

        private static org.locationtech.jts.geom.Geometry unionGeometryCollection​(org.locationtech.jts.geom.Geometry geom)
      • cutUnwrappedGeomInto360

        private static org.locationtech.jts.geom.Geometry cutUnwrappedGeomInto360​(org.locationtech.jts.geom.Geometry geom)
        This "pages" through standard geo boundaries offset by multiples of 360 longitudinally that intersect geom, and the intersecting results of a page and the geom are shifted into the standard -180 to +180 and added to a new geometry that is returned.