Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'd like to write GML using Geotools. Unfortunately, I can't find documentation on a GML Writer (except for this one from 2006: http://docs.codehaus.org/display/GEOTOOLS/WFS+++GML+DataStore).

Could you point me at documentation/examples?

share|improve this question
add comment

3 Answers 3

I will be trying to migrate the geotools documentation to a different technology (other than wiki) so that the code examples do not go out of date.

Update this is now done (I gathered things up so all the geometry examples are together):

Here is a complete example from that page:

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

File locationFile = new File("location.xsd");
locationFile = locationFile.getCanonicalFile();
locationFile.createNewFile();

URL locationURL = locationFile.toURI().toURL();
URL baseURL = locationFile.getParentFile().toURI().toURL();

FileOutputStream xsd = new FileOutputStream(locationFile);

GML encode = new GML(Version.GML2);
encode.setBaseURL(baseURL);
encode.setNamespace("location", locationURL.toExternalForm());
encode.encode(xsd, TYPE);

xsd.close();

SimpleFeatureCollection collection = FeatureCollections.newCollection("internal");
WKTReader2 wkt = new WKTReader2();

collection.add(SimpleFeatureBuilder.build(TYPE, new Object[] { wkt.read("POINT (1 2)"),"name1" }, null));
collection.add(SimpleFeatureBuilder.build(TYPE, new Object[] { wkt.read("POINT (4 4)"),"name2" }, null));

ByteArrayOutputStream xml = new ByteArrayOutputStream();

GML encode2 = new GML(Version.GML2);
encode2.setBaseURL(baseURL);
encode2.setNamespace("location", "location.xsd");
encode2.encode(out2, collection);

xml.close();

String gml = xml.toString();

Additional examples of how to use the 4 different GML parsing technologies is the test cases included with the source code.

  1. SAX
  2. DOM
  3. GTXML version 1.x (used for GML2 in WFSDataStore VERSION=1.0)
  4. GTXML version 4.x (used for everything else now )

The two GTXML technologies are basically a combination of the best part of the SAX parser with the ability to figure out which fragment of code (called a binding) to use to parse each element as it comes in (based on looking the element up in the schema).

share|improve this answer
    
I am getting the following exception when trying to encode a SimpleFeatureCollection using the code above. "java.lang.IllegalStateException: Cannot encode a feature collection using GML2 (only WFS)". I am using 8.3, any idea? –  Thomas Mar 13 '13 at 11:41
add comment

You can also look at http://svn.osgeo.org/geotools/trunk/modules/library/xml/src/test/java/org/geotools/GMLTest.java to see how the tests do it. The key section seems to be:

GML encode2 = new GML(Version.GML2);
    encode2.setBaseURL(baseURL);
    encode2.setNamespace("location", "location.xsd");
    encode2.encode(out2, collection);

    out.close();

Where collection is a featureCollection.

share|improve this answer
add comment

Try:

//create the encoder with the gml 2.0 configuration
org.geotools.xml.Configuration configuration = new org.geotools.gml2.GMLConfiguration();
org.geotools.xml.Encoder encoder = new org.geotools.xml.Encoder( configuration );

//output stream to serialize to
OutputStream xml = ...

//encode
encoder.encode( featureCollection, new QName( "http://www.geotools.org/test", "featureType1"));

Documentation:

share|improve this answer
    
Correct link, wrong code sample? ;) ... Guess you meant org.geotools.xml.Encoder and not parser –  underdark Nov 29 '10 at 15:31
    
yes as above. flaky internet day... –  Mapperz Nov 29 '10 at 19:55
    
I did [a copy/paste] error day ;) –  Mapperz Nov 29 '10 at 22:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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