KMLParser implements FeatureCollectionParser { @Override @SuppressWarnings("unchecked") public SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS, CoordinateReferenceSystem targetCRS) throws ServiceException { try (InputStream in = new BufferedInputStream(new FileInputStream(file))) { PullParser parser = new PullParser(new KMLConfiguration(), in, KML.Placemark); SimpleFeature f; Set<String> extendedData = new HashSet<String>(); DefaultFeatureCollection fc = new DefaultFeatureCollection(); while ((f = (SimpleFeature) parser.parse()) != null) { Map<String, Object> untypedData = (Map<String, Object>) f.getUserData().get("UntypedExtendedData"); if (untypedData != null) { untypedData.keySet().forEach((k) -> extendedData.add(k)); } fc.add(f); } return processFeatures(targetCRS, fc, extendedData); } catch (XMLStreamException e) { throw new UserLayerException("XMLStreamException occured: " + e.getMessage(), UserLayerException.ErrorType.PARSER, UserLayerException.ErrorType.INVALID_FORMAT); } catch (IOException e) { throw new ServiceException("IOException occured: " + e.getMessage()); } catch (SAXException e) { throw new UserLayerException("Invalid KML file: " + e.getMessage(), UserLayerException.ErrorType.PARSER, UserLayerException.ErrorType.INVALID_FORMAT); } }  @Override String getSuffix(); @Override @SuppressWarnings("unchecked") SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS,
            CoordinateReferenceSystem targetCRS); static final String SUFFIX; static final String KML_NAME; static final String KML_DESC; static final String KML_GEOM; }
@Test public void testParseExtendedData() throws ServiceException, URISyntaxException, FactoryException { File file = new File(getClass().getResource("user_data.kml").toURI()); KMLParser kml = new KMLParser(); SimpleFeatureCollection fc = kml.parse(file, null, CRS.decode("EPSG:3067")); SimpleFeatureType schema = fc.getSchema(); boolean found = false; assertEquals(5, schema.getAttributeCount()); assertEquals(Geometry.class, schema.getDescriptor(KMLParser.KML_GEOM).getType().getBinding()); assertEquals(String.class, schema.getDescriptor("Opening hours").getType().getBinding()); assertEquals(5, fc.size()); SimpleFeatureIterator it = fc.features(); while (it.hasNext()){ SimpleFeature feature = it.next(); assertEquals(5, feature.getAttributeCount()); if ("Ruskeasuo".equals(feature.getAttribute(KMLParser.KML_NAME))){ found = true; Point p = (Point) feature.getDefaultGeometry(); Coordinate c = p.getCoordinate(); assertEquals(383726.9520816681, c.x, 1e-6); assertEquals(6676234.520781213, c.y, 1e-6); assertEquals(0.0, c.z, 0.0); assertEquals("Floorball arena", feature.getAttribute(KMLParser.KML_DESC)); assertEquals("9-20", feature.getAttribute("Opening hours")); assertEquals("", feature.getAttribute("Additional info")); } else if ("Route".equals(feature.getAttribute(KMLParser.KML_NAME))) { LineString line = (LineString) feature.getDefaultGeometry(); assertEquals(157, line.getCoordinates().length); assertEquals("Route from the Arena center to the Codecorner", feature.getAttribute(KMLParser.KML_DESC)); } } assertEquals("Failed to parse feature Ruskeasuo", true, found); }
@Test public void testParseSampleData() throws ServiceException, URISyntaxException, FactoryException { File file = new File(getClass().getResource("samples.kml").toURI()); KMLParser kml = new KMLParser(); SimpleFeatureCollection fc = kml.parse(file, null, CRS.decode("EPSG:4326")); boolean foundHidden = false; boolean foundPentagon = false; SimpleFeatureType schema = fc.getSchema(); assertEquals(3, schema.getAttributeCount()); assertEquals(20, fc.size()); SimpleFeatureIterator it = fc.features(); while (it.hasNext()){ SimpleFeature feature = it.next(); assertEquals(3, feature.getAttributeCount()); if (feature.getDefaultGeometry() == null){ foundHidden = true; assertEquals("Descriptive HTML", feature.getAttribute(KMLParser.KML_NAME)); } else if ("The Pentagon".equals(feature.getAttribute(KMLParser.KML_NAME))) { foundPentagon = true; assertEquals("Shouldn't have kml LookAt attribute", null, feature.getAttribute("LookAt")); Polygon p = (Polygon) feature.getDefaultGeometry(); assertEquals(1, p.getNumInteriorRing()); LineString ring = p.getExteriorRing(); assertEquals (6, ring.getNumPoints()); Coordinate c = ring.getCoordinateN(0); assertEquals(38.87253259892824, c.x, 1e-6); assertEquals(-77.05788457660967, c.y, 1e-6); assertEquals(100.0, c.z, 0); ring = p.getInteriorRingN(0); assertEquals (6, ring.getNumPoints()); c = ring.getCoordinateN(0); assertEquals(38.87154239798456, c.x, 1e-6); assertEquals(-77.05668055019126, c.y, 1e-6); assertEquals(100.0, c.z, 0); } } assertEquals(true, foundHidden); assertEquals("Can't find The Pentagon. Has plane chrashed or just the test failed", true, foundPentagon); }