@Test
 public void testAsPositions() throws IOException {
   List<String> strings =
       asList(
           "151.2393322528181,-33.59862693992532,0", "151.2274390264927,-33.59631160091919,1.5");
   List<KmlPosition> positions = format.asKmlPositions(strings);
   assertEquals(2, positions.size());
   KmlPosition position1 = positions.get(0);
   assertDoubleEquals(151.2393322528181, position1.getLongitude());
   assertDoubleEquals(-33.59862693992532, position1.getLatitude());
   assertDoubleEquals(0.0, position1.getElevation());
   KmlPosition position2 = positions.get(1);
   assertDoubleEquals(151.2274390264927, position2.getLongitude());
   assertDoubleEquals(-33.59631160091919, position2.getLatitude());
   assertDoubleEquals(1.5, position2.getElevation());
 }
 @Test
 public void testPointCoordinatesWithoutSpaces() throws Exception {
   String string =
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
           + "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
           + "<Document><Placemark><Point>\n"
           + "<coordinates>151.2393322528181,-33.59862693992532,0\n"
           + "</coordinates>\n"
           + "</Point></Placemark></Document></kml>";
   ParserContext<KmlRoute> context = new ParserContextImpl<>();
   format.read(new ByteArrayInputStream(string.getBytes()), null, context);
   List<KmlRoute> routes = context.getRoutes();
   assertEquals(1, routes.size());
   KmlRoute route = routes.get(0);
   assertEquals(1, route.getPositionCount());
   KmlPosition position = route.getPositions().get(0);
   assertDoubleEquals(151.2393322528181, position.getLongitude());
   assertDoubleEquals(-33.59862693992532, position.getLatitude());
   assertNull(position.getSpeed());
   assertDoubleEquals(0.0, position.getElevation());
 }
  private FolderType createMarks(KmlRoute route, int startIndex, int endIndex) {
    ObjectFactory objectFactory = new ObjectFactory();

    FolderType marks = objectFactory.createFolderType();
    marks.setName(MARKS);
    marks.setVisibility(FALSE);
    marks.setOpen(FALSE);

    double currentDistance = 0, previousDistance = 0;
    int currentKiloMeter = 1;
    List<KmlPosition> positions = route.getPositions();
    for (int i = startIndex + 1; i < endIndex; i++) {
      KmlPosition previousPosition = positions.get(i - 1);
      KmlPosition currentPosition = positions.get(i);

      Double distance = currentPosition.calculateDistance(previousPosition);
      if (isEmpty(distance)) continue;

      currentDistance += distance;
      if (currentDistance >= METERS_BETWEEN_MARKS) {
        // calculate the point at the kilometer mark that's between the current position and the
        // previous one.
        // it is possible, that there's more than one point to create
        // see: http://www.movable-type.co.uk/scripts/latlong.html and
        // http://williams.best.vwh.net/avform.htm#LL
        KmlPosition intermediate =
            new KmlPosition(
                previousPosition.getLongitude(),
                previousPosition.getLatitude(),
                null,
                null,
                null,
                null);

        // remaining distance between the last point and the mark
        double remainingDistance = METERS_BETWEEN_MARKS - (previousDistance % METERS_BETWEEN_MARKS);
        do {
          double angle = toRadians(intermediate.calculateAngle(currentPosition));
          double latitude1 = toRadians(intermediate.getLatitude());
          double longitude1 = toRadians(intermediate.getLongitude());
          double latitude2 =
              asin(
                  sin(latitude1) * cos(remainingDistance / EARTH_RADIUS)
                      + cos(latitude1) * sin(remainingDistance / EARTH_RADIUS) * cos(angle));
          double longitude2 =
              longitude1
                  + atan2(
                      sin(angle) * sin(remainingDistance / EARTH_RADIUS) * cos(latitude1),
                      cos(remainingDistance / EARTH_RADIUS) - sin(latitude1) * sin(latitude2));
          intermediate.setLatitude(toDegrees(latitude2));
          intermediate.setLongitude(toDegrees(longitude2));

          PlacemarkType placeMark =
              createMark(
                  currentKiloMeter++, intermediate.getLongitude(), intermediate.getLatitude());
          marks.getAbstractFeatureGroup().add(objectFactory.createPlacemark(placeMark));

          remainingDistance = METERS_BETWEEN_MARKS;
        } while (toDouble(intermediate.calculateDistance(currentPosition)) > METERS_BETWEEN_MARKS);

        currentDistance = currentDistance % METERS_BETWEEN_MARKS;
      }
      previousDistance = currentDistance;
    }
    return marks;
  }