adding raster map patch to popup menus
[udig:jgrass.git] / plugins / eu.hydrologis.jgrass.ui / src / eu / hydrologis / jgrass / ui / popups / PatchMapsAction.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.hydrologis.jgrass.ui.popups;
19
20 import java.io.File;
21 import java.lang.reflect.InvocationTargetException;
22 import java.util.ArrayList;
23 import java.util.List;
24
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.action.IAction;
31 import org.eclipse.jface.dialogs.InputDialog;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.operation.IRunnableWithProgress;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.Menu;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.ui.IObjectActionDelegate;
41 import org.eclipse.ui.IWorkbenchPart;
42 import org.eclipse.ui.IWorkbenchWindow;
43 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
44 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate;
45 import org.eclipse.ui.PlatformUI;
46 import org.geotools.coverage.grid.GridEnvelope2D;
47 import org.geotools.coverage.grid.GridGeometry2D;
48 import org.geotools.coverage.grid.io.AbstractGridFormat;
49 import org.geotools.gce.grassraster.JGrassConstants;
50 import org.geotools.gce.grassraster.JGrassMapEnvironment;
51 import org.geotools.gce.grassraster.JGrassRegion;
52 import org.geotools.geometry.DirectPosition2D;
53 import org.geotools.geometry.Envelope2D;
54 import org.geotools.geometry.jts.ReferencedEnvelope;
55 import org.geotools.parameter.Parameter;
56 import org.jgrasstools.gears.io.grasslegacy.modules.GrassMosaicLegacy;
57 import org.opengis.coverage.grid.GridCoverageReader;
58 import org.opengis.geometry.Envelope;
59 import org.opengis.parameter.GeneralParameterValue;
60 import org.opengis.referencing.crs.CoordinateReferenceSystem;
61
62 import eu.hydrologis.jgrass.ui.utils.EclipseProgressMonitorAdapter;
63 import eu.udig.catalog.jgrass.JGrassPlugin;
64 import eu.udig.catalog.jgrass.core.JGrassMapGeoResource;
65 import eu.udig.catalog.jgrass.utils.JGrassCatalogUtilities;
66
67 /**
68  * Action that patches grass rasters.
69  * 
70  * @author Andrea Antonello (www.hydrologis.com)
71  */
72 public class PatchMapsAction implements IObjectActionDelegate, IWorkbenchWindowActionDelegate, IWorkbenchWindowPulldownDelegate {
73
74     IStructuredSelection selection = null;
75
76     public void setActivePart( IAction action, IWorkbenchPart targetPart ) {
77     }
78
79     public void run( IAction action ) {
80
81         IRunnableWithProgress operation = new IRunnableWithProgress(){
82             public void run( final IProgressMonitor pm ) throws InvocationTargetException, InterruptedException {
83                 Display.getDefault().syncExec(new Runnable(){
84                     public void run() {
85
86                         final Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
87
88                         final List< ? > toList = selection.toList();
89                         if (toList.size() < 1) {
90                             MessageDialog.openWarning(shell, "Mosaic Warning",
91                                     "To use the mosaic command, at least two maps have to be supplied.");
92                             return;
93                         }
94
95                         InputDialog iDialog = new InputDialog(shell, "Mosaic map name",
96                                 "Please enter the name for the patched map", "patched", null);
97                         iDialog.open();
98                         String patchedName = iDialog.getValue();
99                         if (patchedName != null && patchedName.length() > 0) {
100                             patchedName = patchedName.replaceAll("\\s+", "_");
101                         } else {
102                             return;
103                         }
104
105                         try {
106                             List<File> mapsList = new ArrayList<File>();
107                             JGrassMapEnvironment mapEnvironment = null;
108                             for( Object object : toList ) {
109                                 if (object instanceof JGrassMapGeoResource) {
110                                     JGrassMapGeoResource mr = (JGrassMapGeoResource) object;
111                                     File mapFile = mr.getMapFile();
112                                     mapsList.add(mapFile);
113                                     if (mapEnvironment == null) {
114                                         mapEnvironment = mr.getjGrassMapEnvironment();
115                                     }
116
117                                 }
118                             }
119
120                             GrassMosaicLegacy mosaic = new GrassMosaicLegacy();
121                             mosaic.inGeodataFiles = mapsList;
122                             mosaic.pm = new EclipseProgressMonitorAdapter(pm);
123                             File newFile = new File(mapEnvironment.getCellFolder(), patchedName);
124                             mosaic.outFile = newFile.getAbsolutePath();
125                             JGrassRegion region = mapEnvironment.getActiveRegion();
126                             mosaic.pRes = region.getNSResolution();
127                             mosaic.process();
128                             File mapsetFile = mapEnvironment.getMAPSET();
129                             JGrassCatalogUtilities.addMapToCatalog(mapsetFile.getParent(), mapsetFile.getName(), patchedName,
130                                     JGrassConstants.GRASSBINARYRASTERMAP);
131                         } catch (Exception e) {
132                             String message = "An error occurred while exporting the maps.";
133                             ExceptionDetailsDialog.openError("ERROR", message, IStatus.ERROR, JGrassPlugin.PLUGIN_ID, e);
134                         }
135
136                     }
137                 });
138             }
139         };
140
141         PlatformGIS.runInProgressDialog("Patch maps...", true, operation, true);
142
143     }
144
145     /**
146     * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
147     *      org.eclipse.jface.viewers.ISelection)
148     */
149     public void selectionChanged( IAction action, ISelection selection ) {
150
151         if (selection instanceof IStructuredSelection)
152             this.selection = (IStructuredSelection) selection;
153     }
154
155     /*
156     * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
157     */
158     public void dispose() {
159     }
160
161     /*
162     * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
163     */
164     public void init( IWorkbenchWindow window ) {
165         // do nothing
166     }
167
168     /*
169     * @see org.eclipse.ui.IWorkbenchWindowPulldownDelegate#getMenu(org.eclipse.swt.widgets.Control)
170     */
171     public Menu getMenu( Control parent ) {
172         return null;
173     }
174     /**
175      * Utility method to create read parameters for {@link GridCoverageReader} 
176      * 
177      * @param width the needed number of columns.
178      * @param height the needed number of columns.
179      * @param north the northern boundary.
180      * @param south the southern boundary.
181      * @param east the eastern boundary.
182      * @param west the western boundary.
183      * @param crs the {@link CoordinateReferenceSystem}. Can be null, even if it should not.
184      * @return the {@link GeneralParameterValue array of parameters}.
185      */
186     public static GeneralParameterValue[] createGridGeometryGeneralParameter( int width, int height, double north, double south,
187             double east, double west, CoordinateReferenceSystem crs ) {
188         GeneralParameterValue[] readParams = new GeneralParameterValue[1];
189         Parameter<GridGeometry2D> readGG = new Parameter<GridGeometry2D>(AbstractGridFormat.READ_GRIDGEOMETRY2D);
190         GridEnvelope2D gridEnvelope = new GridEnvelope2D(0, 0, width, height);
191         Envelope env;
192         if (crs != null) {
193             env = new ReferencedEnvelope(west, east, south, north, crs);
194         } else {
195             DirectPosition2D minDp = new DirectPosition2D(west, south);
196             DirectPosition2D maxDp = new DirectPosition2D(east, north);
197             env = new Envelope2D(minDp, maxDp);
198         }
199         readGG.setValue(new GridGeometry2D(gridEnvelope, env));
200         readParams[0] = readGG;
201
202         return readParams;
203     }
204
205 }