@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); }
@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); } }
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); } } }
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); } }  }
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); }
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; }