//---------------------------------------------------------
bool CMine_Sweeper::MakeBoard(int level)
{
	int		i, x, y;
	CSG_Colors	Colors;

	switch( level )
	{
		case 0:	Mine_NX = 8;	Mine_NY = 8;	N_Mines=10;
			break;
		
		case 1: Mine_NX = 16;	Mine_NY = 16;	N_Mines=40;
			break;
		
		case 2: Mine_NX = 30;	Mine_NY = 16;	N_Mines=99;
			break;
	}

	pInput	= SG_Create_Grid(SG_DATATYPE_Int,SPRITE_SIZE*Mine_NX, SPRITE_SIZE*Mine_NY);
	pInput->Set_Name(_TL("Mine Sweeper"));
	Parameters("GRID")->Set_Value(pInput);

	//-----------------------------------------------------
	CSG_Parameter	*pLUT	= DataObject_Get_Parameter(pInput, "LUT");

	if( pLUT && pLUT->asTable() )
	{
		pLUT->asTable()->Del_Records();

		for(i=0; i<16; i++)
		{
			CSG_Table_Record	*pRecord	= pLUT->asTable()->Add_Record();
			
			pRecord->Set_Value(0, SG_GET_RGB(mine_res_color[i*3], mine_res_color[i*3+1], mine_res_color[i*3+2]));
			pRecord->Set_Value(3, i);
		}

		DataObject_Set_Parameter(pInput, pLUT);
		DataObject_Set_Parameter(pInput, "COLORS_TYPE", 1);	// Color Classification Type: Lookup Table
	}

	Colors.Set_Count(16);
	for ( i=0;i<16; i++)
	{
		Colors.Set_Color(i, SG_GET_RGB(mine_res_color[i*3],	mine_res_color[i*3+1],	mine_res_color[i*3+2]));
	}
	DataObject_Set_Colors(pInput, Colors);
	DataObject_Update(pInput, 0.0, 15.0, true);

	//-----------------------------------------------------
	for(  y = 0; y <  Mine_NY; y++)	
	for(  x = 0; x <  Mine_NX; x++)
	{
		SetSprite(x,y,SPRITE_CLOSE);
	}

	pInput->Set_Value(0, 0);

	return true;			
}
Example #2
0
//---------------------------------------------------------
void CGrid_Cluster_Analysis::Save_LUT(CSG_Grid *pCluster)
{
	CSG_Parameter	*pLUT	= DataObject_Get_Parameter(pCluster, "LUT");

	if( pLUT && pLUT->asTable() )
	{
		CSG_Parameter_Grid_List	*pGrids	= Parameters("GRIDS")->asGridList();

		CSG_Table	&Statistics	= *Parameters("STATISTICS")->asTable();

		bool	bRGB	= pGrids->Get_Count() >= 3 && Parameters("RGB_COLORS")->asBool();

		for(int iCluster=0; iCluster<Statistics.Get_Count(); iCluster++)
		{
			CSG_Table_Record	*pClass	= pLUT->asTable()->Get_Record(iCluster);

			if( !pClass )
			{
				(pClass	= pLUT->asTable()->Add_Record())->Set_Value(0, SG_Color_Get_Random());
			}

			pClass->Set_Value(1, CSG_String::Format("%s %d", _TL("Cluster"), iCluster + 1));
			pClass->Set_Value(2, "");
			pClass->Set_Value(3, iCluster + 1);
			pClass->Set_Value(4, iCluster + 1);

			if( bRGB )
			{
				#define SET_COLOR_COMPONENT(c, i)	c = (int)(127 + (Statistics[iCluster].asDouble(3 + i) - pGrids->asGrid(i)->Get_Mean()) * 127 / pGrids->asGrid(i)->Get_StdDev()); if( c < 0 ) c = 0; else if( c > 255 ) c = 255;

				int	r; SET_COLOR_COMPONENT(r, 2);
				int	g; SET_COLOR_COMPONENT(g, 1);
				int	b; SET_COLOR_COMPONENT(b, 0);

				pClass->Set_Value(0, SG_GET_RGB(r, g, b));
			}
		}

		pLUT->asTable()->Set_Record_Count(Statistics.Get_Count());

		DataObject_Set_Parameter(pCluster, pLUT);
		DataObject_Set_Parameter(pCluster, "COLORS_TYPE", 1);	// Color Classification Type: Lookup Table
	}
}
Example #3
0
//---------------------------------------------------------
bool CCMD_Module::_Get_Parameters(CSG_Parameters *pParameters)
{
    if( !pParameters || m_CMD.Parse(false) != 0 )
    {
        return( false );
    }

    //-----------------------------------------------------
    int		i;

    for(i=0; i<pParameters->Get_Count(); i++)
    {
        CSG_Parameter	*pParameter	= pParameters->Get_Parameter(i);

        if( pParameter->is_Input() )
        {
            // nop now, loading options first
        }

        else if( pParameter->is_Output() )
        {
            if( pParameter->is_DataObject() && pParameter->is_Optional() && !pParameter->asDataObject() && m_CMD.Found(_Get_ID(pParameter)) )
            {
                pParameter->Set_Value(DATAOBJECT_CREATE);
            }
        }

        else if( pParameter->is_Option() && !pParameter->is_Information() )
        {
            long		l;
            double		d;
            wxString	s;

            switch( pParameter->Get_Type() )
            {
            default:
                break;

            case PARAMETER_TYPE_Parameters:
                _Get_Parameters(pParameter->asParameters());
                break;

            case PARAMETER_TYPE_Bool:
                pParameter->Set_Value(m_CMD.Found(_Get_ID(pParameter)) ? 1 : 0);
                break;

            case PARAMETER_TYPE_Int:
                if( m_CMD.Found(_Get_ID(pParameter), &l) )
                {
                    pParameter->Set_Value((int)l);
                }
                break;

            case PARAMETER_TYPE_Choice:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    if( s.ToLong(&l) )
                    {
                        pParameter->Set_Value((int)l);
                    }
                    else
                    {
                        pParameter->Set_Value(CSG_String(&s));
                    }
                }
                break;

            case PARAMETER_TYPE_Double:
            case PARAMETER_TYPE_Degree:
                if( m_CMD.Found(_Get_ID(pParameter), &s) && s.ToDouble(&d) )
                {
                    pParameter->Set_Value(d);
                }
                break;

            case PARAMETER_TYPE_Range:
                if( m_CMD.Found(_Get_ID(pParameter, wxT("MIN")), &s) && s.ToDouble(&d) )
                {
                    pParameter->asRange()->Set_LoVal(d);
                }

                if( m_CMD.Found(_Get_ID(pParameter, wxT("MAX")), &s) && s.ToDouble(&d) )
                {
                    pParameter->asRange()->Set_HiVal(d);
                }
                break;

            case PARAMETER_TYPE_String:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    pParameter->Set_Value(CSG_String(&s));
                }
                break;

            case PARAMETER_TYPE_Text:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    CSG_File	Stream;

                    if( Stream.Open(CSG_String(&s)) )
                    {
                        CSG_String	t;

                        Stream.Read(t, Stream.Length());

                        pParameter->Set_Value(t.c_str());
                    }
                    else
                    {
                        pParameter->Set_Value(CSG_String(&s));
                    }
                }
                break;

            case PARAMETER_TYPE_FilePath:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    if( pParameter->asFilePath()->is_Multiple() )
                    {
                        s.Prepend(wxT("\""));
                        s.Replace(wxT(";"), wxT("\" \""));
                        s.Append (wxT("\""));
                    }

                    pParameter->Set_Value(CSG_String(&s));
                }
                break;

            case PARAMETER_TYPE_FixedTable:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    CSG_Table	Table(&s);
                    pParameter->asTable()->Assign_Values(&Table);
                }
                break;

            case PARAMETER_TYPE_Grid_System:
                if( pParameter->Get_Children_Count() == 0 )
                {
                    long	nx, ny;
                    double	d, x, y;

                    if(	!m_CMD.Found(_Get_ID(pParameter, wxT("NX")), &nx)
                            ||	!m_CMD.Found(_Get_ID(pParameter, wxT("NY")), &ny)
                            ||	!m_CMD.Found(_Get_ID(pParameter, wxT( "X")), &s) || !s.ToDouble(&x)
                            ||	!m_CMD.Found(_Get_ID(pParameter, wxT( "Y")), &s) || !s.ToDouble(&y)
                            ||	!m_CMD.Found(_Get_ID(pParameter, wxT( "D")), &s) || !s.ToDouble(&d) )
                    {
                        pParameter->asGrid_System()->Assign(-1, 0.0, 0.0, 0, 0);
                    }
                    else
                    {
                        pParameter->asGrid_System()->Assign(d, x, y, (int)nx, (int)ny);
                    }
                }
                break;
            }
        }
    }

    m_pModule->Update_Parameter_States();

    //-----------------------------------------------------
    for(i=0; i<pParameters->Get_Count(); i++)
    {
        CSG_Parameter	*pParameter	= pParameters->Get_Parameter(i);

        if( pParameter->is_Input() )
        {
            if( !_Load_Input(pParameters->Get_Parameter(i)) )
            {
                CMD_Print_Error(pParameters->Get_Parameter(i)->Get_Name());

                return( false );
            }
        }

        else if( pParameter->is_Option() && !pParameter->is_Information() )
        {
            long		l;
            wxString	s;

            switch( pParameter->Get_Type() )
            {
            case PARAMETER_TYPE_Table_Field:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    if( s.ToLong(&l) )
                    {
                        pParameter->Set_Value((int)l);
                    }
                    else
                    {
                        pParameter->Set_Value(CSG_String(&s));
                    }
                }
                break;

            case PARAMETER_TYPE_Table_Fields:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    pParameter->Set_Value(CSG_String(&s));
                }
                break;
            }
        }
    }

    //-----------------------------------------------------
    return( true );
}
//---------------------------------------------------------
bool CGrid_Value_Replace::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Grid	*pGrid	= Parameters("OUTPUT")->asGrid();

	if( !pGrid || pGrid == Parameters("INPUT")->asGrid() )
	{
		pGrid	= Parameters("INPUT")->asGrid();
	}
	else
	{
		pGrid->Assign(Parameters("INPUT")->asGrid());

		DataObject_Set_Parameters(pGrid, Parameters("INPUT")->asGrid());

		pGrid->Fmt_Name("%s [%s]", Parameters("INPUT")->asGrid()->Get_Name(), _TL("Changed"));
	}

	//-----------------------------------------------------
	int		Method	= Parameters("METHOD")->asInt();

	CSG_Table	LUT;

	switch( Method )
	{
	default:	LUT.Create(*Parameters("IDENTITY")->asTable());	break;
	case  1:	LUT.Create(*Parameters("RANGE"   )->asTable());	break;
	case  2:	LUT.Create( Parameters("RANGE"   )->asTable());
		if( SG_UI_Get_Window_Main()	// gui only
		&&  DataObject_Get_Parameter(Parameters("GRID" )->asGrid(), "LUT")
		&&  DataObject_Get_Parameter(Parameters("INPUT")->asGrid(), "LUT") )
		{
			CSG_Table	LUTs[2];

			LUTs[0].Create(*DataObject_Get_Parameter(Parameters("GRID" )->asGrid(), "LUT")->asTable());
			LUTs[1].Create(*DataObject_Get_Parameter(Parameters("INPUT")->asGrid(), "LUT")->asTable());

			for(int i=0; i<LUTs[0].Get_Count(); i++)
			{
				CSG_String	Name	= LUTs[0][i].asString(1);

				for(int j=LUTs[1].Get_Count()-1; j>=0; j--)
				{
					if( !Name.Cmp(LUTs[1][j].asString(1)) )
					{
						CSG_Table_Record	*pReplace	= LUT.Add_Record();

						pReplace->Set_Value(0, LUTs[0][i].asDouble(3));
						pReplace->Set_Value(1, LUTs[1][j].asDouble(3));
						pReplace->Set_Value(2, LUTs[1][j].asDouble(4));

						LUTs[1].Del_Record(j);

						break;
					}
				}
			}

			for(int j=0; j<LUTs[1].Get_Count(); j++)
			{
				LUTs[0].Add_Record(LUTs[1].Get_Record(j));
			}

			DataObject_Add(pGrid);
			CSG_Parameter	*pLUT	= DataObject_Get_Parameter(pGrid, "LUT");
			pLUT->asTable()->Assign_Values(&LUTs[0]);
			DataObject_Set_Parameter(pGrid, pLUT);
		}
		break;
	}

	//-----------------------------------------------------
	if( LUT.Get_Count() == 0 )
	{
		Error_Set(_TL("empty look-up table, nothing to replace"));

		return( false );
	}

	//-----------------------------------------------------
	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#ifndef _DEBUG
		#pragma omp parallel for
		#endif
		for(int x=0; x<Get_NX(); x++)
		{
			double	Value	= pGrid->asDouble(x, y);

			for(int i=0; i<LUT.Get_Count(); i++)
			{
				if( Method == 0 )
				{
					if( LUT[i].asDouble(1) == Value )
					{
						pGrid->Set_Value(x, y, LUT[i].asDouble(0));

						break;
					}
				}
				else
				{
					if( LUT[i].asDouble(1) <= Value && Value <= LUT[i].asDouble(2) )
					{
						pGrid->Set_Value(x, y, LUT[i].asDouble(0));

						break;
					}
				}
			}
		}
	}

	//-----------------------------------------------------
	if( pGrid == Parameters("INPUT")->asGrid() )
	{
		DataObject_Update(pGrid);
	}

	return( true );
}
Example #5
0
//---------------------------------------------------------
CDVWK_SoilMoisture::CDVWK_SoilMoisture(void)
{
	CSG_Parameter	*pNode;

	//-----------------------------------------------------
	Set_Name	(_TL("Soil Moisture Content"));

	Set_Author		(SG_T("(c) 2002 by O.Conrad"));

	Set_Description	(_TW(
		"The WEELS (Wind Erosion on European Light Soils) soil moisture "
		"model dynamically calculates the soil moisture based on the rules "
		"proposed by the DVWK (1996) with input data about:\n"
		"- soil properties (grids: field capacity and permanent wilting point)\n"
		"- land use (grid: crop types)\n"
		"- climate (table: daily values of precipitation, temperature, air humidity)\n\n"

		"References:\n"

		"- DVWK - Deutscher Verband fuer Wasserwirtschaft und Kulturbau e.V. (1996): "
		"'Ermittlung der Verdunstung von Land- und Wasserflaechen', "
		"DVWK Merkblaetter 238/1996, Bonn, 135p.\n"

		"- Boehner, J., Schaefer, W., Conrad, O., Gross, J., Ringeler, A. (2001): "
		"'The WEELS Model: methods, results and limits of wind erosion modelling', "
		"In: Catena, Special Issue\n")
	);

	//-----------------------------------------------------
	pNode	= Parameters.Add_Grid(
		NULL	, "STA_FC"		, _TL("Field Capacity [mm]"),
		_TL(""),
		PARAMETER_INPUT_OPTIONAL
	);

	Parameters.Add_Value(
		pNode	, "STA_FC_DEF"	, _TL("Default"),
		_TL(""),
		PARAMETER_TYPE_Double	, 20.0	, 0.0, true
	);

	pNode	= Parameters.Add_Grid(
		NULL	, "STA_PWP"		, _TL("Permanent Wilting Point [mm]"),
		_TL(""),
		PARAMETER_INPUT_OPTIONAL
	);

	Parameters.Add_Value(
		pNode	, "STA_PWP_DEF"	, _TL("Default"),
		_TL(""),
		PARAMETER_TYPE_Double	, 2.0	, 0.0, true
	);

	pNode	= Parameters.Add_Grid(
		NULL	, "LANDUSE"		, _TL("Land Use"),
		_TL(""),
		PARAMETER_INPUT_OPTIONAL
	);

	Parameters.Add_Value(
		pNode	, "LANDUSE_DEF"	, _TL("Default"),
		_TL(""),
		PARAMETER_TYPE_Int		, -1.0
	);

	//-----------------------------------------------------
	pNode	= Parameters.Add_Grid(
		NULL	, "DYN_W"		, _TL("Soil Moisture"),
		_TL(""),
		PARAMETER_OUTPUT
	);

	//-----------------------------------------------------
	pNode	= Parameters.Add_FixedTable(
		NULL	, "DYN_CLIMATE"	, _TL("Climate Data"),
		_TL("")
	);

	pClimate	= pNode->asTable();
	pClimate->Set_Name(_TL("Climate Data"));
	pClimate->Add_Field(_TL("Precipitation [mm]")	, SG_DATATYPE_Double);
	pClimate->Add_Field(_TL("Temperature (2pm) [DegreeC]"), SG_DATATYPE_Double);
	pClimate->Add_Field(_TL("Air Humidity (2pm) [%%]"), SG_DATATYPE_Double);

	//-----------------------------------------------------
	pNode	= Parameters.Add_FixedTable(
		NULL	, "STA_KC"		, _TL("Crop Coefficients"),
		_TL("")
	);

	pCropCoeff	= pNode->asTable();
	pCropCoeff->Set_Name(_TL("Crop Coefficients"));
	pCropCoeff->Add_Field(_TL("Land Use ID")	, SG_DATATYPE_Int);
	pCropCoeff->Add_Field(_TL("Name")		, SG_DATATYPE_String);
	pCropCoeff->Add_Field(_TL("January")		, SG_DATATYPE_Double);
	pCropCoeff->Add_Field(_TL("February")	, SG_DATATYPE_Double);
	pCropCoeff->Add_Field(_TL("March")		, SG_DATATYPE_Double);
	pCropCoeff->Add_Field(_TL("April")		, SG_DATATYPE_Double);
	pCropCoeff->Add_Field(_TL("May")			, SG_DATATYPE_Double);
	pCropCoeff->Add_Field(_TL("June")		, SG_DATATYPE_Double);
	pCropCoeff->Add_Field(_TL("July")		, SG_DATATYPE_Double);
	pCropCoeff->Add_Field(_TL("August")		, SG_DATATYPE_Double);
	pCropCoeff->Add_Field(_TL("September")	, SG_DATATYPE_Double);
	pCropCoeff->Add_Field(_TL("October")		, SG_DATATYPE_Double);
	pCropCoeff->Add_Field(_TL("November")	, SG_DATATYPE_Double);
	pCropCoeff->Add_Field(_TL("December")	, SG_DATATYPE_Double);

	CSG_Table_Record	*pRec;

#define ADD_RECORD(ID, NAME, m01, m02, m03, m04, m05, m06, m07, m08, m09, m10, m11, m12)	pRec = pCropCoeff->Add_Record();\
	pRec->Set_Value( 0, ID);  pRec->Set_Value( 1, NAME);\
	pRec->Set_Value( 2, m01); pRec->Set_Value( 3, m02); pRec->Set_Value( 4, m03); pRec->Set_Value( 5, m04);\
	pRec->Set_Value( 6, m05); pRec->Set_Value( 7, m06); pRec->Set_Value( 8, m07); pRec->Set_Value( 9, m08);\
	pRec->Set_Value(10, m09); pRec->Set_Value(11, m10); pRec->Set_Value(12, m11); pRec->Set_Value(13, m12);

	//         ID   NAME              Jan   Feb   Mar   Apr   Mai   Jun   Jul   Aug   Sep   Okt   Nov   Dec
	ADD_RECORD(1.0, _TL("Maehweide")		, 1   , 1   , 1   , 1   , 1.05, 1.10, 1.10, 1.05, 1.05, 1   , 1   , 1   );
	ADD_RECORD(2.0, _TL("Winterweizen")	, 0.65, 0.65, 0.80, 0.85, 1.15, 1.45, 1.40, 1   , 0.80, 0.70, 0.65, 0.65);
	ADD_RECORD(3.0, _TL("Wintergerste")	, 1   , 1   , 0.85, 0.95, 1.30, 1.35, 1.25, 1   , 1   , 1   , 1   , 1   );
	ADD_RECORD(4.0, _TL("Sommergerste")	, 1   , 1   , 0.80, 0.90, 1.20, 1.35, 1.20, 1   , 1   , 1   , 1   , 1   );
	ADD_RECORD(5.0, _TL("Winterroggen")	, 0.65, 0.65, 0.85, 0.90, 1.20, 1.30, 1.25, 0.95, 0.80, 0.70, 0.65, 0.65);
	ADD_RECORD(6.0, _TL("Hafer"	)		, 1   , 1   , 0.65, 0.70, 1.10, 1.45, 1.35, 0.95, 1   , 1   , 1   , 1   );
	ADD_RECORD(7.0, _TL("Zuckerrueben")	, 1   , 1   , 1   , 0.50, 0.75, 1.05, 1.40, 1.30, 1.10, 0.85, 1   , 1   );
	ADD_RECORD(8.0, _TL("Kartoffeln")	, 1   , 1   , 1   , 0.50, 0.90, 1.05, 1.45, 1.20, 0.90, 1   , 1   , 1   );
	ADD_RECORD(9.0, _TL("Winterraps")	, 0.65, 0.65, 0.85, 1   , 1.35, 1.35, 1.10, 0.85, 1   , 1   , 0.65, 0.65);
	ADD_RECORD(0.0, _TL("Unknown")		, 1   , 1   , 1   , 1   , 1   , 1   , 1   , 1   , 1   , 1   , 1   , 1   );
#undef ADD_RECORD
}
//---------------------------------------------------------
bool CGrid_CVA::On_Execute(void)
{	
	//-----------------------------------------------------
	CSG_Parameter_Grid_List	*pA	= Parameters("A")->asGridList();
	CSG_Parameter_Grid_List	*pB	= Parameters("B")->asGridList();
	CSG_Parameter_Grid_List	*pC	= Parameters("C")->asGridList();

	if( pA->Get_Grid_Count() != pB->Get_Grid_Count() )
	{
		Error_Set(_TL("number of initial and final state grids differs"));

		return( false );
	}

	if( pA->Get_Grid_Count() == 0 )
	{
		Error_Set(_TL("no grids in list"));

		return( false );
	}

	//-----------------------------------------------------
	int	n	= pA->Get_Grid_Count();

	bool		bAngle	= Parameters("ANGLE")->asBool() && n == 2;
	bool		bC_Out	= Parameters("C_OUT")->asBool();

	CSG_Grid	*pDist	= Parameters("DIST")->asGrid();
	CSG_Grid	*pDir	= Parameters("DIR" )->asGrid();

	//-----------------------------------------------------
	pC->Del_Items();

	if( bC_Out )
	{
		for(int i=0; i<n; i++)
		{
			CSG_Grid	*pGrid	= SG_Create_Grid(Get_System());
			pGrid->Fmt_Name("%s %01d", _TL("Change Vector"), i + 1);
			pC->Add_Item(pGrid);
		}
	}

	//-----------------------------------------------------
	CSG_Parameter	*pLUT;
	CSG_Colors		Colors;

	Colors.Set_Count(100);
	Colors.Set_Ramp(SG_GET_RGB(255, 255, 255), SG_GET_RGB(  0, 127, 127), 0, Colors.Get_Count() / 2);
	Colors.Set_Ramp(SG_GET_RGB(  0, 127, 127), SG_GET_RGB(255,   0,   0),    Colors.Get_Count() / 2, Colors.Get_Count());
	DataObject_Set_Colors(pDist, Colors);

	if( (pLUT = DataObject_Get_Parameter(pDir, "LUT")) == NULL || pLUT->asTable() == NULL || bAngle )
	{
		Colors.Set_Default(100);
		Colors.Set_Ramp_Brighness(255,   0, 0, Colors.Get_Count() / 2);
		Colors.Set_Ramp_Brighness(  0, 255,    Colors.Get_Count() / 2, Colors.Get_Count());
		DataObject_Set_Colors(pDir, Colors);

		DataObject_Set_Parameter(pDir, "COLORS_TYPE", 2);
	}
	else
	{
		pLUT->asTable()->Del_Records();

		for(int i=0, nClasses=(int)pow(2.0, n); i<nClasses; i++)
		{
			CSG_String	s;

			for(int j=0; j<n; j++)
			{
				s	+= i & (int)pow(2.0, j) ? '+' : '-';
			}

			CSG_Table_Record	*pClass	= pLUT->asTable()->Add_Record();
			pClass->Set_Value(1, s);
			pClass->Set_Value(3, i);
			pClass->Set_Value(4, i);
		}

		Colors.Set_Count(pLUT->asTable()->Get_Count());
		Colors.Random();

		for(int c=0; c<pLUT->asTable()->Get_Count(); c++)
		{
			pLUT->asTable()->Get_Record(c)->Set_Value(0, Colors.Get_Color(c));
		}

		DataObject_Set_Parameter(pDir, pLUT);
		DataObject_Set_Parameter(pDir, "COLORS_TYPE", 1);	// Color Classification Type: Lookup Table
	}

	//-----------------------------------------------------
    for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<Get_NX(); x++)
		{
			bool		bOkay;
			int			i, j;
			double		d;
			CSG_Vector	v(n);

			for(i=0, bOkay=true; i<n && bOkay; i++)
			{
				if( pA->Get_Grid(i)->is_NoData(x, y) || pB->Get_Grid(i)->is_NoData(x, y) )
				{
					bOkay	= false;
				}
				else
				{
					v[i]	= pB->Get_Grid(i)->asDouble(x, y) - pA->Get_Grid(i)->asDouble(x, y);
				}
			}

			//---------------------------------------------
			if( bOkay )
			{
				if( bAngle )
				{
					d	= atan2(v[0], v[1]);
				}
				else for(i=0, j=1, d=0.0; i<n; i++, j*=2)
				{
					if( v[i] >= 0.0 )
					{
						d	+= j;
					}
				}

				pDist->Set_Value(x, y, v.Get_Length());
				pDir ->Set_Value(x, y, d);

				for(i=0; i<n && bC_Out; i++)
				{
					pC->Get_Grid(i)->Set_Value(x, y, v[i]);
				}
			}

			//---------------------------------------------
			else
			{
				pDist->Set_NoData(x, y);
				pDir ->Set_NoData(x, y);

				for(i=0; i<n && bC_Out; i++)
				{
					pC->Get_Grid(i)->Set_NoData(x, y);
				}
			}
        }
    }

	return( true );
}
Example #7
0
//---------------------------------------------------------
void CTC_Classification::Set_LUT(CSG_Grid *pLandforms, int nLevels)
{
	CSG_Parameter	*pLUT	= DataObject_Get_Parameter(pLandforms, "LUT");

	if( !pLUT || !pLUT->asTable() )
	{
		return;
	}

	pLUT->asTable()->Del_Records();

	switch( nLevels )
	{
	case 1:		// 8 classes
		//        l  s  c  t
		ADD_CLASS(1, 1, 1, 1, 0x7F4633, "steep"     );	// class  1
		ADD_CLASS(1, 1, 1, 0, 0xFF00FF, "steep"     );	// class  2
		ADD_CLASS(1, 1, 0, 1, 0xC16E2E, "steep"     );	// class  3
		ADD_CLASS(1, 1, 0, 0, 0xFC97CF, "steep"     );	// class  4

		ADD_CLASS(1, 0, 1, 1, 0x66A726, "gentle"    );	// class  5
		ADD_CLASS(1, 0, 1, 0, 0xFFFF40, "gentle"    );	// class  6
		ADD_CLASS(1, 0, 0, 1, 0xAEFF7B, "gentle"    );	// class  7
		ADD_CLASS(1, 0, 0, 0, 0xFFFFBF, "gentle"    );	// class  8
		break;

	case 2:		// 12 classes
		ADD_CLASS(1, 1, 1, 1, 0x7F4633, "steep"     );	// class  1
		ADD_CLASS(1, 1, 1, 0, 0xFF00FF, "steep"     );	// class  2
		ADD_CLASS(1, 1, 0, 1, 0xC16E2E, "steep"     );	// class  3
		ADD_CLASS(1, 1, 0, 0, 0xFC97CF, "steep"     );	// class  4

		ADD_CLASS(2, 1, 1, 1, 0xFA9600, "moderate"  );	// class  5
		ADD_CLASS(2, 1, 1, 0, 0xFD43A8, "moderate"  );	// class  6
		ADD_CLASS(2, 1, 0, 1, 0xFBCF66, "moderate"  );	// class  7
		ADD_CLASS(2, 1, 0, 0, 0xF9C4D4, "moderate"  );	// class  8

		ADD_CLASS(2, 0, 1, 1, 0x66A726, "gentle"    );	// class  9
		ADD_CLASS(2, 0, 1, 0, 0xFFFF40, "gentle"    );	// class 10
		ADD_CLASS(2, 0, 0, 1, 0xAEFF7B, "gentle"    );	// class 11
		ADD_CLASS(2, 0, 0, 0, 0xFFFFBF, "gentle"    );	// class 12
		break;

	default:	// 16 classes
		ADD_CLASS(1, 1, 1, 1, 0x7F4633, "very steep");	// class  1
		ADD_CLASS(1, 1, 1, 0, 0xFF00FF, "very steep");	// class  2
		ADD_CLASS(1, 1, 0, 1, 0xC16E2E, "very steep");	// class  3
		ADD_CLASS(1, 1, 0, 0, 0xFC97CF, "very steep");	// class  4

		ADD_CLASS(2, 1, 1, 1, 0xFA9600, "steep"     );	// class  5
		ADD_CLASS(2, 1, 1, 0, 0xFD43A8, "steep"     );	// class  6
		ADD_CLASS(2, 1, 0, 1, 0xFBCF66, "steep"     );	// class  7
		ADD_CLASS(2, 1, 0, 0, 0xF9C4D4, "steep"     );	// class  8

		ADD_CLASS(3, 1, 1, 1, 0x019E73, "moderate"  );	// class  9
		ADD_CLASS(3, 1, 1, 0, 0xC4B71D, "moderate"  );	// class 10
		ADD_CLASS(3, 1, 0, 1, 0x0072B1, "moderate"  );	// class 11
		ADD_CLASS(3, 1, 0, 0, 0xD7D700, "moderate"  );	// class 12

		ADD_CLASS(3, 0, 1, 1, 0xA0FD8E, "gentle"    );	// class 13
		ADD_CLASS(3, 0, 1, 0, 0xE9E915, "gentle"    );	// class 14
		ADD_CLASS(3, 0, 0, 1, 0x003B5B, "gentle"    );	// class 15
		ADD_CLASS(3, 0, 0, 0, 0xF2FEC0, "gentle"    );	// class 16
		break;
	}

	DataObject_Set_Parameter(pLandforms, pLUT);
	DataObject_Set_Parameter(pLandforms, "COLORS_TYPE", 1);	// Color Classification Type: Lookup Table
}
Example #8
0
//---------------------------------------------------------
bool CParam_Scale::On_Execute(void)
{
	//-----------------------------------------------------
	bool		bConstrain;
	int			Index[6];
	double		zScale, Tol_Slope, Tol_Curve;
	CSG_Matrix	Normal;

	//-----------------------------------------------------
	bConstrain	= Parameters("CONSTRAIN")->asBool();
	zScale		= Parameters("ZSCALE"   )->asDouble();	if( zScale <= 0.0 )	{	zScale	= 1.0;	}
	Tol_Slope	= Parameters("TOL_SLOPE")->asDouble();
	Tol_Curve	= Parameters("TOL_CURVE")->asDouble();

	m_pDEM		= Parameters("DEM"      )->asGrid();

	//-----------------------------------------------------
	CSG_Grid	*pFeature	= Parameters("FEATURES" )->asGrid();
	CSG_Grid	*pElevation	= Parameters("ELEVATION")->asGrid();
	CSG_Grid	*pSlope		= Parameters("SLOPE"    )->asGrid();
	CSG_Grid	*pAspect	= Parameters("ASPECT"   )->asGrid();
	CSG_Grid	*pProfC		= Parameters("PROFC"    )->asGrid();
	CSG_Grid	*pPlanC		= Parameters("PLANC"    )->asGrid();
	CSG_Grid	*pLongC		= Parameters("LONGC"    )->asGrid();
	CSG_Grid	*pCrosC		= Parameters("CROSC"    )->asGrid();
	CSG_Grid	*pMiniC		= Parameters("MINIC"    )->asGrid();
	CSG_Grid	*pMaxiC		= Parameters("MAXIC"    )->asGrid();

	//-----------------------------------------------------
	if( !Get_Weights() )
	{
		return( false );
	}

	if( !Get_Normal(Normal) )
	{
		return( false );
	}

	// To constrain the quadtratic through the central cell, ignore the calculations involving the
	// coefficient f. Since these are all in the last row and column of the matrix, simply redimension.
	if( !SG_Matrix_LU_Decomposition(bConstrain ? 5 : 6, Index, Normal.Get_Data()) )
	{
		return( false );
	}

	//-----------------------------------------------------
	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<Get_NX(); x++)
		{
			CSG_Vector	Observed;

			double	elevation, slope, aspect, profc, planc, longc, crosc, minic, maxic;

			if( Get_Observed(x, y, Observed, bConstrain)
			&&  SG_Matrix_LU_Solve(bConstrain ? 5 : 6, Index, Normal, Observed.Get_Data()) )
			{
				Get_Parameters(zScale, Observed.Get_Data(), elevation, slope, aspect, profc, planc, longc, crosc, minic, maxic);

				GRID_SET_VALUE(pFeature  , Get_Feature(slope, minic, maxic, crosc, Tol_Slope, Tol_Curve));
				GRID_SET_VALUE(pElevation, elevation + m_pDEM->asDouble(x, y));	// Add central elevation back
				GRID_SET_VALUE(pSlope    , slope);
				GRID_SET_VALUE(pAspect   , aspect);
				GRID_SET_VALUE(pProfC    , profc);
				GRID_SET_VALUE(pPlanC    , planc);
				GRID_SET_VALUE(pLongC    , longc);
				GRID_SET_VALUE(pCrosC    , crosc);
				GRID_SET_VALUE(pMiniC    , minic);
				GRID_SET_VALUE(pMaxiC    , maxic);
			}
			else
			{
				GRID_SET_NODATA(pFeature);
				GRID_SET_NODATA(pElevation);
				GRID_SET_NODATA(pSlope);
				GRID_SET_NODATA(pAspect);
				GRID_SET_NODATA(pProfC);
				GRID_SET_NODATA(pPlanC);
				GRID_SET_NODATA(pLongC);
				GRID_SET_NODATA(pCrosC);
				GRID_SET_NODATA(pMiniC);
				GRID_SET_NODATA(pMaxiC);
			}
		}
	}

	//-----------------------------------------------------
	CSG_Parameter	*pLUT	= DataObject_Get_Parameter(pFeature, "LUT");

	if( pLUT && pLUT->asTable() )
	{
		pLUT->asTable()->Del_Records();

		LUT_SET_CLASS(FLAT   , _TL("Planar"       ), SG_GET_RGB(180, 180, 180));
		LUT_SET_CLASS(PIT    , _TL("Pit"          ), SG_GET_RGB(  0,   0,   0));
		LUT_SET_CLASS(CHANNEL, _TL("Channel"      ), SG_GET_RGB(  0,   0, 255));
		LUT_SET_CLASS(PASS   , _TL("Pass (saddle)"), SG_GET_RGB(  0, 255,   0));
		LUT_SET_CLASS(RIDGE  , _TL("Ridge"        ), SG_GET_RGB(255, 255,   0));
		LUT_SET_CLASS(PEAK   , _TL("Peak"         ), SG_GET_RGB(255,   0,   0));

		DataObject_Set_Parameter(pFeature, pLUT);

		DataObject_Set_Parameter(pFeature, "COLORS_TYPE", 1);	// Color Classification Type: Lookup Table
	}

	//-----------------------------------------------------
	DataObject_Set_Colors(pSlope , 11, SG_COLORS_YELLOW_RED);
	DataObject_Set_Colors(pAspect, 11, SG_COLORS_ASPECT_3);
	DataObject_Set_Colors(pProfC , 11, SG_COLORS_RED_GREY_BLUE, true);
	DataObject_Set_Colors(pPlanC , 11, SG_COLORS_RED_GREY_BLUE, false);
	DataObject_Set_Colors(pLongC , 11, SG_COLORS_RED_GREY_BLUE, true);
	DataObject_Set_Colors(pCrosC , 11, SG_COLORS_RED_GREY_BLUE, true);
	DataObject_Set_Colors(pMiniC , 11, SG_COLORS_RED_GREY_BLUE, true);
	DataObject_Set_Colors(pMaxiC , 11, SG_COLORS_RED_GREY_BLUE, true);

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CGrid_Classify_Supervised::Set_Classification(CSG_Classifier_Supervised &Classifier)
{
	//-----------------------------------------------------
	CSG_Grid	*pClasses	= Parameters("CLASSES")->asGrid();

	//-----------------------------------------------------
	CSG_Parameter	*pLUT	= DataObject_Get_Parameter(pClasses, "LUT");

	if( pLUT && pLUT->asTable() )
	{
		CSG_Parameter_Grid_List	*pGrids	= Parameters("GRIDS")->asGridList();

		bool	bRGB	= pGrids->Get_Count() >= 3 && Parameters("RGB_COLORS")->asBool();

		for(int iClass=0; iClass<Classifier.Get_Class_Count(); iClass++)
		{
			CSG_Table_Record	*pClass	= pLUT->asTable()->Get_Record(iClass);

			if( !pClass )
			{
				(pClass	= pLUT->asTable()->Add_Record())->Set_Value(0, SG_Color_Get_Random());
			}

			pClass->Set_Value(1, Classifier.Get_Class_ID(iClass).c_str());
			pClass->Set_Value(2, "");
			pClass->Set_Value(3, iClass + 1);
			pClass->Set_Value(4, iClass + 1);

			if( bRGB )
			{
				#define SET_COLOR_COMPONENT(c, i)	c = (int)(127 + (Classifier.Get_Class_Mean(iClass, i) - pGrids->asGrid(i)->Get_Mean()) * 127 / pGrids->asGrid(i)->Get_StdDev()); if( c < 0 ) c = 0; else if( c > 255 ) c = 255;

				int	r; SET_COLOR_COMPONENT(r, 2);
				int	g; SET_COLOR_COMPONENT(g, 1);
				int	b; SET_COLOR_COMPONENT(b, 0);

				pClass->Set_Value(0, SG_GET_RGB(r, g, b));
			}
		}

		pLUT->asTable()->Set_Record_Count(Classifier.Get_Class_Count());

		DataObject_Set_Parameter(pClasses, pLUT);
		DataObject_Set_Parameter(pClasses, "COLORS_TYPE", 1);	// Color Classification Type: Lookup Table
	}

	pClasses->Set_Name(CSG_String::Format("%s [%s]", _TL("Classification"), CSG_Classifier_Supervised::Get_Name_of_Method(Parameters("METHOD")->asInt()).c_str()));

	//-----------------------------------------------------
	CSG_Grid	*pQuality	= Parameters("QUALITY")->asGrid();

	if( pQuality )
	{
		DataObject_Set_Colors(pQuality, 11, SG_COLORS_YELLOW_GREEN);

		pQuality->Set_Name(CSG_String::Format("%s [%s]", _TL("Classification Quality"), CSG_Classifier_Supervised::Get_Name_of_Quality(Parameters("METHOD")->asInt()).c_str()));
	}

	//-----------------------------------------------------
	return( true );
}
Example #10
0
//---------------------------------------------------------
bool CSoil_Texture_Table::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Table	*pTable		= Parameters("TABLE")->asTable();

	int		iSand		= Parameters("SAND"   )->asInt();
	int		iSilt		= Parameters("SILT"   )->asInt();
	int		iClay		= Parameters("CLAY"   )->asInt();
	int		iTexture	= Parameters("TEXTURE")->asInt();

	//-----------------------------------------------------
	if( (iSand >= 0 ? 1 : 0) + (iSilt >= 0 ? 1 : 0) + (iClay >= 0 ? 1 : 0) < 2 )
	{
		Error_Set(_TL("at least two contents (sand, silt, clay) have to be given"));

		return( false );
	}

	//-----------------------------------------------------
	if( iTexture < 0 )
	{
		iTexture	= pTable->Get_Field_Count();

		pTable->Add_Field("TEXTURE", SG_DATATYPE_String);
	}

	//-----------------------------------------------------
	for(int i=0; i<pTable->Get_Count() && Set_Progress(i, pTable->Get_Count()); i++)
	{
		CSG_Table_Record	*pRecord	= pTable->Get_Record(i);

		if(	(iSand >= 0 && pRecord->is_NoData(iSand))
		||	(iSilt >= 0 && pRecord->is_NoData(iSilt))
		||	(iClay >= 0 && pRecord->is_NoData(iClay)) )
		{
			pRecord->Set_NoData(iTexture);
		}
		else
		{
			int		Class	= -1;

			if( iSand >= 0 && iSilt >= 0 && iClay >= 0 )
			{
				double	Sum;

				Class	= Get_Texture(pRecord->asDouble(iSand), pRecord->asDouble(iSilt), pRecord->asDouble(iClay), Sum);
			}
			else if( iSilt < 0 )
			{
				Class	= Get_Texture_SandClay(pRecord->asDouble(iSand), pRecord->asDouble(iClay));
			}
			else if( iClay < 0 )
			{
				Class	= Get_Texture_SandSilt(pRecord->asDouble(iSand), pRecord->asDouble(iSilt));
			}
			else if( iSand < 0 )
			{
				Class	= Get_Texture_SiltClay(pRecord->asDouble(iSilt), pRecord->asDouble(iClay));
			}

			pRecord->Set_Value (iTexture, Classes[Class].Key);
		}
	}

	DataObject_Update(pTable);

	//-----------------------------------------------------
	CSG_Parameter	*pLUT	= DataObject_Get_Parameter(pTable, "LUT");

	if( pLUT && pLUT->asTable() )
	{
		CSG_Table	*pClasses	= pLUT->asTable();

		for(int iClass=0; iClass<12; iClass++)
		{
			CSG_Table_Record	*pClass	= pClasses->Get_Record(iClass);

			if( pClass == NULL )
			{
				pClass	= pClasses->Add_Record();
			}

			pClass->Set_Value(0, Classes[iClass].Color);
			pClass->Set_Value(1, Classes[iClass].Name);
			pClass->Set_Value(2, Classes[iClass].Name);
			pClass->Set_Value(3, Classes[iClass].Key);
			pClass->Set_Value(4, Classes[iClass].Key);
		}

		while( pClasses->Get_Count() > 12 )
		{
			pClasses->Del_Record(pClasses->Get_Count() - 1);
		}

		DataObject_Set_Parameter(pTable, pLUT);	// Lookup Table
		DataObject_Set_Parameter(pTable, "LUT_ATTRIB" , iTexture);	// Lookup Table Attribute
		DataObject_Set_Parameter(pTable, "COLORS_TYPE", 1       );	// Color Classification Type: Lookup Table
	}

	//-----------------------------------------------------
	return( true );
}
Example #11
0
//---------------------------------------------------------
bool CSoil_Texture::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Grid	*pSand	= Parameters("SAND"   )->asGrid();
	CSG_Grid	*pSilt	= Parameters("SILT"   )->asGrid();
	CSG_Grid	*pClay	= Parameters("CLAY"   )->asGrid();
	CSG_Grid	*pClass	= Parameters("TEXTURE")->asGrid();
	CSG_Grid	*pSum	= Parameters("SUM"    )->asGrid();

	//-----------------------------------------------------
	if( (pSand ? 1 : 0) + (pSilt ? 1 : 0) + (pClay ? 1 : 0) < 2 )
	{
		Error_Set(_TL("at least two contents (sand, silt, clay) have to be given"));

		return( false );
	}

	//-----------------------------------------------------
	pClass->Set_NoData_Value(-1.0);

	CSG_Parameter	*pLUT	= DataObject_Get_Parameter(pClass, "LUT");

	if( pLUT && pLUT->asTable() )
	{
		CSG_Table	*pClasses	= pLUT->asTable();

		for(int iClass=0; iClass<12; iClass++)
		{
			CSG_Table_Record	*pClass	= pClasses->Get_Record(iClass);

			if( pClass == NULL )
			{
				pClass	= pClasses->Add_Record();
			}

			pClass->Set_Value(0, Classes[iClass].Color);
			pClass->Set_Value(1, Classes[iClass].Name);
			pClass->Set_Value(2, Classes[iClass].Key);
			pClass->Set_Value(3, iClass);
			pClass->Set_Value(4, iClass);
		}

		while( pClasses->Get_Count() > 12 )
		{
			pClasses->Del_Record(pClasses->Get_Count() - 1);
		}

		DataObject_Set_Parameter(pClass, pLUT);	// Lookup Table
		DataObject_Set_Parameter(pClass, "COLORS_TYPE", 1);	// Color Classification Type: Lookup Table
	}

	//-----------------------------------------------------
	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<Get_NX(); x++)
		{
			if(	(pSand && pSand->is_NoData(x, y))
			||	(pSilt && pSilt->is_NoData(x, y))
			||	(pClay && pClay->is_NoData(x, y)) )
			{
				SG_GRID_PTR_SAFE_SET_NODATA(pClass, x, y);
				SG_GRID_PTR_SAFE_SET_NODATA(pSum  , x, y);
			}
			else
			{
				int		Class	= -1;
				double	Sum		= 100.0;

				if( pSand && pSilt && pClay )
				{
					Class	= Get_Texture(pSand->asDouble(x, y), pSilt->asDouble(x, y), pClay->asDouble(x, y), Sum);
				}
				else if( !pSilt )
				{
					Class	= Get_Texture_SandClay(pSand->asDouble(x, y), pClay->asDouble(x, y));
				}
				else if( !pClay )
				{
					Class	= Get_Texture_SandSilt(pSand->asDouble(x, y), pSilt->asDouble(x, y));
				}
				else if( !pSand )
				{
					Class	= Get_Texture_SiltClay(pSilt->asDouble(x, y), pClay->asDouble(x, y));
				}

				SG_GRID_PTR_SAFE_SET_VALUE(pClass, x, y, Class);
				SG_GRID_PTR_SAFE_SET_VALUE(pSum  , x, y, Sum  );
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}