1

I want to use GeoTools to convert collection of SimpleFeature objects to GML 3.2.1 geometry.

However, I'm dealing with this:

  • Creation of XSD works with both GML 2 and 3.
  • Creation of GML works only with GML 2.
  • With version 3, it creates empty string.

This is the piece of code:

    import org.geotools.GML;
    import org.geotools.data.DataUtilities;
    import org.geotools.data.collection.ListFeatureCollection;
    import org.geotools.feature.simple.SimpleFeatureBuilder;
    import org.geotools.geometry.jts.WKTReader2;
    import org.opengis.feature.simple.SimpleFeature;
    import org.opengis.feature.simple.SimpleFeatureType;

...

    SimpleFeatureType featureType = DataUtilities.createType("location", "geom:Point,name:String");

    File xsdFile = new File("C:/Dev/VTS/GML_samples/location.xsd");
    xsdFile = xsdFile.getCanonicalFile();
    xsdFile.createNewFile();
    URL xsdURL = xsdFile.toURI().toURL();
    URL baseURL = xsdFile.getParentFile().toURI().toURL();

    // Creation of XSD file using GML 3 works:
    GML xsdEncoder = new GML(GML.Version.GML3);
    FileOutputStream xsd = new FileOutputStream(xsdFile);
    xsdEncoder.setBaseURL(baseURL);
    xsdEncoder.setNamespace("location", xsdURL.toExternalForm());
    xsdEncoder.encode(xsd, featureType);
    xsd.close();

    // Simple Feature collection:
    WKTReader2 wkt = new WKTReader2();
    List<SimpleFeature> collection = new LinkedList<SimpleFeature>();
    collection.add(SimpleFeatureBuilder.build(featureType, new Object[] { wkt.read("POINT (1 2)"),"name1" }, null));
    collection.add(SimpleFeatureBuilder.build(featureType, new Object[] { wkt.read("POINT (4 4)"),"name2" }, null));

    // Creation of GML file using GML 3 doesn't work. It does work with GML 2 though:
    // GML gmlEncoder = new GML(GML.Version.GML2); // this works
    GML gmlEncoder = new GML(GML.Version.GML3); // this doesn't work
    ByteArrayOutputStream xml = new ByteArrayOutputStream();
    gmlEncoder.setLegacy(true);
    gmlEncoder.setBaseURL(baseURL);
    gmlEncoder.setNamespace("location", "location.xsd");
    gmlEncoder.encode(xml,  new ListFeatureCollection(featureType, collection));
    xml.close();

    System.out.println(xml.toString());
1

It seems that the GML encoder silently ignores Version.GML3 and thus you get no output! I've filed a bug report and put in a PR to throw an exception if this happens.

If you want to write GML3 out you should use Version.WFS1_1 instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.