5

What is the best way to programatically convert WKT String to GML String? Every reasonable library (at best available in some Maven repository) can be used.

Till now I came up with this solution using JTS. Its written in Java:

public String wktToGml(String wktGeometry) {
   WKTReader wktR = new WKTReader();
   Geometry geom = wktR.read(wktGeometry);

   // write JTS to string
   GMLWriter gmlW = new GMLWriter(true);
   String gml = gmlW.write(geom);
   return gml;
}

But I'm not sure if it's correct and most effective way. There are probably some problems with LF (linefeed) symbols. Probably there should be also solution using geotools library.

I'm new to this field, I'm trying to gain some overview.

  • do you want a full GML document or just the gml:Polygon (or whatever) fragment? – Ian Turton Mar 8 '13 at 10:07
  • probably just gml:Polygon – Infragile Mar 8 '13 at 10:12
  • 1
    then you are probably fine with what you have – Ian Turton Mar 8 '13 at 10:21
1

A slightly cleaner way of doing this with the option of GML 2 or 3 output is:

public class WKTToGML {

  public static void main(String[] args) throws IOException, ParseException {
    System.out.println(WKTToGML2("POLYGON((1 0,1 1,0 1,0 0,1 0))"));
    System.out.println(WKTToGML3("POLYGON((1 0,1 1,0 1,0 0,1 0))"));

  }

  static public String WKTToGML2(String wkt) throws IOException, ParseException {
    WKTReader wktR = new WKTReader();
    Geometry geom = wktR.read(wkt);

    org.geotools.xml.Configuration configuration = new org.geotools.gml2.GMLConfiguration();
    org.geotools.xml.Encoder encoder = new org.geotools.xml.Encoder( configuration );
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    encoder.encode(geom, GML._Geometry,out);
    return out.toString();

  }
  static public String WKTToGML3(String wkt) throws IOException, ParseException {
    WKTReader wktR = new WKTReader();
    Geometry geom = wktR.read(wkt);

    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();
    encoder.encode(geom, org.geotools.gml3.GML.geometryMember,out);
    return out.toString();

  }
}

gives the following output:

<?xml version="1.0" encoding="UTF-8"?><gml:Polygon xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>1.0,0.0 1.0,1.0 0.0,1.0 0.0,0.0 1.0,0.0</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon>
<?xml version="1.0" encoding="UTF-8"?><gml:geometryMember xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" xmlns:sch="http://www.ascc.net/xml/schematron" xmlns:xlink="http://www.w3.org/1999/xlink"><gml:Polygon srsDimension="2"><gml:exterior><gml:LinearRing srsDimension="2"><gml:posList>1.0 0.0 1.0 1.0 0.0 1.0 0.0 0.0 1.0 0.0</gml:posList></gml:LinearRing></gml:exterior></gml:Polygon></gml:geometryMember>

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.