adding kml import and export
[udig:corrupt-do_not_use.git] / plugins / eu.udig.tools.jgrass / src / eu / udig / tools / jgrass / kml / core / Kmlutils.java
1 /*
2  * JGrass - Free Open Source Java GIS http://www.jgrass.org 
3  * (C) HydroloGIS - www.hydrologis.com 
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package eu.udig.tools.jgrass.kml.core;
19
20 import java.io.BufferedWriter;
21 import java.io.ByteArrayOutputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileWriter;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import net.refractions.udig.project.ui.ApplicationGIS;
29
30 import org.geotools.data.simple.SimpleFeatureCollection;
31 import org.geotools.feature.FeatureCollection;
32 import org.geotools.feature.FeatureCollections;
33 import org.geotools.feature.FeatureIterator;
34 import org.geotools.feature.simple.SimpleFeatureBuilder;
35 import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
36 import org.geotools.feature.type.GeometryDescriptorImpl;
37 import org.geotools.feature.type.GeometryTypeImpl;
38 import org.geotools.geometry.jts.JTS;
39 import org.geotools.kml.KML;
40 import org.geotools.kml.KMLConfiguration;
41 import org.geotools.referencing.CRS;
42 import org.geotools.referencing.crs.DefaultGeographicCRS;
43 import org.geotools.xml.Encoder;
44 import org.geotools.xml.StreamingParser;
45 import org.opengis.feature.simple.SimpleFeature;
46 import org.opengis.feature.simple.SimpleFeatureType;
47 import org.opengis.feature.type.AttributeDescriptor;
48 import org.opengis.feature.type.GeometryDescriptor;
49 import org.opengis.feature.type.GeometryType;
50 import org.opengis.referencing.crs.CoordinateReferenceSystem;
51 import org.opengis.referencing.operation.MathTransform;
52
53 import com.vividsolutions.jts.geom.Geometry;
54
55 /**
56  * Utilities to convert kml to features and back (taken from geotools testcases).
57  * 
58  * @author Andrea Antonello (www.hydrologis.com)
59  */
60 public class Kmlutils {
61
62     @SuppressWarnings("nls")
63     public static final String[] IGNORED_ATTR = {"LookAt", "Style", "Region"};
64
65     /**
66      * Transform a kml file in a {@link SimpleFeatureCollection}.
67      * 
68      * @param kml the file to convert.
69      * @return trhe generated feature collection.
70      * @throws Exception
71      */
72     public static SimpleFeatureCollection kmlFile2FeatureCollection( File kml ) throws Exception {
73         StreamingParser parser = new StreamingParser(new KMLConfiguration(), new FileInputStream(kml), KML.Placemark);
74
75         SimpleFeatureCollection newCollection = FeatureCollections.newCollection();
76         int index = 0;
77         SimpleFeature f;
78         DefaultGeographicCRS crs = DefaultGeographicCRS.WGS84;
79         while( (f = (SimpleFeature) parser.parse()) != null ) {
80             Geometry geometry = (Geometry) f.getDefaultGeometry();
81
82             SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
83             b.setName(kml.getName());
84             b.setCRS(crs);
85             b.add("the_geom", Geometry.class);
86             SimpleFeatureType type = b.buildFeatureType();
87             SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
88             builder.addAll(new Object[]{geometry});
89             SimpleFeature feature = builder.buildFeature(type.getTypeName() + "." + index++); //$NON-NLS-1$
90             newCollection.add(feature);
91         }
92
93         return newCollection;
94     }
95
96     /**
97      * Writes the {@link FeatureCollection} to disk in KML format.
98      * 
99      * @param kmlFile the file to write.
100      * @param featureCollection the collection to transform.
101      * @throws Exception
102      */
103     public static void writeKml( File kmlFile, SimpleFeatureCollection featureCollection ) throws Exception {
104         CoordinateReferenceSystem epsg4326 = DefaultGeographicCRS.WGS84;
105         CoordinateReferenceSystem crs = featureCollection.getSchema().getCoordinateReferenceSystem();
106         MathTransform mtrans = CRS.findMathTransform(crs, epsg4326, true);
107
108         FeatureCollection<SimpleFeatureType, SimpleFeature> newCollection = FeatureCollections.newCollection();
109         FeatureIterator<SimpleFeature> featuresIterator = featureCollection.features();
110         while( featuresIterator.hasNext() ) {
111             SimpleFeature f = featuresIterator.next();
112             Geometry g = (Geometry) f.getDefaultGeometry();
113             if (!mtrans.isIdentity()) {
114                 g = JTS.transform(g, mtrans);
115             }
116             f.setDefaultGeometry(g);
117             newCollection.add(f);
118         }
119
120         Encoder encoder = new Encoder(new KMLConfiguration());
121         encoder.setIndenting(true);
122         ByteArrayOutputStream out = new ByteArrayOutputStream();
123         encoder.encode(newCollection, KML.kml, out);
124
125         String kmlString = new String(out.toByteArray());
126         BufferedWriter bW = null;
127         try {
128             bW = new BufferedWriter(new FileWriter(kmlFile));
129             bW.write(kmlString);
130         } finally {
131             if (bW != null)
132                 bW.close();
133         }
134     }
135
136 }