Package org.apache.sis.io.wkt
Class WKTFormat
- All Implemented Interfaces:
- Serializable,- Cloneable,- Localized
Parser and formatter for Well Known Text (WKT) strings.
 This format handles a pair of 
Parser and Formatter,
 used by the parse(…) and format(…) methods respectively.
 WKTFormat objects allow the following configuration:
 - The preferred authority of object name to
       format (see Formatter.getNameAuthority()for more information).
- The symbols to use (curly braces or brackets, etc).
- The transliterator to use for replacing Unicode characters by ASCII ones.
- Whether ANSI X3.64 colors are allowed or not (default is not).
- The indentation.
String expansion
Because the strings to be parsed by this class are long and tend to contain repetitive substrings,WKTFormat provides a mechanism for performing string substitutions before the parsing take place.
 Long strings can be assigned short names by calls to the addFragment(String, String) method.
 After fragments have been added, any call to a parsing method will replace all occurrences (except in
 quoted text) of tokens like $foo by the WKT fragment named "foo".
 Example:
 In the example below, the 
 $WGS84 substring which appear in the argument given to the
 parseObject(…) method will be expanded into the full GeodeticCRS[“WGS84”, …]
 string before the parsing proceed.
 Note that the parsing of WKT fragment does not always produce the same object. In particular, the default linear and angular units depend on the context in which the WKT fragment appears.addFragment("deg", "AngleUnit[“degree”, 0.0174532925199433]");…etc…
addFragment("lat", "Axis[“Latitude”, NORTH, $deg]");
addFragment("lon", "Axis[“Longitude”, EAST, $deg]");
addFragment("MyBaseCRS", "GeodeticCRS[“WGS84”, Datum[], CS[…etc…], $lat, $lon]");…etc…
Object crs = parseObject("ProjectedCRS[“Mercator_1SP”, $MyBaseCRS,]");
Limitations
- The WKT format is not lossless!
       Objects formatted by WKTFormatare not guaranteed to be identical after parsing. Some metadata may be lost or altered, but the coordinate operations between two CRS should produce the same numerical results provided that the two CRS were formatted independently (do not rely onGeneralDerivedCRS.getConversionFromBase()for instance).
- Instances of this class are not synchronized for multi-threading.
       It is recommended to create separated format instances for each thread.
       If multiple threads access a WKTFormatconcurrently, it must be synchronized externally.
- Serialized objects of this class are not guaranteed to be compatible with future Apache SIS releases. Serialization support is appropriate for short term storage or RMI between applications running the same version of Apache SIS.
- Since:
- 0.4
- See Also:
Defined in the sis-referencing module
- 
Nested Class SummaryNested classes/interfaces inherited from class FormatFormat.Field
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intThe indentation value to give to thesetIndentation(int)method for formatting the complete object on a single line.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidaddFragment(String name, String wkt) Adds a fragment of Well Know Text (WKT).clone()Returns a clone of this format.protected FormatcreateFormat(Class<?> valueType) Creates a new format to use for parsing and formatting values of the given type.voidformat(Object object, Appendable toAppendTo) Formats the specified object as a Well Know Text.Returns the colors to use for syntax coloring, ornullif none.Returns the convention for parsing and formatting WKT elements.<T extends Factory>
 TgetFactory(Class<T> type) Returns one of the factories used by thisWKTFormatfor parsing WKT.Returns the name of all WKT fragments known to thisWKTFormat.intReturns the current indentation to be used for formatting objects.Returns whether WKT keywords should be written with upper cases or camel cases.Returns whether to use short or long WKT keywords.getLocale(Locale.Category category) Returns the locale for the given category.intReturns the maximum number of elements to show in lists of values.Returns the preferred authority to look for when fetching identified object names and identifiers.Returns the symbols used for parsing and formatting WKT.Returns a mapper between Java character sequences and the characters to write in WKT.Returns the type of objects formatted by this class.If warnings occurred during the last WKT parsing or formatting, returns the warnings.parse(CharSequence wkt, ParsePosition pos) Creates an object from the given character sequence.voidsetColors(Colors colors) Sets the colors to use for syntax coloring.voidsetConvention(Convention convention) Sets the convention for parsing and formatting WKT elements.<T extends Factory>
 voidsetFactory(Class<T> type, T factory) Sets one of the factories to be used by thisWKTFormatfor parsing WKT.voidsetIndentation(int indentation) Sets a new indentation to be used for formatting objects.voidsetKeywordCase(KeywordCase keywordCase) Sets whether WKT keywords should be written with upper cases or camel cases.voidsetKeywordStyle(KeywordStyle keywordStyle) Sets whether to use short or long WKT keywords.voidsetMaximumListElements(int limit) Sets a new limit for the number of elements to show in lists.voidsetNameAuthority(Citation authority) Sets the preferred authority for choosing the projection and parameter names.voidsetSymbols(Symbols symbols) Sets the symbols used for parsing and formatting WKT.voidsetTransliterator(Transliterator transliterator) Sets the mapper between Java character sequences and the characters to write in WKT.Methods inherited from class CompoundFormatformat, getFormat, getLocale, getTimeZone, parseObject, parseObjectMethods inherited from class Formatformat, formatToCharacterIterator
- 
Field Details- 
SINGLE_LINEpublic static final int SINGLE_LINEThe indentation value to give to thesetIndentation(int)method for formatting the complete object on a single line.
 
- 
- 
Constructor Details- 
WKTFormatCreates a format for the given locale and timezone. The given locale will be used forInternationalStringlocalization; this is not the locale for number format.- Parameters:
- locale- the locale for the new- Format, or- nullfor- Locale.ROOT.
- timezone- the timezone, or- nullfor UTC.
 
 
- 
- 
Method Details- 
getLocaleReturns the locale for the given category. This method implements the following mapping:- Locale.Category.FORMAT: the value of- Symbols.getLocale(), normally fixed to- Locale.ROOT, used for number formatting.
- Locale.Category.DISPLAY: the- localegiven at construction time, used for- InternationalStringlocalization.
 - Overrides:
- getLocalein class- CompoundFormat<Object>
- Parameters:
- category- the category for which a locale is desired.
- Returns:
- the locale for the given category (never null).
 
- 
getSymbolsReturns the symbols used for parsing and formatting WKT. This method returns an unmodifiable instance. Modifications, if desired, should be applied on a clone of the returned object.- Returns:
- the current set of symbols used for parsing and formatting WKT.
 
- 
setSymbolsSets the symbols used for parsing and formatting WKT.- Parameters:
- symbols- the new set of symbols to use for parsing and formatting WKT.
 
- 
getTransliteratorReturns 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 except- REMARKS["…"].
- 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
 
- 
setTransliteratorSets the mapper between Java character sequences and the characters to write in WKT.If this method is never invoked, or if this method is invoked with a nullvalue, then the default mapper isTransliterator.DEFAULTexcept for WKT formatted according the internal convention.- Parameters:
- transliterator- the new mapper to use, or- nullfor restoring the default value.
- Since:
- 0.6
 
- 
getKeywordCaseReturns whether WKT keywords should be written with upper cases or camel cases.- Returns:
- the case to use for formatting keywords.
 
- 
setKeywordCaseSets whether WKT keywords should be written with upper cases or camel cases.- Parameters:
- keywordCase- the case to use for formatting keywords.
 
- 
getKeywordStyleReturns whether to use short or long WKT keywords.- Returns:
- the style used for formatting keywords.
- Since:
- 0.6
 
- 
setKeywordStyleSets whether to use short or long WKT keywords.- Parameters:
- keywordStyle- the style to use for formatting keywords.
- Since:
- 0.6
 
- 
getColorsReturns the colors to use for syntax coloring, ornullif none. This method returns an unmodifiable instance. Modifications, if desired, should be applied on a clone of the returned object. By default there is no syntax coloring.- Returns:
- the colors for syntax coloring, or nullif none.
 
- 
setColorsSets the colors to use for syntax coloring. This property applies only when formatting text.Newly created WKTFormats have no syntax coloring. If a non-null argument likeColors.DEFAULTis given to this method, then theformat(…)method tries to highlight most of the elements that are relevant toUtilities.equalsIgnoreMetadata(Object, Object).- Parameters:
- colors- the colors for syntax coloring, or- nullif none.
 
- 
getConventionReturns the convention for parsing and formatting WKT elements. The default value isConvention.WKT2.- Returns:
- the convention to use for formatting WKT elements (never null).
 
- 
setConventionSets the convention for parsing and formatting WKT elements.- Parameters:
- convention- the new convention to use for parsing and formatting WKT elements.
 
- 
getNameAuthorityReturns the preferred authority to look for when fetching identified object names and identifiers. The difference between various authorities are most easily seen in projection and parameter names.Example: The following table shows the names given by various organizations or projects for the same projection:If no authority has been explicitly set, then this method returns the default authority for the current convention.Projection name examples Authority Projection name EPSG Mercator (variant A) OGC Mercator_1SP GEOTIFF CT_Mercator - Returns:
- the organization, standard or project to look for when fetching projection and parameter names.
- See Also:
 
- 
setNameAuthoritySets the preferred authority for choosing the projection and parameter names. If non-null, the given priority will have precedence over the authority usually associated to the convention. Anullvalue restore the default behavior.- Parameters:
- authority- the new authority, or- nullfor inferring it from the convention.
- See Also:
 
- 
getIndentationpublic int getIndentation()Returns the current indentation to be used for formatting objects. The -1 value means that the whole WKT is to be formatted on a single line.- Returns:
- the current indentation.
 
- 
setIndentationpublic void setIndentation(int indentation) Sets a new indentation to be used for formatting objects. The -1 value means that the whole WKT is to be formatted on a single line.- Parameters:
- indentation- the new indentation to use.
- See Also:
 
- 
getMaximumListElementspublic int getMaximumListElements()Returns the maximum number of elements to show in lists of values. If a list length is greater than this limit, then only the first and last elements will be shown together with a message saying that some elements were omitted. This limit is useful in particular withMathTransformparameter values ofdouble[]type, since those parameters may be large interpolation tables.- Returns:
- the current lists size limit, or Integer.MAX_VALUEif unlimited.
- Since:
- 1.0
 
- 
setMaximumListElementspublic void setMaximumListElements(int limit) Sets a new limit for the number of elements to show in lists. If this method is never invoked, then the default is unlimited.- Parameters:
- limit- the new lists size limit, or- Integer.MAX_VALUEif unlimited.
- Since:
- 1.0
 
- 
getFactoryReturns one of the factories used by thisWKTFormatfor parsing WKT. The giventypeargument can be one of the following values:- CRSFactory.class
- CSFactory.class
- DatumFactory.class
- MathTransformFactory.class
- CoordinateOperationFactory.class
 - Type Parameters:
- T- the compile-time type of the- typeargument.
- Parameters:
- type- the factory type.
- Returns:
- the factory used by this WKTFormatfor the given type.
- Throws:
- IllegalArgumentException- if the- typeargument is not one of the valid values.
 
- 
setFactorySets one of the factories to be used by thisWKTFormatfor parsing WKT. The giventypeargument can be one of the following values:- CRSFactory.class
- CSFactory.class
- DatumFactory.class
- MathTransformFactory.class
- CoordinateOperationFactory.class
 LimitationThe current implementation does not serialize the given factories, because they are usually notSerializable. The factories used byWKTFormatinstances after deserialization are the default ones.- Type Parameters:
- T- the compile-time type of the- typeargument.
- Parameters:
- type- the factory type.
- factory- the factory to be used by this- WKTFormatfor the given type.
- Throws:
- IllegalArgumentException- if the- typeargument is not one of the valid values.
 
- 
getValueTypeReturns the type of objects formatted by this class. This method has to returnObject.classsince it is the only common parent to all object types accepted by this formatter.- Specified by:
- getValueTypein class- CompoundFormat<Object>
- Returns:
- Object.class
 
- 
getFragmentNamesReturns the name of all WKT fragments known to thisWKTFormat. The returned collection is initially empty. WKT fragments can be added by call toaddFragment(String, String).The returned collection is modifiable. In particular, a call to Set.clear()removes all fragments from thisWKTFormat.- Returns:
- the name of all fragments known to this WKTFormat.
 
- 
addFragmentAdds a fragment of Well Know Text (WKT). Thewktargument given to this method can contains itself other fragments specified in some previous calls to this method.Example if the following method is invoked:For removing a fragment, use
 Then other WKT strings parsed by thisaddFragment("MyEllipsoid", "Ellipsoid[“Bessel 1841”, 6377397.155, 299.1528128, ID[“EPSG”,“7004”]]");WKTFormatinstance can refer to the above fragment as below (WKT after the ellipsoid omitted for brevity):Object crs = parseObject("GeodeticCRS[“Tokyo”, Datum[“Tokyo”, $MyEllipsoid], …]");getFragmentNames().remove(name).- Parameters:
- name- the name to assign to the WKT fragment (case-sensitive). Must be a valid Unicode identifier.
- wkt- the Well Know Text (WKT) fragment represented by the given identifier.
- Throws:
- IllegalArgumentException- if the given name is not a valid Unicode identifier or if a fragment is already associated to that name.
- ParseException- if an error occurred while parsing the given WKT.
 
- 
parseCreates an object from the given character sequence. The parsing begins at the index given by theposargument. After successful parsing,ParsePosition.getIndex()gives the position after the last parsed character. In case of error,ParseException.getErrorOffset()gives the position of the first illegal character.- Specified by:
- parsein class- CompoundFormat<Object>
- Parameters:
- wkt- the character sequence for the object to parse.
- pos- index of the first character to parse (on input) or after last parsed character (on output).
- Returns:
- the parsed object (never null).
- Throws:
- ParseException- if an error occurred while parsing the WKT.
 
- 
formatFormats the specified object as a Well Know Text. The formatter accepts at least the following types:FormattableObject,IdentifiedObject,MathTransform,GeographicBoundingBox,VerticalExtent,TemporalExtent,Envelope,PositionandUnit.- Specified by:
- formatin class- CompoundFormat<Object>
- Parameters:
- object- the object to format.
- toAppendTo- where the text is to be appended.
- Throws:
- IOException- if an error occurred while writing to- toAppendTo.
- See Also:
 
- 
createFormatCreates a new format to use for parsing and formatting values of the given type. This method is invoked the first time that a format is needed for the given type. ThevalueTypecan be any types declared in the parent class.- Overrides:
- createFormatin class- CompoundFormat<Object>
- Parameters:
- valueType- the base type of values to parse or format.
- Returns:
- the format to use for parsing of formatting values of the given type, or nullif none.
 
- 
getWarningsIf warnings occurred during the last WKT parsing or formatting, returns the warnings. Otherwise returnsnull. The warnings are cleared every time a new object is parsed or formatted.- Returns:
- the warnings of the last parsing of formatting operation, or nullif none.
- Since:
- 0.6
 
- 
cloneReturns a clone of this format. The clone has the same configuration (including any added fragments), except the warnings.- Overrides:
- clonein class- CompoundFormat<Object>
- Returns:
- a clone of this format.
 
 
-