|
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
|
2 |
*
|
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
|
4 |
* of the Valencian Government (CIT)
|
|
5 |
*
|
|
6 |
* This program is free software; you can redistribute it and/or
|
|
7 |
* modify it under the terms of the GNU General Public License
|
|
8 |
* as published by the Free Software Foundation; either version 2
|
|
9 |
* of the License, or (at your option) any later version.
|
|
10 |
*
|
|
11 |
* This program is distributed in the hope that it will be useful,
|
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 |
* GNU General Public License for more details.
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License
|
|
17 |
* along with this program; if not, write to the Free Software
|
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
19 |
* MA 02110-1301, USA.
|
|
20 |
*
|
|
21 |
*/
|
|
22 |
package org.gvsig.raster.reproject.app;
|
|
23 |
|
|
24 |
import java.io.File;
|
|
25 |
import java.util.HashMap;
|
|
26 |
import java.util.Map;
|
|
27 |
|
|
28 |
import org.cresques.cts.IProjection;
|
|
29 |
import org.gvsig.app.prepareAction.PrepareContext;
|
|
30 |
import org.gvsig.app.prepareAction.PrepareContextView;
|
|
31 |
import org.gvsig.app.prepareAction.PrepareDataStoreParameters;
|
|
32 |
import org.gvsig.app.prepareAction.PrepareLayer;
|
|
33 |
import org.gvsig.fmap.crs.CRSFactory;
|
|
34 |
import org.gvsig.fmap.dal.DALLocator;
|
|
35 |
import org.gvsig.fmap.dal.DataManager;
|
|
36 |
import org.gvsig.fmap.dal.DataStoreParameters;
|
|
37 |
import org.gvsig.fmap.dal.coverage.RasterLocator;
|
|
38 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
|
|
39 |
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
|
|
40 |
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
|
|
41 |
import org.gvsig.fmap.dal.coverage.util.CRSUtils;
|
|
42 |
import org.gvsig.fmap.mapcontext.MapContextLocator;
|
|
43 |
import org.gvsig.fmap.mapcontext.MapContextManager;
|
|
44 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
|
|
45 |
import org.gvsig.fmap.mapcontext.layers.FLayer;
|
|
46 |
import org.gvsig.raster.algorithm.RasterBaseAlgorithmLibrary;
|
|
47 |
import org.gvsig.raster.algorithm.process.IProcessActions;
|
|
48 |
import org.gvsig.raster.algorithm.process.ProcessException;
|
|
49 |
import org.gvsig.raster.algorithm.process.RasterProcess;
|
|
50 |
import org.gvsig.raster.fmap.layers.DefaultFLyrRaster;
|
|
51 |
import org.gvsig.raster.fmap.layers.FLyrRaster;
|
|
52 |
import org.gvsig.raster.reproject.algorithm.ReprojectProcess;
|
|
53 |
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
|
|
54 |
import org.gvsig.raster.tools.app.basic.config.Configuration;
|
|
55 |
import org.gvsig.raster.tools.app.basic.raster.gui.wizard.ReprojectionQueue;
|
|
56 |
import org.gvsig.raster.tools.app.basic.raster.gui.wizard.proj.RasterProjectionActionsDialog;
|
|
57 |
import org.gvsig.tools.exception.BaseException;
|
|
58 |
|
|
59 |
public class PrepareLayerAskProjection implements PrepareDataStoreParameters, PrepareLayer, IProcessActions {
|
|
60 |
protected CRSUtils crsUtil = RasterLocator.getManager().getCRSUtils();
|
|
61 |
private boolean changeReprojectionOption = true;
|
|
62 |
private int lastReprojectionOption = RasterDataParameters.NEW_PROJETION_TO_THE_LAYER;
|
|
63 |
private ReprojectionQueue queue = ReprojectionQueue.getSingleton();
|
|
64 |
|
|
65 |
@SuppressWarnings("deprecation")
|
|
66 |
public DataStoreParameters prepare(DataStoreParameters storeParameters,
|
|
67 |
PrepareContext context) throws BaseException {
|
|
68 |
if(!(storeParameters instanceof RasterDataParameters))
|
|
69 |
return storeParameters;
|
|
70 |
|
|
71 |
DataManager dataManager = DALLocator.getDataManager();
|
|
72 |
RasterDataStore dataStore = (RasterDataStore)dataManager.createStore(storeParameters);
|
|
73 |
|
|
74 |
if (Configuration.getValue("general_ask_projection", Boolean.valueOf(false)).booleanValue()) {
|
|
75 |
IProjection proj = readProjection(dataStore);
|
|
76 |
if(proj != null) {
|
|
77 |
((RasterDataParameters)storeParameters).setDynValue("CRS", proj);
|
|
78 |
compareProjections(proj, context.getViewProjection(), dataStore);
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
if(dataStore != null)
|
|
83 |
dataStore.close();
|
|
84 |
|
|
85 |
if(((RasterDataParameters)storeParameters).getReprojectionOption() == RasterDataParameters.NEW_PROJETION_TO_THE_LAYER)
|
|
86 |
((RasterDataParameters)storeParameters).setDynValue("CRS", null);
|
|
87 |
|
|
88 |
if(((RasterDataParameters)storeParameters).getReprojectionOption() == RasterDataParameters.NOT_LOAD)
|
|
89 |
return null;
|
|
90 |
|
|
91 |
return storeParameters;
|
|
92 |
}
|
|
93 |
|
|
94 |
public void post(DataStoreParameters storeParameters, PrepareContext context) {
|
|
95 |
changeReprojectionOption = true;
|
|
96 |
}
|
|
97 |
|
|
98 |
public void pre(DataStoreParameters storeParameters, PrepareContext context) {
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* If this flag is true the reprojection options can be changed by the user
|
|
103 |
* @return
|
|
104 |
*/
|
|
105 |
public boolean getChangeOption() {
|
|
106 |
return changeReprojectionOption;
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* If this flag is true the reprojection options can be changed by the user
|
|
111 |
* @param changeOption
|
|
112 |
*/
|
|
113 |
public void setChangeOption(boolean changeOption) {
|
|
114 |
this.changeReprojectionOption = changeOption;
|
|
115 |
}
|
|
116 |
|
|
117 |
public FLayer prepare(FLayer layer, PrepareContextView context) {
|
|
118 |
if (!(layer instanceof DefaultFLyrRaster))
|
|
119 |
return layer;
|
|
120 |
DefaultFLyrRaster lyrRaster = (DefaultFLyrRaster) layer;
|
|
121 |
String layerName = lyrRaster.getName();
|
|
122 |
|
|
123 |
RasterDataParameters params = (RasterDataParameters)lyrRaster.getDataStore().getParameters();
|
|
124 |
int repOption = params.getReprojectionOption();
|
|
125 |
|
|
126 |
if(repOption == RasterDataParameters.REPROJECT_DATA) {
|
|
127 |
RasterDataParameters rasterParams = reproject(lyrRaster, context.getViewProjection());
|
|
128 |
if(rasterParams != null) {
|
|
129 |
MapContextManager mcm = MapContextLocator.getMapContextManager();
|
|
130 |
try {
|
|
131 |
return (DefaultFLyrRaster) mcm.createLayer(layerName, rasterParams);
|
|
132 |
} catch (LoadLayerException e) {
|
|
133 |
return lyrRaster;
|
|
134 |
}
|
|
135 |
/*DefaultFLyrRaster lyr = new DefaultFLyrRaster();
|
|
136 |
DataManager dataManager = DALLocator.getDataManager();
|
|
137 |
try {
|
|
138 |
DataStore dataStore = dataManager.createStore(rasterParams);
|
|
139 |
lyr.setName(layerName);
|
|
140 |
lyr.setDataStore(dataStore);
|
|
141 |
return lyr;
|
|
142 |
} catch (ValidateDataParametersException e) {
|
|
143 |
return lyrRaster;
|
|
144 |
} catch (InitializeException e) {
|
|
145 |
return lyrRaster;
|
|
146 |
} catch (ProviderNotRegisteredException e) {
|
|
147 |
return lyrRaster;
|
|
148 |
} catch (LoadLayerException e) {
|
|
149 |
return lyrRaster;
|
|
150 |
}*/
|
|
151 |
}
|
|
152 |
} else if(repOption == RasterDataParameters.REPROJECT_VIEW) {
|
|
153 |
try {
|
|
154 |
if (lyrRaster != null && lyrRaster.readProjection() != null) {
|
|
155 |
context.getMapControl().setProjection(lyrRaster.readProjection());
|
|
156 |
lyrRaster.setVisible(true);
|
|
157 |
}
|
|
158 |
} catch (RasterDriverException e) {
|
|
159 |
RasterToolsUtil.messageBoxError("Error reading the projection", null, e);
|
|
160 |
}
|
|
161 |
} else if(repOption == RasterDataParameters.DONT_CHANGE_PROJECTION) {
|
|
162 |
lyrRaster.setVisible(true);
|
|
163 |
return lyrRaster;
|
|
164 |
} else if(repOption == RasterDataParameters.NEW_PROJETION_TO_THE_LAYER) {
|
|
165 |
IProjection proj = context.getViewProjection();
|
|
166 |
if(proj != null)
|
|
167 |
lyrRaster.setProjection(proj, false);
|
|
168 |
lyrRaster.setVisible(true);
|
|
169 |
return lyrRaster;
|
|
170 |
}
|
|
171 |
|
|
172 |
return lyrRaster;
|
|
173 |
}
|
|
174 |
|
|
175 |
@SuppressWarnings("unchecked")
|
|
176 |
private RasterDataParameters reproject(FLyrRaster lyrRaster, IProjection dstProj) {
|
|
177 |
RasterDataParameters params = (RasterDataParameters)lyrRaster.getDataStore().getParameters();
|
|
178 |
int repOption = params.getReprojectionOption();
|
|
179 |
|
|
180 |
if(repOption == RasterDataParameters.REPROJECT_DATA) {
|
|
181 |
try {
|
|
182 |
IProjection srcProj = readProjection(lyrRaster.getDataStore());
|
|
183 |
if(srcProj == null)
|
|
184 |
return null;
|
|
185 |
|
|
186 |
String uri = params.getURI();
|
|
187 |
uri = uri.substring(0, uri.lastIndexOf(".")) + "_" + dstProj.getAbrev() + uri.substring(uri.lastIndexOf("."));
|
|
188 |
uri = uri.replace(':', '_');
|
|
189 |
|
|
190 |
if(!new File(uri).exists()) {
|
|
191 |
RasterProcess process = null;
|
|
192 |
try {
|
|
193 |
process = RasterBaseAlgorithmLibrary.getManager().createRasterTask("RasterReprojectionProcess");
|
|
194 |
} catch (ProcessException e1) {
|
|
195 |
RasterToolsUtil.messageBoxError("error_creating_algorithm", null, e1);
|
|
196 |
return params;
|
|
197 |
}
|
|
198 |
process.setActions(this);
|
|
199 |
process.addParam(ReprojectProcess.RASTER_STORE, lyrRaster.getDataStore());
|
|
200 |
process.addParam(ReprojectProcess.SIZEX, 0); //Calculo autom?tico de tama?o
|
|
201 |
process.addParam(ReprojectProcess.SIZEY, 0);
|
|
202 |
process.addParam(ReprojectProcess.PATH, uri);
|
|
203 |
process.addParam(ReprojectProcess.SRC_PROJECTION, srcProj);
|
|
204 |
process.addParam(ReprojectProcess.DST_PROJECTION, dstProj);
|
|
205 |
process.addParam(ReprojectProcess.CELLSIZE, lyrRaster.getDataStore().getCellSize());
|
|
206 |
|
|
207 |
//Despierta la cola que reproyecta im?genes
|
|
208 |
synchronized (queue) {
|
|
209 |
queue.add(process, this);
|
|
210 |
queue.notify();
|
|
211 |
}
|
|
212 |
|
|
213 |
//Se suspende hasta que la cola haya terminado
|
|
214 |
try {
|
|
215 |
synchronized (this) {
|
|
216 |
this.wait();
|
|
217 |
}
|
|
218 |
} catch (InterruptedException e) {
|
|
219 |
}
|
|
220 |
|
|
221 |
if(process.getResult() != null) {
|
|
222 |
HashMap<String, Object> map = (HashMap<String, Object>)process.getResult();
|
|
223 |
String filename = (String)map.get(ReprojectProcess.FILENAME);
|
|
224 |
params.setURI(filename);
|
|
225 |
return params;
|
|
226 |
}
|
|
227 |
} else {
|
|
228 |
params.setURI(uri);
|
|
229 |
return params;
|
|
230 |
}
|
|
231 |
|
|
232 |
} catch (RasterDriverException e) {
|
|
233 |
RasterToolsUtil.messageBoxError("Error creating layer", null, e);
|
|
234 |
}
|
|
235 |
} else if(repOption == RasterDataParameters.REPROJECT_VIEW) {
|
|
236 |
//Esto se cambia en el prepare que tiene acceso al mapcontrol. Este prepare es a nivel de datos
|
|
237 |
}
|
|
238 |
return null;
|
|
239 |
}
|
|
240 |
|
|
241 |
public void end(Object param) {
|
|
242 |
|
|
243 |
}
|
|
244 |
|
|
245 |
/**
|
|
246 |
* Compares two projections and show a dialog to the user to make a decision
|
|
247 |
* @param lyrProj
|
|
248 |
* @param viewProj
|
|
249 |
*/
|
|
250 |
private void compareProjections(IProjection lyrProj, IProjection viewProj, RasterDataStore dataStore) {
|
|
251 |
if(!getChangeOption() || lyrProj == null || viewProj == null) {
|
|
252 |
((RasterDataParameters)dataStore.getParameters()).setReprojectionOption(lastReprojectionOption);
|
|
253 |
return;
|
|
254 |
}
|
|
255 |
|
|
256 |
/*
|
|
257 |
* Si las proyecciones de vista y raster son distintas se lanza el
|
|
258 |
* dialogo de selecci?n de opciones. Este dialogo solo se lanza en caso
|
|
259 |
* de que el checkbox de aplicar a todos los ficheros no haya sido
|
|
260 |
* marcado. En este caso para el resto de ficheros de esa selecci?n se
|
|
261 |
* har? la misma acci?n que se hizo para el primero.
|
|
262 |
*/
|
|
263 |
if (!viewProj.getAbrev().endsWith(lyrProj.getAbrev())) {
|
|
264 |
String layerName = dataStore.getName().substring(dataStore.getName().lastIndexOf(File.separator) + 1, dataStore.getName().length());
|
|
265 |
RasterProjectionActionsDialog dialog = new RasterProjectionActionsDialog(
|
|
266 |
dataStore.isReproyectable(), layerName, RasterDataParameters.REPROJECT_VIEW);
|
|
267 |
if (dialog != null) {
|
|
268 |
lastReprojectionOption = dialog.getRasterProjectionActionsPanel().getSelection();
|
|
269 |
((RasterDataParameters)dataStore.getParameters()).setReprojectionOption(lastReprojectionOption);
|
|
270 |
setChangeOption(!dialog.getChangeProjectionOption());
|
|
271 |
}
|
|
272 |
}
|
|
273 |
}
|
|
274 |
|
|
275 |
/**
|
|
276 |
* Reads the projection from a DataStore
|
|
277 |
* @param dataStore
|
|
278 |
* @return
|
|
279 |
* @throws RasterDriverException
|
|
280 |
*/
|
|
281 |
private IProjection readProjection(RasterDataStore dataStore) throws RasterDriverException {
|
|
282 |
try {
|
|
283 |
crsUtil.setCRSFactory(CRSFactory.cp);
|
|
284 |
if( dataStore == null )
|
|
285 |
return null;
|
|
286 |
return crsUtil.convertWktToIProjection(dataStore.getWktProjection());
|
|
287 |
} catch (Exception e) {
|
|
288 |
throw new RasterDriverException("Problems converting from WKT to IProjection", e);
|
|
289 |
} catch (Error e) {
|
|
290 |
e.printStackTrace();
|
|
291 |
return null;
|
|
292 |
}
|
|
293 |
}
|
|
294 |
|
|
295 |
public String getDescription() {
|
|
296 |
return "Prepare projection for Raster Layer";
|
|
297 |
}
|
|
298 |
|
|
299 |
public String getName() {
|
|
300 |
return "PrepareRasterLayerProjection";
|
|
301 |
}
|
|
302 |
|
|
303 |
public Object create() {
|
|
304 |
return this;
|
|
305 |
}
|
|
306 |
|
|
307 |
public Object create(Object[] args) {
|
|
308 |
return this;
|
|
309 |
}
|
|
310 |
|
|
311 |
@SuppressWarnings("unchecked")
|
|
312 |
public Object create(Map args) {
|
|
313 |
return this;
|
|
314 |
}
|
|
315 |
|
|
316 |
public void interrupted() {
|
|
317 |
}
|
|
318 |
}
|
0 |
319 |
|