public Object decode(String input) throws Exception { return decode(new ByteArrayInputStream(input.getBytes())); }
public Object decode(InputStream input) throws Exception { StreamingParser parser = new StreamingParser(new KMLConfiguration(), input, KML.Placemark); SimpleFeature f = null; ListFeatureCollection features = null; HashMap oldftype = null; SimpleFeatureType type = null; SimpleFeatureBuilder featureBuilder = null; while ((f = (SimpleFeature) parser.parse()) != null) { HashMap ftype = getSignature(f); if (oldftype == null) { oldftype = ftype; type = getType(ftype); featureBuilder = new SimpleFeatureBuilder(type); features = new ListFeatureCollection(type); } else { if (!oldftype.equals(ftype)) { break; } } for (Object oentry : ftype.entrySet()) { Map.Entry entry = (Map.Entry) oentry; featureBuilder.add(f.getAttribute((Name) entry.getKey())); } SimpleFeature fnew = featureBuilder.buildFeature(f.getID()); features.add(fnew); } return features; }
@Test public void testEncodePolygon() throws Exception { FeatureTypeInfo fti = getCatalog().getFeatureTypeByName(getLayerId(MockData.BASIC_POLYGONS)); SimpleFeatureCollection fc = (SimpleFeatureCollection) fti.getFeatureSource(null, null).getFeatures(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ppio.encode(fc, bos); Document dom = dom(new ByteArrayInputStream(bos.toByteArray())); // print(dom); checkValidationErorrs(dom, "./src/test/resources/org/geoserver/wps/ppio/ogckml22.xsd"); assertEquals( "-1.0,5.0 2.0,5.0 2.0,2.0 -1.0,2.0 -1.0,5.0", xpath.evaluate( "//kml:Placemark[@id='BasicPolygons.1107531493644']//kml:LinearRing/kml:coordinates", dom)); }
@Override protected void setUpInternal() throws Exception { GeoServer gs = getGeoServer(); GeoServerInfo global = gs.getGlobal(); SettingsInfo settings = global.getSettings(); ContactInfo contact = settings.getContact(); contact.setContactOrganization("GeoServer"); contact.setOnlineResource("http://www.geoserver.org"); gs.save(global); ppio = new KMLPPIO(gs); }
@Test public void testEncodeLinestring() throws Exception { FeatureTypeInfo fti = getCatalog().getFeatureTypeByName(getLayerId(MockData.LINES)); SimpleFeatureCollection fc = (SimpleFeatureCollection) fti.getFeatureSource(null, null).getFeatures(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ppio.encode(fc, bos); Document dom = dom(new ByteArrayInputStream(bos.toByteArray())); // print(dom); checkValidationErorrs(dom, "./src/test/resources/org/geoserver/wps/ppio/ogckml22.xsd"); // check the data was reprojected to wgs84 assertEquals( "-92.99887316950249,4.523788751137377 -92.99842243632469,4.524241087719057", xpath.evaluate("//kml:LineString/kml:coordinates", dom)); assertEquals( "t0001 ", xpath.evaluate( "//kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='id']", dom)); }
@Test public void testEncodePoints() throws Exception { FeatureTypeInfo fti = getCatalog().getFeatureTypeByName(getLayerId(MockData.POINTS)); SimpleFeatureCollection fc = (SimpleFeatureCollection) fti.getFeatureSource(null, null).getFeatures(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ppio.encode(fc, bos); Document dom = dom(new ByteArrayInputStream(bos.toByteArray())); // print(dom); checkValidationErorrs(dom, "./src/test/resources/org/geoserver/wps/ppio/ogckml22.xsd"); assertEquals(1, xpath.getMatchingNodes("//kml:Placemark", dom).getLength()); assertEquals( "t0000", xpath.evaluate( "//kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='id']", dom)); assertEquals( "-92.99954926766114,4.52401492058674", xpath.evaluate("//kml:Point/kml:coordinates", dom)); assertEquals( "t0000", xpath.evaluate( "//kml:ExtendedData/kml:SchemaData/kml:SimpleData[@name='id']", dom)); } }