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