WfsFactory.eINSTANCE.createFeatureCollectionType(); featureCollectionType.getFeature().add(collection); WfsFactory.eINSTANCE.createFeatureCollectionType(); featureCollectionType.getFeature().add(collection);
@SuppressWarnings({"unchecked", "rawtypes"}) private FeatureCollectionType buildResults(List<FeatureCollection> results) { FeatureCollectionType result = WfsFactory.eINSTANCE.createFeatureCollectionType(); result.setTimeStamp(Calendar.getInstance()); result.getFeature().addAll(results); return result; }
@Override public FeatureCollectionResponse create() { return FeatureCollectionResponse.adapt( ((WfsFactory) getFactory()).createFeatureCollectionType()); }
@Override public FeatureCollectionResponse createResponse() { return new FeatureCollectionResponse.WFS11( ((WfsFactory) getFactory()).createFeatureCollectionType()); }
@Override public Object unadapt(Class target) { if (target.equals(net.opengis.wfs20.FeatureCollectionType.class)) { return adaptee; } else if (target.equals(FeatureCollectionType.class)) { net.opengis.wfs20.FeatureCollectionType source = (net.opengis.wfs20.FeatureCollectionType) adaptee; FeatureCollectionType result = WfsFactory.eINSTANCE.createFeatureCollectionType(); result.getFeature().addAll(source.getMember()); result.setNumberOfFeatures(source.getNumberReturned()); result.setLockId(source.getLockId()); result.setTimeStamp(source.getTimeStamp()); return result; } else { throw new WFSException( "Cannot transform " + adaptee + " to the specified target class " + target); } } }
/** * Saves the feature source contents into a zipped shapefile, returns the output as a byte array */ byte[] writeOut(FeatureCollection fc) throws IOException { ShapeZipOutputFormat zip = new ShapeZipOutputFormat(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fc); zip.write(fct, bos, op); return bos.toByteArray(); }
@Override public void encode(Object object, ContentHandler handler) throws Exception { FeatureCollection features = (FeatureCollection) object; SimpleFeatureType featureType = (SimpleFeatureType) features.getSchema(); FeatureCollectionType fc = WfsFactory.eINSTANCE.createFeatureCollectionType(); fc.getFeature().add(features); Encoder e = new Encoder(configuration); e.getNamespaces().declarePrefix("feature", featureType.getName().getNamespaceURI()); e.encode(fc, getElement(), handler); }
@Override public void encode(Object object, ContentHandler handler) throws Exception { FeatureCollection features = (FeatureCollection) object; SimpleFeatureType featureType = (SimpleFeatureType) features.getSchema(); FeatureCollectionType fc = WfsFactory.eINSTANCE.createFeatureCollectionType(); fc.getFeature().add( features ); Encoder e = new Encoder(configuration); e.getNamespaces().declarePrefix( "feature", featureType.getName().getNamespaceURI() ); e.encode( fc, getElement(), handler ); }
@Test public void testSingle() throws Exception { FeatureSource<? extends FeatureType, ? extends Feature> source = getFeatureSource(MockData.SEVEN); FeatureCollection<? extends FeatureType, ? extends Feature> features = source.getFeatures(); FeatureCollectionResponse fcType = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fcType.getFeature().add(features); ByteArrayOutputStream output = new ByteArrayOutputStream(); producer().write(fcType, output, request(MockData.SEVEN)); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = docBuilder.parse(new ByteArrayInputStream(output.toByteArray())); assertEquals(7, document.getElementsByTagName("cdf:Seven").getLength()); }
@Test public void testMultipleSameNamespace() throws Exception { FeatureCollectionResponse fcType = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fcType.getFeature().add(getFeatureSource(MockData.SEVEN).getFeatures()); fcType.getFeature().add(getFeatureSource(MockData.FIFTEEN).getFeatures()); ByteArrayOutputStream output = new ByteArrayOutputStream(); producer().write(fcType, output, request(MockData.SEVEN, MockData.FIFTEEN)); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = docBuilder.parse(new ByteArrayInputStream(output.toByteArray())); assertEquals( 7 + 15, document.getElementsByTagName("cdf:Seven").getLength() + document.getElementsByTagName("cdf:Fifteen").getLength()); }
@Test public void testCharset() throws Exception { FeatureSource<? extends FeatureType, ? extends Feature> fs; fs = getFeatureSource(SystemTestData.BASIC_POLYGONS); ShapeZipOutputFormat zip = new ShapeZipOutputFormat(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fs.getFeatures()); // add the charset Map options = new HashMap(); options.put("CHARSET", Charset.forName("ISO-8859-15")); gft.setFormatOptions(options); zip.write(fct, bos, op); checkShapefileIntegrity( new String[] {"BasicPolygons"}, new ByteArrayInputStream(bos.toByteArray())); assertEquals("ISO-8859-15", getCharset(new ByteArrayInputStream(bos.toByteArray()))); }
/** * Saves the feature source contents into a zipped shapefile, returns the output as a byte array */ byte[] writeOut(FeatureCollection fc, long maxShpSize, long maxDbfSize) throws IOException { ShapeZipOutputFormat zip = new ShapeZipOutputFormat(); zip.setMaxDbfSize(maxDbfSize); zip.setMaxShpSize(maxShpSize); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fc); zip.write(fct, bos, op); return bos.toByteArray(); }
@Test public void testMultipleDifferentNamespace() throws Exception { FeatureCollectionResponse fcType = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fcType.getFeature().add(getFeatureSource(MockData.SEVEN).getFeatures()); fcType.getFeature().add(getFeatureSource(MockData.POLYGONS).getFeatures()); int npolys = getFeatureSource(MockData.POLYGONS).getFeatures().size(); ByteArrayOutputStream output = new ByteArrayOutputStream(); producer().write(fcType, output, request(MockData.SEVEN, MockData.POLYGONS)); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = docBuilder.parse(new ByteArrayInputStream(output.toByteArray())); assertEquals( 7 + npolys, document.getElementsByTagName("cdf:Seven").getLength() + document.getElementsByTagName("cgf:Polygons").getLength()); } }
@Test public void testESRIFormat() throws Exception { setupESRIPropertyFile(); FeatureSource<? extends FeatureType, ? extends Feature> fs; fs = getFeatureSource(SystemTestData.BASIC_POLYGONS); ShapeZipOutputFormat zip = new ShapeZipOutputFormat(getGeoServer(), getCatalog(), getResourceLoader()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fs.getFeatures()); // add the charset Map options = new HashMap(); options.put("PRJFILEFORMAT", "ESRI"); gft.setFormatOptions(options); zip.write(fct, bos, op); byte[] byteArrayZip = bos.toByteArray(); checkShapefileIntegrity( new String[] {"BasicPolygons"}, new ByteArrayInputStream(byteArrayZip)); checkFileContent( "BasicPolygons.prj", new ByteArrayInputStream(byteArrayZip), get4326_ESRI_WKTContent()); }
@Test public void testTemplateSingleType() throws Exception { // copy the new template to the data dir WorkspaceInfo ws = getCatalog().getWorkspaceByName(SystemTestData.BASIC_POLYGONS.getPrefix()); getDataDirectory() .copyToWorkspaceDir( ws, getClass().getResourceAsStream("shapeziptest.ftl"), "shapezip.ftl"); // setup the request params SimpleFeatureCollection fc = getFeatureSource(SystemTestData.BASIC_POLYGONS).getFeatures(Filter.INCLUDE); ShapeZipOutputFormat zip = new ShapeZipOutputFormat(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fc); // get the file name assertEquals("shapezip_BasicPolygons.zip", zip.getAttachmentFileName(fct, op)); // check the contents zip.write(fct, bos, op); byte[] zipBytes = bos.toByteArray(); checkShapefileIntegrity( new String[] {"theshape_BasicPolygons"}, new ByteArrayInputStream(zipBytes)); }
@Test public void testTemplateMultiType() throws Exception { // copy the new template to the data dir WorkspaceInfo ws = getCatalog().getWorkspaceByName(SystemTestData.BASIC_POLYGONS.getPrefix()); getDataDirectory() .copyToWorkspaceDir( ws, getClass().getResourceAsStream("shapeziptest.ftl"), "shapezip.ftl"); // setup the request params ShapeZipOutputFormat zip = new ShapeZipOutputFormat(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature() .add(getFeatureSource(SystemTestData.BASIC_POLYGONS).getFeatures(Filter.INCLUDE)); fct.getFeature().add(getFeatureSource(SystemTestData.BRIDGES).getFeatures(Filter.INCLUDE)); // get the file name assertEquals("shapezip_BasicPolygons.zip", zip.getAttachmentFileName(fct, op)); // check the contents zip.write(fct, bos, op); byte[] zipBytes = bos.toByteArray(); checkShapefileIntegrity( new String[] {"theshape_BasicPolygons", "theshape_Bridges"}, new ByteArrayInputStream(zipBytes)); }
@Test public void testESRIFormatMultiType() throws Exception { setupESRIPropertyFile(); ShapeZipOutputFormat zip = new ShapeZipOutputFormat(getGeoServer(), getCatalog(), getResourceLoader()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(getFeatureSource(ALL_TYPES).getFeatures()); Map options = new HashMap(); options.put("PRJFILEFORMAT", "ESRI"); gft.setFormatOptions(options); zip.write(fct, bos, op); byte[] byteArrayZip = bos.toByteArray(); final String[] expectedTypes = new String[] {"AllTypesPoint", "AllTypesMPoint", "AllTypesPolygon", "AllTypesLine"}; checkShapefileIntegrity(expectedTypes, new ByteArrayInputStream(byteArrayZip)); for (String fileName : expectedTypes) { checkFileContent( fileName + ".prj", new ByteArrayInputStream(byteArrayZip), get4326_ESRI_WKTContent()); } }
@Test public void testESRIFormatFromDefaultValue() throws Exception { setupESRIPropertyFile(); final GeoServer geoServer = getGeoServer(); setupESRIFormatByDefault(geoServer, true); final FeatureSource fs = getFeatureSource(SystemTestData.BASIC_POLYGONS); final Catalog catalog = getCatalog(); final GeoServerResourceLoader resourceLoader = getResourceLoader(); ShapeZipOutputFormat zip = new ShapeZipOutputFormat(geoServer, catalog, resourceLoader); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FeatureCollectionResponse fct = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); fct.getFeature().add(fs.getFeatures()); // add the charset Map options = new HashMap(); gft.setFormatOptions(options); zip.write(fct, bos, op); byte[] byteArrayZip = bos.toByteArray(); checkShapefileIntegrity( new String[] {"BasicPolygons"}, new ByteArrayInputStream(byteArrayZip)); checkFileContent( "BasicPolygons.prj", new ByteArrayInputStream(byteArrayZip), get4326_ESRI_WKTContent()); }
@Test public void testOutputZipFileNameSpecifiedInFormatOptions() throws Exception { ShapeZipOutputFormat zip = new ShapeZipOutputFormat(getGeoServer(), getCatalog(), getResourceLoader()); FeatureCollectionResponse mockResult = FeatureCollectionResponse.adapt(WfsFactory.eINSTANCE.createFeatureCollectionType()); mockResult.getFeature().add(getFeatureSource(ALL_DOTS).getFeatures(Filter.INCLUDE)); GetFeatureType mockRequest = WfsFactory.eINSTANCE.createGetFeatureType(); Operation mockOperation = new Operation( "GetFeature", getServiceDescriptor10(), null, new Object[] {mockRequest}); assertEquals("All_Types_Dots.zip", zip.getAttachmentFileName(mockResult, mockOperation)); mockRequest.getFormatOptions().put("FILENAME", "REQUEST_SUPPLIED_FILENAME.zip"); assertEquals( "REQUEST_SUPPLIED_FILENAME.zip", zip.getAttachmentFileName(mockResult, mockOperation)); }
@Override protected void setUp() throws Exception { // the data source we'll use for the tests dataStore = new PropertyDataStore(new File("./src/test/java/org/geoserver/wfs/response")); // the output format (and let's add a few output formats to play with ogr = new Ogr2OgrOutputFormat(new GeoServerImpl()); ogr.addFormat(new OgrFormat("KML", "OGR-KML", ".kml", true, "application/vnd.google-earth.kml")); ogr.addFormat(new OgrFormat("KML", "OGR-KML-ZIP", ".kml", false, "application/vnd.google-earth.kml")); ogr.addFormat(new OgrFormat("CSV", "OGR-CSV", ".csv", true, "text/csv")); ogr.addFormat(new OgrFormat("SHP", "OGR-SHP", ".shp", false, null)); ogr.addFormat(new OgrFormat("MapInfo File", "OGR-MIF", ".mif", false, null, "-dsco", "FORMAT=MIF")); ogr.setOgrExecutable(Ogr2OgrTestUtil.getOgr2Ogr()); ogr.setGdalData(Ogr2OgrTestUtil.getGdalData()); // the EMF objects used to talk with the output format gft = WfsFactory.eINSTANCE.createGetFeatureType(); fct = WfsFactory.eINSTANCE.createFeatureCollectionType(); op = new Operation("GetFeature", new Service("WFS", null, new Version("1.0.0"), Arrays.asList("GetFeature")), null, new Object[] { gft }); }