gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.algorithm / org.gvsig.raster.tools.algorithm.saveraster / src / main / java / org / gvsig / raster / tools / algorithm / saveraster / SaveRasterProcess.java @ 2848
History | View | Annotate | Download (11 KB)
1 | 2480 | nbrodin | /* 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.algorithm.saveraster; |
||
23 | |||
24 | import java.awt.Dimension; |
||
25 | import java.awt.geom.AffineTransform; |
||
26 | import java.io.File; |
||
27 | import java.io.IOException; |
||
28 | import java.util.Date; |
||
29 | |||
30 | import org.cresques.cts.IProjection; |
||
31 | import org.gvsig.fmap.dal.coverage.RasterLocator; |
||
32 | import org.gvsig.fmap.dal.coverage.RasterManager; |
||
33 | import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
||
34 | import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
||
35 | import org.gvsig.fmap.dal.coverage.datastruct.Params; |
||
36 | import org.gvsig.fmap.dal.coverage.datastruct.ViewPortData; |
||
37 | import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
||
38 | import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
||
39 | import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
||
40 | import org.gvsig.fmap.dal.coverage.process.TaskEventManager; |
||
41 | import org.gvsig.fmap.dal.coverage.store.DataServerWriter; |
||
42 | import org.gvsig.fmap.dal.coverage.store.RasterWriter; |
||
43 | import org.gvsig.fmap.dal.coverage.util.CRSUtils; |
||
44 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
45 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
46 | import org.gvsig.fmap.mapcontext.layers.FLayers; |
||
47 | import org.gvsig.raster.algorithm.process.ProcessException; |
||
48 | import org.gvsig.raster.algorithm.process.DataProcess; |
||
49 | |||
50 | /**
|
||
51 | * Thread que se encarga de llamar a los writer para realizar la tarea de
|
||
52 | * salvado y/p compresi?n
|
||
53 | *
|
||
54 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
55 | */
|
||
56 | public class SaveRasterProcess extends DataProcess { |
||
57 | public static String VIEWPORT = "ViewPort"; |
||
58 | public static String PATH = "Path"; |
||
59 | public static String DIMENSION = "Dimension"; |
||
60 | public static String BLOCKSIZE = "Blocksize"; |
||
61 | public static String FILENAME = "FileName"; |
||
62 | public static String PARAMS = "WriterParams"; |
||
63 | public static String FLYRS = "FLyrs"; |
||
64 | public static String TIME = "Time"; |
||
65 | public static String PROJECTION = "Projection"; |
||
66 | |||
67 | private IProjection projection = null; |
||
68 | private ViewPort viewPort = null; |
||
69 | private Dimension dimension = null; |
||
70 | private RasterizeFLayers rasterizerLayer = null; |
||
71 | private String fileName = ""; |
||
72 | private Params writerParams = null; |
||
73 | //private CopyDataset jp2Copy = null;
|
||
74 | private boolean supportImage = false; |
||
75 | private RasterManager rManager = RasterLocator.getManager();
|
||
76 | //private Logger logger = LoggerFactory.getLogger(RasterProcess.class.toString());
|
||
77 | |||
78 | public static void registerParameters() { |
||
79 | registerInputParameter(VIEWPORT, ViewPort.class, SaveRasterAlgorithmLibrary.PROCESS_LABEL); |
||
80 | registerInputParameter(PATH, String.class, SaveRasterAlgorithmLibrary.PROCESS_LABEL);
|
||
81 | registerInputParameter(DIMENSION, Dimension.class, SaveRasterAlgorithmLibrary.PROCESS_LABEL);
|
||
82 | registerInputParameter(FLYRS, FLayers.class, SaveRasterAlgorithmLibrary.PROCESS_LABEL); |
||
83 | registerInputParameter(PARAMS, Params.class, SaveRasterAlgorithmLibrary.PROCESS_LABEL); |
||
84 | registerOutputParameter(PROJECTION, IProjection.class, SaveRasterAlgorithmLibrary.PROCESS_LABEL); |
||
85 | |||
86 | registerOutputParameter(FILENAME, String.class, SaveRasterAlgorithmLibrary.PROCESS_LABEL);
|
||
87 | registerOutputParameter(TIME, Long.class, SaveRasterAlgorithmLibrary.PROCESS_LABEL);
|
||
88 | } |
||
89 | |||
90 | public void init() { |
||
91 | viewPort = getParam(VIEWPORT) != null ? (ViewPort)getParam(VIEWPORT) : null; |
||
92 | dimension = getParam(DIMENSION) != null ? (Dimension) getParam(DIMENSION) : null; |
||
93 | FLayers flyrs = getParam(FLYRS) != null ? (FLayers) getParam(FLYRS) : null; |
||
94 | int blockSize = getIntParam(BLOCKSIZE);
|
||
95 | rasterizerLayer = new RasterizeFLayers(flyrs, viewPort, blockSize);
|
||
96 | fileName = getStringParam(FILENAME); |
||
97 | writerParams = getParam(PARAMS) != null ? (Params) getParam(PARAMS) : null; |
||
98 | projection = getParam(PROJECTION) != null ? (IProjection) getParam(PROJECTION) : null; |
||
99 | //supportImage = getBooleanParam("remotelayers") && fileName.endsWith(".jp2");
|
||
100 | } |
||
101 | |||
102 | /**
|
||
103 | * Procesos de escritura de una porci?n de la vista.
|
||
104 | */
|
||
105 | public void process() throws ProcessInterruptedException, ProcessException { |
||
106 | TaskEventManager task = rManager.getRasterTask(); |
||
107 | |||
108 | //jp2Copy = null;
|
||
109 | long t2;
|
||
110 | long t1 = new java.util.Date().getTime(); |
||
111 | |||
112 | //Creamos el driver
|
||
113 | Envelope env = viewPort.getAdjustedEnvelope(); |
||
114 | Extent ex = rManager.getDataStructFactory().createExtent(env.getMinimum(0), env.getMaximum(1), env.getMaximum(0), env.getMinimum(1)); |
||
115 | Dimension imgSz = viewPort.getImageSize();
|
||
116 | ViewPortData vpData = rManager.getDataStructFactory().createViewPortData(viewPort.getProjection(), ex, imgSz ); |
||
117 | 2848 | cmartinez | vpData.setDPI((int)viewPort.getDPI());
|
118 | 2480 | nbrodin | AffineTransform at = new AffineTransform(vpData.getExtent().width() / imgSz.width, |
119 | 0, 0, |
||
120 | -(vpData.getExtent().height() / imgSz.height), |
||
121 | vpData.getExtent().getULX(), |
||
122 | vpData.getExtent().getULY()); |
||
123 | String oldFileName = fileName;
|
||
124 | if(supportImage) {
|
||
125 | fileName = fileName.substring(0, Math.min(fileName.lastIndexOf(File.separator) + 1, fileName.length() - 1)); |
||
126 | fileName += rManager.getFileUtils().usesOnlyLayerName() + ".tif";
|
||
127 | writerParams = getWriterParams(fileName); |
||
128 | } |
||
129 | |||
130 | //Ejecutamos el driver con los datos pasados
|
||
131 | try {
|
||
132 | write(fileName, at, writerParams, rasterizerLayer); |
||
133 | |||
134 | if(task.getEvent() != null) |
||
135 | task.manageEvent(task.getEvent()); |
||
136 | |||
137 | /*if(supportImage) {
|
||
138 | try {
|
||
139 | insertLineLog(Messages.getText("saving_jp2"));
|
||
140 | jp2Copy = new CopyDataset(fileName, oldFileName, incrementableTask);
|
||
141 | jp2Copy.copy();
|
||
142 | new File(fileName).delete();
|
||
143 | new File(rManager.getFileUtils().getRMFNameFromFileName(fileName)).delete();
|
||
144 | } catch (LoadLayerException e) {
|
||
145 | throw new ProcessException("error_processing", e);
|
||
146 | }
|
||
147 | jp2Copy = null;
|
||
148 | } */
|
||
149 | t2 = new Date().getTime(); |
||
150 | try {
|
||
151 | saveRasterFinalize(oldFileName, (t2 - t1)); |
||
152 | } catch(ArrayIndexOutOfBoundsException exc) { |
||
153 | //Si la ventana se ha cerrado ya es porque ha sido cancelada por lo que
|
||
154 | //producir? esta excepci?n. En este caso no se lanza la ventana de informaci?n
|
||
155 | //de finalizaci?n.
|
||
156 | } |
||
157 | |||
158 | } catch(IOException ev) { |
||
159 | throw new ProcessException("error_processing", ev); |
||
160 | } catch(OutOfMemoryError ev) { |
||
161 | throw new ProcessException("memoria_excedida", ev); |
||
162 | } finally {
|
||
163 | if (incrementableTask != null) { |
||
164 | incrementableTask.processFinalize(); |
||
165 | incrementableTask = null;
|
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | |||
170 | /**
|
||
171 | *
|
||
172 | * @param name
|
||
173 | * @param at
|
||
174 | * @throws IOException
|
||
175 | * @throws ProcessException
|
||
176 | * @throws InterruptedException
|
||
177 | */
|
||
178 | private void write(String name, AffineTransform at, Params writerParams, DataServerWriter rasterizerLayer) throws IOException, ProcessInterruptedException, ProcessException { |
||
179 | //TODO: Usar el nuevo API basado en NewRasterStoreParameters y DataServerExplorer. Ejemplo en ClippingProcess
|
||
180 | |||
181 | TaskEventManager task = rManager.getRasterTask(); |
||
182 | RasterWriter geoRasterWriter = null;
|
||
183 | try {
|
||
184 | //TODO: FUNCIONALIDAD: Poner los gerWriter con la proyecci?n de la vista
|
||
185 | geoRasterWriter = rManager.createWriter(rasterizerLayer, name, |
||
186 | 3, at, dimension.width,
|
||
187 | dimension.height, Buffer.TYPE_IMAGE, writerParams, null); |
||
188 | CRSUtils crsUtil = RasterLocator.getManager().getCRSUtils(); |
||
189 | geoRasterWriter.setWkt(crsUtil.convertIProjectionToWkt(projection)); |
||
190 | } catch (NotSupportedExtensionException e) {
|
||
191 | throw new ProcessException("extension_no_soportada", e); |
||
192 | } catch (RasterDriverException e) {
|
||
193 | throw new ProcessException("no_driver_escritura", e); |
||
194 | } |
||
195 | |||
196 | if(task.getEvent() != null) |
||
197 | task.manageEvent(task.getEvent()); |
||
198 | |||
199 | geoRasterWriter.setCancellableRasterDriver(new ExternalCancellable(incrementableTask));
|
||
200 | geoRasterWriter.dataWrite(); |
||
201 | geoRasterWriter.writeClose(); |
||
202 | } |
||
203 | |||
204 | /**
|
||
205 | * Obtiene los par?metros del driver de escritura. Si el driver no se ha creado a?n se obtienen
|
||
206 | * unos par?metros con la inicializaci?n por defecto. Si se ha creado ya y se han modificado se
|
||
207 | * devuelven los par?metros con las modificaciones. Si se cambia de driver se devolver? un WriterParams
|
||
208 | * como si fuera la primera vez que se abre.
|
||
209 | * @param name Nombre del fichero sobre el que se salva.
|
||
210 | * @return WriterParams
|
||
211 | */
|
||
212 | private Params getWriterParams(String name) throws ProcessException { |
||
213 | RasterWriter writer = null;
|
||
214 | String ext = rManager.getFileUtils().getExtensionFromFileName(name);
|
||
215 | try {
|
||
216 | if(writer == null) //La primera vez que se obtiene el driver |
||
217 | writer = rManager.createWriter(name); |
||
218 | else {
|
||
219 | String newType = rManager.getProviderServices().getWriteDriverType(ext);
|
||
220 | String oldType = writer.getDriverName();
|
||
221 | if(!newType.equals(oldType)) //Cambio de driver despu?s de haber seleccionado y modificado las propiedades de uno |
||
222 | writer = rManager.createWriter(name); |
||
223 | } |
||
224 | |||
225 | if(writer == null) |
||
226 | throw new ProcessException("no_driver_escritura"); |
||
227 | |||
228 | return writer.getParams();
|
||
229 | |||
230 | } catch (NotSupportedExtensionException e1) {
|
||
231 | throw new ProcessException("no_driver_escritura", e1); |
||
232 | } catch (RasterDriverException e1) {
|
||
233 | throw new ProcessException("no_driver_escritura", e1); |
||
234 | } |
||
235 | } |
||
236 | |||
237 | /**
|
||
238 | * Acciones que se realizan al finalizar de salvar a raster.
|
||
239 | * @param fileName Nombre del fichero
|
||
240 | * @param milis Tiempo que ha tardado en ejecutarse
|
||
241 | */
|
||
242 | private void saveRasterFinalize(String fileName, long milis) { |
||
243 | if (incrementableTask != null) |
||
244 | incrementableTask.hideWindow(); |
||
245 | externalActions.end(new Object[]{fileName, new Long(milis)}); |
||
246 | } |
||
247 | |||
248 | /*
|
||
249 | * (non-Javadoc)
|
||
250 | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
|
||
251 | */
|
||
252 | public int getPercent() { |
||
253 | if(rasterizerLayer != null) |
||
254 | return rasterizerLayer.getPercent();
|
||
255 | return 0; |
||
256 | } |
||
257 | |||
258 | /*
|
||
259 | * (non-Javadoc)
|
||
260 | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
||
261 | */
|
||
262 | public String getTitle() { |
||
263 | if(rasterizerLayer != null) |
||
264 | return rasterizerLayer.getTitle();
|
||
265 | return null; |
||
266 | } |
||
267 | |||
268 | /*
|
||
269 | * (non-Javadoc)
|
||
270 | * @see org.gvsig.rastertools.RasterProcess#getLog()
|
||
271 | */
|
||
272 | public String getLog() { |
||
273 | if(rasterizerLayer != null) |
||
274 | return rasterizerLayer.getLog();
|
||
275 | return null; |
||
276 | } |
||
277 | |||
278 | /*
|
||
279 | * (non-Javadoc)
|
||
280 | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
|
||
281 | */
|
||
282 | public String getLabel() { |
||
283 | if(rasterizerLayer != null) |
||
284 | return rasterizerLayer.getLabel();
|
||
285 | return null; |
||
286 | } |
||
287 | } |