Class MilitaryGridReferenceSystem.Coder
- Enclosing class:
- MilitaryGridReferenceSystem
Conversions between direct positions and references in the Military Grid Reference System (MGRS).
 Each 
Coder instance can read references at arbitrary precision, but formats at the
 specified precision.
 The same Coder instance can be reused for reading or writing many MGRS references.
 See the MilitaryGridReferenceSystem enclosing class for usage example.
Immutability and thread safety
This class is not thread-safe. A new instance must be created for each thread, or synchronization must be applied by the caller.- Since:
- 0.8
Defined in the sis-referencing-by-identifiers module
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedCoder()Creates a new coder initialized to the default precision and separator.
- 
Method SummaryModifier and TypeMethodDescriptiondecode(CharSequence reference) Decodes the given MGRS reference into a position and an envelope.encode(DirectPosition position) Encodes the given position into a MGRS reference.encode(DirectPosition position, Quantity<?> precision) Encodes the given position into a MGRS reference with the given precision.Returns an iterator over all MGRS references that intersect the given envelope.Returns a stream of all MGRS references that intersect the given envelope.booleanReturns whether the decoded locations should be clipped to the valid area.doubleReturns the precision of the references formatted by this coder.getPrecision(DirectPosition position) Returns the precision of the references formatted by this coder.Returns the reference system for which MGRS references will be encoded or decoded.Returns the separator to insert between each component of the MGRS identifier.voidsetClipToValidArea(boolean clip) Sets whether the decoded locations should be clipped to the valid area.voidsetPrecision(double precision) Sets the desired precision of the references formatted by this coder.voidsetPrecision(Quantity<?> precision, DirectPosition position) Sets the desired precision of the references formatted by this coder.voidsetSeparator(String separator) Sets the separator to insert between each component of the MGRS identifier.
- 
Constructor Details- 
Coderprotected Coder()Creates a new coder initialized to the default precision and separator.
 
- 
- 
Method Details- 
getReferenceSystemReturns the reference system for which MGRS references will be encoded or decoded.- Specified by:
- getReferenceSystemin class- ReferencingByIdentifiers.Coder
- Returns:
- the enclosing reference system.
- Since:
- 1.3
 
- 
getPrecisionpublic double getPrecision()Returns the precision of the references formatted by this coder. This method returns one of the following values:
 Values smaller than 1 (e.g. 0.01 for a centimetre precision) may also be returned if that value has been explicitly set, but sub-metric precision are usually not used with MGRS.MGRS reference precisions Precision (m) Reference example 1 4 Q FJ 12345 67890 10 4 Q FJ 1234 6789 100 4 Q FJ 123 678 1000 4 Q FJ 12 67 10 000 4 Q FJ 1 6 100 000 4 Q FJ (approximated) 1 000 000 4 Q - Returns:
- precision of formatted references in metres.
 
- 
setPrecisionpublic void setPrecision(double precision) Sets the desired precision of the references formatted by this coder. This method rounds the given precision to one of the power of 10 documented in thegetPrecision()method.- Parameters:
- precision- the desired precision in metres.
- Throws:
- ArithmeticException- if the given precision is zero, negative, infinity or NaN.
 
- 
getPrecisionReturns the precision of the references formatted by this coder. This method returns the same value asgetPrecision()but as a quantity.- Specified by:
- getPrecisionin class- ReferencingByIdentifiers.Coder
- Parameters:
- position- ignored (can be null).
- Returns:
- precision of formatted references in metres.
- Since:
- 1.3
 
- 
setPrecisionpublic void setPrecision(Quantity<?> precision, DirectPosition position) throws IncommensurableException Sets the desired precision of the references formatted by this coder. If the given quantity uses angular units, it is converted to an approximate precision in metres at the latitude of given position. Then this method delegates tosetPrecision(double).- Specified by:
- setPrecisionin class- ReferencingByIdentifiers.Coder
- Parameters:
- precision- the desired precision in a linear or angular unit.
- position- location where the specified precision is desired, or- nullfor the equator.
- Throws:
- IncommensurableException- if the given precision does not use linear or angular units.
- ArithmeticException- if the precision is zero, negative, infinity or NaN.
- Since:
- 1.3
 
- 
getSeparatorReturns the separator to insert between each component of the MGRS identifier. Components are zone number, latitude band, 100 000-metres square identifier and numerical values. By default the separator is an empty string, which produce references like "4QFJ12345678".- Returns:
- the separator to insert between each component of the MGRS identifier, or an empty string if none.
 
- 
setSeparatorSets the separator to insert between each component of the MGRS identifier. Components are zone number, latitude band, 100 000-metres square identifier and numerical values. By default the separator is an empty string, which produce references like "4QFJ12345678". If the separator is set to a space, then the references will be formatted like "4 Q FJ 1234 5678".Note that a MGRS reference is normally written as an entity without spaces, parentheses, dashes, or decimal points. Invoking this method with a non-empty separator produces non-conform MGRS, but is sometimes convenient for readability or for use in file systems (with the '/'separator).- Parameters:
- separator- the separator to insert between each component of the MGRS identifier.
 
- 
getClipToValidAreapublic boolean getClipToValidArea()Returns whether the decoded locations should be clipped to the valid area. The default value istrue.- Returns:
- trueif decoded locations are clipped to the valid area.
 
- 
setClipToValidAreapublic void setClipToValidArea(boolean clip) Sets whether the decoded locations should be clipped to the valid area. MGRS 100 km squares can actually be smaller than 100 km when the square overlaps two UTM zones or two latitude bands. We may have half of a square in a zone and the other half in the other zone. By default, thedecode(CharSequence)method clips the square to the zone where it belongs. Invoking this method with thefalsevalue disables this behavior.- Parameters:
- clip- whether the decoded locations should be clipped to the valid area.
 
- 
encodeEncodes the given position into a MGRS reference. The given position must have a Coordinate Reference System (CRS) associated to it.- Specified by:
- encodein class- ReferencingByIdentifiers.Coder
- Parameters:
- position- the coordinate to encode.
- Returns:
- MGRS encoding of the given position.
- Throws:
- TransformException- if an error occurred while transforming the given coordinate to a MGRS reference.
 
- 
encodepublic String encode(DirectPosition position, Quantity<?> precision) throws IncommensurableException, TransformException Encodes the given position into a MGRS reference with the given precision. This is equivalent to invokingsetPrecision(Quantity, DirectPosition)beforeencode(DirectPosition), except that it is potentially more efficient.- Overrides:
- encodein class- ReferencingByIdentifiers.Coder
- Parameters:
- position- the coordinate to encode.
- precision- the desired precision in a linear or angular unit.
- Returns:
- MGRS encoding of the given position.
- Throws:
- ArithmeticException- if the precision is zero, negative, infinity or NaN.
- IncommensurableException- if the given precision does not use linear or angular units.
- TransformException- if an error occurred while transforming the given coordinate to a MGRS reference.
- Since:
- 1.3
 
- 
encodeReturns an iterator over all MGRS references that intersect the given envelope. The given envelope must have a Coordinate Reference System (CRS) associated to it. If the CRS is geographic, the envelope is allowed to span the anti-meridian. The MGRS references may be returned in any iteration order.Possible evolution: current implementation does not clip the cells to UPS/UTM valid areas before to test for intersection withareaOfInterest. Consequently, the iterator may return slightly more cells than expected. A future version may filter the cells more accurately. If an application needs the same set of cells than what current the implementation returns, it can invokesetClipToValidArea(false)for preserving current behavior in future Apache SIS versions.- Parameters:
- areaOfInterest- envelope of desired MGRS references.
- Returns:
- an iterator over MGRS references intersecting the given area of interest.
- Throws:
- TransformException- if an error occurred while transforming the area of interest.
 
- 
encodeReturns a stream of all MGRS references that intersect the given envelope. The given envelope must have a Coordinate Reference System (CRS) associated to it. If the CRS is geographic, the envelope is allowed to span the anti-meridian. The MGRS references may be returned in any order.Possible evolution: current implementation does not clip the cells to UPS/UTM valid areas before to test for intersection withareaOfInterest. Consequently, the iterator may return slightly more cells than expected. A future version may filter the cells more accurately. If an application needs the same set of cells than what current the implementation returns, it can invokesetClipToValidArea(false)for preserving current behavior in future Apache SIS versions.- Parameters:
- areaOfInterest- envelope of desired MGRS references.
- parallel-- truefor a parallel stream, or- falsefor a sequential stream.
- Returns:
- a stream of MGRS references intersecting the given area of interest.
- Throws:
- TransformException- if an error occurred while transforming the area of interest.
 
- 
decodeDecodes the given MGRS reference into a position and an envelope. The Coordinate Reference System (CRS) associated to the returned position depends on the given reference.Upcoming API change — generalization
 in a future SIS version, the type of returned element may be generalized to theorg.opengis.referencing.gazetteer.Locationinterface. This change is pending GeoAPI revision.- Specified by:
- decodein class- ReferencingByIdentifiers.Coder
- Parameters:
- reference- MGRS string to decode.
- Returns:
- a new position with the longitude at coordinate 0 and latitude at coordinate 1.
- Throws:
- TransformException- if an error occurred while parsing the given string.
 
 
-