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