Package org.apache.sis.geometry
Class GeneralDirectPosition
java.lang.Object
org.apache.sis.io.wkt.FormattableObject
org.apache.sis.geometry.AbstractDirectPosition
org.apache.sis.geometry.GeneralDirectPosition
- All Implemented Interfaces:
Serializable
,Cloneable
,org.opengis.geometry.coordinate.Position
,org.opengis.geometry.DirectPosition
- Direct Known Subclasses:
PositionTransformer
public class GeneralDirectPosition
extends AbstractDirectPosition
implements Serializable, Cloneable
A mutable
DirectPosition
(the coordinates of a position) of arbitrary dimension.
This particular implementation of DirectPosition
is said "General" because it
uses an array of coordinates
of an arbitrary length. If the direct
position is known to be always two-dimensional, then DirectPosition2D
provides
a more efficient implementation.
Coordinate Reference System (CRS) optionality
SinceDirectPosition
s, as data types, will often be included in larger objects
(such as Geometry
) that have references
to CoordinateReferenceSystem
, the getCoordinateReferenceSystem()
method
may returns null
if this particular DirectPosition
is included in such
larger object. In this case, the coordinate reference system is implicitly assumed to take
on the value of the containing object's CoordinateReferenceSystem
.- Since:
- 0.3
- Version:
- 1.2
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal double[]
The coordinates of the direct position.private static Field
Used for setting thecoordinates
field during aclone()
operation only.private org.opengis.referencing.crs.CoordinateReferenceSystem
The coordinate reference system for this position, ornull
.private static final long
Serial number for inter-operability with different versions. -
Constructor Summary
ConstructorsConstructorDescriptionGeneralDirectPosition
(double... coordinates) Constructs a position with the specified coordinates.GeneralDirectPosition
(int dimension) Constructs a position with the specified number of dimensions.Constructs a position initialized to the values parsed from the given string in Well Known Text (WKT) format.GeneralDirectPosition
(org.opengis.geometry.DirectPosition point) Constructs a position initialized to the same values than the specified point.GeneralDirectPosition
(org.opengis.referencing.crs.CoordinateReferenceSystem crs) Constructs a position using the specified coordinate reference system. -
Method Summary
Modifier and TypeMethodDescriptionclone()
Returns a deep copy of this position.boolean
Returnstrue
if the specified object is also aDirectPosition
with equal coordinates and equal CRS.final double[]
Returns a sequence of numbers that hold the coordinate of this position in its reference system.final org.opengis.referencing.crs.CoordinateReferenceSystem
Returns the coordinate reference system in which the coordinate is given.(package private) static Field
getCoordinatesField
(Class<?> type) Returns the"coordinates"
field of the given class and gives write permission to it.final int
The length of coordinate sequence (the number of entries).final double
getOrdinate
(int dimension) Returns the coordinate at the specified dimension.int
hashCode()
Returns a hash value for this coordinate tuple.void
setCoordinate
(double... coordinates) Sets the coordinate values along all dimensions.void
setCoordinateReferenceSystem
(org.opengis.referencing.crs.CoordinateReferenceSystem crs) Sets the coordinate reference system in which the coordinate is given.void
setLocation
(org.opengis.geometry.DirectPosition position) Sets this coordinate to the specified direct position.void
setOrdinate
(int dimension, double value) Sets the coordinate value along the specified dimension.toString()
Formats this position in the Well Known Text (WKT) format.Methods inherited from class org.apache.sis.geometry.AbstractDirectPosition
castOrCopy, formatTo, getDirectPosition, normalize, parse, toString
Methods inherited from class org.apache.sis.io.wkt.FormattableObject
print, toString, toWKT
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerial number for inter-operability with different versions.- See Also:
-
coordinatesField
Used for setting thecoordinates
field during aclone()
operation only. Will be fetch when first needed. -
coordinates
public final double[] coordinatesThe coordinates of the direct position. The length of this array is the dimension of this direct position. -
crs
private org.opengis.referencing.crs.CoordinateReferenceSystem crsThe coordinate reference system for this position, ornull
.
-
-
Constructor Details
-
GeneralDirectPosition
public GeneralDirectPosition(org.opengis.referencing.crs.CoordinateReferenceSystem crs) Constructs a position using the specified coordinate reference system. The number of dimensions is inferred from the coordinate reference system. All coordinate values are initialized to zero.- Parameters:
crs
- the coordinate reference system to be given to this position.
-
GeneralDirectPosition
Constructs a position with the specified number of dimensions. All coordinate values are initialized to zero.- Parameters:
dimension
- number of dimensions.- Throws:
NegativeArraySizeException
- ifdimension
is negative.
-
GeneralDirectPosition
public GeneralDirectPosition(double... coordinates) Constructs a position with the specified coordinates. This constructor assigns the given array directly (without clone) to thecoordinates
field. Consequently, callers shall not recycle the same array for creating many instances.Implementation note: the array is not cloned because this is usually not needed, especially in the context of variable argument lengths since the array is often created implicitly. Furthermore, thecoordinates
field is public, so cloning the array would not protect the state of this object anyway.Caution: if only one number is specified, make sure that the number type is
double
,float
orlong
otherwise theGeneralDirectPosition(int)
constructor would be invoked with a very different meaning. For example, for creating a one-dimensional coordinate initialized to the coordinate value 100, usenew GeneralDirectPosition(100.0)
, notnew GeneralDirectPosition(100)
, because the latter would actually create a position with 100 dimensions.- Parameters:
coordinates
- the coordinate values. This array is not cloned.
-
GeneralDirectPosition
public GeneralDirectPosition(org.opengis.geometry.DirectPosition point) Constructs a position initialized to the same values than the specified point. This is a copy constructor.- Parameters:
point
- the position to copy.
-
GeneralDirectPosition
Constructs a position initialized to the values parsed from the given string in Well Known Text (WKT) format. The given string is typically aPOINT
element like below: However, this constructor is lenient to other types likePOINT ZM
.- Parameters:
wkt
- thePOINT
or other kind of element to parse.- Throws:
IllegalArgumentException
- if the given string cannot be parsed.- See Also:
-
-
Method Details
-
getDimension
public final int getDimension()The length of coordinate sequence (the number of entries). This is always equals to the length of thecoordinates
array.- Specified by:
getDimension
in interfaceorg.opengis.geometry.DirectPosition
- Returns:
- the dimensionality of this position.
-
getCoordinateReferenceSystem
public final org.opengis.referencing.crs.CoordinateReferenceSystem getCoordinateReferenceSystem()Returns the coordinate reference system in which the coordinate is given. May benull
if this particularDirectPosition
is included in a larger object with such a reference to a CRS.- Specified by:
getCoordinateReferenceSystem
in interfaceorg.opengis.geometry.DirectPosition
- Overrides:
getCoordinateReferenceSystem
in classAbstractDirectPosition
- Returns:
- the coordinate reference system, or
null
.
-
setCoordinateReferenceSystem
public void setCoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem crs) throws org.opengis.geometry.MismatchedDimensionException Sets the coordinate reference system in which the coordinate is given.- Parameters:
crs
- the new coordinate reference system, ornull
.- Throws:
org.opengis.geometry.MismatchedDimensionException
- if the specified CRS does not have the expected number of dimensions.
-
getCoordinate
public final double[] getCoordinate()Returns a sequence of numbers that hold the coordinate of this position in its reference system.API note: This method is final for ensuring consistency with thecoordinates
, array field, which is public.- Specified by:
getCoordinate
in interfaceorg.opengis.geometry.DirectPosition
- Overrides:
getCoordinate
in classAbstractDirectPosition
- Returns:
- a copy of the
coordinates
array.
-
setCoordinate
public void setCoordinate(double... coordinates) throws org.opengis.geometry.MismatchedDimensionException Sets the coordinate values along all dimensions. -
getOrdinate
Returns the coordinate at the specified dimension.API note: This method is final for ensuring consistency with thecoordinates
, array field, which is public.- Specified by:
getOrdinate
in interfaceorg.opengis.geometry.DirectPosition
- Parameters:
dimension
- the dimension in the range 0 to dimension-1.- Returns:
- the coordinate at the specified dimension.
- Throws:
IndexOutOfBoundsException
- if the specified dimension is out of bounds.
-
setOrdinate
Sets the coordinate value along the specified dimension.- Specified by:
setOrdinate
in interfaceorg.opengis.geometry.DirectPosition
- Overrides:
setOrdinate
in classAbstractDirectPosition
- Parameters:
dimension
- the dimension for the coordinate of interest.value
- the coordinate value of interest.- Throws:
IndexOutOfBoundsException
- if the specified dimension is out of bounds.
-
setLocation
public void setLocation(org.opengis.geometry.DirectPosition position) throws org.opengis.geometry.MismatchedDimensionException Sets this coordinate to the specified direct position. If the specified position contains a coordinate reference system (CRS), then the CRS for this position will be set to the CRS of the specified position.- Overrides:
setLocation
in classAbstractDirectPosition
- Parameters:
position
- the new position for this point, ornull
for setting all coordinate values toNaN
.- Throws:
org.opengis.geometry.MismatchedDimensionException
- if the given position does not have the expected dimension.
-
toString
Formats this position in the Well Known Text (WKT) format. The returned string is like below, wherex₀
,x₁
,x₂
, etc. are the coordinate values at index 0, 1, 2, etc.: This method formats the numbers as withDouble.toString(double)
(i.e. without fixed number of fraction digits). The string returned by this method can be parsed by theGeneralDirectPosition
constructor.- Overrides:
toString
in classAbstractDirectPosition
- Returns:
- this position as a
POINT
in Well Known Text (WKT) format.
-
getCoordinatesField
Returns the"coordinates"
field of the given class and gives write permission to it. This method should be invoked only fromclone()
method.- Throws:
NoSuchFieldException
-
clone
Returns a deep copy of this position. -
hashCode
public int hashCode()Returns a hash value for this coordinate tuple. This method returns a value compliant with the contract documented in theDirectPosition.hashCode()
javadoc. Consequently, it should be possible to mix differentDirectPosition
implementations in the same hash map.- Specified by:
hashCode
in interfaceorg.opengis.geometry.DirectPosition
- Overrides:
hashCode
in classAbstractDirectPosition
- Returns:
- a hash code value for this position.
-
equals
Returnstrue
if the specified object is also aDirectPosition
with equal coordinates and equal CRS. This method performs the comparison as documented in theDirectPosition.equals(Object)
javadoc. In particular, the given object is not required to be of the same implementation class. Consequently, it should be possible to mix differentDirectPosition
implementations in the same hash map.- Specified by:
equals
in interfaceorg.opengis.geometry.DirectPosition
- Overrides:
equals
in classAbstractDirectPosition
- Parameters:
object
- the object to compare with this position.- Returns:
true
if the given object is equal to this position.
-