Exemplo n.º 1
0
//---------------------------------------------------------
int CDecision_Tree::Get_Class(CSG_Parameters *pDecision, CSG_Table *pTable)
{
	CSG_String	ID_Root	= pDecision->Get_Identifier(); if( !ID_Root.Cmp(SG_T("ROOT")) ) ID_Root.Clear();

	for(int Child=0; Child<=1; Child++)
	{
		CSG_String	ID	= ID_Root + (Child == 0 ? SG_T("A") : SG_T("B"));

		if( pDecision->Get_Parameter(ID + SG_T("|NODE"))->asBool() )
		{
			Get_Class(pDecision->Get_Parameter(ID)->asParameters(), pTable);
		}
		else
		{
			CSG_Table_Record	*pRecord	= pTable->Add_Record();

			pRecord->Set_Value(0, SG_GET_RGB(rand() * 255.0 / RAND_MAX, rand() * 255.0 / RAND_MAX, rand() * 255.0 / RAND_MAX));
			pRecord->Set_Value(1, pDecision->Get_Parameter(ID + SG_T("|NAME"))->asString());
			pRecord->Set_Value(2, pDecision->Get_Parameter(ID + SG_T("|NAME"))->asString());
			pRecord->Set_Value(3, Get_Class(ID));
			pRecord->Set_Value(4, Get_Class(ID));
		}
	}

	return( pTable->Get_Count() );
}
Exemplo n.º 2
0
//---------------------------------------------------------
int CDecision_Tree::Get_Class(CSG_Parameters *pDecision, const TSG_Point &Point)
{
	double		Value;
	CSG_Grid	*pGrid	= pDecision->Get_Parameter("GRID")->asGrid();

	if( pGrid && pGrid->Get_Value(Point, Value) )
	{
		CSG_String	ID	= pDecision->Get_Identifier(); if( !ID.Cmp(SG_T("ROOT")) ) ID.Clear();

		ID	+= Value < pDecision->Get_Parameter("THRESHOLD")->asDouble() ? SG_T("A") : SG_T("B");

		if( pDecision->Get_Parameter(ID + SG_T("|NODE"))->asBool() )
		{
			return( Get_Class(pDecision->Get_Parameter(ID)->asParameters(), Point) );
		}
		else
		{
			return( Get_Class(ID) );
		}
	}

	return( -1 );
}
Exemplo n.º 3
0
//---------------------------------------------------------
bool CDecision_Tree::On_Execute(void)
{
	CSG_Grid	*pClasses;

	//-----------------------------------------------------
	pClasses	= Parameters("CLASSES")	->asGrid();
	pClasses	->Set_NoData_Value(-1);

	//-----------------------------------------------------
	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(int x=0; x<Get_NX(); x++)
		{
			pClasses->Set_Value(x, y, Get_Class(Parameters("ROOT")->asParameters(), Get_System()->Get_Grid_to_World(x, y)));
		}
	}

	//-----------------------------------------------------
	CSG_Parameters	P;

	if( DataObject_Get_Parameters(pClasses, P) && P("COLORS_TYPE") && P("LUT") )
	{
		CSG_Table	*pTable	= P("LUT")->asTable();

		pTable->Del_Records();

		Get_Class(Parameters("ROOT")->asParameters(), pTable);

		P("COLORS_TYPE")->Set_Value(1);	// Color Classification Type: Lookup Table

		DataObject_Set_Parameters(pClasses, P);
	}

	//-----------------------------------------------------
	return( true );
}
Exemplo n.º 4
0
//---------------------------------------------------------
bool CTC_Classification::Get_Classes(void)
{
	//-----------------------------------------------------
	int	Level, nLevels	= 1 + Parameters("TYPE")->asInt();

	CSG_Grid	*pLandforms	= Parameters("LANDFORMS")->asGrid();

	pLandforms->Assign(0.0);
	pLandforms->Set_NoData_Value(CLASS_FLAG_NODATA);

	Set_LUT(pLandforms, nLevels);

	//-----------------------------------------------------
	for(Level=1; Level<=nLevels && Process_Get_Okay(); Level++)
	{
		Process_Set_Text(CSG_String::Format("%s: %d", _TL("Level"), Level));

		m_Mean_Slope		= Level == 1 ? m_pSlope    ->Get_Mean() : m_Stat_Slope    .Get_Mean();
		m_Mean_Convexity	= Level == 1 ? m_pConvexity->Get_Mean() : m_Stat_Convexity.Get_Mean();
		m_Mean_Texture  	= Level == 1 ? m_pTexture  ->Get_Mean() : m_Stat_Texture  .Get_Mean();

		m_Stat_Slope    .Invalidate();
		m_Stat_Convexity.Invalidate();
		m_Stat_Texture  .Invalidate();

		for(int y=0; y<Get_NY() && Set_Progress(y); y++)
		{
			for(int x=0; x<Get_NX(); x++)
			{
				if( pLandforms->asInt(x, y) == 0 )
				{
					pLandforms->Set_Value(x, y, Get_Class(Level, x, y, Level == nLevels));
				}
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CGrid_Aspect_Slope_Map::On_Execute(void)
{
	CSG_Grid	*pAspect, *pSlope, *pAspectSlope;
	CSG_Table	*pLUT;

	int						iAspectCount	= 9;
	static const double		AspectBreaks[]	= {0.000, 0.393, 1.178, 1.963, 2.749, 3.534, 4.320, 5.105, 5.890, 6.283};
	static const int		AspectClass[]	= {1, 2, 3, 4, 5, 6, 7, 8, 1};

	int						iSlopeCount		= 4;
	static const double		SlopeBreaks[]	= {0.000, 0.087, 0.349, 0.698, 1.571};
	static const int		SlopeClass[]	= {10, 20, 30, 40};


	pAspect			= Parameters("ASPECT")->asGrid();
	pSlope			= Parameters("SLOPE")->asGrid();
	pAspectSlope	= Parameters("ASPECT_SLOPE")->asGrid();
	pLUT			= Parameters("LUT")->asTable();


	//-----------------------------------------------------
	if( pLUT == NULL )
		pLUT = new CSG_Table();
	else
		pLUT->Destroy();

	pLUT->Set_Name(SG_T("LUT_Aspect-Slope"));

	pLUT->Add_Field(SG_T("COLOR"),			SG_DATATYPE_Int);
	pLUT->Add_Field(SG_T("NAME"),			SG_DATATYPE_String);
	pLUT->Add_Field(SG_T("DESCRIPTION"),	SG_DATATYPE_String);
	pLUT->Add_Field(SG_T("MINIMUM"),		SG_DATATYPE_Int);
	pLUT->Add_Field(SG_T("MAXIMUM"),		SG_DATATYPE_Int);

	for(int i=0; i<25; i++)
	{
		CSG_Table_Record	*pRecord = pLUT->Add_Record();

		pRecord->Set_Value(0, LUT_COLOR[i]);
		pRecord->Set_Value(1, LUT_NAME[i]);
		pRecord->Set_Value(2, SG_T(""));
		pRecord->Set_Value(3, LUT_BREAK[i]);
		pRecord->Set_Value(4, LUT_BREAK[i+1]);
	}


	//-----------------------------------------------------
	#pragma omp parallel for
	for(sLong n=0; n<Get_NCells(); n++)
	{
		int		iAspectClass, iSlopeClass;

		if( pAspect->is_NoData(n) || pSlope->is_NoData(n) )
		{
			pAspectSlope->Set_NoData(n);
		}
		else
		{
			iAspectClass	= Get_Class(pAspect->asDouble(n), iAspectCount, AspectBreaks, AspectClass);
			iSlopeClass		= Get_Class(pSlope->asDouble(n), iSlopeCount, SlopeBreaks, SlopeClass);

			pAspectSlope->Set_Value(n, iAspectClass + iSlopeClass);
		}
	}


	//-----------------------------------------------------
	CSG_Parameters	Parms;

	if( DataObject_Get_Parameters(pAspectSlope, Parms) && Parms("COLORS_TYPE") && Parms("LUT") )
	{
		Parms("LUT")->asTable()->Assign(pLUT);
		Parms("COLORS_TYPE")->Set_Value(1);

		DataObject_Set_Parameters(pAspectSlope, Parms);
	}

	if( Parameters("LUT")->asTable() == NULL )
	{
		delete pLUT;
	}


	//-----------------------------------------------------
	return( true );
}
Exemplo n.º 6
0
//---------------------------------------------------------
bool CChange_Detection::On_Execute(void)
{
	bool		bNoChange;
	int			iInitial, iFinal;
	CSG_Matrix	Identity;
	CSG_Table	Initial, Final, *pChanges;
	CSG_Grid	*pInitial, *pFinal, *pChange;

	//-----------------------------------------------------
	pInitial	= Parameters("INITIAL")	->asGrid();
	pFinal		= Parameters("FINAL")	->asGrid();
	pChange		= Parameters("CHANGE")	->asGrid();
	pChanges	= Parameters("CHANGES")	->asTable();
	bNoChange	= Parameters("NOCHANGE")->asBool();

	if( !Get_Classes(Initial, pInitial, true) )
	{
		Error_Set(_TL("no class definitions for initial state"));

		return( false );
	}

	if( !Get_Classes(Final, pFinal, false) )
	{
		Error_Set(_TL("no class definitions for final state"));

		return( false );
	}

	if( !Get_Changes(Initial, Final, pChanges, Identity) )
	{
		return( false );
	}

	//-----------------------------------------------------
	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(int x=0; x<Get_NX(); x++)
		{
			iInitial	= Get_Class(Initial, pInitial->asDouble(x, y));
			iFinal		= Get_Class(Final  , pFinal  ->asDouble(x, y));

			if( bNoChange || !Identity[iInitial][iFinal] )
			{
				pChanges->Get_Record(iInitial)->Add_Value(1 + iFinal, 1);

				pChange->Set_Value(x, y, (pChanges->Get_Field_Count() - 1) * iInitial + iFinal);
			}
			else
			{
				pChange->Set_Value(x, y, -1);
			}
		}
	}

	//-----------------------------------------------------
	CSG_Parameters	P;

	if( DataObject_Get_Parameters(pChange, P) && P("COLORS_TYPE") && P("LUT") )
	{
		CSG_Table	*pLUT	= P("LUT")->asTable();

		CSG_Colors	cRandom(pChanges->Get_Count());

		cRandom.Random();

		pLUT->Del_Records();

		for(iInitial=0; iInitial<pChanges->Get_Count(); iInitial++)
		{
			CSG_Colors	cRamp(pChanges->Get_Field_Count() - 1);

			cRamp.Set_Ramp(cRandom[iInitial], cRandom[iInitial]);
			cRamp.Set_Ramp_Brighness(225, 50);

			for(iFinal=0; iFinal<pChanges->Get_Field_Count()-1; iFinal++)
			{
				if( pChanges->Get_Record(iInitial)->asInt(1 + iFinal) > 0 )
				{
					CSG_Table_Record	*pClass	= pLUT->Add_Record();

					pClass->Set_Value(0, cRamp.Get_Color(iFinal));
					pClass->Set_Value(1, CSG_String::Format(SG_T("%s >> %s"), pChanges->Get_Record(iInitial)->asString(0), pChanges->Get_Field_Name(1 + iFinal)));
					pClass->Set_Value(3, (pChanges->Get_Field_Count() - 1) * iInitial + iFinal);
					pClass->Set_Value(4, (pChanges->Get_Field_Count() - 1) * iInitial + iFinal);
				}
			}
		}

		P("COLORS_TYPE")->Set_Value(1);	// Color Classification Type: Lookup Table

		DataObject_Set_Parameters(pChange, P);
	}

	//-----------------------------------------------------
	double	Factor;

	switch( Parameters("OUTPUT")->asInt() )
	{
	default:	Factor	= 1.0;						break;	// cells
	case 1:		Factor	= 100.0 / Get_NCells();		break;	// percent
	case 2:		Factor	= M_SQR(Get_Cellsize());	break;	// area
	}

	if( Factor != 1.0 )
	{
		for(iInitial=0; iInitial<pChanges->Get_Count(); iInitial++)
		{
			for(iFinal=0; iFinal<pChanges->Get_Field_Count()-1; iFinal++)
			{
				pChanges->Get_Record(iInitial)->Mul_Value(1 + iFinal, Factor);
			}
		}
	}

	//-----------------------------------------------------
	pChanges	->Set_Name(CSG_String::Format(SG_T("%s [%s >> %s]"), _TL("Changes"), pInitial->Get_Name(), pFinal->Get_Name()));

	pChange		->Set_Name(CSG_String::Format(SG_T("%s [%s >> %s]"), _TL("Changes"), pInitial->Get_Name(), pFinal->Get_Name()));
	pChange		->Set_NoData_Value(-1);

	return( true );
}
Exemplo n.º 7
0
//---------------------------------------------------------
bool CGrid_Class_Statistics_For_Polygons::Get_Classes(CSG_Grid *pGrid, CSG_Shapes *pPolygons)
{
	m_Classes.Destroy();

	//-----------------------------------------------------
	if( Parameters("GRID_VALUES")->asInt() == 0 )
	{
		sLong	iCell;

		CSG_Category_Statistics	Classes(pGrid->Get_Type());

		for(iCell=0; iCell<pGrid->Get_NCells(); iCell++)
		{
			if( !pGrid->is_NoData(iCell) )
			{
				Classes	+= pGrid->asDouble(iCell);
			}
		}

		//-------------------------------------------------
		if( Classes.Get_Count() > 0 )
		{
			Classes.Sort();

			for(int iClass=0; iClass<Classes.Get_Count(); iClass++)
			{
				pPolygons->Add_Field(Classes.asString(iClass), SG_DATATYPE_Double);
			}

			m_Classes.Create(*Get_System(), SG_DATATYPE_Short);

			for(iCell=0; iCell<pGrid->Get_NCells(); iCell++)
			{
				m_Classes.Set_Value(iCell, pGrid->is_NoData(iCell) ? -1 : Classes.Get_Category(pGrid->asDouble(iCell)));
			}
		}
	}

	//-----------------------------------------------------
	else
	{
		int		fNam, fMin, fMax;

		CSG_Table	*pLUT	= NULL;

		if( (pLUT = Parameters("GRID_LUT")->asTable()) != NULL )
		{
			fNam	= Parameters("GRID_LUT_NAM")->asInt();
			fMin	= Parameters("GRID_LUT_MIN")->asInt();
			fMax	= Parameters("GRID_LUT_MAX")->asInt();

			if( fNam < 0 || fNam >= pLUT->Get_Field_Count() )	{	fNam	= fMin;	}
			if( fMax < 0 || fMax >= pLUT->Get_Field_Count() )	{	fMax	= fMin;	}
		}
		else if( DataObject_Get_Parameter(pGrid, "LUT") && (pLUT = DataObject_Get_Parameter(pGrid, "LUT")->asTable()) != NULL )
		{
			fNam	= 1;
			fMin	= 3;
			fMax	= 4;
		}

		//-------------------------------------------------
		if( pLUT && pLUT->Get_Count() > 0 )
		{
			for(int iClass=0; iClass<pLUT->Get_Count(); iClass++)
			{
				pPolygons->Add_Field((*pLUT)[iClass].asString(fNam), SG_DATATYPE_Double);
			}

			m_Classes.Create(*Get_System(), SG_DATATYPE_Short);

			for(sLong iCell=0; iCell<pGrid->Get_NCells(); iCell++)
			{
				m_Classes.Set_Value(iCell, Get_Class(pGrid->asDouble(iCell), *pLUT, fMin, fMax));
			}
		}
	}

	//-----------------------------------------------------
	return( m_Classes.is_Valid() );
}