Package org.apache.sis.io.wkt
Class Formatter
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:
Defined in the sis-referencing module
-
Constructor Summary
ConstructorsConstructorDescriptionCreates 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.voidappend(boolean value) Appends a boolean value.voidappend(double number) Appends an floating point value.voidappend(long number) Appends an integer value.voidappend(String text, ElementKind type) Appends a character string between quotes.voidAppends a date.voidAppends a unit in aUnit[…]element or one of the specialized elements.voidappend(FormattableObject object) Appends the givenFormattableObject.voidAppends rows of numbers.voidappend(GeographicBoundingBox bbox, int fractionDigits) Appends the given geographic bounding box in aBBOX[…]element.voidappend(MathTransform transform) Appends the given math transform, typically (but not necessarily) in aPARAM_MT[…]element.voidAppends an enumeration or code list value.voidappendAny(Object value) Appends an object or an array of objects.delegateTo(Object other) Delegates the formatting to anotherFormattableObjectimplementation.final ConventionReturns the convention to use for formatting the WKT.getEnclosingElement(int depth) Returns the enclosing WKT element, ornullif element being formatted is the root.final LocaleReturns the locale to use for localizingInternationalStringinstances.final CitationReturns the preferred authority for choosing the projection and parameter names.final TransliteratorReturns a mapper between Java character sequences and the characters to write in WKT.booleanhasContextualUnit(int depth) Returnstrueif the element at the given depth specified a contextual unit.voidindent(int amount) Increases or decreases the indentation.booleanReturnstrueif the WKT written by this formatter is not strictly compliant to the WKT specification.voidnewLine()Request a line separator before the next element to format.voidrestoreContextualUnit(Unit<?> unit, Unit<?> previous) Restores the contextual unit to its previous state before the call toaddContextualUnit(Unit).voidsetInvalidWKT(Class<?> unformattable, Exception cause) Marks the current WKT representation of the given class as not strictly compliant with the WKT specification.voidsetInvalidWKT(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 theKeywordStylevalue.toContextualUnit(Unit<Q> unit) Returns the unit to use instead of the given one, orunitif 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_LINEfor 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.DEFAULTfor performing replacements like "é" → "e" in all WKT elements exceptREMARKS["…"].Transliterator.IDENTITYfor 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 the
WKTFormat.setNameAuthority(Citation)method. This is not necessarily the authority who created the object to format.Example: The EPSG name of theEPSG:6326datum 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 localizingInternationalStringinstances. This is not the locale for formatting dates and numbers.- Specified by:
getLocalein 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+1increases the indentation by the amount of spaces specified at construction time, and a value of-1reduces 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 theKeywordStylevalue. 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[…]DatumScope[…]ReferenceSystem,Datum,CoordinateOperationArea[…]ReferenceSystem,Datum,CoordinateOperationBBox[…]ReferenceSystem,Datum,CoordinateOperationVerticalExtent[…]ReferenceSystem,Datum,CoordinateOperationTimeExtent[…]ReferenceSystem,Datum,CoordinateOperationId[…]Authority[…]IdentifiedObjectRemarks[…]ReferenceSystem,CoordinateOperation- Parameters:
object- the formattable object to append to the WKT, ornullif none.
- Invoke
-
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 becauseGeographicBoundingBoxdoes 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, ornullif 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, ornullif none.type- the key of the colors to apply if syntax coloring is enabled, ornullif 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, ornullif 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, ornullif 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, ornullif none.fractionDigits- the number of fraction digits for each column in a row, ornullfor 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,ParametricUnitandTimeUnit. 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, ornullif 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 anotherFormattableObjectimplementation. Invoking this method is equivalent to first verifying theotherclass, then delegating as below:
This method is useful forreturn other.formatTo(this);
FormattableObjectwhich 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, ornullif 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) Returnstrueif the element at the given depth specified a contextual unit. This method returnstrueif the formattable object given bygetEnclosingElement(depth)has invokedaddContextualUnit(Unit)with a non-null unit at least once.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 quantityQwithout 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_UNITSjavadoc for more information.- Type Parameters:
Q- the unit quantity.- Parameters:
unit- the contextual unit to add, ornullif none.- Returns:
- the previous contextual unit for quantity
Q, ornullif 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, orunitif 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()Returnstrueif the WKT written by this formatter is not strictly compliant to the WKT specification. This method returnstrueifsetInvalidWKT(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:
trueif 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, ornullif 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, ornullif 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.
-