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

Reply via email to