Class PrecisionModel

Hierarchy: Object , PrecisionModel
All Implemented Interfaces: Comparable , Serializable
public class PrecisionModel
implements Serializable , Comparable
Specifies the precision model of the Coordinates in a Geometry. In other words, specifies the grid of allowable points for all Geometrys.

The makePrecise(Coordinate) method allows rounding a coordinate to a "precise" value; that is, one whose precision is known exactly.

Coordinates are assumed to be precise in geometries. That is, the coordinates are assumed to be rounded to the precision model given for the geometry. JTS input routines automatically round coordinates to the precision model before creating Geometries. All internal operations assume that coordinates are rounded to the precision model. Constructive methods (such as boolean operations) always round computed coordinates to the appropriate precision model.

Currently three types of precision model are supported:

  • FLOATING - represents full double precision floating point. This is the default precision model used in JTS
  • FLOATING_SINGLE - represents single precision floating point.
  • FIXED - represents a model with a fixed number of decimal places. A Fixed Precision Model is specified by a scale factor. The scale factor specifies the size of the grid which numbers are rounded to. Input coordinates are mapped to fixed coordinates according to the following equations:
    • jtsPt.x = round( (inputPt.x * scale ) / scale
    • jtsPt.y = round( (inputPt.y * scale ) / scale
For example, to specify 3 decimal places of precision, use a scale factor of 1000. To specify -3 decimal places of precision (i.e. rounding to the nearest 1000), use a scale factor of 0.001.

Coordinates are represented internally as Java double-precision values. Since Java uses the IEEE-394 floating point standard, this provides 53 bits of precision. (Thus the maximum precisely representable integer is 9,007,199,254,740,992 - or almost 16 decimal digits of precision).

JTS binary methods currently do not handle inputs which have different precision models. The precision model of any constructed geometric value is undefined.

Other

  • version: 1.7
public PrecisionModel()
Creates a PrecisionModel with a default precision of FLOATING.
public PrecisionModel(Type modelType)
Creates a PrecisionModel that specifies an explicit precision model type. If the model type is FIXED the scale factor will default to 1.
Parameters:
modelType - modelType the type of the precision model
public PrecisionModel(double scale, double offsetX, double offsetY)
Creates a PrecisionModel that specifies Fixed precision. Fixed-precision coordinates are represented as precise internal coordinates, which are rounded to the grid defined by the scale factor.
Parameters:
scale - scale amount by which to multiply a coordinate after subtracting the offset, to obtain a precise coordinate
offsetX - offsetX not used.
offsetY - offsetY not used.
Deprecation:
offsets are no longer supported, since internal representation is rounded floating point
public PrecisionModel(double scale)
Creates a PrecisionModel that specifies Fixed precision. Fixed-precision coordinates are represented as precise internal coordinates, which are rounded to the grid defined by the scale factor.
Parameters:
scale - scale amount by which to multiply a coordinate after subtracting the offset, to obtain a precise coordinate
public PrecisionModel(PrecisionModel pm)
Copy constructor to create a new PrecisionModel from an existing one.
public static PrecisionModel mostPrecise(PrecisionModel pm1, PrecisionModel pm2)
Determines which of two PrecisionModels is the most precise (allows the greatest number of significant digits).
Parameters:
pm1 - pm1 a PrecisionModel
pm2 - pm2 a PrecisionModel
Returns:
the PrecisionModel which is most precise
public boolean isFloating()
Tests whether the precision model supports floating point
Returns:
true if the precision model supports floating point
public int getMaximumSignificantDigits()
Returns the maximum number of significant digits provided by this precision model. Intended for use by routines which need to print out decimal representations of precise values (such as WKTWriter).

This method would be more correctly called getMinimumDecimalPlaces, since it actually computes the number of decimal places that is required to correctly display the full precision of an ordinate value.

Since it is difficult to compute the required number of decimal places for scale factors which are not powers of 10, the algorithm uses a very rough approximation in this case. This has the side effect that for scale factors which are powers of 10 the value returned is 1 greater than the true value.

Returns:
the maximum number of decimal places provided by this precision model
public double getScale()
Returns the scale factor used to specify a fixed precision model. The number of decimal places of precision is equal to the base-10 logarithm of the scale factor. Non-integral and negative scale factors are supported. Negative scale factors indicate that the places of precision is to the left of the decimal point.
Returns:
the scale factor for the fixed precision model
public Type getType()
Gets the type of this precision model
See also:
Type
Returns:
the type of this precision model
public double getOffsetX()
Returns the x-offset used to obtain a precise coordinate.
Returns:
the amount by which to subtract the x-coordinate before multiplying by the scale
Deprecation:
Offsets are no longer used
public double getOffsetY()
Returns the y-offset used to obtain a precise coordinate.
Returns:
the amount by which to subtract the y-coordinate before multiplying by the scale
Deprecation:
Offsets are no longer used
public void toInternal(Coordinate external, Coordinate internal)
Sets internal to the precise representation of external.
Parameters:
external - external the original coordinate
internal - internal the coordinate whose values will be changed to the precise representation of external
Deprecation:
use makePrecise instead
public Coordinate toInternal(Coordinate external)
Returns the precise representation of external.
Parameters:
external - external the original coordinate
Returns:
the coordinate whose values will be changed to the precise representation of external
Deprecation:
use makePrecise instead
public Coordinate toExternal(Coordinate internal)
Returns the external representation of internal.
Parameters:
internal - internal the original coordinate
Returns:
the coordinate whose values will be changed to the external representation of internal
Deprecation:
no longer needed, since internal representation is same as external representation
public void toExternal(Coordinate internal, Coordinate external)
Sets external to the external representation of internal.
Parameters:
internal - internal the original coordinate
external - external the coordinate whose values will be changed to the external representation of internal
Deprecation:
no longer needed, since internal representation is same as external representation
public double makePrecise(double val)
Rounds a numeric value to the PrecisionModel grid. Asymmetric Arithmetic Rounding is used, to provide uniform rounding behaviour no matter where the number is on the number line.

This method has no effect on NaN values.

Note: Java's Math#rint uses the "Banker's Rounding" algorithm, which is not suitable for precision operations elsewhere in JTS.

public void makePrecise(Coordinate coord)
Rounds a Coordinate to the PrecisionModel grid.
public String toString()
public boolean equals(Object other)
public int compareTo(Object o)
Compares this PrecisionModel object with the specified object for order. A PrecisionModel is greater than another if it provides greater precision. The comparison is based on the value returned by the getMaximumSignificantDigits method. This comparison is not strictly accurate when comparing floating precision models to fixed models; however, it is correct when both models are either floating or fixed.
Parameters:
o - o the PrecisionModel with which this PrecisionModel is being compared
Returns:
a negative integer, zero, or a positive integer as this PrecisionModel is less than, equal to, or greater than the specified PrecisionModel