Hello list,

Unfortunately, I have to use an old version of Geotools, as my client is 
reliant on software that is build in java 8 and cannot upgrade. I think the 
latest version of Geotools that was working with java 8 is 20.5.
Now I have the following problem and have not been able to find a solution in 
the last week, searching various forums and this mailing list.
I have a WKT (or Geometry or SimpleFeature) and need to convert it to GML (to 
submit into a WFS insert transaction).
I have tried using the encoder in different ways:

  *   Feeding it either a SimpleFeature, Geometry derived from SimpleFeature or 
Geometry derived from WKT.
  *   Using GML._Geometry,  GML.MultiPolygon or various other QNames to encode 
the feature/geometry.
I get depending on the above, a variety of errors:
If I try to encode a Geometry I usually get the error that gid property is 
missing:
Exception in thread "main" java.lang.RuntimeException: Failed to get property: 
gid
      at 
org.geotools.xml.impl.GetPropertyExecutor.visit(GetPropertyExecutor.java:105)
      at 
org.geotools.xml.impl.BindingWalker$BindingExecutionChain.execute(BindingWalker.java:220)
      at org.geotools.xml.impl.BindingWalker.walk(BindingWalker.java:186)
      at 
org.geotools.xml.impl.BindingVisitorDispatch.walk(BindingVisitorDispatch.java:47)
      at 
org.geotools.xml.impl.BindingVisitorDispatch.walk(BindingVisitorDispatch.java:32)
      at org.geotools.xml.Encoder.encode(Encoder.java:883)
      at org.geotools.xml.Encoder.encode(Encoder.java:584)
      at org.geotools.tutorial.App.main(App.java)
Caused by: java.lang.ClassCastException: org.locationtech.jts.geom.MultiPolygon 
incompatible with com.vividsolutions.jts.geom.Geometry
      at 
org.geotools.gml3.bindings.AbstractGeometryTypeBinding.getProperty(AbstractGeometryTypeBinding.java:129)
      at 
org.geotools.xml.impl.GetPropertyExecutor.visit(GetPropertyExecutor.java:103)
      ... 7 more

Code:
String woPolygonWKT = "MULTIPOLYGON (((4 0, 2 2, 4 4, 6 2, 4 0)), ((2 1, 
1.9807852804032304 0.8049096779838718, 1.9238795325112867 0.6173165676349102, 
1.8314696123025453 0.4444297669803978, 1.7071067811865475 0.2928932188134525, 
1.5555702330196022 0.1685303876974548, 1.3826834323650898 0.0761204674887133, 
1.1950903220161284 0.0192147195967696, 1 0, 0.8049096779838718 
0.0192147195967696, 0.6173165676349103 0.0761204674887133, 0.444429766980398 
0.1685303876974547, 0.2928932188134525 0.2928932188134524, 0.1685303876974547 
0.4444297669803978, 0.0761204674887132 0.6173165676349106, 0.0192147195967695 
0.8049096779838723, 0 1.0000000000000007, 0.0192147195967698 
1.1950903220161293, 0.0761204674887137 1.382683432365091, 0.1685303876974555 
1.5555702330196035, 0.2928932188134536 1.7071067811865488, 0.4444297669803993 
1.8314696123025462, 0.6173165676349122 1.9238795325112874, 0.8049096779838739 
1.9807852804032309, 1.0000000000000024 2, 1.1950903220161309 1.98078528040323, 
1.3826834323650925 1.9238795325112856, 1.5555702330196048 1.8314696123025436, 
1.70710678118655 1.7071067811865452, 1.8314696123025471 1.5555702330195993, 
1.9238795325112883 1.3826834323650863, 1.9807852804032313 1.1950903220161244, 2 
1)))";
            WKTReader wktR = new WKTReader();
          Geometry geom2;
            try {
                  geom2 = wktR.read(woPolygonWKT);
                  System.out.println(geom2.toString());
                  //geom2.setSRID(4326);
                  org.geotools.xml.Configuration configuration = new 
org.geotools.gml3.GMLConfiguration();
                org.geotools.xml.Encoder encoder = new 
org.geotools.xml.Encoder( configuration );
                ByteArrayOutputStream out = new ByteArrayOutputStream();

                try {
                  System.out.println("trying to encode");
                        encoder.encode(geom2, 
org.geotools.gml3.GML.MultiPolygon,out);
                  } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
                  System.out.println(out.toString());
            } catch (ParseException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
            }

I have also tried:
      public String convertSimpleFeatureToGML(SimpleFeature simpleFeature) {
          GMLConfiguration gmlConfiguration = new GMLConfiguration(true);
            Geometry geom = (Geometry) simpleFeature.getDefaultGeometry();

          Encoder encoder = new Encoder(gmlConfiguration);
          ByteArrayOutputStream boas = new ByteArrayOutputStream();
          try {
              encoder.encode(geom, GML.MultiPolygon, boas);
              return boas.toString();
          } catch (IOException e) {
              e.printStackTrace();
              return "";
          }

      }
And then calling this function with a simpleFeature that contains the 
multipolygon described above in the WKT.

If anyone can help me, it would be greatly appreciated.

Kind Regards / Met vriendelijke groet,
Asset People B.V.

Mieke Zwart

M. +31 (0) 612 967 788
E. mieke.zw...@assetpeople.com<mailto:mieke.zw...@assetpeople.com>

_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to