svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.raster.tools / org.gvsig.raster.tools.lib / org.gvsig.raster.tools.lib.impl / src / main / java / org / gvsig / raster / tools / lib / impl / DefaultRasterClip.java @ 44831
History | View | Annotate | Download (13.8 KB)
1 | 44831 | jjdelcerro | package org.gvsig.raster.tools.lib.impl; |
---|---|---|---|
2 | |||
3 | import java.io.File; |
||
4 | import java.io.IOException; |
||
5 | import java.util.List; |
||
6 | |||
7 | import org.apache.commons.io.FileUtils; |
||
8 | import org.cresques.cts.ICoordTrans; |
||
9 | import org.cresques.cts.IProjection; |
||
10 | |||
11 | import org.gvsig.fmap.dal.DALLocator; |
||
12 | import org.gvsig.fmap.dal.DataManager; |
||
13 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
14 | import org.gvsig.fmap.dal.DataServerExplorerParameters; |
||
15 | import org.gvsig.fmap.dal.exception.DataException; |
||
16 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
17 | import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
||
18 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
19 | import org.gvsig.fmap.dal.raster.NewRasterStoreParameters; |
||
20 | import org.gvsig.fmap.dal.raster.RasterQuery; |
||
21 | import org.gvsig.fmap.dal.raster.RasterStore; |
||
22 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
||
23 | import org.gvsig.fmap.dal.spi.DALSPILocator; |
||
24 | import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
||
25 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
26 | import org.gvsig.fmap.mapcontext.MapContext; |
||
27 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
28 | import org.gvsig.fmap.mapcontext.MapContextManager; |
||
29 | import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
||
30 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
31 | import org.gvsig.fmap.mapcontext.raster.api.RasterLayer; |
||
32 | import org.gvsig.raster.lib.buffer.api.BandInfo; |
||
33 | import org.gvsig.raster.lib.buffer.api.Buffer; |
||
34 | import org.gvsig.raster.lib.buffer.api.BufferLocator; |
||
35 | import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
||
36 | import org.gvsig.raster.tools.lib.api.RasterClip; |
||
37 | import org.gvsig.raster.tools.lib.api.exceptions.ClipException; |
||
38 | import org.gvsig.tools.dispose.DisposeUtils; |
||
39 | import org.gvsig.tools.dynobject.DynObject; |
||
40 | import org.gvsig.tools.locator.LocatorException; |
||
41 | import org.gvsig.tools.task.SimpleTaskStatus; |
||
42 | |||
43 | |||
44 | /**
|
||
45 | * @author fdiaz
|
||
46 | *
|
||
47 | */
|
||
48 | public class DefaultRasterClip implements RasterClip { |
||
49 | |||
50 | private RasterLayer layer;
|
||
51 | private Envelope envelope;
|
||
52 | private double pixelSizeX; |
||
53 | private double pixelSizeY; |
||
54 | private int interpolationMethod; |
||
55 | private List<Integer> bands; |
||
56 | private String layerNamePrefix; |
||
57 | private boolean createOneLayerPerBand; |
||
58 | private boolean saveToNewRasterFile; |
||
59 | private File folder; |
||
60 | private int columns; |
||
61 | private int rows; |
||
62 | |||
63 | /**
|
||
64 | *
|
||
65 | */
|
||
66 | public DefaultRasterClip() {
|
||
67 | } |
||
68 | |||
69 | @Override
|
||
70 | public RasterLayer getLayer() {
|
||
71 | return layer;
|
||
72 | } |
||
73 | |||
74 | @Override
|
||
75 | public void setLayer(RasterLayer layer) { |
||
76 | this.layer = layer;
|
||
77 | } |
||
78 | |||
79 | @Override
|
||
80 | public void execute(SimpleTaskStatus taskStatus) throws ClipException { |
||
81 | MapContext mapContext = this.layer.getMapContext();
|
||
82 | ICoordTrans ct = layer.getCoordTrans(); |
||
83 | |||
84 | Envelope envelopeInLayerProj = this.getEnvelope();
|
||
85 | Envelope envelopeInRasterProj = envelopeInLayerProj; |
||
86 | double pixelSizeX = this.getPixelSizeX(); |
||
87 | double pixelSizeY = this.getPixelSizeY(); |
||
88 | if (ct != null) { |
||
89 | envelopeInRasterProj = envelopeInLayerProj.convert(ct.getInverted()); |
||
90 | pixelSizeX = this.getPixelSizeX() * envelopeInRasterProj.getLength(0) / envelopeInLayerProj.getLength(0); |
||
91 | pixelSizeY = this.getPixelSizeY() * envelopeInRasterProj.getLength(1) / envelopeInLayerProj.getLength(1); |
||
92 | } |
||
93 | |||
94 | |||
95 | RasterQuery rasterQuery = layer.createRasterQuery(); |
||
96 | rasterQuery.setPixelSize(pixelSizeX); |
||
97 | rasterQuery.setClip(envelopeInRasterProj); |
||
98 | |||
99 | RasterStore rasterStore = layer.getRasterStore(); |
||
100 | for (Integer bandNumber : bands) { |
||
101 | rasterStore.createBandQuery(bandNumber); |
||
102 | rasterQuery.addBand(rasterStore.createBandQuery(bandNumber)); |
||
103 | } |
||
104 | |||
105 | Buffer clipped = null; |
||
106 | Buffer firstInterpolated = null; |
||
107 | Buffer converted = null; |
||
108 | Buffer interpolated = null; |
||
109 | try {
|
||
110 | |||
111 | try {
|
||
112 | clipped = rasterStore.getRasterSet(rasterQuery); |
||
113 | } catch (DataException e) {
|
||
114 | throw new ClipException("Can't create the raster clip.", e); |
||
115 | } |
||
116 | |||
117 | firstInterpolated = clipped; |
||
118 | //FIXME: Creo que no es necesaria esta primera interpolaci?n, la dejo comentarizada, de momento por si cambiamos de idea.
|
||
119 | |||
120 | // if(ct != null && Math.abs(envelopeInLayerProj.getLength(0)/clipped.getColumns()-pixelSizeX)>0.01){
|
||
121 | // if (converted.getColumns() != this.getColumns() || converted.getRows() != this.getRows()) {
|
||
122 | // try {
|
||
123 | // firstInterpolated =
|
||
124 | // clipped.createInterpolated(
|
||
125 | // (int)Math.floor(envelopeInLayerProj.getLength(1)/pixelSizeY),
|
||
126 | // (int)Math.floor(envelopeInLayerProj.getLength(0)/pixelSizeX),
|
||
127 | // interpolationMethod,
|
||
128 | // taskStatus);
|
||
129 | // } catch (LocatorException | BufferException e) {
|
||
130 | // throw new ClipException("Can't interpolate buffer.", e);
|
||
131 | // }
|
||
132 | // }
|
||
133 | |||
134 | // }
|
||
135 | |||
136 | converted = firstInterpolated; |
||
137 | |||
138 | if (ct != null) { |
||
139 | try {
|
||
140 | converted = firstInterpolated.convert(ct, taskStatus); |
||
141 | } catch (BufferException e) {
|
||
142 | throw new ClipException("Can't convert buffer from '" + ct.getPDest().getAbrev() + "' to '" |
||
143 | + ct.getPOrig().getAbrev() + "'.", e);
|
||
144 | } |
||
145 | } |
||
146 | |||
147 | interpolated = converted; |
||
148 | if (converted.getColumns() != this.getColumns() || converted.getRows() != this.getRows()) { |
||
149 | try {
|
||
150 | interpolated = |
||
151 | converted.createInterpolated(this.getRows(), this.getColumns(), interpolationMethod, taskStatus); |
||
152 | } catch (LocatorException | BufferException e) {
|
||
153 | throw new ClipException("Can't interpolate buffer.", e); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | String layerName = layerNamePrefix;
|
||
158 | |||
159 | if(this.createOneLayerPerBand){ |
||
160 | List<Buffer> buffers = BufferLocator.getBufferManager().createOneBufferPerBand(interpolated, true); |
||
161 | int counter = 0; |
||
162 | for (Buffer buffer : buffers) { |
||
163 | BandInfo bandInfo = buffer.getBands()[0].getBandInfo();
|
||
164 | String bandLayerName = layerName+" - B"+counter; //bandInfo.getBand()+" - "+bandInfo.getName(); |
||
165 | bandLayerName = getFreeLayerName(mapContext, bandLayerName); |
||
166 | File file = exportBuffer(buffer, bandLayerName);
|
||
167 | mapContext.getLayers().add(createLayer(interpolated.getProjection(), bandLayerName, file)); |
||
168 | counter++; |
||
169 | } |
||
170 | |||
171 | } else {
|
||
172 | layerName = getFreeLayerName(mapContext, layerName); |
||
173 | File file = exportBuffer(interpolated, layerName);
|
||
174 | mapContext.getLayers().add(createLayer(interpolated.getProjection(), layerName, file)); |
||
175 | } |
||
176 | |||
177 | } catch (IOException | ValidateDataParametersException | DataException | LocatorException | BufferException | LoadLayerException e) { |
||
178 | // TODO Auto-generated catch block
|
||
179 | e.printStackTrace(); |
||
180 | } finally {
|
||
181 | DisposeUtils.dispose(clipped); |
||
182 | DisposeUtils.dispose(converted); |
||
183 | DisposeUtils.dispose(interpolated); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /**
|
||
188 | * @param interpolated
|
||
189 | * @param layerName
|
||
190 | * @param file
|
||
191 | * @throws InitializeException
|
||
192 | * @throws ProviderNotRegisteredException
|
||
193 | * @throws ValidateDataParametersException
|
||
194 | * @throws ClipException
|
||
195 | * @throws LoadLayerException
|
||
196 | */
|
||
197 | private FLayer createLayer(IProjection proj, String layerName, File file) throws InitializeException, |
||
198 | ProviderNotRegisteredException, ValidateDataParametersException, ClipException, LoadLayerException { |
||
199 | DataManager manager = DALLocator.getDataManager(); |
||
200 | DataServerExplorerParameters explorerParams = |
||
201 | manager.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
202 | FilesystemServerExplorer explorer = |
||
203 | (FilesystemServerExplorer) manager |
||
204 | .openServerExplorer(FilesystemServerExplorer.NAME, explorerParams); |
||
205 | String type = explorer.getProviderName(file);
|
||
206 | |||
207 | DynObject params = manager.createStoreParameters(type); |
||
208 | |||
209 | if (params.getDynClass().getDynField("file") != null) { |
||
210 | params.setDynValue("file", file);
|
||
211 | } |
||
212 | if (params.getDynClass().getDynField("crs") != null) { |
||
213 | params.setDynValue("crs", proj);
|
||
214 | } |
||
215 | |||
216 | RasterStore store = null;
|
||
217 | try {
|
||
218 | store = (RasterStore) manager.openStore(type, params); |
||
219 | // store = (RasterStore) manager.openStore("PNG", params);
|
||
220 | } catch (ValidateDataParametersException | InitializeException | ProviderNotRegisteredException e) {
|
||
221 | throw new ClipException("Can't open store.", e); |
||
222 | } |
||
223 | MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
||
224 | FLayer newLayer = mapContextManager.createLayer(layerName, store); |
||
225 | return newLayer;
|
||
226 | } |
||
227 | |||
228 | /**
|
||
229 | * @param mapContext
|
||
230 | * @param layerName
|
||
231 | * @return
|
||
232 | */
|
||
233 | private String getFreeLayerName(MapContext mapContext, String layerName) { |
||
234 | int counter = 1; |
||
235 | FLayer newLayer = mapContext.getLayers().getLayer(layerName); |
||
236 | while (newLayer != null) { |
||
237 | layerName = layerName + "_" + counter;
|
||
238 | newLayer = mapContext.getLayers().getLayer(layerName); |
||
239 | } |
||
240 | return layerName;
|
||
241 | } |
||
242 | |||
243 | |||
244 | /**
|
||
245 | * @param buffer
|
||
246 | * @param layerName
|
||
247 | * @throws InitializeException
|
||
248 | * @throws ProviderNotRegisteredException
|
||
249 | * @throws IOException
|
||
250 | * @throws ValidateDataParametersException
|
||
251 | * @throws DataException
|
||
252 | */
|
||
253 | private File exportBuffer(Buffer buffer, String layerName) throws InitializeException, |
||
254 | ProviderNotRegisteredException, IOException, ValidateDataParametersException, DataException {
|
||
255 | /* As? se crea un archivo raster tiff*/
|
||
256 | |||
257 | String providerName = "GTiff"; |
||
258 | String extension = "tif"; |
||
259 | |||
260 | DataManagerProviderServices manager = DALSPILocator.getDataManagerProviderServices(); |
||
261 | DataServerExplorerParameters eparams; |
||
262 | eparams = manager.createServerExplorerParameters("FilesystemExplorer");
|
||
263 | |||
264 | File destFile = null; |
||
265 | if (saveToNewRasterFile && this.folder != null) { |
||
266 | destFile = FileUtils.getFile(folder, layerName + "." + extension);
|
||
267 | } else {
|
||
268 | destFile = File.createTempFile(layerName, "." + extension); |
||
269 | } |
||
270 | |||
271 | File parent = destFile.getParentFile();
|
||
272 | if (!parent.exists()) {
|
||
273 | parent.mkdirs(); |
||
274 | } |
||
275 | |||
276 | eparams.setDynValue("initialpath", destFile);
|
||
277 | DataServerExplorer serverExplorer = manager.openServerExplorer(eparams.getExplorerName(), eparams); |
||
278 | |||
279 | NewRasterStoreParameters params = (NewRasterStoreParameters) serverExplorer.getAddParameters(providerName); |
||
280 | params.setDynValue("file", destFile);
|
||
281 | |||
282 | params.setDynValue("compress", "NONE"); |
||
283 | params.setDynValue("tfw", false); |
||
284 | |||
285 | int bands = this.bands.size(); |
||
286 | switch (bands) {
|
||
287 | case 3: |
||
288 | params.setDynValue("photometric", "RGB"); |
||
289 | break;
|
||
290 | case 4: |
||
291 | params.setDynValue("photometric", "RGB"); |
||
292 | params.setDynValue("alpha", "NON-PREMULTIPLIED"); |
||
293 | break;
|
||
294 | default:
|
||
295 | params.setDynValue("photometric", "MINISBLACK"); |
||
296 | break;
|
||
297 | } |
||
298 | |||
299 | params.setBuffer(buffer); |
||
300 | serverExplorer.add(providerName, params, true);
|
||
301 | return destFile;
|
||
302 | } |
||
303 | |||
304 | @Override
|
||
305 | public void setEnvelope(Envelope envelope) { |
||
306 | this.envelope = envelope;
|
||
307 | } |
||
308 | |||
309 | @Override
|
||
310 | public Envelope getEnvelope() {
|
||
311 | return this.envelope; |
||
312 | } |
||
313 | |||
314 | @Override
|
||
315 | public void setPixelSizeX(double pixelSizeX) { |
||
316 | this.pixelSizeX = pixelSizeX;
|
||
317 | } |
||
318 | |||
319 | @Override
|
||
320 | public double getPixelSizeX() { |
||
321 | return this.pixelSizeX; |
||
322 | } |
||
323 | |||
324 | @Override
|
||
325 | public void setPixelSizeY(double pixelSizeY) { |
||
326 | this.pixelSizeY = pixelSizeY;
|
||
327 | } |
||
328 | |||
329 | @Override
|
||
330 | public double getPixelSizeY() { |
||
331 | return this.pixelSizeY; |
||
332 | } |
||
333 | |||
334 | @Override
|
||
335 | public void setInterpolationMethod(int interpolationMethod) { |
||
336 | this.interpolationMethod = interpolationMethod;
|
||
337 | } |
||
338 | |||
339 | @Override
|
||
340 | public int getInterpolationMethod() { |
||
341 | return this.interpolationMethod; |
||
342 | } |
||
343 | |||
344 | @Override
|
||
345 | public void setBands(List<Integer> bands) { |
||
346 | this.bands = bands;
|
||
347 | } |
||
348 | |||
349 | @Override
|
||
350 | public List<Integer> getBands() { |
||
351 | return this.bands; |
||
352 | } |
||
353 | |||
354 | @Override
|
||
355 | public void setLayerNamePrefix(String layerNamePrefix) { |
||
356 | this.layerNamePrefix = layerNamePrefix;
|
||
357 | } |
||
358 | |||
359 | @Override
|
||
360 | public String getLayerNamePrefix() { |
||
361 | return this.layerNamePrefix; |
||
362 | } |
||
363 | |||
364 | @Override
|
||
365 | public void setCreateOneLayerPerBand(boolean createOneLayerPerBand) { |
||
366 | this.createOneLayerPerBand = createOneLayerPerBand;
|
||
367 | } |
||
368 | |||
369 | @Override
|
||
370 | public boolean isCreateOneLayerPerBand() { |
||
371 | return this.createOneLayerPerBand; |
||
372 | } |
||
373 | |||
374 | @Override
|
||
375 | public void setFolder(File folder) { |
||
376 | this.folder = folder;
|
||
377 | } |
||
378 | |||
379 | @Override
|
||
380 | public File getFolder() { |
||
381 | return this.folder; |
||
382 | } |
||
383 | |||
384 | @Override
|
||
385 | public void setSaveToNewRasterFile(boolean saveToNewRasterFile) { |
||
386 | this.saveToNewRasterFile = saveToNewRasterFile;
|
||
387 | } |
||
388 | |||
389 | @Override
|
||
390 | public boolean isSaveToNewRasterFile() { |
||
391 | return this.saveToNewRasterFile; |
||
392 | } |
||
393 | |||
394 | @Override
|
||
395 | public int getRows() { |
||
396 | return rows;
|
||
397 | } |
||
398 | |||
399 | @Override
|
||
400 | public void setRows(int rows) { |
||
401 | this.rows = rows;
|
||
402 | } |
||
403 | |||
404 | @Override
|
||
405 | public int getColumns() { |
||
406 | return columns;
|
||
407 | } |
||
408 | |||
409 | @Override
|
||
410 | public void setColumns(int columns) { |
||
411 | this.columns = columns;
|
||
412 | } |
||
413 | |||
414 | } |