Object
Formatter
- All Implemented Interfaces:
Localized
Provides support methods for formatting a Well Known Text (WKT).
Formatter
instances are created by WKTFormat
and given to the
FormattableObject.formatTo(Formatter)
method of the object to format.
Formatter
provides the following services:
- A series of
append(…)
methods to be invoked by theformatTo(Formatter)
implementations. - Contextual information. In particular, the contextual units depend on the enclosing WKT element.
- A flag for declaring the object unformattable.
- Since:
- 0.4
- See Also:
-
Constructor Summary
ConstructorDescriptionCreates a new formatter instance with the default configuration.Formatter
(Convention convention, Symbols symbols, int indentation) Creates a new formatter instance with the specified convention, symbols and indentation. -
Method Summary
Modifier and TypeMethodDescriptionaddContextualUnit
(Unit<Q> unit) Adds a unit to use for the next measurements of the quantityQ
.void
append
(boolean value) Appends a boolean value.void
append
(double number) Appends an floating point value.void
append
(long number) Appends an integer value.void
append
(String text, ElementKind type) Appends a character string between quotes.void
Appends a date.void
Appends a unit in aUnit[…]
element or one of the specialized elements.void
append
(FormattableObject object) Appends the givenFormattableObject
.void
Appends rows of numbers.void
append
(GeographicBoundingBox bbox, int fractionDigits) Appends the given geographic bounding box in aBBOX[…]
element.void
append
(MathTransform transform) Appends the given math transform, typically (but not necessarily) in aPARAM_MT[…]
element.void
Appends an enumeration or code list value.void
append
(InternationalString scope, Extent area) Appends the usage (scope and domain of validity) of an object.void
appendAny
(Object value) Appends an object or an array of objects.delegateTo
(Object other) Delegates the formatting to anotherFormattableObject
implementation.final Convention
Returns the convention to use for formatting the WKT.getEnclosingElement
(int depth) Returns the enclosing WKT element, ornull
if element being formatted is the root.final Locale
Returns the locale to use for localizingInternationalString
instances.final Citation
Returns the preferred authority for choosing the projection and parameter names.final Transliterator
Returns a mapper between Java character sequences and the characters to write in WKT.boolean
hasContextualUnit
(int depth) Returnstrue
if the element at the given depth specified a contextual unit.void
indent
(int amount) Increases or decreases the indentation.boolean
Returnstrue
if the WKT written by this formatter is not strictly compliant to the WKT specification.void
newLine()
Request a line separator before the next element to format.void
restoreContextualUnit
(Unit<?> unit, Unit<?> previous) Restores the contextual unit to its previous state before the call toaddContextualUnit(Unit)
.void
setInvalidWKT
(Class<?> unformattable, Exception cause) Marks the current WKT representation of the given class as not strictly compliant with the WKT specification.void
setInvalidWKT
(IdentifiedObject unformattable, Exception cause) Marks the current WKT representation of the given object as not strictly compliant with the WKT specification.shortOrLong
(String shortKeyword, String longKeyword) Selects a short or long keyword depending on theKeywordStyle
value.toContextualUnit
(Unit<Q> unit) Returns the unit to use instead of the given one, orunit
if there is no replacement.Returns a string representation of this formatter for debugging purpose.toWKT()
Returns the WKT formatted by this object.
-
Constructor Details
-
Formatter
public Formatter()Creates a new formatter instance with the default configuration. -
Formatter
Creates a new formatter instance with the specified convention, symbols and indentation.- Parameters:
convention
- the convention to use.symbols
- the symbols.indentation
- the amount of spaces to use in indentation for WKT formatting, orWKTFormat.SINGLE_LINE
for formatting the whole WKT on a single line.
-
-
Method Details
-
getConvention
Returns the convention to use for formatting the WKT. The default isConvention.WKT2
.- Returns:
- the convention (never
null
). - See Also:
-
getTransliterator
Returns a mapper between Java character sequences and the characters to write in WKT. The intent is to specify how to write characters that are not allowed in WKT strings according ISO 19162 specification. Return values can be:Transliterator.DEFAULT
for performing replacements like "é" → "e" in all WKT elements exceptREMARKS["…"]
.Transliterator.IDENTITY
for preserving non-ASCII characters.- Any other user supplied mapping.
- Returns:
- the mapper between Java character sequences and the characters to write in WKT.
- Since:
- 0.6
- See Also:
-
getNameAuthority
Returns the preferred authority for choosing the projection and parameter names. The preferred authority can be set by theWKTFormat.setNameAuthority(Citation)
method. This is not necessarily the authority who created the object to format.Example
The EPSG name of theEPSG:6326
datum is "World Geodetic System 1984". However if the preferred authority is OGC, then the formatted datum name will rather look like "WGS84" (the exact string depends on the object aliases).- Returns:
- the authority for projection and parameter names.
- See Also:
-
getLocale
Returns the locale to use for localizingInternationalString
instances. This is not the locale for formatting dates and numbers.- Specified by:
getLocale
in interfaceLocalized
- Returns:
- the locale to use for localizing international strings.
-
newLine
public void newLine()Request a line separator before the next element to format. Invoking this method before anyappend(…)
method call will cause the next element to appear on the next line.This method has no effect in any of the following cases:
- This method has already been invoked before the next
append(…)
. - The indentation is
WKTFormat.SINGLE_LINE
.
- This method has already been invoked before the next
-
indent
public void indent(int amount) Increases or decreases the indentation. A value of+1
increases the indentation by the amount of spaces specified at construction time, and a value of-1
reduces it by the same amount.- Parameters:
amount
- +1 for increasing the indentation, or -1 for decreasing it, or 0 for no-op.
-
shortOrLong
Selects a short or long keyword depending on theKeywordStyle
value. This method can be used byFormattableObject.formatTo(Formatter)
implementations for choosing the return value.- Parameters:
shortKeyword
- the keyword to return if the style isKeywordStyle.SHORT
.longKeyword
- the keyword to return if the style isKeywordStyle.LONG
.- Returns:
- the short or long keyword depending on the keyword style setting.
- Since:
- 0.6
- See Also:
-
append
Appends the givenFormattableObject
. This method performs the following steps:- Invoke
object.formatTo(this)
. - Prepend the keyword returned by the above method call (e.g.
"GEOCS"
). - If the given object is an instance of
IdentifiedObject
, then append complementary information:
Complementary WKT elements WKT 2 element WKT 1 element For types Anchor[…]
Datum
Scope[…]
ReferenceSystem
,Datum
,CoordinateOperation
Area[…]
ReferenceSystem
,Datum
,CoordinateOperation
BBox[…]
ReferenceSystem
,Datum
,CoordinateOperation
VerticalExtent[…]
ReferenceSystem
,Datum
,CoordinateOperation
TimeExtent[…]
ReferenceSystem
,Datum
,CoordinateOperation
Id[…]
Authority[…]
IdentifiedObject
Remarks[…]
ReferenceSystem
,CoordinateOperation
- Parameters:
object
- the formattable object to append to the WKT, ornull
if none.
- Invoke
-
append
Appends the usage (scope and domain of validity) of an object. The arguments are the components of anDefaultObjectDomain
. The given extent is decomposed in horizontal, vertical and temporal components. The horizontal component uses the default number of fraction digits recommended by ISO 19162.Usage element
In a WKT string, the given elements should be enclosed in anUSAGE[…]
element according the ISO 19162:2019 standard but not according the previous version (2015). TheUSAGE[…]
enclosing element shall be provided when needed by the caller.- Parameters:
scope
- description of domain of usage, ornull
if none.area
- area for which the object is valid, ornull
if none.- Since:
- 1.4
-
append
Appends the given geographic bounding box in aBBOX[…]
element. Longitude and latitude values will be formatted in decimal degrees. Longitudes are relative to the Greenwich meridian, with values increasing toward East. Latitudes values are increasing toward North.Numerical precision
The ISO 19162 standards recommends to format those values with only 2 decimal digits. This is becauseGeographicBoundingBox
does not specify the datum, so this box is an approximated information only.- Parameters:
bbox
- the geographic bounding box to append to the WKT, ornull
.fractionDigits
- the number of fraction digits to use. The recommended value is 2.
-
append
Appends the given math transform, typically (but not necessarily) in aPARAM_MT[…]
element.- Parameters:
transform
- the transform object to append to the WKT, ornull
if none.
-
append
Appends a character string between quotes. The element separator will be written before the text if needed.- Parameters:
text
- the string to format to the WKT, ornull
if none.type
- the key of the colors to apply if syntax coloring is enabled, ornull
if none.
-
append
Appends an enumeration or code list value. The element separator will be written before the code list if needed.For the WKT 2 format, this method uses the ISO name if available (for example
"northEast"
). For the WKT 1 format, this method uses the programmatic name instead (for example"NORTH_EAST"
).- Parameters:
code
- the code list to append to the WKT, ornull
if none.
-
append
Appends a date. The element separator will be written before the date if needed.- Parameters:
date
- the date to append to the WKT, ornull
if none.
-
append
public void append(boolean value) Appends a boolean value. The element separator will be written before the boolean if needed.- Parameters:
value
- the boolean to append to the WKT.
-
append
public void append(long number) Appends an integer value. The element separator will be written before the number if needed.- Parameters:
number
- the integer to append to the WKT.
-
append
public void append(double number) Appends an floating point value. The element separator will be written before the number if needed.- Parameters:
number
- the floating point value to append to the WKT.
-
append
Appends rows of numbers. Each number is separated by a space, and each row is separated by a comma. Rows usually have all the same length, but this is not mandatory. This method can be used for formatting geometries or matrix.- Parameters:
rows
- the rows to append, ornull
if none.fractionDigits
- the number of fraction digits for each column in a row, ornull
for default. A precision can be specified for each column because those columns are often different dimensions of a Coordinate Reference System (CRS), each with their own units of measurement. If a row contains more numbers thanfractionDigits.length
, then the last value in this array is repeated for all remaining row numbers.- Since:
- 1.0
-
append
Appends a unit in aUnit[…]
element or one of the specialized elements. Specialized elements areAngleUnit
,LengthUnit
,ScaleUnit
,ParametricUnit
andTimeUnit
. By default, specialized unit keywords are used with the WKT 2 convention.Example
append(Units.KILOMETRE)
will append "LengthUnit["km", 1000]
" to the WKT.- Parameters:
unit
- the unit to append to the WKT, ornull
if none.- See Also:
-
appendAny
Appends an object or an array of objects. This method performs the following choices:- If the given value is
null
, then this method appends the "null
" string (without quotes). - Otherwise if the given value is an array, then this method appends the opening sequence symbol, formats all elements by invoking this method recursively, then appends the closing sequence symbol.
- Otherwise if the value type is assignable to the argument type of one of the
append(…)
methods in this class, then the formatting will be delegated to that method. - Otherwise the given value is appended as a quoted text with its
toString()
representation.
- Parameters:
value
- the value to append to the WKT, ornull
.
- If the given value is
-
delegateTo
Delegates the formatting to anotherFormattableObject
implementation. Invoking this method is equivalent to first verifying theother
class, then delegating as below:return other.formatTo(this);
FormattableObject
which are wrapper around another object. It allows to delegate the WKT formatting to the wrapped object.- Parameters:
other
- the object to format with this formatter.- Returns:
- the value returned by
FormattableObject.formatTo(Formatter)
. - Throws:
UnformattableObjectException
- Since:
- 0.5
-
getEnclosingElement
Returns the enclosing WKT element, ornull
if element being formatted is the root. This method can be invoked by child elements having some aspects that depend on the enclosing element.- Parameters:
depth
- 1 for the immediate parent, 2 for the parent of the parent, etc.- Returns:
- the parent element at the given depth, or
null
.
-
hasContextualUnit
public boolean hasContextualUnit(int depth) Returnstrue
if the element at the given depth specified a contextual unit. This method returnstrue
if the formattable object given bygetEnclosingElement(depth)
has invokedaddContextualUnit(Unit)
with a non-null unit at least once.Usage note
The main purpose of this method is to allowAXIS[…]
elements to determine if they should inherit the unit specified by the enclosing CRS, or if they should specify their unit explicitly.- Parameters:
depth
- 1 for the immediate parent, 2 for the parent of the parent, etc.- Returns:
- whether the parent element at the given depth has invoked
addContextualUnit(…)
at least once.
-
addContextualUnit
Adds a unit to use for the next measurements of the quantityQ
. The given unit will apply to all WKT elements containing a value of quantityQ
without their ownUNIT[…]
element, until therestoreContextualUnit(Unit, Unit)
method is invoked.If the given unit is null, then this method does nothing and returns
null
.Special case
If the WKT conventions areWKT1_COMMON_UNITS
, then this method ignores the given unit and returnsnull
. SeeConvention.WKT1_COMMON_UNITS
javadoc for more information.- Type Parameters:
Q
- the unit quantity.- Parameters:
unit
- the contextual unit to add, ornull
if none.- Returns:
- the previous contextual unit for quantity
Q
, ornull
if none.
-
restoreContextualUnit
Restores the contextual unit to its previous state before the call toaddContextualUnit(Unit)
. This method is used in the following pattern:final Unit<?> previous = formatter.addContextualUnit(unit); // ... format some WKT elements here. formatter.restoreContextualUnit(unit, previous);
- Parameters:
unit
- the value given in argument toaddContextualUnit(unit)
(can benull
).previous
- the value returned byaddContextualUnit(unit)
(can benull
).- Throws:
IllegalStateException
- if this method has not been invoked in the pattern documented above.- Since:
- 0.6
-
toContextualUnit
Returns the unit to use instead of the given one, orunit
if there is no replacement. This method searches for a unit specified byaddContextualUnit(Unit)
which is compatible with the given unit.- Type Parameters:
Q
- the quantity of the unit.- Parameters:
unit
- the unit to replace by the contextual unit, ornull
.- Returns:
- a contextual unit compatible with the given unit, or
unit
(which may be null) if no contextual unit has been found.
-
isInvalidWKT
public boolean isInvalidWKT()Returnstrue
if the WKT written by this formatter is not strictly compliant to the WKT specification. This method returnstrue
ifsetInvalidWKT(IdentifiedObject, Exception)
has been invoked at least once. The action to take regarding invalid WKT is caller-dependent. For example,FormattableObject.toString()
will accepts loose WKT formatting and ignore this flag, whileFormattableObject.toWKT()
requires strict WKT formatting and will thrown an exception if this flag is set.- Returns:
true
if the WKT is invalid.
-
setInvalidWKT
Marks the current WKT representation of the given object as not strictly compliant with the WKT specification. This method can be invoked by implementations ofFormattableObject.formatTo(Formatter)
when the object to format is more complex than what the WKT specification allows. Applications can testisInvalidWKT()
later for checking WKT validity.- Parameters:
unformattable
- the object that cannot be formatted,cause
- the cause for the failure to format, ornull
if the cause is not an exception.
-
setInvalidWKT
Marks the current WKT representation of the given class as not strictly compliant with the WKT specification. This method can be used as an alternative tosetInvalidWKT(IdentifiedObject, Exception)
when the problematic object is not an instance ofIdentifiedObject
.- Parameters:
unformattable
- the class of the object that cannot be formatted,cause
- the cause for the failure to format, ornull
if the cause is not an exception.
-
toWKT
Returns the WKT formatted by this object.- Returns:
- the WKT formatted by this formatter.
-
toString
Returns a string representation of this formatter for debugging purpose.
-