Spatial4n.Core
Encodes the given latitude and longitude into a geohash
Latitude to encode
Longitude to encode
Geohash encoding of the longitude and latitude
Decodes the given geohash into a latitude and longitude
@param geohash Geohash to deocde
@return Array with the latitude at index 0, and longitude at index 1
Returns min-max lat, min-max lon.
Array of geohashes 1 level below the baseGeohash. Sorted.
See the table at http://en.wikipedia.org/wiki/Geohash
The International Union of Geodesy and Geophysics says the Earth's mean radius in KM is:
[1] http://en.wikipedia.org/wiki/Earth_radius
Calculate the p-norm (i.e. length) between two vectors
The first vector
The second vector
The power (2 for cartesian distance, 1 for manhattan, etc.)
The length. See http://en.wikipedia.org/wiki/Lp_space
Calculate the p-norm (i.e. length) between two vectors
The first vector
The second vector
The power (2 for cartesian distance, 1 for manhattan, etc.)
If you've precalculated oneOverPower and cached it, use this method to save one division operation over {@link #vectorDistance(double[], double[], double)}.
The length.
Return the coordinates of a vector that is the corner of a box (upper right or lower left), assuming a Rectangular
coordinate system. Note, this does not apply for points on a sphere or ellipse (although it could be used as an approximation).
@param center The center point
@param result Holds the result, potentially resizing if needed.
@param distance The d from the center to the corner
@param upperRight If true, return the coords for the upper right corner, else return the lower left.
@return The point, either the upperLeft or the lower right
Given a start point (startLat, startLon) and a bearing on a sphere of radius sphereRadius, return the destination point.
@param startLat The starting point latitude, in radians
@param startLon The starting point longitude, in radians
@param distanceRAD The distance to travel along the bearing in radians.
@param bearingRAD The bearing, in radians. North is a 0, moving clockwise till radians(360).
@param result A preallocated array to hold the results. If null, a new one is constructed.
@return The destination point, in radians. First entry is latitude, second is longitude
Puts in range -90 <= lat_deg <= 90.
The delta longitude of a point-distance. In other words, half the width of
the bounding box of a circle.
The latitude of the horizontal axis (e.g. left-right line)
of a circle. The horizontal axis of a circle passes through its furthest
left-most and right-most edges. On a 2D plane, this result is always
from.getY()
but, perhaps surprisingly, on a sphere it is going
to be slightly different.
The square of the cartesian Distance. Not really a distance, but useful if all that matters is
comparing the result to another one.
@param vec1 The first point
@param vec2 The second point
@return The squared cartesian distance
@param lat1 The y coordinate of the first point, in radians
@param lon1 The x coordinate of the first point, in radians
@param lat2 The y coordinate of the second point, in radians
@param lon2 The x coordinate of the second point, in radians
@return The distance between the two points, as determined by the Haversine formula, in radians.
Calculates the distance between two lat/lng's using the Law of Cosines. Due to numeric conditioning
errors, it is not as accurate as the Haversine formula for small distances. But with
double precision, it isn't that bad --
allegedly 1 meter.
See
Why is law of cosines more preferable than haversine when calculating distance between two latitude-longitude points?
The arguments and return value are in radians.
Calculates the great circle distance using the Vincenty Formula, simplified for a spherical model. This formula
is accurate for any pair of points. The equation
was taken from Wikipedia.
The arguments are in radians, and the result is in radians.
Converts a distance in the units of the radius to degrees (360 degrees are
in a circle). A spherical earth model is assumed.
Converts a distance in the units of radius
(e.g. kilometers)
to radians (multiples of the radius). A spherical earth model is assumed.
Same as {@link Math#toRadians(double)} but 3x faster (multiply vs. divide).
See CompareRadiansSnippet.java in tests.
Same as {@link Math#toDegrees(double)} but 3x faster (multiply vs. divide).
See CompareRadiansSnippet.java in tests.
The set of spatial relationships. Naming is consistent with OGC spec conventions as seen in SQL/MM and others.
No equality case. If two Shape instances are equal then the result might be CONTAINS or WITHIN, and
some logic might fail under this edge condition when it's not careful to check.
Client code must be written to detect this and act accordingly. In RectangleImpl.Relate(), it checks
for this explicitly, for example. TestShapes2D.assertRelation() checks too.
Not commutative! WITHIN.inverse().inverse() != WITHIN.
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.
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
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).
Calculates the area of the shape in the units of {@link
com.spatial4j.core.distance.DistanceUnits}. If ctx is null then simple
Euclidean calculations will be used. This figure can be an estimate.
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
The distance between from
and to
.
The distance between from
and Point(toX,toY)
.
Calculates where a destination point is given an origin (from
)
distance, and bearing (given in degrees -- 0-360).
Calculates the bounding box of a circle, as specified by its center point
and distance.
The Y
coordinate of the horizontal axis (e.g. left-right line)
of a circle. The horizontal axis of a circle passes through its furthest
left-most and right-most edges. On a 2D plane, this result is always
from.getY()
but, perhaps surprisingly, on a sphere it is going
to be slightly different.
A circle, also known as a point-radius since that is what it is comprised of.
Expert: Resets the state of this shape given the arguments. This is a
performance feature to avoid excessive Shape object allocation as well as
some argument error checking. Mutable shapes is error-prone so use with
care.
The distance from the point's center to its edge, measured in
{@link com.spatial4j.core.distance.DistanceUnits}.
Factory for a SpatialContext.
The factory class is lookuped up via "spatialContextFactory" in args
then falling back to a Java system property (with initial caps). If neither are specified
then {@link SimpleSpatialContextFactory} is chosen.
Subclasses should simply construct the instance from the initialized configuration.
Only meaningful for geospatial contexts.
Expert: Resets the state of this shape given the arguments. This is a
performance feature to avoid excessive Shape object allocation as well as
some argument error checking. Mutable shapes is error-prone so use with
care.
All {@link Rectangle} implementations should use this definition of {@link Object#equals(Object)}.
A circle as it exists on the surface of a sphere.
A circle, also known as a point-radius, based on a
{@link com.spatial4j.core.distance.DistanceCalculator} which does all the work. This implementation
implementation should work for both cartesian 2D and geodetic sphere surfaces.
The Y
coordinate of where the circle axis intersect.
The X
coordinate of where the circle axis intersect.
All {@link Circle} implementations should use this definition of {@link Object#hashCode()}.
Called after bounding box is intersected.
INTERSECTS or CONTAINS from enclosingBox's intersection
DISJOINT, CONTAINS, or INTERSECTS (not WITHIN)
Returns either 0 for none, 1 for some, or 4 for all.
Reads a shape from a given string (ie, X Y, XMin XMax... WKT)
- Point: X Y
1.23 4.56
- BOX: XMin YMin XMax YMax
1.23 4.56 7.87 4.56
-
WKT (Well Known Text)
POLYGON( ... )
Note:Polygons and WKT might not be supported by this
spatial context; you'll have to use {@link com.spatial4j.core.context.jts.JtsSpatialContext}.
@param value A string representation of the shape; not null.
@return A Shape; not null.
@see #writeShape
Writes a shape to a String, in a format that can be read by {@link #readShape(String)}.
Reads geospatial latitude then a comma then longitude.
A basic 2D implementation of a Point.
The X coordinate, or Longitude in geospatial contexts.
The Y coordinate, or Latitude in geospatial contexts.
A simple constructor without normalization / validation.
All {@link Point} implementations should use this definition of {@link Object#equals(Object)}.
All {@link Point} implementations should use this definition of {@link Object#hashCode()}.
Calculates based on Euclidean / Cartesian 2d plane.
A base class for a Distance Calculator that assumes a spherical earth model.
WARNING: geoms is copied by reference.
Utility methods related to parsing shapes.
Given a string containing dimension values encoded in it, separated by commas, return a String array of length dimension
containing the values.
A preallocated array. Must be size dimension. If it is not it will be resized.
The value to parse
The expected number of values for the point
An array of the values that make up the point (aka vector)
Given a string containing dimension values encoded in it, separated by commas, return a double array of length dimension
containing the values.
A preallocated array. Must be size dimension. If it is not it will be resized.
The value to parse
The expected number of values for the point
An array of the values that make up the point (aka vector)
A popular default SpatialContext implementation for geospatial.
Establishes geo vs cartesian / Euclidean.
Optional; defaults to Haversine or cartesian depending on units.
Optional; defaults to GEO_WORLDBOUNDS or MAX_WORLDBOUNDS depending on units.
Is this a geospatial context (true) or simply 2d spatial (false).
Ensure fits in {@link #getWorldBounds()}
Ensure fits in {@link #getWorldBounds()}
Construct a point.
Construct a rectangle. The parameters will be normalized.
Construct a rectangle. If just one longitude is on the dateline (+/- 180)
then potentially adjust its sign to ensure the rectangle does not cross the
dateline.