[geotk] How to generate a WFS GetFeature response with geotoolkit ?

Guilhem guilhem.legal at geomatys.fr
Wed Apr 16 02:26:47 PDT 2014


hi François,

To write feature collection, you can't use MarshallerPool (JAXB) because 
its dynamical.
You must use JAXP feature writer.
here is a piece of code doing what you are trying to do:

        final GeometryFactory gf = new GeometryFactory();

         final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
         ftb.setName("station:stationType");
         ftb.add("name", String.class);
         CoordinateReferenceSystem wgs84 = CRS.forCode("CRS:84");
         ftb.add("the_geom", PointType.class, wgs84);
         ftb.setDefaultGeometry("the_geom");
         final SimpleFeatureType sft = ftb.buildSimpleFeatureType();
         SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(sft);
         sfb.set("name", "Toulouse");
         sfb.set("the_geom", gf.createPoint(new Coordinate(1.4526, 
43.5957)));
         final Feature feat = sfb.buildFeature("id-0");

         FeatureCollection collection = 
FeatureStoreUtilities.collection(sft, Arrays.asList(feat));

         final JAXPStreamFeatureWriter writer = new 
JAXPStreamFeatureWriter("3.2.1", "1.1.0", new HashMap<String, String>());
         writer.setOutput(System.out);
         writer.writeFeatureCollection(collection, false, 1);


Guilhem Legal

Le 15/04/2014 22:50, Francois Andre a écrit :
> Hi everybody,
>
> I'd like to generate a WFS GetFeature response with geotoolkit.
>
> I've found here and there some clues but I just don't really know how 
> to link everything together
>
> Here is the XML i'd like to generate:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <wfs:FeatureCollection numberOfFeatures="1" 
> timeStamp="2014-04-15T15:22:49.133+02:00"
>       xsi:schemaLocation="http://www.resif.fr/station 
> http://localhost/wfs/schema.xsd http://www.opengis.net/wfs 
> http://ogo.heig-vd.ch:80/geoserver/schemas/wfs/1.1.0/wfs.xsd 
> <http://ogo.heig-vd.ch/geoserver/schemas/wfs/1.1.0/wfs.xsd>"
>       xmlns:station="http://www.resif.fr/station"
>       xmlns:ogc="http://www.opengis.net/ogc"
>       xmlns:wfs="http://www.opengis.net/wfs"
>       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>       xmlns:gml="http://www.opengis.net/gml"  >
>
>       <gml:featureMembers>
>       <station:station gml:id="cities.248">
> <station:station_name>Toulouse</station:station_name>
>         <station:the_geom>
>           <gml:Point srsDimension="2" 
> srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
>             <gml:pos>1.4434 43.6045 <tel:1.4434%2043.6045></gml:pos>
>           </gml:Point>
>         </station:the_geom>
>       </station:station>
>       </gml:featureMembers>
>       </wfs:FeatureCollection>
>
> Here is my unworking coding attempt :
>
> @Test
> public void GetFeatureTest() throws Exception
> {
> final GeometryFactory gf = new GeometryFactory();
> GregorianCalendar c = new GregorianCalendar();
> XMLGregorianCalendar timeStamp = 
> DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
> String version = "1.1.0";
> FeatureCollectionType response = (FeatureCollectionType) 
> WFSXmlFactory.buildFeatureCollection(version, "id", 2, timeStamp);
> final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
> ftb.setName("station:stationType");
> ftb.add("name", String.class);
> DefaultGeographicCRS wgs84 = DefaultGeographicCRS.WGS84;
> ftb.add("the_geom", PointType.class, wgs84);
> ftb.setDefaultGeometry("the_geom");
> final SimpleFeatureType sft = ftb.buildSimpleFeatureType();
> SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(sft);
> sfb.set("name", "Toulouse");
> sfb.set("the_geom", gf.createPoint(new Coordinate(1.4526, 43.5957)));
> final Feature feat = sfb.buildFeature("id-0");
>
> Marshaller marshaller = 
> WFSMarshallerPool.getInstance().acquireMarshaller();
> final StringWriter sw = new StringWriter();
> marshaller.marshal(response, sw);
> WFSMarshallerPool.getInstance().recycle(marshaller);
> System.out.println(sw.toString());
> }
>
> So, if someone can explain how to link FeatureType and 
> FeaturePropertyType and all the other stuff, i'd be grateful :-) !
>
> François
>
>
> _______________________________________________
> Geotoolkit mailing list
> Geotoolkit at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/geotoolkit

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/geotoolkit/attachments/20140416/7d0dad36/attachment.html>


More information about the Geotoolkit mailing list