gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster.tools / org.gvsig.raster.tools.swing / org.gvsig.raster.tools.swing.impl / src / main / java / org / gvsig / raster / tools / swing / impl / clip / ClipPanelController.java @ 6659
History | View | Annotate | Download (37.3 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2017 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.raster.tools.swing.impl.clip; |
24 |
|
25 |
import java.awt.Image; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.awt.event.ActionListener; |
28 |
import java.io.File; |
29 |
import java.text.NumberFormat; |
30 |
import java.util.List; |
31 |
import java.util.Locale; |
32 |
|
33 |
import javax.swing.JComponent; |
34 |
import javax.swing.JFileChooser; |
35 |
import javax.swing.JOptionPane; |
36 |
import javax.swing.ListSelectionModel; |
37 |
import javax.swing.event.DocumentEvent; |
38 |
import javax.swing.event.DocumentListener; |
39 |
import javax.swing.event.ListSelectionEvent; |
40 |
import javax.swing.event.ListSelectionListener; |
41 |
import javax.swing.table.DefaultTableColumnModel; |
42 |
import javax.swing.table.TableColumn; |
43 |
import javax.swing.table.TableColumnModel; |
44 |
import javax.swing.text.DefaultFormatterFactory; |
45 |
import javax.swing.text.NumberFormatter; |
46 |
|
47 |
import org.cresques.cts.ICoordTrans; |
48 |
import org.cresques.cts.IProjection; |
49 |
import org.slf4j.Logger; |
50 |
import org.slf4j.LoggerFactory; |
51 |
|
52 |
import org.gvsig.fmap.IconThemeHelper; |
53 |
import org.gvsig.fmap.dal.exception.DataException; |
54 |
import org.gvsig.fmap.dal.raster.api.BandDescriptor; |
55 |
import org.gvsig.fmap.dal.raster.api.RasterQuery; |
56 |
import org.gvsig.fmap.dal.raster.api.RasterSet; |
57 |
import org.gvsig.fmap.dal.raster.api.RasterStore; |
58 |
import org.gvsig.fmap.geom.Geometry.DIMENSIONS; |
59 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
60 |
import org.gvsig.fmap.geom.GeometryLocator; |
61 |
import org.gvsig.fmap.geom.GeometryManager; |
62 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
63 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
64 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
65 |
import org.gvsig.fmap.geom.primitive.Envelope; |
66 |
import org.gvsig.fmap.mapcontext.raster.api.RasterLayer; |
67 |
import org.gvsig.fmap.mapcontrol.MapControl; |
68 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
69 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.RectangleBehavior; |
70 |
import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent; |
71 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener; |
72 |
import org.gvsig.raster.tools.lib.api.RasterClip; |
73 |
import org.gvsig.raster.tools.lib.api.RasterToolsLocator; |
74 |
import org.gvsig.raster.tools.lib.api.exceptions.ClipException; |
75 |
import org.gvsig.raster.tools.lib.api.exceptions.RasterToolCreatingPanelException; |
76 |
import org.gvsig.raster.tools.swing.api.ClipPanel; |
77 |
import org.gvsig.tools.ToolsLocator; |
78 |
import org.gvsig.tools.i18n.I18nManager; |
79 |
import org.gvsig.tools.locator.LocatorException; |
80 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
81 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
82 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
83 |
|
84 |
/**
|
85 |
* @author fdiaz
|
86 |
*
|
87 |
*/
|
88 |
public class ClipPanelController extends ClipPanelView implements ClipPanel, ActionListener, ListSelectionListener, DocumentListener { |
89 |
|
90 |
/**
|
91 |
*
|
92 |
*/
|
93 |
private static final long serialVersionUID = 6979744092502525949L; |
94 |
private static final Logger LOG = LoggerFactory.getLogger(ClipPanelController.class); |
95 |
private static final String DEFAULT_LAYER_NAME= "Clip"; |
96 |
private static final String CLIP_TOOL_NAME = "raster-clip-tool"; |
97 |
|
98 |
private boolean canceled; |
99 |
|
100 |
// The elements are defined in the Buffer interface, the array should not be
|
101 |
// changed.
|
102 |
private static String[] interpolationMethods = { "_nearest_neighbour", "_bilinear", "_inverse_distance", |
103 |
"_bicubic_spline", "_bspline" }; |
104 |
|
105 |
private RasterLayer layer;
|
106 |
private Envelope layerEnvelope;
|
107 |
private double layerPixelSizeX; |
108 |
private double layerPixelSizeY; |
109 |
private int layerColumns; |
110 |
private int layerRows; |
111 |
|
112 |
private Envelope envelope;
|
113 |
private boolean updatingResolution; |
114 |
private boolean updatingEnvelope; |
115 |
private boolean updatingPixels; |
116 |
private MapControl mapControl;
|
117 |
private String previousTool; |
118 |
ICoordTrans ct; |
119 |
|
120 |
/**
|
121 |
* @param layer
|
122 |
* @param mapControl
|
123 |
* @throws RasterToolCreatingPanelException
|
124 |
*
|
125 |
*/
|
126 |
public ClipPanelController(RasterLayer layer, MapControl mapControl) throws RasterToolCreatingPanelException { |
127 |
this.mapControl = mapControl;
|
128 |
this.previousTool = mapControl.getCurrentTool();
|
129 |
this.layer = layer;
|
130 |
canceled = false;
|
131 |
translate(); |
132 |
IProjection layerProj = this.layer.getProjection();
|
133 |
IProjection viewProj = mapControl.getProjection(); |
134 |
ct = null;
|
135 |
if(!layerProj.equals(viewProj)){
|
136 |
ct = layerProj.getCT(viewProj); |
137 |
} |
138 |
try {
|
139 |
layerEnvelope = this.layer.getFullEnvelope();
|
140 |
// layerEnvelope = this.layer.getRasterStore().getEnvelope();
|
141 |
} catch (LocatorException | DataException e1) {
|
142 |
LOG.warn("Can't get the envelope of the layer", e1);
|
143 |
throw new RasterToolCreatingPanelException("Can't get the envelope of the layer "+layer.getName(),e1); |
144 |
} |
145 |
// if(ct!=null && layerEnvelope!=null){
|
146 |
// layerEnvelope = layerEnvelope.convert(ct);
|
147 |
// }
|
148 |
|
149 |
RasterStore rasterStore = this.layer.getRasterStore();
|
150 |
|
151 |
try {
|
152 |
RasterSet rasterSet = rasterStore.getRasterSet(); |
153 |
layerColumns = rasterSet.getColumns(); |
154 |
layerRows = rasterSet.getRows(); |
155 |
layerPixelSizeX = layerEnvelope.getLength(DIMENSIONS.X)/layerColumns; |
156 |
layerPixelSizeY = layerEnvelope.getLength(DIMENSIONS.Y)/layerRows; |
157 |
} catch (DataException e1) {
|
158 |
LOG.warn("Can't get the raster set of the layer", e1);
|
159 |
throw new RasterToolCreatingPanelException("Can't get the raster set of the layer "+layer.getName(),e1); |
160 |
} |
161 |
|
162 |
initializeComponents(); |
163 |
|
164 |
setFullEnvelope(); |
165 |
txtCellWidth.setValue(layerPixelSizeX); |
166 |
txtCellHeight.setValue(layerPixelSizeY); |
167 |
|
168 |
updateResolution(true);
|
169 |
updatingResolution=false;
|
170 |
} |
171 |
|
172 |
@Override
|
173 |
public JComponent asJComponent() { |
174 |
return this; |
175 |
} |
176 |
|
177 |
private void initializeComponents() { |
178 |
this.btnUpBand.addActionListener(this); |
179 |
this.btnDownBand.addActionListener(this); |
180 |
tblBands.setRowSelectionAllowed(true);
|
181 |
tblBands.setColumnSelectionAllowed(false);
|
182 |
tblBands.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
183 |
tblBands.getSelectionModel().addListSelectionListener(this);
|
184 |
BandsTableModel model = new BandsTableModel(this.layer.getRasterStore()); |
185 |
tblBands.setModel(model); |
186 |
|
187 |
tblBands.setColumnModel(new DefaultTableColumnModel()); |
188 |
TableColumnModel columnModel = tblBands.getColumnModel();
|
189 |
columnModel.setColumnSelectionAllowed(false);
|
190 |
for (int i = 0; i < model.getColumnCount(); i++) { |
191 |
TableColumn tableColumn = new TableColumn(i); |
192 |
tableColumn.setIdentifier(model.getColumnName(i)); |
193 |
tableColumn.setHeaderValue(model.getColumnName(i)); |
194 |
columnModel.addColumn(tableColumn); |
195 |
} |
196 |
|
197 |
rbtnCellSize.addActionListener(this);
|
198 |
rbtnWxH.addActionListener(this);
|
199 |
|
200 |
btnFullLayer.addActionListener(this);
|
201 |
cmbInterpolationMethod.setSelectedIndex(0);
|
202 |
cmbInterpolationMethod.setEditable(false);
|
203 |
|
204 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
205 |
lblIconPix1.setIcon(iconTheme.get("upleft"));
|
206 |
lblIconPix2.setIcon(iconTheme.get("downright"));
|
207 |
lblIconReal1.setIcon(iconTheme.get("upleft"));
|
208 |
lblIconReal2.setIcon(iconTheme.get("downright"));
|
209 |
btnFullLayer.setIcon(iconTheme.get("fullextent-icon"));
|
210 |
btnViewSelection.setIcon(iconTheme.get("selecttool-icon"));
|
211 |
btnRois.setIcon(iconTheme.get("roi"));
|
212 |
btnPixelsRound.setIcon(iconTheme.get("round-icon"));
|
213 |
|
214 |
txtReal1X.getDocument().addDocumentListener(this);
|
215 |
|
216 |
txtReal1Y.getDocument().addDocumentListener(this);
|
217 |
txtReal2X.getDocument().addDocumentListener(this);
|
218 |
txtReal2Y.getDocument().addDocumentListener(this);
|
219 |
|
220 |
txtPix1X.getDocument().addDocumentListener(this);
|
221 |
txtPix1Y.getDocument().addDocumentListener(this);
|
222 |
txtPix2X.getDocument().addDocumentListener(this);
|
223 |
txtPix2Y.getDocument().addDocumentListener(this);
|
224 |
|
225 |
btnApply.addActionListener(this);
|
226 |
btnAccept.addActionListener(this);
|
227 |
btnCancel.addActionListener(this);
|
228 |
|
229 |
chkSaveTo.addActionListener(this);
|
230 |
btnFolderChooser.addActionListener(this);
|
231 |
|
232 |
txtWidth.getDocument().addDocumentListener(this);
|
233 |
txtHeight.getDocument().addDocumentListener(this);
|
234 |
|
235 |
|
236 |
btnRestore.addActionListener(this);
|
237 |
txtCellWidth.getDocument().addDocumentListener(this);
|
238 |
txtCellHeight.getDocument().addDocumentListener(this);
|
239 |
|
240 |
btnPixelsRound.addActionListener(this);
|
241 |
btnViewSelection.addActionListener(this);
|
242 |
} |
243 |
|
244 |
private void translate() { |
245 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
246 |
|
247 |
lblPixelCoord.setText(i18nManager.getTranslation(lblPixelCoord.getText())); |
248 |
lblRealCoord.setText(i18nManager.getTranslation(lblRealCoord.getText())); |
249 |
for (int i = 0; i < tabClip.getTabCount(); i++) { |
250 |
tabClip.setTitleAt(i, i18nManager.getTranslation(tabClip.getTitleAt(i))); |
251 |
} |
252 |
|
253 |
lblPix1X.setText(i18nManager.getTranslation(lblPix1X.getText())); |
254 |
lblPix1Y.setText(i18nManager.getTranslation(lblPix1Y.getText())); |
255 |
lblPix2X.setText(i18nManager.getTranslation(lblPix2X.getText())); |
256 |
lblPix2Y.setText(i18nManager.getTranslation(lblPix2Y.getText())); |
257 |
|
258 |
btnViewSelection.setText(""); //i18nManager.getTranslation(btnViewSelection.getText())); |
259 |
btnViewSelection.setToolTipText(i18nManager.getTranslation(btnViewSelection.getToolTipText())); |
260 |
|
261 |
btnFullLayer.setText(""); //i18nManager.getTranslation(btnFullLayer.getText())); |
262 |
btnFullLayer.setToolTipText(i18nManager.getTranslation(btnFullLayer.getToolTipText())); |
263 |
|
264 |
btnPixelsRound.setText(""); //i18nManager.getTranslation(btnPixelsRound.getText())); |
265 |
btnPixelsRound.setToolTipText(i18nManager.getTranslation(btnPixelsRound.getToolTipText())); |
266 |
|
267 |
btnLoad.setText(i18nManager.getTranslation(btnLoad.getText())); |
268 |
btnLoad.setToolTipText(i18nManager.getTranslation(btnLoad.getToolTipText())); |
269 |
|
270 |
btnSave.setText(i18nManager.getTranslation(btnSave.getText())); |
271 |
btnSave.setToolTipText(i18nManager.getTranslation(btnSave.getToolTipText())); |
272 |
|
273 |
btnRois.setText(""); //i18nManager.getTranslation(btnRois.getText())); |
274 |
btnRois.setToolTipText(i18nManager.getTranslation(btnRois.getToolTipText())); |
275 |
|
276 |
lblWidth.setText(i18nManager.getTranslation(lblWidth.getText())); |
277 |
lblCellWidth.setText(i18nManager.getTranslation(lblCellWidth.getText())); |
278 |
lblCellHeight.setText(i18nManager.getTranslation(lblCellHeight.getText())); |
279 |
lblHeight.setText(i18nManager.getTranslation(lblHeight.getText())); |
280 |
|
281 |
lblResolutionMode.setText(i18nManager.getTranslation(lblResolutionMode.getText())); |
282 |
lblInterpolation.setText(i18nManager.getTranslation(lblInterpolation.getText())); |
283 |
|
284 |
btnRestore.setText(i18nManager.getTranslation(btnRestore.getText())); |
285 |
btnRestore.setToolTipText(i18nManager.getTranslation(btnRestore.getToolTipText())); |
286 |
|
287 |
rbtnCellSize.setText(i18nManager.getTranslation(rbtnCellSize.getText())); |
288 |
rbtnCellSize.setToolTipText(i18nManager.getTranslation(rbtnCellSize.getToolTipText())); |
289 |
|
290 |
rbtnWxH.setText(i18nManager.getTranslation(rbtnWxH.getText())); |
291 |
rbtnWxH.setToolTipText(i18nManager.getTranslation(rbtnWxH.getToolTipText())); |
292 |
|
293 |
lblLayerNames.setText(i18nManager.getTranslation(lblLayerNames.getText())); |
294 |
txtLayerNames.setText(i18nManager.getTranslation("_newlayer"));
|
295 |
|
296 |
chkOnePerBand.setText(i18nManager.getTranslation(chkOnePerBand.getText())); |
297 |
chkOnePerBand.setToolTipText(i18nManager.getTranslation(chkOnePerBand.getToolTipText())); |
298 |
|
299 |
chkSaveTo.setText(i18nManager.getTranslation(chkSaveTo.getText())); |
300 |
chkSaveTo.setToolTipText(i18nManager.getTranslation(chkSaveTo.getToolTipText())); |
301 |
|
302 |
lblFolder.setText(i18nManager.getTranslation(lblFolder.getText())); |
303 |
|
304 |
btnFolderChooser.setText(i18nManager.getTranslation(btnFolderChooser.getText())); |
305 |
btnFolderChooser.setToolTipText(i18nManager.getTranslation(btnFolderChooser.getToolTipText())); |
306 |
|
307 |
btnApply.setText(i18nManager.getTranslation(btnApply.getText())); |
308 |
btnApply.setToolTipText(i18nManager.getTranslation(btnApply.getToolTipText())); |
309 |
|
310 |
btnAccept.setText(i18nManager.getTranslation(btnAccept.getText())); |
311 |
btnAccept.setToolTipText(i18nManager.getTranslation(btnAccept.getToolTipText())); |
312 |
|
313 |
btnCancel.setText(i18nManager.getTranslation(btnCancel.getText())); |
314 |
btnCancel.setToolTipText(i18nManager.getTranslation(btnCancel.getToolTipText())); |
315 |
|
316 |
btnUpBand.setText(i18nManager.getTranslation(btnUpBand.getText())); |
317 |
btnUpBand.setToolTipText(i18nManager.getTranslation(btnUpBand.getToolTipText())); |
318 |
|
319 |
btnDownBand.setText(i18nManager.getTranslation(btnDownBand.getText())); |
320 |
btnDownBand.setToolTipText(i18nManager.getTranslation(btnDownBand.getToolTipText())); |
321 |
|
322 |
cmbInterpolationMethod.removeAllItems(); |
323 |
|
324 |
for (int i = 0; i < interpolationMethods.length; i++) { |
325 |
cmbInterpolationMethod.addItem(i18nManager.getTranslation(interpolationMethods[i])); |
326 |
} |
327 |
|
328 |
|
329 |
NumberFormat numberInstance = NumberFormat.getNumberInstance(); |
330 |
numberInstance.setGroupingUsed(false);
|
331 |
numberInstance.setMaximumFractionDigits(Integer.MAX_VALUE);
|
332 |
NumberFormatter numberFormatter = new NumberFormatter(numberInstance); |
333 |
DefaultFormatterFactory tf = new DefaultFormatterFactory( |
334 |
numberFormatter, |
335 |
numberFormatter, |
336 |
numberFormatter, |
337 |
numberFormatter); |
338 |
txtReal1X.setFormatterFactory(tf); |
339 |
|
340 |
txtReal1Y.setFormatterFactory(tf); |
341 |
txtReal2X.setFormatterFactory(tf); |
342 |
txtReal2Y.setFormatterFactory(tf); |
343 |
txtCellWidth.setFormatterFactory(tf); |
344 |
txtCellHeight.setFormatterFactory(tf); |
345 |
|
346 |
NumberFormat integerInstance = NumberFormat.getIntegerInstance(); |
347 |
integerInstance.setGroupingUsed(false);
|
348 |
numberFormatter = new NumberFormatter(integerInstance); |
349 |
numberFormatter.setMinimum(0);
|
350 |
numberFormatter.setMaximum(layerColumns-1);
|
351 |
tf = new DefaultFormatterFactory( |
352 |
numberFormatter, |
353 |
numberFormatter, |
354 |
numberFormatter, |
355 |
numberFormatter); |
356 |
txtPix1X.setFormatterFactory(tf); |
357 |
|
358 |
numberFormatter = new NumberFormatter(integerInstance); |
359 |
numberFormatter.setMinimum(0);
|
360 |
numberFormatter.setMaximum(layerRows-1);
|
361 |
tf = new DefaultFormatterFactory( |
362 |
numberFormatter, |
363 |
numberFormatter, |
364 |
numberFormatter, |
365 |
numberFormatter); |
366 |
txtPix1Y.setFormatterFactory(tf); |
367 |
numberFormatter = new NumberFormatter(integerInstance); |
368 |
numberFormatter.setMinimum(0);
|
369 |
numberFormatter.setMaximum(layerColumns-1);
|
370 |
tf = new DefaultFormatterFactory( |
371 |
numberFormatter, |
372 |
numberFormatter, |
373 |
numberFormatter, |
374 |
numberFormatter); |
375 |
txtPix2X.setFormatterFactory(tf); |
376 |
numberFormatter = new NumberFormatter(integerInstance); |
377 |
numberFormatter.setMinimum(0);
|
378 |
numberFormatter.setMaximum(layerRows-1);
|
379 |
tf = new DefaultFormatterFactory( |
380 |
numberFormatter, |
381 |
numberFormatter, |
382 |
numberFormatter, |
383 |
numberFormatter); |
384 |
txtPix2Y.setFormatterFactory(tf); |
385 |
|
386 |
numberFormatter = new NumberFormatter(integerInstance); |
387 |
numberFormatter.setMinimum(0);
|
388 |
tf = new DefaultFormatterFactory( |
389 |
numberFormatter, |
390 |
numberFormatter, |
391 |
numberFormatter, |
392 |
numberFormatter); |
393 |
txtWidth.setFormatterFactory(tf); |
394 |
txtHeight.setFormatterFactory(tf); |
395 |
|
396 |
} |
397 |
|
398 |
/**
|
399 |
* @param locale
|
400 |
*
|
401 |
*/
|
402 |
public void setLocate(Locale locale) { |
403 |
Locale l = super.getLocale(); |
404 |
if (!l.equals(locale)) {
|
405 |
translate(); |
406 |
} |
407 |
super.setLocale(locale);
|
408 |
} |
409 |
|
410 |
@Override
|
411 |
public void valueChanged(ListSelectionEvent e) { |
412 |
if (e.getSource() == tblBands) {
|
413 |
if (tblBands.getSelectedRowCount() == 1) { |
414 |
if (tblBands.getSelectedRow() > 0) { |
415 |
btnUpBand.setEnabled(true);
|
416 |
} else {
|
417 |
btnUpBand.setEnabled(false);
|
418 |
} |
419 |
if (tblBands.getSelectedRow() < tblBands.getRowCount() - 1) { |
420 |
btnDownBand.setEnabled(true);
|
421 |
} else {
|
422 |
btnDownBand.setEnabled(false);
|
423 |
} |
424 |
} else {
|
425 |
btnUpBand.setEnabled(false);
|
426 |
btnDownBand.setEnabled(false);
|
427 |
} |
428 |
} |
429 |
} |
430 |
|
431 |
@Override
|
432 |
public void actionPerformed(ActionEvent e) { |
433 |
if (e.getSource() == rbtnCellSize || e.getSource() == rbtnWxH) {
|
434 |
lblCellHeight.setEnabled(rbtnCellSize.isSelected()); |
435 |
txtCellWidth.setEnabled(rbtnCellSize.isSelected()); |
436 |
lblCellWidth.setEnabled(rbtnCellSize.isSelected()); |
437 |
txtCellHeight.setEnabled(rbtnCellSize.isSelected()); |
438 |
|
439 |
lblWidth.setEnabled(rbtnWxH.isSelected()); |
440 |
txtWidth.setEnabled(rbtnWxH.isSelected()); |
441 |
lblHeight.setEnabled(rbtnWxH.isSelected()); |
442 |
txtHeight.setEnabled(rbtnWxH.isSelected()); |
443 |
} |
444 |
|
445 |
RasterStore rasterStore = this.layer.getRasterStore();
|
446 |
if (e.getSource() == btnFullLayer) {
|
447 |
setFullEnvelope(); |
448 |
updateResolution(true);
|
449 |
} |
450 |
|
451 |
if (e.getSource() == btnRois) {
|
452 |
try {
|
453 |
// FIXME: Ver qu? hacemos con las ROIs y coger el envelope
|
454 |
Envelope envelope = GeometryLocator.getGeometryManager().createEnvelope(SUBTYPES.GEOM2D); |
455 |
setEnvelope(envelope); |
456 |
RasterQuery query = rasterStore.createRasterQuery().setClip(envelope); |
457 |
RasterSet rasterSet = rasterStore.getRasterSet(query); |
458 |
setPixels(0, 0, rasterSet.getColumns() - 1, rasterSet.getRows() - 1); |
459 |
} catch (LocatorException | CreateEnvelopeException e1) {
|
460 |
LOG.warn("Can't get the envelope of the rois of the layer", e1);
|
461 |
// TODO Gestionar la excepci?n ?mostrar di?logo de advertencia?
|
462 |
} catch (DataException e1) {
|
463 |
LOG.warn("Can't get the raster set of the envelope of the ROIs of the layer", e1);
|
464 |
// TODO Gestionar la excepci?n ?mostrar di?logo de advertencia?
|
465 |
} |
466 |
updateResolution(false);
|
467 |
} |
468 |
|
469 |
if (e.getSource() == btnViewSelection) {
|
470 |
|
471 |
if(!CLIP_TOOL_NAME.equalsIgnoreCase(this.mapControl.getCurrentTool())){ |
472 |
this.previousTool = this.mapControl.getCurrentTool(); |
473 |
} |
474 |
|
475 |
RectangleBehavior behavior = new RectangleBehavior(new RectangleListener() { |
476 |
|
477 |
@Override
|
478 |
public Image getImageCursor() { |
479 |
return IconThemeHelper.getImage("cursor-select-by-rectangle"); |
480 |
} |
481 |
|
482 |
@Override
|
483 |
public boolean cancelDrawing() { |
484 |
return false; |
485 |
} |
486 |
|
487 |
@Override
|
488 |
public void rectangle(EnvelopeEvent event) throws BehaviorException { |
489 |
Envelope selectedEnvelope = event.getWorldCoordRect(); |
490 |
if(selectedEnvelope.intersects(ClipPanelController.this.layerEnvelope)){
|
491 |
Envelope intersection = null;
|
492 |
try {
|
493 |
intersection = (selectedEnvelope.getGeometry().intersection(ClipPanelController.this.layerEnvelope.getGeometry())).getEnvelope(); |
494 |
} catch (GeometryOperationNotSupportedException | GeometryOperationException e) {
|
495 |
LOG.warn("Can't gets intersection between selected envelope and the layer's envelope", e);
|
496 |
} |
497 |
|
498 |
if(intersection!=null){ |
499 |
ClipPanelController.this.setEnvelope(intersection); |
500 |
updatingPixels = true;
|
501 |
if(ClipPanelController.this.updatePixelsFromEnvelope()) {
|
502 |
updateSize(); |
503 |
}; |
504 |
updatingPixels = false;
|
505 |
|
506 |
} |
507 |
} |
508 |
ClipPanelController.this.returnMapControlToPreviousTool(); |
509 |
} |
510 |
}); |
511 |
|
512 |
// if(!this.mapControl.hasTool(CLIP_TOOL_NAME)){
|
513 |
this.mapControl.addBehavior(CLIP_TOOL_NAME, behavior);
|
514 |
// }
|
515 |
this.mapControl.setTool(CLIP_TOOL_NAME);
|
516 |
} |
517 |
|
518 |
BandsTableModel bandsTableModel = (BandsTableModel) tblBands.getModel(); |
519 |
if (e.getSource() == btnUpBand) {
|
520 |
int selectedRow = tblBands.getSelectedRow();
|
521 |
bandsTableModel.up(selectedRow); |
522 |
tblBands.getSelectionModel().setSelectionInterval(selectedRow - 1, selectedRow - 1); |
523 |
} |
524 |
|
525 |
if (e.getSource() == btnDownBand) {
|
526 |
int selectedRow = tblBands.getSelectedRow();
|
527 |
bandsTableModel.down(selectedRow); |
528 |
tblBands.getSelectionModel().setSelectionInterval(selectedRow + 1, selectedRow + 1); |
529 |
} |
530 |
|
531 |
if (e.getSource() == btnCancel) {
|
532 |
canceled=true;
|
533 |
doClose(); |
534 |
} |
535 |
|
536 |
if (e.getSource() == btnApply) {
|
537 |
canceled=false;
|
538 |
doApply(); |
539 |
} |
540 |
|
541 |
if (e.getSource() == btnAccept) {
|
542 |
canceled=false;
|
543 |
doAccept(); |
544 |
} |
545 |
|
546 |
if (e.getSource() == chkSaveTo) {
|
547 |
boolean selected = chkSaveTo.isSelected();
|
548 |
txtFolder.setEnabled(selected); |
549 |
btnFolderChooser.setEnabled(selected); |
550 |
lblFolder.setEnabled(selected); |
551 |
} |
552 |
|
553 |
if (e.getSource() == btnFolderChooser) {
|
554 |
//FIXME:
|
555 |
JFileChooser fc = null; |
556 |
if(!txtFolder.getText().isEmpty()){
|
557 |
fc = new JFileChooser(txtFolder.getText()); |
558 |
} else {
|
559 |
fc = new JFileChooser(); |
560 |
} |
561 |
|
562 |
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
563 |
fc.setMultiSelectionEnabled(false);
|
564 |
int result = fc.showOpenDialog(this); |
565 |
if(result == JFileChooser.APPROVE_OPTION){ |
566 |
txtFolder.setText(fc.getSelectedFile().getAbsolutePath()); |
567 |
} |
568 |
} |
569 |
|
570 |
if (e.getSource() == btnRestore) {
|
571 |
updateResolution(true);
|
572 |
} |
573 |
|
574 |
if (e.getSource() == btnPixelsRound) {
|
575 |
if(updateEnvelopeFromPixels()){
|
576 |
updateSize(); |
577 |
} |
578 |
// updatePixelsFromEnvelope();
|
579 |
} |
580 |
|
581 |
} |
582 |
|
583 |
protected void returnMapControlToPreviousTool() { |
584 |
mapControl.setTool(this.previousTool);
|
585 |
|
586 |
} |
587 |
|
588 |
/**
|
589 |
*
|
590 |
*/
|
591 |
private void setFullEnvelope() { |
592 |
this.envelope = this.layerEnvelope; |
593 |
setPixels(0, 0, layerColumns - 1, layerRows - 1); |
594 |
setEnvelope(envelope); |
595 |
} |
596 |
|
597 |
private void doApply() { |
598 |
|
599 |
|
600 |
Envelope envelope = calculateEnvelope(); |
601 |
Double pixelSizeX = calculatePixelSizeX();
|
602 |
Double pixelSizeY = calculatePixelSizeY();
|
603 |
int interpolationMethod = cmbInterpolationMethod.getSelectedIndex()+1; |
604 |
String layerNamePrefix = txtLayerNames.getText();
|
605 |
if(layerNamePrefix.isEmpty()){
|
606 |
layerNamePrefix = DEFAULT_LAYER_NAME; |
607 |
} |
608 |
boolean createOneLayerPerBand = chkOnePerBand.isSelected();
|
609 |
boolean saveToNewRasterFile = chkSaveTo.isSelected();
|
610 |
|
611 |
// List<BandDescriptor> bands = ((BandsTableModel)tblBands.getModel()).getSelectedBands();
|
612 |
List<Integer> bands = ((BandsTableModel)tblBands.getModel()).getSelectedBands(); |
613 |
RasterClip rasterClip = null;
|
614 |
if(saveToNewRasterFile){
|
615 |
String path = txtFolder.getText();
|
616 |
if(path.isEmpty()){
|
617 |
//TODO: ?Exception?
|
618 |
} |
619 |
File folder = new File(txtFolder.getText()); |
620 |
rasterClip = RasterToolsLocator.getManager().createRasterClip( |
621 |
this.layer,
|
622 |
envelope, |
623 |
pixelSizeX, |
624 |
pixelSizeY, |
625 |
calculateColumns(), |
626 |
calculateRows(), |
627 |
interpolationMethod, |
628 |
bands, |
629 |
layerNamePrefix, createOneLayerPerBand, saveToNewRasterFile, folder); |
630 |
} else {
|
631 |
rasterClip = RasterToolsLocator.getManager().createRasterClip( |
632 |
this.layer,
|
633 |
envelope, |
634 |
pixelSizeX, |
635 |
pixelSizeY, |
636 |
calculateColumns(), |
637 |
calculateRows(), |
638 |
interpolationMethod, |
639 |
bands, |
640 |
layerNamePrefix, createOneLayerPerBand); |
641 |
} |
642 |
if (rasterClip != null) { |
643 |
try {
|
644 |
rasterClip.execute(null);
|
645 |
} catch (ClipException e) {
|
646 |
LOG.warn("Can't create raster clip.", e);
|
647 |
|
648 |
ThreadSafeDialogsManager dlgManager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
649 |
String message = "_cant_create_raster_clip"; |
650 |
String title = "_error_creating_clip"; |
651 |
dlgManager.messageDialog(message, title, JOptionPane.WARNING_MESSAGE);
|
652 |
} |
653 |
} |
654 |
} |
655 |
|
656 |
private void doAccept() { |
657 |
doApply(); |
658 |
doClose(); |
659 |
} |
660 |
|
661 |
private void doClose() { |
662 |
setVisible(false);
|
663 |
returnMapControlToPreviousTool(); |
664 |
} |
665 |
|
666 |
private void updateResolution(boolean updatePixelSize) { |
667 |
updatingResolution=true;
|
668 |
txtWidth.setValue(calculateColumns()); |
669 |
txtHeight.setValue(calculateRows()); |
670 |
if(updatePixelSize){
|
671 |
txtCellWidth.setValue(calculatePixelSizeX()); |
672 |
txtCellHeight.setValue(calculatePixelSizeY()); |
673 |
} |
674 |
updatingResolution=false;
|
675 |
} |
676 |
|
677 |
private Integer calculateColumns() { |
678 |
Integer x1 = (Integer) txtPix1X.getValue(); |
679 |
Integer x2 = (Integer) txtPix2X.getValue(); |
680 |
return x2 - x1 + 1; |
681 |
} |
682 |
|
683 |
private Integer calculateRows() { |
684 |
Integer y1 = (Integer) txtPix1Y.getValue(); |
685 |
Integer y2 = (Integer) txtPix2Y.getValue(); |
686 |
return y2 - y1 + 1; |
687 |
} |
688 |
|
689 |
private Double calculatePixelSizeX() { |
690 |
return this.envelope.getLength(DIMENSIONS.X) / calculateColumns(); |
691 |
} |
692 |
|
693 |
private Double calculatePixelSizeY() { |
694 |
return this.envelope.getLength(DIMENSIONS.Y) / calculateRows(); |
695 |
} |
696 |
|
697 |
private void setEnvelope(Envelope envelope) { |
698 |
updatingEnvelope = true;
|
699 |
this.envelope = envelope;
|
700 |
txtReal1X.setValue(envelope.getMinimum(DIMENSIONS.X)); |
701 |
txtReal1Y.setValue(envelope.getMaximum(DIMENSIONS.Y)); |
702 |
txtReal2X.setValue(envelope.getMaximum(DIMENSIONS.X)); |
703 |
txtReal2Y.setValue(envelope.getMinimum(DIMENSIONS.Y)); |
704 |
updatingEnvelope = false;
|
705 |
} |
706 |
|
707 |
private void setPixels(int x1, int y1, int x2, int y2) { |
708 |
updatingPixels = true;
|
709 |
txtPix1X.setValue(new Integer(x1)); |
710 |
txtPix1Y.setValue(new Integer(y1)); |
711 |
txtPix2X.setValue(new Integer(x2)); |
712 |
txtPix2Y.setValue(new Integer(y2)); |
713 |
updatingPixels = false;
|
714 |
} |
715 |
|
716 |
private int getInterpolationMethod() { |
717 |
return cmbInterpolationMethod.getSelectedIndex() + 1; |
718 |
} |
719 |
|
720 |
/*
|
721 |
* Creates an envelope from the data of the corresponding text fields.
|
722 |
*/
|
723 |
private Envelope calculateEnvelope() {
|
724 |
// Coercion coercion = new CoerceToDouble();
|
725 |
boolean fail = false; |
726 |
Double x1 = ((Number) txtReal1X.getValue()).doubleValue(); |
727 |
Double y1 = ((Number) txtReal1Y.getValue()).doubleValue(); |
728 |
Double x2 = ((Number) txtReal2X.getValue()).doubleValue(); |
729 |
Double y2 = ((Number) txtReal2Y.getValue()).doubleValue(); |
730 |
try {
|
731 |
if (x1 != null && y1 != null && x2 != null && y2 != null && x1 != null && x2 >= x1 && y2 <= y1) { |
732 |
this.envelope = GeometryLocator.getGeometryManager().createEnvelope(x1, y2, x2, y1, SUBTYPES.GEOM2D);
|
733 |
} else {
|
734 |
fail = true;
|
735 |
} |
736 |
} catch (Exception e2) { |
737 |
fail = true;
|
738 |
} |
739 |
|
740 |
if (fail) {
|
741 |
ThreadSafeDialogsManager dlgManager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
742 |
String message =
|
743 |
"_cant_create_the_envelope_with_this_coordinates_XcolonX_XparenthesisX_XUL1X_XUL2X_XdashX_XDR1X_XDR2X_XparenthesisX";
|
744 |
String title = "_error_updating_envelope"; |
745 |
dlgManager.messageDialog(message, |
746 |
new String[] { txtReal1X.getText(), txtReal2Y.getText(), txtReal2X.getText(), txtReal1Y.getText() }, |
747 |
title, JOptionPane.WARNING_MESSAGE);
|
748 |
LOG.warn(message); |
749 |
} |
750 |
return this.envelope; |
751 |
} |
752 |
|
753 |
@Override
|
754 |
public boolean isCanceled() { |
755 |
return canceled;
|
756 |
} |
757 |
|
758 |
@Override
|
759 |
public void insertUpdate(DocumentEvent e) { |
760 |
changedUpdate(e); |
761 |
} |
762 |
|
763 |
@Override
|
764 |
public void removeUpdate(DocumentEvent e) { |
765 |
changedUpdate(e); |
766 |
} |
767 |
|
768 |
@Override
|
769 |
public void changedUpdate(DocumentEvent e) { |
770 |
if (e.getDocument() == txtWidth.getDocument()) {
|
771 |
if(updatingResolution){
|
772 |
return;
|
773 |
} |
774 |
updatingResolution = true;
|
775 |
Integer value = (Integer) txtWidth.getValue(); |
776 |
Envelope envelope = this.envelope; //calculateEnvelope(); |
777 |
double pixelSizeX = envelope.getLength(DIMENSIONS.X) / value;
|
778 |
double ratio = layerPixelSizeX/layerPixelSizeY;
|
779 |
double pixelSizeY = pixelSizeX/ratio;
|
780 |
txtCellWidth.setValue(pixelSizeX); |
781 |
txtCellHeight.setValue(pixelSizeY); |
782 |
txtHeight.setValue((int)Math.round(envelope.getLength(DIMENSIONS.Y) / pixelSizeY)); |
783 |
updatingResolution = false;
|
784 |
} |
785 |
if (e.getDocument() == txtHeight.getDocument()) {
|
786 |
if(updatingResolution){
|
787 |
return;
|
788 |
} |
789 |
updatingResolution = true;
|
790 |
Integer value = (Integer) txtHeight.getValue(); |
791 |
Envelope envelope = this.envelope; //calculateEnvelope(); |
792 |
double pixelSizeY = envelope.getLength(DIMENSIONS.Y) / value;
|
793 |
double ratio = layerPixelSizeX/layerPixelSizeY;
|
794 |
double pixelSizeX = pixelSizeY*ratio;
|
795 |
txtCellWidth.setValue(pixelSizeX); |
796 |
txtCellHeight.setValue(pixelSizeY); |
797 |
txtWidth.setValue((int)Math.round(envelope.getLength(DIMENSIONS.X) / pixelSizeX)); |
798 |
updatingResolution = false;
|
799 |
} |
800 |
if (e.getDocument() == txtCellWidth.getDocument()) {
|
801 |
if(updatingResolution){
|
802 |
return;
|
803 |
} |
804 |
updatingResolution = true;
|
805 |
Double pixelSizeX = ((Number) txtCellWidth.getValue()).doubleValue(); |
806 |
if (pixelSizeX != null) { |
807 |
Envelope envelope = this.envelope; //calculateEnvelope(); |
808 |
double ratio = layerPixelSizeX/layerPixelSizeY;
|
809 |
double pixelSizeY = pixelSizeX/ratio;
|
810 |
txtCellHeight.setValue(pixelSizeY); |
811 |
txtWidth.setValue((int) Math.round(envelope.getLength(DIMENSIONS.X) / pixelSizeX)); |
812 |
txtHeight.setValue((int) Math.round(envelope.getLength(DIMENSIONS.Y) / pixelSizeY)); |
813 |
} |
814 |
updatingResolution = false;
|
815 |
} |
816 |
|
817 |
if (e.getDocument() == txtCellHeight.getDocument()) {
|
818 |
if(updatingResolution){
|
819 |
return;
|
820 |
} |
821 |
updatingResolution = true;
|
822 |
Double pixelSizeY = ((Number) txtCellHeight.getValue()).doubleValue(); |
823 |
if (pixelSizeY != null) { |
824 |
Envelope envelope = this.envelope; //calculateEnvelope(); |
825 |
double ratio = layerPixelSizeX/layerPixelSizeY;
|
826 |
double pixelSizeX = pixelSizeY*ratio;
|
827 |
txtCellWidth.setValue(pixelSizeX); |
828 |
txtWidth.setValue((int) Math.round(envelope.getLength(DIMENSIONS.X) / pixelSizeX)); |
829 |
txtHeight.setValue((int) Math.round(envelope.getLength(DIMENSIONS.Y) / pixelSizeY)); |
830 |
} |
831 |
updatingResolution = false;
|
832 |
} |
833 |
|
834 |
if(e.getDocument() == txtPix1X.getDocument() || e.getDocument() == txtPix2X.getDocument() || e.getDocument() == txtPix1Y.getDocument() || e.getDocument() == txtPix2Y.getDocument()){
|
835 |
if(updatingPixels){
|
836 |
return;
|
837 |
} |
838 |
if(txtPix1X.getValue()==null || txtPix2X.getValue()==null || txtPix1Y.getValue()==null || txtPix2Y.getValue()==null){ |
839 |
return;
|
840 |
} |
841 |
updatingEnvelope = true;
|
842 |
if(updateEnvelopeFromPixels()){
|
843 |
updateSize(); |
844 |
}; |
845 |
updatingEnvelope = false;
|
846 |
} |
847 |
|
848 |
if(e.getDocument() == txtReal1X.getDocument() || e.getDocument() == txtReal2X.getDocument() || e.getDocument() == txtReal1Y.getDocument() || e.getDocument() == txtReal2Y.getDocument()){
|
849 |
if(updatingEnvelope){
|
850 |
return;
|
851 |
} |
852 |
if(txtReal1X.getValue()==null || txtReal2X.getValue()==null || txtReal1Y.getValue()==null || txtReal2Y.getValue()==null){ |
853 |
return;
|
854 |
} |
855 |
updatingPixels = true;
|
856 |
if(updatePixelsFromEnvelope()){
|
857 |
updateSize(); |
858 |
}; |
859 |
updatingPixels = false;
|
860 |
} |
861 |
} |
862 |
|
863 |
private void updateSize() { |
864 |
double pixelSizeX = ((Number)txtCellWidth.getValue()).doubleValue(); |
865 |
double ratio = layerPixelSizeX/layerPixelSizeY;
|
866 |
|
867 |
txtWidth.setValue((int)Math.round(this.envelope.getLength(DIMENSIONS.X) / pixelSizeX)); |
868 |
txtHeight.setValue((int)Math.round(this.envelope.getLength(DIMENSIONS.Y) / (pixelSizeX/ratio))); |
869 |
} |
870 |
|
871 |
private boolean updatePixelsFromEnvelope() { |
872 |
updatingPixels = true;
|
873 |
Double real1X = ((Number) txtReal1X.getValue()).doubleValue(); |
874 |
Double real2X = ((Number) txtReal2X.getValue()).doubleValue(); |
875 |
Double real1Y = ((Number) txtReal1Y.getValue()).doubleValue(); |
876 |
Double real2Y = ((Number) txtReal2Y.getValue()).doubleValue(); |
877 |
if (real1X == null || real2X < real1X || real1X < layerEnvelope.getMinimum(DIMENSIONS.X)) { |
878 |
real1X = layerEnvelope.getMinimum(DIMENSIONS.X); |
879 |
} |
880 |
if (real1Y == null || real2Y > real1Y || real1Y > layerEnvelope.getMaximum(DIMENSIONS.Y)) { |
881 |
real1Y = layerEnvelope.getMaximum(DIMENSIONS.Y); |
882 |
} |
883 |
if (real2X == null || real2X < real1X || real2X > layerEnvelope.getMaximum(DIMENSIONS.X)) { |
884 |
real2X = layerEnvelope.getMaximum(DIMENSIONS.X); |
885 |
} |
886 |
if (real2Y == null || real2Y > real1Y || real2Y < layerEnvelope.getMinimum(DIMENSIONS.Y)) { |
887 |
real2Y = layerEnvelope.getMinimum(DIMENSIONS.Y); |
888 |
} |
889 |
|
890 |
setPixels((int) Math.round((real1X - layerEnvelope.getMinimum(DIMENSIONS.X)) / layerPixelSizeX), |
891 |
(int) Math.round((layerEnvelope.getMaximum(DIMENSIONS.Y) - real1Y) / layerPixelSizeY), |
892 |
(int) Math.round((real2X - layerEnvelope.getMinimum(DIMENSIONS.X)) / layerPixelSizeX) - 1, |
893 |
(int) Math.round((layerEnvelope.getMaximum(DIMENSIONS.Y) - real2Y) / layerPixelSizeY) - 1); |
894 |
updatingPixels = false;
|
895 |
return true; |
896 |
} |
897 |
|
898 |
|
899 |
private boolean updateEnvelopeFromPixels() { |
900 |
updatingEnvelope = true;
|
901 |
|
902 |
Integer pix1X = null; |
903 |
pix1X = (Integer) txtPix1X.getValue();
|
904 |
Integer pix2X = null; |
905 |
pix2X = (Integer) txtPix2X.getValue();
|
906 |
Integer pix1Y = null; |
907 |
pix1Y = (Integer) txtPix1Y.getValue();
|
908 |
Integer pix2Y = null; |
909 |
pix2Y = (Integer) txtPix2Y.getValue();
|
910 |
if (pix1X == null || pix1X < 0 || pix2X < pix1X) { |
911 |
pix1X = 0;
|
912 |
} |
913 |
if (pix1Y == null || pix1Y < 0 || pix2Y < pix1Y) { |
914 |
pix1Y = 0;
|
915 |
} |
916 |
if (pix2X == null || pix2X < pix1X || pix2X >= layerColumns) { |
917 |
pix2X = layerColumns - 1;
|
918 |
} |
919 |
if (pix2Y == null || pix2Y < pix1Y || pix2Y >= layerRows) { |
920 |
pix2Y = layerRows - 1;
|
921 |
} |
922 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
923 |
try {
|
924 |
setEnvelope(geomManager.createEnvelope( |
925 |
pix1X * layerPixelSizeX + layerEnvelope.getMinimum(DIMENSIONS.X), |
926 |
layerEnvelope.getMaximum(DIMENSIONS.Y) - ((pix2Y + 1) * layerPixelSizeY) ,
|
927 |
(pix2X + 1) * layerPixelSizeX + layerEnvelope.getMinimum(DIMENSIONS.X),
|
928 |
layerEnvelope.getMaximum(DIMENSIONS.Y) - (pix1Y * layerPixelSizeY) , |
929 |
SUBTYPES.GEOM2D)); |
930 |
updatingEnvelope = false;
|
931 |
return true; |
932 |
} catch (CreateEnvelopeException e) {
|
933 |
LOG.debug("Can't create the envelope", e);
|
934 |
updatingEnvelope = false;
|
935 |
return false; |
936 |
} |
937 |
} |
938 |
} |