adding kml import and export
[udig:corrupt-do_not_use.git] / plugins / eu.udig.tools.jgrass / src / eu / udig / tools / jgrass / kml / wizard / KmlImportWizard.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.lang.reflect.InvocationTargetException;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import net.refractions.udig.catalog.CatalogPlugin;
27 import net.refractions.udig.catalog.IGeoResource;
28 import net.refractions.udig.project.ui.ApplicationGIS;
29 import net.refractions.udig.ui.ExceptionDetailsDialog;
30 import net.refractions.udig.ui.PlatformGIS;
31
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.core.runtime.IStatus;
34 import org.eclipse.jface.operation.IRunnableWithProgress;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.jface.wizard.Wizard;
37 import org.eclipse.ui.INewWizard;
38 import org.eclipse.ui.IWorkbench;
39 import org.eclipse.ui.plugin.AbstractUIPlugin;
40 import org.geotools.data.FeatureStore;
41 import org.geotools.data.simple.SimpleFeatureCollection;
42 import org.opengis.feature.simple.SimpleFeature;
43 import org.opengis.feature.simple.SimpleFeatureType;
44
45 import eu.udig.tools.jgrass.JGrassToolsPlugin;
46 import eu.udig.tools.jgrass.kml.core.Kmlutils;
47
48 /**
49  * @author Andrea Antonello - www.hydrologis.com
50  */
51 public class KmlImportWizard extends Wizard implements INewWizard {
52
53     private KmlImportWizardPage mainPage;
54
55     public static boolean canFinish = false;
56
57     private final Map<String, String> params = new HashMap<String, String>();
58
59     public KmlImportWizard() {
60         super();
61     }
62
63     public void init( IWorkbench workbench, IStructuredSelection selection ) {
64         setWindowTitle("Kml file import");
65         setDefaultPageImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(JGrassToolsPlugin.PLUGIN_ID,
66                 "icons/icon_kml48.png")); //$NON-NLS-1$
67         setNeedsProgressMonitor(true);
68         mainPage = new KmlImportWizardPage("Kml file import", params); //$NON-NLS-1$
69     }
70
71     public void addPages() {
72         super.addPages();
73         addPage(mainPage);
74     }
75
76     public boolean canFinish() {
77         return super.canFinish() && canFinish;
78     }
79
80     public boolean performFinish() {
81
82         final File kmlFile = mainPage.getKmlFile();
83
84         /*
85          * run with backgroundable progress monitoring
86          */
87         IRunnableWithProgress operation = new IRunnableWithProgress(){
88
89             public void run( IProgressMonitor pm ) throws InvocationTargetException, InterruptedException {
90                 try {
91                     SimpleFeatureCollection collection = Kmlutils.kmlFile2FeatureCollection(kmlFile);
92
93                     IGeoResource resource = CatalogPlugin.getDefault().getLocalCatalog()
94                             .createTemporaryResource(collection.getSchema());
95
96                     FeatureStore<SimpleFeatureType, SimpleFeature> store = resource.resolve(FeatureStore.class, pm);
97                     store.addFeatures(collection);
98
99                     ApplicationGIS.addLayersToMap(ApplicationGIS.getActiveMap(), Collections.singletonList(resource), -1);
100                 } catch (Exception e) {
101                     e.printStackTrace();
102                     String message = "An error occurred during KML to feature layer conversion.";
103                     ExceptionDetailsDialog.openError(null, message, IStatus.ERROR, JGrassToolsPlugin.PLUGIN_ID, e);
104                 }
105             }
106         };
107
108         PlatformGIS.runInProgressDialog("Importing kml data", true, operation, true);
109
110         return true;
111     }
112 }