Encoding to KML

2 messages Options
Embed this post
Permalink
José Carlos Martínez

Encoding to KML

Reply Threaded More More options
Print post
Permalink
Hi everyone,
This is my first email to the list. First off all, I would like to say
thanks to all the people who are involved in this fantastic project.

I am trying to convert from a GeoTools geometry to KML, but I can not
find any example about that. I do not know if Geotools can encode KML or
just decode though.

Could anyone give me a brief example about how to do this?
In particular I want to convert from a JTS geometry to a KML text.

Thanks a lot,
Jose Martinez







------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
mbedward

Re: Encoding to KML

Reply Threaded More More options
Print post
Permalink
Hi José

Thanks for your nice words. Praise is always gratefully accepted :-)

For KML, the gt-xsd-kml module is your friend.  Here's a little
example that works directly with a JTS Geometry object...

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.io.WKTReader;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.kml.KML;
import org.geotools.kml.KMLConfiguration;
import org.geotools.xml.Encoder;

public class KMLExample {

    public static void main(String[] args) throws Exception {
        /*
         * Create a triangular polygon
         */
        GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null);
        WKTReader reader = new WKTReader(gf);
        Geometry triangle = reader.read("POLYGON((0 0, 10 20, 20 0, 0 0))");

        /*
         * Encode the polygon as KML and dump it to the console
         */
        Encoder encoder = new Encoder(new KMLConfiguration());
        encoder.setIndenting(true);
        encoder.encode(triangle, KML.Polygon, System.out);
    }
}

You should see the following output:

<?xml version="1.0" encoding="UTF-8"?>
<kml:Polygon xmlns:kml="http://earth.google.com/kml/2.1">
    <kml:outerBoundaryIs>
        <kml:LinearRing>
            <kml:coordinates>0.0,0.0 10.0,20.0 20.0,0.0
0.0,0.0</kml:coordinates>
        </kml:LinearRing>
    </kml:outerBoundaryIs>
</kml:Polygon>

Hope that helps.

Michael

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users