gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / raster / gui / wizard / PrepareLayerAskProjection.java @ 1848
History | View | Annotate | Download (11.8 KB)
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.tools.app.basic.raster.gui.wizard; |
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.fmap.layers.DefaultFLyrRaster; |
47 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
48 |
import org.gvsig.raster.tools.algorithm.base.RasterBaseAlgorithmLibrary; |
49 |
import org.gvsig.raster.tools.algorithm.base.process.IProcessActions; |
50 |
import org.gvsig.raster.tools.algorithm.base.process.ProcessException; |
51 |
import org.gvsig.raster.tools.algorithm.base.process.RasterProcess; |
52 |
import org.gvsig.raster.tools.algorithm.reproject.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.proj.RasterProjectionActionsDialog; |
56 |
import org.gvsig.tools.exception.BaseException; |
57 |
|
58 |
public class PrepareLayerAskProjection implements PrepareDataStoreParameters, PrepareLayer, IProcessActions { |
59 |
protected CRSUtils crsUtil = RasterLocator.getManager().getCRSUtils();
|
60 |
private boolean changeReprojectionOption = true; |
61 |
private int lastReprojectionOption = RasterDataParameters.NEW_PROJETION_TO_THE_LAYER; |
62 |
private ReprojectionQueue queue = ReprojectionQueue.getSingleton();
|
63 |
|
64 |
/*
|
65 |
* (non-Javadoc)
|
66 |
* @see org.gvsig.app.prepareAction.PrepareDataStoreParameters#prepare(org.gvsig.fmap.dal.DataStoreParameters, org.gvsig.app.prepareAction.PrepareContext)
|
67 |
*/
|
68 |
@SuppressWarnings("deprecation") |
69 |
public DataStoreParameters prepare(DataStoreParameters storeParameters,
|
70 |
PrepareContext context) throws BaseException {
|
71 |
if(!(storeParameters instanceof RasterDataParameters)) |
72 |
return storeParameters;
|
73 |
|
74 |
DataManager dataManager = DALLocator.getDataManager(); |
75 |
RasterDataStore dataStore = (RasterDataStore)dataManager.createStore(storeParameters); |
76 |
|
77 |
if (Configuration.getValue("general_ask_projection", Boolean.valueOf(false)).booleanValue()) { |
78 |
IProjection proj = readProjection(dataStore); |
79 |
if(proj != null) { |
80 |
((RasterDataParameters)storeParameters).setDynValue("CRS", proj);
|
81 |
compareProjections(proj, context.getViewProjection(), dataStore); |
82 |
} |
83 |
} |
84 |
|
85 |
if(dataStore != null) |
86 |
dataStore.close(); |
87 |
|
88 |
if(((RasterDataParameters)storeParameters).getReprojectionOption() == RasterDataParameters.NEW_PROJETION_TO_THE_LAYER)
|
89 |
((RasterDataParameters)storeParameters).setDynValue("CRS", null); |
90 |
|
91 |
if(((RasterDataParameters)storeParameters).getReprojectionOption() == RasterDataParameters.NOT_LOAD)
|
92 |
return null; |
93 |
|
94 |
return storeParameters;
|
95 |
} |
96 |
|
97 |
public void post(DataStoreParameters storeParameters, PrepareContext context) { |
98 |
changeReprojectionOption = true;
|
99 |
} |
100 |
|
101 |
public void pre(DataStoreParameters storeParameters, PrepareContext context) { |
102 |
} |
103 |
|
104 |
/**
|
105 |
* If this flag is true the reprojection options can be changed by the user
|
106 |
* @return
|
107 |
*/
|
108 |
public boolean getChangeOption() { |
109 |
return changeReprojectionOption;
|
110 |
} |
111 |
|
112 |
/**
|
113 |
* If this flag is true the reprojection options can be changed by the user
|
114 |
* @param changeOption
|
115 |
*/
|
116 |
public void setChangeOption(boolean changeOption) { |
117 |
this.changeReprojectionOption = changeOption;
|
118 |
} |
119 |
|
120 |
/*
|
121 |
* (non-Javadoc)
|
122 |
* @see org.gvsig.app.prepareAction.PrepareLayer#prepare(org.gvsig.fmap.mapcontext.layers.FLayer, org.gvsig.app.prepareAction.PrepareContextView)
|
123 |
*/
|
124 |
public FLayer prepare(FLayer layer, PrepareContextView context) {
|
125 |
if (!(layer instanceof DefaultFLyrRaster)) |
126 |
return layer;
|
127 |
DefaultFLyrRaster lyrRaster = (DefaultFLyrRaster) layer; |
128 |
String layerName = lyrRaster.getName();
|
129 |
|
130 |
RasterDataParameters params = (RasterDataParameters)lyrRaster.getDataStore().getParameters(); |
131 |
int repOption = params.getReprojectionOption();
|
132 |
|
133 |
if(repOption == RasterDataParameters.REPROJECT_DATA) {
|
134 |
RasterDataParameters rasterParams = reproject(lyrRaster, context.getViewProjection()); |
135 |
if(rasterParams != null) { |
136 |
MapContextManager mcm = MapContextLocator.getMapContextManager(); |
137 |
try {
|
138 |
return (DefaultFLyrRaster) mcm.createLayer(layerName, rasterParams);
|
139 |
} catch (LoadLayerException e) {
|
140 |
return lyrRaster;
|
141 |
} |
142 |
/*DefaultFLyrRaster lyr = new DefaultFLyrRaster();
|
143 |
DataManager dataManager = DALLocator.getDataManager();
|
144 |
try {
|
145 |
DataStore dataStore = dataManager.createStore(rasterParams);
|
146 |
lyr.setName(layerName);
|
147 |
lyr.setDataStore(dataStore);
|
148 |
return lyr;
|
149 |
} catch (ValidateDataParametersException e) {
|
150 |
return lyrRaster;
|
151 |
} catch (InitializeException e) {
|
152 |
return lyrRaster;
|
153 |
} catch (ProviderNotRegisteredException e) {
|
154 |
return lyrRaster;
|
155 |
} catch (LoadLayerException e) {
|
156 |
return lyrRaster;
|
157 |
}*/
|
158 |
} |
159 |
} else if(repOption == RasterDataParameters.REPROJECT_VIEW) { |
160 |
try {
|
161 |
if (lyrRaster != null && lyrRaster.readProjection() != null) { |
162 |
context.getMapControl().setProjection(lyrRaster.readProjection()); |
163 |
lyrRaster.setVisible(true);
|
164 |
} |
165 |
} catch (RasterDriverException e) {
|
166 |
RasterToolsUtil.messageBoxError("Error reading the projection", null, e); |
167 |
} |
168 |
} else if(repOption == RasterDataParameters.DONT_CHANGE_PROJECTION) { |
169 |
lyrRaster.setVisible(true);
|
170 |
return lyrRaster;
|
171 |
} else if(repOption == RasterDataParameters.NEW_PROJETION_TO_THE_LAYER) { |
172 |
IProjection proj = context.getViewProjection(); |
173 |
if(proj != null) |
174 |
lyrRaster.setProjection(proj, false);
|
175 |
lyrRaster.setVisible(true);
|
176 |
return lyrRaster;
|
177 |
} |
178 |
|
179 |
return lyrRaster;
|
180 |
} |
181 |
|
182 |
@SuppressWarnings("unchecked") |
183 |
private RasterDataParameters reproject(FLyrRaster lyrRaster, IProjection dstProj) {
|
184 |
RasterDataParameters params = (RasterDataParameters)lyrRaster.getDataStore().getParameters(); |
185 |
int repOption = params.getReprojectionOption();
|
186 |
|
187 |
if(repOption == RasterDataParameters.REPROJECT_DATA) {
|
188 |
try {
|
189 |
IProjection srcProj = readProjection(lyrRaster.getDataStore()); |
190 |
if(srcProj == null) |
191 |
return null; |
192 |
|
193 |
String uri = params.getURI();
|
194 |
uri = uri.substring(0, uri.lastIndexOf(".")) + "_" + dstProj.getAbrev() + uri.substring(uri.lastIndexOf(".")); |
195 |
uri = uri.replace(':', '_'); |
196 |
|
197 |
if(!new File(uri).exists()) { |
198 |
RasterProcess process = null;
|
199 |
try {
|
200 |
process = RasterBaseAlgorithmLibrary.getManager().createRasterTask("RasterReprojectionProcess");
|
201 |
} catch (ProcessException e1) {
|
202 |
RasterToolsUtil.messageBoxError("error_creating_algorithm", null, e1); |
203 |
return params;
|
204 |
} |
205 |
process.setActions(this);
|
206 |
process.addParam(ReprojectProcess.RASTER_STORE, lyrRaster.getDataStore()); |
207 |
process.addParam(ReprojectProcess.SIZEX, 0); //Calculo autom?tico de tama?o |
208 |
process.addParam(ReprojectProcess.SIZEY, 0);
|
209 |
process.addParam(ReprojectProcess.PATH, uri); |
210 |
process.addParam(ReprojectProcess.SRC_PROJECTION, srcProj); |
211 |
process.addParam(ReprojectProcess.DST_PROJECTION, dstProj); |
212 |
process.addParam(ReprojectProcess.CELLSIZE, lyrRaster.getDataStore().getCellSize()); |
213 |
|
214 |
//Despierta la cola que reproyecta im?genes
|
215 |
synchronized (queue) {
|
216 |
queue.add(process, this);
|
217 |
queue.notify(); |
218 |
} |
219 |
|
220 |
//Se suspende hasta que la cola haya terminado
|
221 |
try {
|
222 |
synchronized (this) { |
223 |
this.wait();
|
224 |
} |
225 |
} catch (InterruptedException e) { |
226 |
} |
227 |
|
228 |
if(process.getResult() != null) { |
229 |
HashMap<String, Object> map = (HashMap<String, Object>)process.getResult(); |
230 |
String filename = (String)map.get(ReprojectProcess.FILENAME); |
231 |
params.setURI(filename); |
232 |
return params;
|
233 |
} |
234 |
} else {
|
235 |
params.setURI(uri); |
236 |
return params;
|
237 |
} |
238 |
|
239 |
} catch (RasterDriverException e) {
|
240 |
RasterToolsUtil.messageBoxError("Error creating layer", null, e); |
241 |
} |
242 |
} else if(repOption == RasterDataParameters.REPROJECT_VIEW) { |
243 |
//Esto se cambia en el prepare que tiene acceso al mapcontrol. Este prepare es a nivel de datos
|
244 |
} |
245 |
return null; |
246 |
} |
247 |
|
248 |
public void end(Object param) { |
249 |
|
250 |
} |
251 |
|
252 |
/**
|
253 |
* Compares two projections and show a dialog to the user to make a decision
|
254 |
* @param lyrProj
|
255 |
* @param viewProj
|
256 |
*/
|
257 |
private void compareProjections(IProjection lyrProj, IProjection viewProj, RasterDataStore dataStore) { |
258 |
if(!getChangeOption() || lyrProj == null || viewProj == null) { |
259 |
((RasterDataParameters)dataStore.getParameters()).setReprojectionOption(lastReprojectionOption); |
260 |
return;
|
261 |
} |
262 |
|
263 |
/*
|
264 |
* Si las proyecciones de vista y raster son distintas se lanza el
|
265 |
* dialogo de selecci?n de opciones. Este dialogo solo se lanza en caso
|
266 |
* de que el checkbox de aplicar a todos los ficheros no haya sido
|
267 |
* marcado. En este caso para el resto de ficheros de esa selecci?n se
|
268 |
* har? la misma acci?n que se hizo para el primero.
|
269 |
*/
|
270 |
if (!viewProj.getAbrev().endsWith(lyrProj.getAbrev())) {
|
271 |
String layerName = dataStore.getName().substring(dataStore.getName().lastIndexOf(File.separator) + 1, dataStore.getName().length()); |
272 |
RasterProjectionActionsDialog dialog = new RasterProjectionActionsDialog(
|
273 |
dataStore.isReproyectable(), layerName, RasterDataParameters.REPROJECT_VIEW); |
274 |
if (dialog != null) { |
275 |
lastReprojectionOption = dialog.getRasterProjectionActionsPanel().getSelection(); |
276 |
((RasterDataParameters)dataStore.getParameters()).setReprojectionOption(lastReprojectionOption); |
277 |
setChangeOption(!dialog.getChangeProjectionOption()); |
278 |
} |
279 |
} |
280 |
} |
281 |
|
282 |
/**
|
283 |
* Reads the projection from a DataStore
|
284 |
* @param dataStore
|
285 |
* @return
|
286 |
* @throws RasterDriverException
|
287 |
*/
|
288 |
private IProjection readProjection(RasterDataStore dataStore) throws RasterDriverException { |
289 |
try {
|
290 |
crsUtil.setCRSFactory(CRSFactory.cp); |
291 |
if( dataStore == null ) |
292 |
return null; |
293 |
return crsUtil.convertWktToIProjection(dataStore.getWktProjection());
|
294 |
} catch (Exception e) { |
295 |
throw new RasterDriverException("Problems converting from WKT to IProjection", e); |
296 |
} catch (Error e) { |
297 |
e.printStackTrace(); |
298 |
return null; |
299 |
} |
300 |
} |
301 |
|
302 |
public String getDescription() { |
303 |
return "Prepare projection for Raster Layer"; |
304 |
} |
305 |
|
306 |
public String getName() { |
307 |
return "PrepareRasterLayerProjection"; |
308 |
} |
309 |
|
310 |
public Object create() { |
311 |
return this; |
312 |
} |
313 |
|
314 |
public Object create(Object[] args) { |
315 |
return this; |
316 |
} |
317 |
|
318 |
@SuppressWarnings("unchecked") |
319 |
public Object create(Map args) { |
320 |
return this; |
321 |
} |
322 |
|
323 |
public void interrupted() { |
324 |
} |
325 |
} |