Class CoordinateFormat
- Object
-
- Format
-
- CompoundFormat<DirectPosition>
-
- CoordinateFormat
-
- All Implemented Interfaces:
Serializable,Cloneable,Localized
public class CoordinateFormat extends CompoundFormat<DirectPosition>
Formats spatiotemporal coordinates using number, angle and date formats inferred from the coordinate system. The format for each coordinate is inferred from the coordinate system units using the following rules:- Coordinate values in angular units are formatted as angles using
AngleFormat. - Coordinate values in temporal units are formatted as dates using
DateFormat. - Other values are formatted as numbers using
NumberFormatfollowed by the unit symbol formatted byUnitFormat.
applyPattern(Class, String)public method, or by overriding theCompoundFormat.createFormat(Class)protected method.This format does not transform the given coordinates in a unique CRS. If the coordinates need to be formatted in a specific CRS, then the caller should transform the position before to format it.
- Since:
- 0.8
- See Also:
AngleFormat,UnitFormat,GeneralDirectPosition, Serialized Form
Defined in the
sis-referencingmodule
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class Format
Format.Field
-
-
Constructor Summary
Constructors Constructor Description CoordinateFormat()Constructs a new coordinate format with default locale and timezone.CoordinateFormat(Locale locale, TimeZone timezone)Constructs a new coordinate format for the specified locale and timezone.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanapplyPattern(Class<?> valueType, String pattern)Sets the pattern for number, angle or date fields.CoordinateFormatclone()Returns a clone of this format.Stringformat(DirectPosition position)Formats the given coordinate.voidformat(DirectPosition position, Appendable toAppendTo)Formats the given coordinate and appends the resulting text to the given stream or buffer.CoordinateReferenceSystemgetDefaultCRS()Returns the coordinate reference system to use if no CRS is explicitly associated to a givenDirectPosition.StringgetPattern(Class<?> valueType)Returns the pattern for number, angle or date fields.StringgetSeparator()Returns the separator between each coordinate (number, angle or date).Class<DirectPosition>getValueType()Returns the base type of values parsed and formatted by thisFormatinstance.DirectPositionparse(CharSequence text, ParsePosition pos)Parses a coordinate from the given character sequence.voidsetDefaultCRS(CoordinateReferenceSystem crs)Sets the coordinate reference system to use if no CRS is explicitly associated to a givenDirectPosition.voidsetPrecision(double resolution, Unit<?> unit)Adjusts the number of fraction digits to show in coordinates for achieving the given precision.voidsetSeparator(String separator)Sets the separator between each coordinate.-
Methods inherited from class CompoundFormat
createFormat, format, getFormat, getLocale, getLocale, getTimeZone, parseObject, parseObject
-
Methods inherited from class Format
format, formatToCharacterIterator
-
-
-
-
Method Detail
-
getSeparator
public String getSeparator()
Returns the separator between each coordinate (number, angle or date). The default value is a single space.- Returns:
- the current coordinate separator.
-
setSeparator
public void setSeparator(String separator)
Sets the separator between each coordinate. The default value is a single space.- Parameters:
separator- the new coordinate separator.
-
getDefaultCRS
public CoordinateReferenceSystem getDefaultCRS()
Returns the coordinate reference system to use if no CRS is explicitly associated to a givenDirectPosition.- Returns:
- the default coordinate reference system, or
nullif none.
-
setDefaultCRS
public void setDefaultCRS(CoordinateReferenceSystem crs)
Sets the coordinate reference system to use if no CRS is explicitly associated to a givenDirectPosition. This CRS is only a default; positions given in another CRS are not automatically transformed to that CRS before formatting.- Parameters:
crs- the default coordinate reference system, ornullif none.
-
setPrecision
public void setPrecision(double resolution, Unit<?> unit)Adjusts the number of fraction digits to show in coordinates for achieving the given precision. TheNumberFormatandAngleFormatare configured for coordinates expressed in the default coordinate reference system defined at the moment this method is invoked. The number of fraction digits is not updated if a different CRS is specified after this method call or if the coordinates to format are associated to a different CRS.The given resolution will be converted to the units used by coordinate system axes. For example if a 10 metres resolution is specified but the default CRS axes use kilometres, then this method converts the resolution to 0.01 kilometre and uses that value for inferring that coordinates should be formatted with 2 fraction digits. If the resolution is specified in an angular units such as degrees, this method uses the ellipsoid authalic radius for computing an equivalent resolution in linear units. For example if the ellipsoid of default CRS is WGS84, then this method considers a resolution of 1 second of angle as equivalent to a resolution of about 31 meters. Conversions work also in the opposite direction (from linear to angular units) and are also used for choosing which angle fields (degrees, minutes or seconds) to show.
- Parameters:
resolution- the desired resolution.unit- unit of the desired resolution.- Since:
- 1.0
- See Also:
NumberFormat.setMaximumFractionDigits(int),AngleFormat.setPrecision(double, boolean)
-
getPattern
public String getPattern(Class<?> valueType)
Returns the pattern for number, angle or date fields. The givenvalueTypeshould beNumber.class,Angle.class,Date.classor a sub-type of the above. This method may returnnullif the underlying format can not provide a pattern.Pattern availability for type of value Value type Base format class Format with pattern NumberNumberFormatDecimalFormatAngleAngleFormatAngleFormatDateDateFormatSimpleDateFormat- Parameters:
valueType- the base type of coordinate values to parse and format:Number.class,Angle.classorDate.class.- Returns:
- the pattern for fields of the given type, or
nullif not applicable. - See Also:
CompoundFormat.getFormat(Class)
-
applyPattern
public boolean applyPattern(Class<?> valueType, String pattern)
Sets the pattern for number, angle or date fields. The pattern syntax depends on thevalueTypeargument:- If
valueTypeisNumber.class, then the pattern syntax shall be as described in theDecimalFormatclass. This pattern may be used for any coordinate to be formatted as plain number, for example in Cartesian coordinate system. - If
valueTypeisAngle.class, then the pattern syntax shall be as described in theAngleFormatclass. This pattern may be used for any coordinate to be formatted as latitude or longitude, for example in ellipsoidal coordinate system. - If
valueTypeisDate.class, then the pattern syntax shall be as described in theSimpleDateFormatclass. This pattern may be used for any coordinate to be formatted as date and time, for example in time coordinate system.
- Parameters:
valueType- the base type of coordinate values to parse and format:Number.class,Angle.classorDate.class.pattern- the pattern as specified inDecimalFormat,AngleFormatorSimpleDateFormatjavadoc.- Returns:
trueif the pattern has been applied, orfalseifvalueTypedoes not specify a known type or if the format associated to that type does not support patterns.- Throws:
IllegalArgumentException- if the given pattern is invalid.
- If
-
getValueType
public final Class<DirectPosition> getValueType()
Returns the base type of values parsed and formatted by thisFormatinstance.- Specified by:
getValueTypein classCompoundFormat<DirectPosition>- Returns:
DirectPosition.class.
-
format
public String format(DirectPosition position)
Formats the given coordinate.- Parameters:
position- the coordinate to format.- Returns:
- the formatted position.
-
format
public void format(DirectPosition position, Appendable toAppendTo) throws IOException
Formats the given coordinate and appends the resulting text to the given stream or buffer.- Specified by:
formatin classCompoundFormat<DirectPosition>- Parameters:
position- the coordinate to format.toAppendTo- where the text is to be appended.- Throws:
IOException- if an error occurred while writing to the given appendable.
-
parse
public DirectPosition parse(CharSequence text, ParsePosition pos) throws ParseException
Parses a coordinate from the given character sequence. This method presumes that the coordinate reference system is the default CRS. The parsing begins at the index given by theposargument. If parsing succeeds, then theposindex is updated to the index after the last coordinate value and the parsed coordinate is returned. Otherwise (if parsing fails), theposindex is left unchanged, theposerror index is set to the index of the first unparsable character and an exception is thrown with a similar error index.- Specified by:
parsein classCompoundFormat<DirectPosition>- Parameters:
text- the character sequence for the coordinate to parse.pos- the index where to start the parsing.- Returns:
- the parsed coordinate (never
null). - Throws:
ParseException- if an error occurred while parsing the coordinate.
-
clone
public CoordinateFormat clone()
Returns a clone of this format.- Overrides:
clonein classCompoundFormat<DirectPosition>- Returns:
- a clone of this format.
-
-