root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridTools / sortRaster / SortRasterAlgorithm.java @ 59
History | View | Annotate | Download (2.63 KB)
1 | 59 | nbrodin | package es.unex.sextante.gridTools.sortRaster; |
---|---|---|---|
2 | |||
3 | import java.util.Arrays; |
||
4 | |||
5 | import es.unex.sextante.core.GeoAlgorithm; |
||
6 | import es.unex.sextante.core.AnalysisExtent; |
||
7 | import es.unex.sextante.core.Sextante; |
||
8 | import es.unex.sextante.dataObjects.IRasterLayer; |
||
9 | import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
||
10 | import es.unex.sextante.exceptions.RepeatedParameterNameException; |
||
11 | import es.unex.sextante.rasterWrappers.GridCell; |
||
12 | |||
13 | public class SortRasterAlgorithm |
||
14 | extends
|
||
15 | GeoAlgorithm { |
||
16 | |||
17 | public static final String INPUT = "INPUT"; |
||
18 | public static final String RESULT = "RESULT"; |
||
19 | |||
20 | |||
21 | @Override
|
||
22 | public void defineCharacteristics() { |
||
23 | |||
24 | setName(Sextante.getText("Sort"));
|
||
25 | setGroup(Sextante.getText("Basic_tools_for_raster_layers"));
|
||
26 | setUserCanDefineAnalysisExtent(false);
|
||
27 | try {
|
||
28 | m_Parameters.addInputRasterLayer(INPUT, Sextante.getText("Layer"), true); |
||
29 | addOutputRasterLayer(RESULT, Sextante.getText("Sorted_raster_layer"));
|
||
30 | } |
||
31 | catch (final RepeatedParameterNameException e) { |
||
32 | Sextante.addErrorToLog(e); |
||
33 | } |
||
34 | |||
35 | } |
||
36 | |||
37 | |||
38 | @Override
|
||
39 | public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
||
40 | |||
41 | int i;
|
||
42 | int iCells;
|
||
43 | GridCell cell; |
||
44 | |||
45 | final IRasterLayer layer = m_Parameters.getParameterValueAsRasterLayer(INPUT);
|
||
46 | layer.setFullExtent(); |
||
47 | final GridCell[] cells = getSortedArrayOfCells(layer); |
||
48 | final AnalysisExtent gridExtent = new AnalysisExtent(layer); |
||
49 | final IRasterLayer sorted = getNewRasterLayer(RESULT, layer.getName() + Sextante.getText("[sorted]"), |
||
50 | IRasterLayer.RASTER_DATA_TYPE_INT, gridExtent); |
||
51 | |||
52 | iCells = layer.getNX() * layer.getNY(); |
||
53 | |||
54 | for (i = 0; (i < iCells) && setProgress(i, iCells); i++) { |
||
55 | cell = cells[i]; |
||
56 | if (!layer.isNoDataValue(cell.getValue())) {
|
||
57 | sorted.setCellValue(cell.getX(), cell.getY(), i); |
||
58 | } |
||
59 | else {
|
||
60 | sorted.setNoData(cell.getX(), cell.getY()); |
||
61 | } |
||
62 | |||
63 | } |
||
64 | |||
65 | return !m_Task.isCanceled();
|
||
66 | |||
67 | } |
||
68 | |||
69 | |||
70 | public GridCell[] getSortedArrayOfCells(final IRasterLayer layer) { |
||
71 | |||
72 | int i;
|
||
73 | int iX, iY;
|
||
74 | final int iNX = layer.getNX(); |
||
75 | final int iCells = layer.getNX() * layer.getNY(); |
||
76 | GridCell[] cells;
|
||
77 | GridCell cell; |
||
78 | |||
79 | cells = new GridCell[iCells];
|
||
80 | |||
81 | for (i = 0; i < iCells; i++) { |
||
82 | iX = i % iNX; |
||
83 | iY = i / iNX; |
||
84 | cell = new GridCell(iX, iY, layer.getCellValueAsDouble(iX, iY));
|
||
85 | cells[i] = cell; |
||
86 | } |
||
87 | |||
88 | Arrays.sort(cells);
|
||
89 | |||
90 | return cells;
|
||
91 | |||
92 | } |
||
93 | |||
94 | } |