Class ShapeCollection<S extends Shape>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<S>
-
- org.locationtech.spatial4j.shape.ShapeCollection<S>
-
- All Implemented Interfaces:
java.lang.Iterable<S>
,java.util.Collection<S>
,java.util.List<S>
,Shape
public class ShapeCollection<S extends Shape> extends java.util.AbstractList<S> implements Shape
A collection of Shape objects, analogous to an OGC GeometryCollection. The implementation demands a List (with random access) so that the order can be retained if an application requires it, although logically it's treated as an unordered Set, mostly.Ideally,
relate(Shape)
should return the same result no matter what the shape order is, although the default implementation can be order dependent when the shapes overlap; seerelateContainsShortCircuits()
. To improve performance slightly, the caller could order the shapes by largest first so that relate() will have a greater chance of short-circuit'ing sooner. As the Shape contract states; it may return intersects when the best answer is actually contains or within. If any shape intersects the provided shape then that is the answer.This implementation is not optimized for a large number of shapes; relate is O(N). A more sophisticated implementation might do an R-Tree based on bbox'es, for example.
-
-
Constructor Summary
Constructors Constructor Description ShapeCollection(java.util.List<S> shapes, SpatialContext ctx)
WARNING:shapes
is copied by reference.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Rectangle
computeBoundingBox(java.util.Collection<? extends Shape> shapes, SpatialContext ctx)
protected static boolean
computeMutualDisjoint(java.util.List<? extends Shape> shapes)
Computes whether the shapes are mutually disjoint.boolean
equals(java.lang.Object o)
The sub-classes of Shape generally implement the same contract forObject.equals(Object)
andObject.hashCode()
amongst the same sub-interface type.S
get(int index)
double
getArea(SpatialContext ctx)
Calculates the area of the shape, in square-degrees.Rectangle
getBoundingBox()
Get the bounding box for this Shape.ShapeCollection
getBuffered(double distance, SpatialContext ctx)
Returns a buffered version of this shape.Point
getCenter()
Returns the center point of this shape.SpatialContext
getContext()
Get the SpatialContext that created the Shapejava.util.List<S>
getShapes()
boolean
hasArea()
Does the shape have area? This will be false for points and lines.int
hashCode()
SpatialRelation
relate(Shape other)
Describe the relationship between the two objects.protected boolean
relateContainsShortCircuits()
Called by relate() to determine whether to return early if it finds CONTAINS, instead of checking the remaining shapes.int
size()
java.lang.String
toString()
-
Methods inherited from class java.util.AbstractList
add, add, addAll, clear, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
-
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
-
-
-
Field Detail
-
ctx
protected final SpatialContext ctx
-
bbox
protected final Rectangle bbox
-
-
Constructor Detail
-
ShapeCollection
public ShapeCollection(java.util.List<S> shapes, SpatialContext ctx)
WARNING:shapes
is copied by reference.- Parameters:
shapes
- Copied by reference! (make a defensive copy if caller modifies)
-
-
Method Detail
-
computeBoundingBox
protected Rectangle computeBoundingBox(java.util.Collection<? extends Shape> shapes, SpatialContext ctx)
-
getShapes
public java.util.List<S> getShapes()
-
get
public S get(int index)
-
size
public int size()
-
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
- Specified by:
getBoundingBox
in interfaceShape
-
getCenter
public Point getCenter()
Description copied from interface:Shape
Returns the center point of this shape. This is usually the same asgetBoundingBox().getCenter()
but it doesn't have to be.Postcondition:
this.relate(this.getCenter()) == CONTAINS
-
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).
-
getBuffered
public ShapeCollection 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.- Specified by:
getBuffered
in interfaceShape
- Returns:
- Not null, and the returned shape should contain the current shape.
-
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
If the shapes are equal then the result is CONTAINS (preferred) or WITHIN.
-
relateContainsShortCircuits
protected boolean relateContainsShortCircuits()
Called by relate() to determine whether to return early if it finds CONTAINS, instead of checking the remaining shapes. It will do so without calling this method if the "other" shape is a Point. If a remaining shape finds INTERSECTS, then INTERSECTS will be returned. The only problem with this returning true is that if some of the shapes overlap, it's possible that the result of relate() could be dependent on the order of the shapes, which could be unexpected / wrong depending on the application. The default implementation returns true because it probably doesn't matter. If it does, a subclass could add a boolean flag that this method could return. That flag could be initialized to true only if the shapes are mutually disjoint.- See Also:
.
-
computeMutualDisjoint
protected static boolean computeMutualDisjoint(java.util.List<? extends Shape> shapes)
Computes whether the shapes are mutually disjoint. This is a utility method offered for use by a subclass implementingrelateContainsShortCircuits()
. Beware: this is an O(N^2) algorithm.. Consequently, consider safely assuming non-disjoint if shapes.size() > 10 or something. And if all shapes are a Point then the result of this method doesn't ultimately matter.
-
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.
-
toString
public java.lang.String toString()
-
equals
public boolean equals(java.lang.Object o)
Description copied from interface:Shape
The sub-classes of Shape generally implement the same contract forObject.equals(Object)
andObject.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.
-
hashCode
public int hashCode()
-
getContext
public SpatialContext getContext()
Description copied from interface:Shape
Get the SpatialContext that created the Shape- Specified by:
getContext
in interfaceShape
-
-