1 /**********************************************************
2  * Version $Id$
3  *********************************************************/
4 /*******************************************************************************
5     SortRaster.cpp
6     Copyright (C) Victor Olaya
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA
21 *******************************************************************************/
22 
23 #include "Grid_SortRaster.h"
24 
CSortRaster(void)25 CSortRaster::CSortRaster(void)
26 {
27 	Set_Name		(_TL("Grid Cell Index"));
28 
29 	Set_Author		("Victor Olaya (c) 2004");
30 
31 	Set_Description	(_TW(
32 		"Creates an index grid according to the cell values either in ascending or descending order."
33 	));
34 
35 	Parameters.Add_Grid(
36 		NULL	, "GRID"	, _TL("Grid"),
37 		_TL(""),
38 		PARAMETER_INPUT
39 	);
40 
41 	Parameters.Add_Grid(
42 		NULL	, "INDEX"	, _TL("Index"),
43 		_TL(""),
44 		PARAMETER_OUTPUT, true, SG_DATATYPE_Long
45 	);
46 
47 	Parameters.Add_Choice(
48 		NULL	, "ORDER"	, _TL("Sorting Order"),
49 		_TL(""),
50 		CSG_String::Format(SG_T("%s|%s|"),
51 			_TL("ascending"),
52 			_TL("descending")
53 		)
54 	);
55 }
56 
On_Execute(void)57 bool CSortRaster::On_Execute(void)
58 {
59 	CSG_Grid	*pGrid	= Parameters("GRID")->asGrid();
60 
61 	if( !pGrid->Set_Index() )
62 	{
63 		Error_Set(_TL("index creation failed"));
64 
65 		return( false );
66 	}
67 
68 	CSG_Grid	*pIndex	= Parameters("INDEX")->asGrid();
69 
70 	pIndex->Set_NoData_Value(-1.0);
71 	pIndex->Assign_NoData();
72 
73 	bool	bDown	= Parameters("ORDER")->asInt() == 1;
74 
75 	for(sLong i=0, Index=0; i<Get_NCells() && Set_Progress_NCells(i); i++)
76 	{
77 		int	ix, iy;
78 
79 		if( pGrid->Get_Sorted(i, ix, iy, bDown) )
80 		{
81 			pIndex->Set_Value(ix, iy, Index++);
82 		}
83 	}
84 
85 	return( true );
86 }
87