Hi Jody, Thank you very much for trying to help me, and forgive me for not reporting back yet that i have found a working solution;
In the gt-xsd-wfs module there is a unit test, "WFSFeatureCollectionEncodingTest" which provides a working example that fixed my problem, the one thing i missed is that apparently to do this with the org.geotools.xsd.Encoder class i need to wrap my FeatureCollection in a FeatureCollectionType wrapper. Now i had looked at the unit test several times already but assumed at first glance that this was encoding a DescribeFeatureType output because of the class name "FeatureCollectionType" of the main object it starts with. But when i run it i could see what was going on, the "FeatureCollectionType" class name is a bit confusing here. The working code i have now, which works well: var featureCollectionType = WfsFactory.eINSTANCE.createFeatureCollectionType(); featureCollectionType.getFeature().add(featureCollection); var configuration = new WFSConfiguration(); var encoder = new Encoder(configuration); encoder.getNamespaces().declarePrefix("myprefix", "http://myuri"); try (var output = FastBufferedOutputStream.create(response.getOutputStream())) { encoder.encode(featureCollectionType, WFS.FeatureCollection, output); } If you can find a working unit test with the GML class doing GML 3.2 encoding i would like to see it, i could not find it. I totally agree with your statement about OpenGIS consortium making things too complicated, i guess there are too many parties involved that all want to have their say in these standards, my experience is that usually the 1.0 standards are better than the 2.0 version etc. that follow. Unfortunately, ArcGIS will not accept GeoJSON from a WFS implementation, at least i was told so by ESRI. Now that it works, all is well that ends well, adding the JSON output literally took me 5 minutes using Geotools ! Best Regards, Bart Adriaanse Software developer at Deltares.nl On Wed, 12 Apr 2023 at 18:01, Jody Garnett <jody.garn...@gmail.com> wrote: > Bart: > > One thing I could not see in your example is your schema? GML 3.2 is not > a document format - it is a choose your own adventure that you can use to > define your own document structure (for your simple features) as a schema > document. > > The GeoTools docs try to explain this and include examples for encoding > your simple feature type as an XSD document; and then using that XSD > document to configure a GMLConfiguration allowing your simple feature > collection to be written out to a document. > For WFS the XSD document comes from the DescribeFeatureType operation. > > I am afraid OGC makes things terribly complicated which is why a lot of > folks use GeoJSON when communicating with a WFS.... > Personally I think GML is a better format for archival or interchange > purposes where data integrity needs to be maintained and understood across > systems. > > In the example you provided the root element of the document was used as > WFS.FeatureCollection, and depends on "SampleFeature" being setup to extend > abstract feature. The WFS.FeatureCollection members are part of the > substitution group "AbstractFeature". > > The https://docs.geotools.org/stable/userguide/library/xml/geometry.html > snippet for writing out XSD was: > > SimpleFeatureType TYPE = DataUtilities.createType("location", > "geom:Point,name:String"); > > GML encode = new GML(Version.GML3); > encode.setBaseURL(new URL("http://localhost/")); > encode.setNamespace("location", "http://localhost/location.xsd"); > encode.encode(outputStream, TYPE); > > You can check your schema document when you have generated it to ensure > "SampleFeature" is setup in this manner so that the result can be encoded > as a WFS FeatureCollection. > > > -- > Jody Garnett > > > On Mon, Apr 3, 2023 at 4:11 AM Bart Adriaanse <bart.adriaa...@gmail.com> > wrote: > >> Hi there, >> >> I have been struggling with this for some time, even after investigating >> the source code of both Geotools and GeoServer i cannot seem to get this to >> work. >> >> I have a extremely simple WFS 1.0 interface in an application and need to >> update this to WFS 2.0 but i am stuck at encoding a SimpleFeatureCollection >> to GML 3.2 >> >> I would be very grateful if someone who is very familiar with Geotools >> can find some time to see what is wrong with my code, here is a copy of the >> Junit test i am trying to get to work: >> >> import org.geotools.data.DataUtilities; >> import org.geotools.data.collection.ListFeatureCollection; >> import org.geotools.data.simple.SimpleFeatureCollection; >> import org.geotools.feature.simple.SimpleFeatureBuilder; >> import org.geotools.feature.simple.SimpleFeatureTypeBuilder; >> import org.geotools.gml3.v3_2.GML; >> import org.geotools.gml3.v3_2.GMLConfiguration; >> import org.geotools.wfs.WFS; >> import org.geotools.xsd.Encoder; >> import org.junit.Assert; >> import org.junit.Test; >> import org.locationtech.jts.geom.Coordinate; >> import org.locationtech.jts.geom.GeometryFactory; >> import org.locationtech.jts.geom.Point; >> import org.opengis.feature.simple.SimpleFeature; >> import org.opengis.feature.simple.SimpleFeatureType; >> >> import javax.xml.namespace.QName; >> import java.io.ByteArrayOutputStream; >> import java.io.IOException; >> import java.nio.charset.StandardCharsets; >> >> public class EncodeGML3_2Example { >> >> @Test >> public void encodeGML32() { >> try { >> // Create a SimpleFeatureType >> SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder(); >> typeBuilder.setName("SampleFeature"); >> typeBuilder.add("geom", Point.class); >> typeBuilder.add("name", String.class); >> SimpleFeatureType featureType = typeBuilder.buildFeatureType(); >> >> // Create and add features to SimpleFeatureCollection >> ListFeatureCollection featureList = new ListFeatureCollection(featureType >> ); >> SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder( >> featureType); >> GeometryFactory geometryFactory = new GeometryFactory(); >> Point point1 = geometryFactory.createPoint(new Coordinate(10, 10)); >> featureBuilder.add(point1); >> featureBuilder.add("Point 1"); >> SimpleFeature feature1 = featureBuilder.buildFeature("1"); >> Point point2 = geometryFactory.createPoint(new Coordinate(20, 20)); >> featureBuilder.add(point2); >> featureBuilder.add("Point 2"); >> SimpleFeature feature2 = featureBuilder.buildFeature("2"); >> featureList.add(feature1); >> featureList.add(feature2); >> SimpleFeatureCollection featureCollection = DataUtilities.collection( >> featureList); >> >> // Encode the SimpleFeatureCollection as GML 3.2 >> GMLConfiguration gmlConfig = new GMLConfiguration(true); >> Encoder encoder = new Encoder(gmlConfig); >> encoder.setSchemaLocation("http://www.opengis.net/gml", " >> http://schemas.opengis.net/gml/3.2.1/gml.xsd"); >> encoder.setNamespaceAware(true); >> encoder.setRootElementType(WFS.FeatureCollection); >> String gml = encoder.encodeAsString(featureCollection, GML. >> FeatureCollection); >> >> Assert.assertTrue(!gml.isEmpty()); >> >> } catch (Exception e) { >> e.printStackTrace(); >> } >> } >> >> } >> _______________________________________________ >> GeoTools-GT2-Users mailing list >> GeoTools-GT2-Users@lists.sourceforge.net >> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users >> >
_______________________________________________ GeoTools-GT2-Users mailing list GeoTools-GT2-Users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users