Hi,

i twiddled a little with the examples from the Geotools homepage to encode a 
feature collection to GML. Using the example code for GML2 encoding worked 
fine, using GML3 does not work for me, there is neither output from the GML3 
encoder nor any hint whats wrong. The source code is at the end of the mail. It 
would be nice, if someone could take a look on the code and give me a hint, why 
the encoding to GML3 does not work.

Thanks in advance,
Oliver


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import org.geotools.geometry.jts.WKTReader2;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.DataUtilities;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.SchemaException;
import org.geotools.GML;
import org.geotools.GML.Version;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.opengis.feature.simple.SimpleFeatureType;

import com.vividsolutions.jts.io.ParseException;

public class GMLExample {
        public static void main(String [] args) {
                encodeGML2();
                encodeGML3();
        }
        
        public static void encodeGML2() {
                try {
                        File locationFile = new File("location.xsd");
                        
                        locationFile = locationFile.getCanonicalFile();
                        locationFile.createNewFile();
                        
                        URL locationURL = locationFile.toURI().toURL();
                        URL baseURL = 
locationFile.getParentFile().toURI().toURL();
                        
                        FileOutputStream xsd = null;
                        
                        xsd = new FileOutputStream(locationFile);
                        
                        SimpleFeatureType TYPE = 
DataUtilities.createType("location", "geom:Point,name:String");
                        
                        GML encode = new GML(Version.GML2);
                        encode.setBaseURL(baseURL);
                        encode.setNamespace("location", 
locationURL.toExternalForm());
                        encode.encode(xsd, TYPE);
                        
                        xsd.close();
                        
                        SimpleFeatureCollection collection = 
FeatureCollections.newCollection();
                        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.WFS1_0);
                        encode2.setBaseURL(baseURL);
                        encode2.setNamespace("location", "location.xsd");
                        encode2.encode(xml, collection);
                        
                        xml.close();
                        
                        System.out.println("GML2");
                        System.out.println(xml.toString());
                } catch (IOException ioe) {
                        // TODO Auto-generated catch block
                        ioe.printStackTrace();
                } catch (SchemaException se) {
                        // TODO Auto-generated catch block
                        se.printStackTrace();
                } catch (ParseException pe) {
                        // TODO Auto-generated catch block
                        pe.printStackTrace();
                }
        }
        
        public static void encodeGML3() {
                try {
                        File locationFile = new File("location.xsd");
                
                        locationFile = locationFile.getCanonicalFile();
                        locationFile.createNewFile();
                        
                        URL locationURL = locationFile.toURI().toURL();
                        URL baseURL = 
locationFile.getParentFile().toURI().toURL();
                        
                        FileOutputStream xsd = null;
                        
                        xsd = new FileOutputStream(locationFile);
                        
                        SimpleFeatureType TYPE = null;
                        
                        TYPE = DataUtilities.createType("location", 
"geom:Point,name:String");
                        
                        GML encode = new GML(Version.GML3);
                        encode.setBaseURL(baseURL);
                        encode.setNamespace("location", 
locationURL.toExternalForm());
                        encode.encode(xsd, TYPE);
                        
                        xsd.close();
                        
                        SimpleFeatureCollection collection = 
FeatureCollections.newCollection();
                        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.GML3);
                        encode2.setBaseURL(baseURL);
                        encode2.setNamespace("location", "location.xsd");
                        encode2.encode(xml, collection);
                        
                        xml.close();
                        System.out.println("GML3");
                        System.out.println(xml.toString());
                } catch (IOException ioe) {
                        // TODO Auto-generated catch block
                        ioe.printStackTrace();
                } catch (ParseException pe) {
                        // TODO Auto-generated catch block
                        pe.printStackTrace();
                } catch (SchemaException se) {
                        // TODO Auto-generated catch block
                        se.printStackTrace();
                }
        }
}

------------------------------------------------------------------------------
Malware Security Report: Protecting Your Business, Customers, and the 
Bottom Line. Protect your business and customers by understanding the 
threat from malware and how it can impact your online business. 
http://www.accelacomm.com/jaw/sfnl/114/51427462/
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to