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