private FolderType createSpeed(KmlRoute route, int startIndex, int endIndex) { ObjectFactory objectFactory = new ObjectFactory(); FolderType folderType = objectFactory.createFolderType(); folderType.setName(SPEED); folderType.setVisibility(FALSE); folderType.setOpen(FALSE); folderType.getAbstractFeatureGroup().add(objectFactory.createScreenOverlay(createSpeedbar())); int segmentIndex = 0; List<String> coordinates = new ArrayList<>(); Integer previousSpeedClass = null; Double previousSpeed = null; KmlPosition previous = null; List<KmlPosition> positions = route.getPositions(); // since the speed of a position is the average speed of the previous segment for (int i = startIndex; i < endIndex; i++) { KmlPosition position = positions.get(i); Double speed = null; if (position.hasSpeed()) speed = position.getSpeed(); else if (previous != null) speed = previous.calculateSpeed(position); if (speed == null) speed = previousSpeed; if (speed == null) continue; coordinates.add(createCoordinates(position, false)); int speedClass = getSpeedClass(speed); if (previousSpeedClass != null && previousSpeedClass != speedClass) { PlacemarkType placemarkType = createSpeedSegment(++segmentIndex, previousSpeedClass, coordinates); folderType.getAbstractFeatureGroup().add(objectFactory.createPlacemark(placemarkType)); coordinates.clear(); coordinates.add(createCoordinates(position, false)); } previousSpeedClass = speedClass; previousSpeed = speed; previous = position; } return segmentIndex > 0 ? folderType : null; }
@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()); }