Hi Justin,

I created a small test app to convert shape to KML. Wheras it works with the conventional KML support, I do get an almost empty file when using the 2.2 support.

Peter


On 12/03/2012 04:13 PM, Peter Hopfgartner wrote:
On 12/03/2012 03:16 PM, Justin Deoliveira wrote:
Hey Peter,

This work is really just about parsing, and actually specifically being able to parse placemarks and kml feature data with ExtendedData. No work was done for encoding but it would be a relatively straight forward change. If you are interested in working on that I would be happy to help out if need be.

-Justin

Hi Justin,

I'll probably work on this in a couple of days and I'll be happily asking for help, then.

Regards,

Peter

On Mon, Dec 3, 2012 at 1:51 AM, Peter Hopfgartner <peter.hopfgart...@r3-gis.com <mailto:peter.hopfgart...@r3-gis.com>> wrote:

    On 11/29/2012 07:29 PM, Justin Deoliveira wrote:
    Hi all,

    Recently myself and a colleague of mine (Rob Marianski) worked
    on some improved parsing for KML. Including support for parsing
    KML 2.2 along with some of its advanced "custom data" features,
    ie "SchemaData" and "ExtendedData".

    The work is mostly constrained to the kml bindings which afaik
    don't see much use but there were a few core changes required,
    including adding a new PullParser to the xsd-core module.
    Written against the jaxp XMLStreamReader interface it should be
    a good replacement for STreamingParser. While this was not
    required for the KML work it seemed worth it to come up with a
    better implementation for stream parsing.

    Anyways, the changes have been pushed to master but i was hoping
    to backport to 8.x. So i opened a pull request for review:

    https://github.com/geotools/geotools/pull/67

    Thanks!

    -Justin

-- Justin Deoliveira
    OpenGeo - http://opengeo.org
    Enterprise support for open source geospatial.



    Hi Justin,

    out of curiosity: When transforming data to KML with ogr2ogr, all
    generic data attributes are included in ExtendedData. Would your
    changes allow this too, when encoding for KML?

    Regards,

    Peter


    
------------------------------------------------------------------------------
    Keep yourself connected to Go Parallel:
    VERIFY Test and improve your parallel project with help from experts
    and peers.http://goparallel.sourceforge.net


    _______________________________________________
    GeoTools-Devel mailing list
    GeoTools-Devel@lists.sourceforge.net  
<mailto:GeoTools-Devel@lists.sourceforge.net>
    https://lists.sourceforge.net/lists/listinfo/geotools-devel

package org.geotools.prototypes;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.store.ReprojectingFeatureCollection;
import org.geotools.kml.KML;
import org.geotools.kml.KMLConfiguration;
import org.geotools.referencing.CRS;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.geotools.xml.Encoder;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CRSAuthorityFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

/**
 * Prompts the user for a SHP and transform it to KML.
 */
public class Shp2Kml {

	/**
	 * Prompts the user for a shapefile
	 */
	public static void main(String[] args) throws Exception {
		File file = null;
		if (args.length > 0) {
			String fileName;
			fileName = args[0];
			file = new File(fileName);
		} else {
			file = JFileDataStoreChooser.showOpenFile("shp", null);
		}

		if (file == null) {
			return;
		}

		FileDataStore store = FileDataStoreFinder.getDataStore(file);
		SimpleFeatureSource featureSource = store.getFeatureSource();

		SimpleFeatureCollection collection = featureSource.getFeatures();

		String path = file.getAbsolutePath();
		String newPath = path.substring(0, path.length() - 4) + ".v21.kml";
		File kmlFile = new File(newPath);

		Shp2Kml shp2kml = new Shp2Kml();
		shp2kml.writeToKML(new FileOutputStream(kmlFile), collection);
	}

	public void writeToKML(OutputStream lFileOutputStream,
			SimpleFeatureCollection collection) throws IOException,
			NoSuchAuthorityCodeException, FactoryException {
		
		CRSAuthorityFactory factory = CRS.getAuthorityFactory(true);
		CoordinateReferenceSystem targetCRS = factory
				.createCoordinateReferenceSystem("EPSG:4326");
		SimpleFeatureCollection wgs84Collection = new ReprojectingFeatureCollection(collection, targetCRS);

		Encoder lEncoder = new Encoder(new KMLConfiguration());
		lEncoder.setIndenting(true);
		lEncoder.encode(wgs84Collection, KML.kml, lFileOutputStream);
		lFileOutputStream.close();
	}

}
package org.geotools.prototypes;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.store.ReprojectingFeatureCollection;
import org.geotools.kml.v22.KML;
import org.geotools.kml.v22.KMLConfiguration;
import org.geotools.referencing.CRS;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.geotools.xml.Encoder;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CRSAuthorityFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

/**
 * Prompts the user for a SHP and transform it to KML.
 */
public class Shp2Kml22 {

	/**
	 * Prompts the user for a shapefile
	 */
	public static void main(String[] args) throws Exception {
		File file = null;
		if (args.length > 0) {
			String fileName;
			fileName = args[0];
			file = new File(fileName);
		} else {
			file = JFileDataStoreChooser.showOpenFile("shp", null);
		}

		if (file == null) {
			return;
		}

		FileDataStore store = FileDataStoreFinder.getDataStore(file);
		SimpleFeatureSource featureSource = store.getFeatureSource();

		SimpleFeatureCollection collection = featureSource.getFeatures();

		String path = file.getAbsolutePath();
		String newPath = path.substring(0, path.length() - 4) + ".v22.kml";
		File kmlFile = new File(newPath);

		Shp2Kml22 shp2kml = new Shp2Kml22();
		shp2kml.writeToKML(new FileOutputStream(kmlFile), collection);
	}

	public void writeToKML(OutputStream lFileOutputStream,
			SimpleFeatureCollection collection) throws IOException,
			NoSuchAuthorityCodeException, FactoryException {
		
		CRSAuthorityFactory factory = CRS.getAuthorityFactory(true);
		CoordinateReferenceSystem targetCRS = factory
				.createCoordinateReferenceSystem("EPSG:4326");
		SimpleFeatureCollection wgs84Collection = new ReprojectingFeatureCollection(collection, targetCRS);

		Encoder lEncoder = new Encoder(new KMLConfiguration());
		lEncoder.setIndenting(true);
		lEncoder.encode(wgs84Collection, KML.kml, lFileOutputStream);
		lFileOutputStream.close();
	}

}

Attachment: states.v22.kml
Description: application/vnd.google-earth.kml

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
GeoTools-Devel mailing list
GeoTools-Devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to