adding kml import and export
[udig:corrupt-do_not_use.git] / plugins / eu.udig.tools.jgrass / src / eu / udig / tools / jgrass / kml / wizard / KmlExportWizard.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.wizard;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.lang.reflect.InvocationTargetException;
23
24 import net.refractions.udig.catalog.IGeoResource;
25 import net.refractions.udig.ui.ExceptionDetailsDialog;
26 import net.refractions.udig.ui.PlatformGIS;
27
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.jface.operation.IRunnableWithProgress;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.jface.wizard.Wizard;
33 import org.eclipse.ui.IExportWizard;
34 import org.eclipse.ui.IWorkbench;
35 import org.eclipse.ui.plugin.AbstractUIPlugin;
36 import org.geotools.coverage.grid.GridCoverage2D;
37 import org.geotools.coverage.grid.io.AbstractGridFormat;
38 import org.geotools.coverage.grid.io.imageio.GeoToolsWriteParams;
39 import org.geotools.coverage.processing.Operations;
40 import org.geotools.data.FeatureStore;
41 import org.geotools.data.simple.SimpleFeatureSource;
42 import org.geotools.gce.arcgrid.ArcGridFormat;
43 import org.geotools.gce.arcgrid.ArcGridWriteParams;
44 import org.geotools.gce.arcgrid.ArcGridWriter;
45 import org.geotools.gce.geotiff.GeoTiffFormat;
46 import org.geotools.gce.geotiff.GeoTiffWriteParams;
47 import org.geotools.gce.geotiff.GeoTiffWriter;
48 import org.geotools.referencing.CRS;
49 import org.joda.time.format.DateTimeFormat;
50 import org.joda.time.format.DateTimeFormatter;
51 import org.opengis.coverage.grid.GridCoverage;
52 import org.opengis.parameter.GeneralParameterValue;
53 import org.opengis.parameter.ParameterValueGroup;
54 import org.opengis.referencing.FactoryException;
55 import org.opengis.referencing.crs.CoordinateReferenceSystem;
56 import org.opengis.referencing.operation.MathTransform;
57
58 import eu.udig.tools.jgrass.JGrassToolsPlugin;
59 import eu.udig.tools.jgrass.kml.core.Kmlutils;
60
61 /**
62  * @author Andrea Antonello (www.hydrologis.com)
63  */
64 public class KmlExportWizard extends Wizard implements IExportWizard {
65
66     public static boolean canFinish = false;
67     private KmlExportWizardPage mainPage;
68     public static DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyyMMddHHmm"); //$NON-NLS-1$
69
70     public KmlExportWizard() {
71         super();
72     }
73
74     public void init( IWorkbench workbench, IStructuredSelection selection ) {
75         setWindowTitle("Kml export");
76         setDefaultPageImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(JGrassToolsPlugin.PLUGIN_ID,
77                 "icons/export_wiz.png")); //$NON-NLS-1$
78         setNeedsProgressMonitor(true);
79
80         mainPage = new KmlExportWizardPage();
81     }
82
83     public void addPages() {
84         super.addPages();
85         addPage(mainPage);
86     }
87
88     public boolean performFinish() {
89         /*
90          * run with backgroundable progress monitoring
91          */
92         IRunnableWithProgress operation = new IRunnableWithProgress(){
93
94             public void run( IProgressMonitor pm ) throws InvocationTargetException, InterruptedException {
95
96                 IGeoResource geoResource = mainPage.getGeoResource();
97                 String filePath = mainPage.getFilePath();
98
99                 /*
100                  * finally do some processing
101                  */
102                 pm.beginTask("Exporting map...", IProgressMonitor.UNKNOWN);
103                 try {
104                     if (geoResource.canResolve(SimpleFeatureSource.class)) {
105                         SimpleFeatureSource featureStore = (SimpleFeatureSource) geoResource.resolve(SimpleFeatureSource.class,
106                                 pm);
107                         Kmlutils.writeKml(new File(filePath), featureStore.getFeatures());
108                     } else {
109                         throw new IOException("The selected resource is not a feature layer: " + geoResource.getTitle());
110                     }
111
112                 } catch (Exception e) {
113                     e.printStackTrace();
114                     String message = "An error occurred while exporting the resource: " + geoResource.getTitle();
115                     ExceptionDetailsDialog.openError(null, message, IStatus.ERROR, JGrassToolsPlugin.PLUGIN_ID, e);
116                 }
117                 pm.done();
118
119             }
120         };
121
122         PlatformGIS.runInProgressDialog("Exporting to kml...", true, operation, true);
123         return true;
124     }
125
126     public boolean canFinish() {
127         return super.canFinish() && canFinish;
128     }
129
130 }