public class ShapeCollection<S extends Shape> extends AbstractList<S> implements Shape
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; see relateContainsShortCircuits()
.
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.
Modifier and Type | Field and Description |
---|---|
protected Rectangle |
bbox |
protected SpatialContext |
ctx |
protected List<S> |
shapes |
modCount
Constructor and Description |
---|
ShapeCollection(List<S> shapes,
SpatialContext ctx)
WARNING:
shapes is copied by reference. |
Modifier and Type | Method and Description |
---|---|
protected Rectangle |
computeBoundingBox(Collection<? extends Shape> shapes,
SpatialContext ctx) |
protected static boolean |
computeMutualDisjoint(List<? extends Shape> shapes)
Computes whether the shapes are mutually disjoint.
|
boolean |
equals(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. |
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 Shape
|
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() |
String |
toString() |
add, add, addAll, clear, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
parallelStream, removeIf, stream
protected final SpatialContext ctx
protected final Rectangle bbox
public ShapeCollection(List<S> shapes, SpatialContext ctx)
shapes
is copied by reference.shapes
- Copied by reference! (make a defensive copy if caller modifies)protected Rectangle computeBoundingBox(Collection<? extends Shape> shapes, SpatialContext ctx)
public S get(int index)
public int size()
public Rectangle getBoundingBox()
Shape
Postcondition: this.getBoundingBox().relate(this) == CONTAINS
getBoundingBox
in interface Shape
public Point getCenter()
Shape
getBoundingBox().getCenter()
but it doesn't have to be.
Postcondition: this.relate(this.getCenter()) == CONTAINS
public boolean hasArea()
Shape
public ShapeCollection getBuffered(double distance, SpatialContext ctx)
Shape
getBuffered
in interface Shape
public SpatialRelation relate(Shape other)
Shape
If the shapes are equal then the result is CONTAINS (preferred) or WITHIN.
protected boolean relateContainsShortCircuits()
.
protected static boolean computeMutualDisjoint(List<? extends Shape> shapes)
relateContainsShortCircuits()
.
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.public double getArea(SpatialContext ctx)
Shape
public String toString()
toString
in class AbstractCollection<S extends Shape>
public boolean equals(Object o)
Shape
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.public int hashCode()
public SpatialContext getContext()
Shape
getContext
in interface Shape
Copyright © 2020 LocationTech. All rights reserved.