public class Region extends Object implements Named
Region is a polygonal area on the surface of the earth. The
vertices comprising the border of each Region are stored internally
as latitude-longitude coordinate pairs in an Area, facilitating
operations such as union, intersect, and contains. Insidedness rules follow
those defined in the Shape interface.
New Regions are created exclusively through static factory
methods, some of which require the specification of a BorderType. If
one wishes to create a geographic Region that represents a rectangle
in a Mercator projection, BorderType.MERCATOR_LINEAR should be used,
otherwise, the border will follow a BorderType.GREAT_CIRCLE between
two points. Over small distances, great circle paths are approximately the
same as linear, Mercator paths. Over longer distances, a great circle is a
better representation of a line on a globe. Internally, great circles are
approximated by multiple straight line segments that have a maximum length of
100km.
A Region may also have interior (or negative) areas. Any call to
contains(Location) for a Location within or on the
border of such an interior area will return false, subject to the
rules of insidedness.
Note: The current implementation does not support regions that are intended to span ±180°. Any such regions will wrap the long way around the earth and results are undefined. Regions that encircle either pole are not supported either.
Note: Due to rounding errors and the use of an Area
internally to define a Region's border,
contains(Location) may not always return the expected result
near a border. See contains(Location) for further details.
Use the Regions factory class to create new regions.
Shape,
Area,
BorderType,
Regions| Modifier and Type | Method and Description |
|---|---|
void |
addInterior(Region region)
Adds an interior (donut-hole) to this
Region. |
Area |
area()
Returns a deep copy of the internal
Area used to manage this
Region. |
LocationList |
border()
Returns a reference to the internal, immutable
LocationList of
points that decribe the border of this Region. |
Bounds |
bounds()
Lazily create the bounds of this region.
|
boolean |
contains(Location loc)
Returns whether the given
Location is inside this Region. |
boolean |
contains(Region region)
Tests whether another
Region is entirely contained within this
Region. |
double |
distanceToLocation(Location loc)
Returns the minimum horizonatal distance (in km) between the border of this
Region and the Location specified. |
boolean |
equals(Object obj) |
boolean |
equalsRegion(Region r)
Compares the geometry of this
Region to another and returns
true if they are the same, ignoring any differences in name. |
double |
extent()
Returns a flat-earth estimate of the area of this region in km2.
|
int |
hashCode() |
List<LocationList> |
interiors()
Returns an unmodifiable
List view of the internal
LocationLists (also unmodifiable) of points that decribe the
interiors of this Region, if such exist. |
boolean |
isRectangular()
Returns whether this
Region is rectangular in shape when
represented in a Mercator projection. |
String |
name()
Returns an object's display name.
|
String |
toString() |
public boolean contains(Location loc)
Location is inside this Region.
The determination follows the rules of insidedness defined in the
Shape interface.
Note: By using an Area internally to manage this
Region's geometry, there are instances where rounding errors may
cause this method to yield unexpected results. For instance, although a
Region's southernmost point might be initially defined as 40.0°,
the internal Area may return 40.0000000000001 on a call to
getMinLat() and calls to contains(new Location(40,*)) will
return false.
loc - the Location to testtrue if the Location is inside the Region,
false otherwiseShapepublic boolean contains(Region region)
Region is entirely contained within this
Region.region - to checktrue if this contains the Region; false
otherwisepublic boolean isRectangular()
Region is rectangular in shape when
represented in a Mercator projection.true if rectangular, false otherwisepublic void addInterior(Region region)
Region. Any call to
contains(Location) for a Location within this
interior area will return false. Any interior Region must
lie entirely inside this Region. Moreover, any interior may not
overlap or enclose any existing interior region. Internally, the border of
the supplied Region is copied and stored as an unmodifiable
List. No reference to the supplied Region is retained.region - to use as an interior or negative spaceNullPointerException - if the supplied Region is nullIllegalArgumentException - if the supplied Region is not
entirly contained within this RegionIllegalArgumentException - if the supplied Region is not
singular (i.e. already has an interior itself)IllegalArgumentException - if the supplied Region overlaps
any existing interior Regioninteriors()public List<LocationList> interiors()
List view of the internal
LocationLists (also unmodifiable) of points that decribe the
interiors of this Region, if such exist. If no interior is defined,
the method returns null.List the interior LocationLists or null
if no interiors are definedpublic LocationList border()
LocationList of
points that decribe the border of this Region.LocationListpublic Area area()
Area used to manage this
Region.Area used by this Regionpublic double extent()
Region's bounding polygon as the
origin of an orthogonal coordinate system. This method is not appropriate
for use with very large Regions where the curvature of the earth is
more significant.public boolean equalsRegion(Region r)
Region to another and returns
true if they are the same, ignoring any differences in name. Use
Region.equals(Object) to include name comparison.r - the Regions to comparetrue if this Region has the same geometry as the
supplied Region, false otherwiseequals(Object)public Bounds bounds()
public double distanceToLocation(Location loc)
Region and the Location specified. If the given
Location is inside the Region, the method returns 0. The
distance algorithm used only works well at short distances (e.g. ≤250 km).loc - the Location to compute a distance toRegion and a pointNullPointerException - if supplied location is nullLocations.minDistanceToLine(Location, LocationList),
Locations.distanceToSegmentFast(Location, Location, Location)public String name()
Named