Class KMLCoordinateTokenizer


  • public class KMLCoordinateTokenizer
    extends java.lang.Object
    Tokenizer to read coordinate values from KML coordinate string. The components of each coordinate tuple are separated by commas, as defined by the KML spec, coordinate tuples are comma separated, and each tuple is separated from the surrounding tuples by whitespace. For example:
     -18.3,23.5,0 -19.3,23.4,1 -20.0,23.5,2
     

    However, some KML files do not follow the spec and embed white space within the coordinate tuples. This tokenizer attempts to be lenient with whitespace handling. If a tuple ends with a comma, the tokenizer considers the next token in the input stream to be part of the same coordinate, not the start of a new coordinate.

    For example:

     -18.3,23.56,9     34.9, 56.0, 2     56.9, 19     90.0,23.9,44
     
    Will be tokenized to four coordinates: (23.56, -18.3, 9), (56.0, 34.9, 2), (56.9, 19, 0), and (90, 23.9, 44).

    The tokenizer also handles coordinate strings with no embedded white space. For example:

     -18.3,23.56,9,34.9,56.0,2
     
    Will be tokenized to two coordinates: (23.56, -18.3, 9), (56.0, 34.9, 2)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean afterComma  
      protected char[] buffer  
      protected int i  
      protected boolean inWord  
      protected java.lang.StringBuilder nextWord  
      protected java.util.List<java.lang.String> words  
    • Constructor Summary

      Constructors 
      Constructor Description
      KMLCoordinateTokenizer​(java.lang.String s)
      Create a tokenizer to read coordinates from a string.
    • Field Detail

      • i

        protected int i
      • buffer

        protected char[] buffer
      • words

        protected java.util.List<java.lang.String> words
      • nextWord

        protected java.lang.StringBuilder nextWord
      • inWord

        protected boolean inWord
      • afterComma

        protected boolean afterComma
    • Constructor Detail

      • KMLCoordinateTokenizer

        public KMLCoordinateTokenizer​(java.lang.String s)
        Create a tokenizer to read coordinates from a string.
        Parameters:
        s - String to read from.
    • Method Detail

      • hasMoreTokens

        public boolean hasMoreTokens()
        Are there more coordinates to read?
        Returns:
        True if there are more coordinates to read from the string.
      • nextPosition

        public Position nextPosition()
                              throws java.lang.NumberFormatException
        Read the next Position from the coordinate string.
        Returns:
        Next Position, or null if an error occurs while parsing the position (number format exception, etc).
        Throws:
        java.lang.NumberFormatException - if the coordinates cannot be parsed to a number.
      • makePosition

        protected Position makePosition()
      • wordBoundary

        protected void wordBoundary()