⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grid_classify_supervised.cpp

📁 这是一个GPS相关的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:

///////////////////////////////////////////////////////////
//                                                       //
//                         SAGA                          //
//                                                       //
//      System for Automated Geoscientific Analyses      //
//                                                       //
//                    Module Library:                    //
//                  Grid_Discretisation                  //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//              Grid_Classify_Supervised.cpp             //
//                                                       //
//                 Copyright (C) 2005 by                 //
//                      Olaf Conrad                      //
//                                                       //
//-------------------------------------------------------//
//                                                       //
// This file is part of 'SAGA - System for Automated     //
// Geoscientific Analyses'. SAGA is free software; you   //
// can redistribute it and/or modify it under the terms  //
// of the GNU General Public License as published by the //
// Free Software Foundation; version 2 of the License.   //
//                                                       //
// SAGA is distributed in the hope that it will be       //
// useful, but WITHOUT ANY WARRANTY; without even the    //
// implied warranty of MERCHANTABILITY or FITNESS FOR A  //
// PARTICULAR PURPOSE. See the GNU General Public        //
// License for more details.                             //
//                                                       //
// You should have received a copy of the GNU General    //
// Public License along with this program; if not,       //
// write to the Free Software Foundation, Inc.,          //
// 59 Temple Place - Suite 330, Boston, MA 02111-1307,   //
// USA.                                                  //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//    e-mail:     oconrad@saga-gis.org                   //
//                                                       //
//    contact:    Olaf Conrad                            //
//                Institute of Geography                 //
//                University of Goettingen               //
//                Goldschmidtstr. 5                      //
//                37077 Goettingen                       //
//                Germany                                //
//                                                       //
///////////////////////////////////////////////////////////

//---------------------------------------------------------


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
#include <string.h>

#include "Grid_Classify_Supervised.h"


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
enum
{
	CLASS_NR			= 0,
	CLASS_ID,
	CLASS_N,
	CLASS_M,
	CLASS_S
};


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
#define GET_GRID_VALUE(x, y, i)	(m_bNormalise ? (m_pGrids->asGrid(i)->asDouble(x, y) - m_pGrids->asGrid(i)->Get_ArithMean()) / sqrt(m_pGrids->asGrid(i)->Get_Variance()) : m_pGrids->asGrid(i)->asDouble(x, y))


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
CGrid_Classify_Supervised::CGrid_Classify_Supervised(void)
{
	CSG_Parameter	*pNode;

	//-----------------------------------------------------
	Set_Name		(_TL("Supervised Classification"));

	Set_Author		(_TL("Copyrights (c) 2005 by Olaf Conrad"));

	Set_Description	(_TW(
		"Supervised Classification: Minimum Distance, Maximum Likelihood.\n"
	));

	//-----------------------------------------------------
	Parameters.Add_Grid_List(
		NULL	, "GRIDS"			, _TL("Grids"),
		_TL(""),
		PARAMETER_INPUT
	);

	pNode	= Parameters.Add_Shapes(
		NULL	, "POLYGONS"		, _TL("Training Areas"),
		_TL(""),
		PARAMETER_INPUT, SHAPE_TYPE_Polygon
	);

	Parameters.Add_Table_Field(
		pNode	, "FIELD"			, _TL("Class Identifier"),
		_TL("")
	);

	Parameters.Add_Table(
		NULL	, "CLASSES"			, _TL("Class Information"),
		_TL(""),
		PARAMETER_OUTPUT
	);

	Parameters.Add_Grid(
		NULL	, "RESULT"			, _TL("Classification"),
		_TL(""),
		PARAMETER_OUTPUT, true, GRID_TYPE_Char
	);

	Parameters.Add_Grid(
		NULL	, "ML_PROB"			, _TL("Probability (Maximum Likelihood)"),
		_TL(""),
		PARAMETER_OUTPUT_OPTIONAL
	);

	Parameters.Add_Choice(
		NULL	, "METHOD"			, _TL("Method"),
		_TL(""),
		CSG_String::Format(SG_T("%s|%s|"),
			_TL("Minimum Distance"),
			_TL("Maximum Likelihood")
		), 0
	);

	Parameters.Add_Value(
		NULL	, "NORMALISE"		, _TL("Normalise"),
		_TL("Automatically normalise grids before classifying. Useful for minimum distance classification."),
		PARAMETER_TYPE_Bool, false
	);

	Parameters.Add_Value(
		NULL	, "ML_THRESHOLD"	, _TL("Probability Threshold (Percent)"),
		_TL("Let pixel stay unclassified, if maximum likelihood probability is less than threshold."),
		PARAMETER_TYPE_Double, 0.0, 0.0, true, 100.0, true
	);
}

//---------------------------------------------------------
CGrid_Classify_Supervised::~CGrid_Classify_Supervised(void)
{}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
bool CGrid_Classify_Supervised::On_Execute(void)
{
	bool	bResult	= false;

	//-------------------------------------------------
	m_pClasses		= Parameters("CLASSES")		->asTable();
	m_pGrids		= Parameters("GRIDS")		->asGridList();
 	m_pResult		= Parameters("RESULT")		->asGrid();
	m_bNormalise	= Parameters("NORMALISE")	->asBool();
	m_pProbability	= Parameters("ML_PROB")		->asGrid();
	m_ML_Threshold	= Parameters("ML_THRESHOLD")->asDouble();

	//-------------------------------------------------
	if( Initialise() )
	{
		switch( Parameters("METHOD")->asInt() )
		{
		case 0:	default:	bResult	= Set_Minimum_Distance();		break;
		case 1:				bResult	= Set_Maximum_Likelihood();		break;
		}

		Finalise();
	}

	//-------------------------------------------------
	return( bResult );
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
bool CGrid_Classify_Supervised::Initialise(void)
{
	int					x, y, iGrid, iClass, iPolygon, iField;
	double				d, n;
	TSG_Point			p;
	CSG_Table_Record	*pClass;
	CSG_Shapes			*pPolygons;
	CSG_Shape_Polygon	*pPolygon;

	//-----------------------------------------------------
	for(iGrid=m_pGrids->Get_Count()-1; iGrid>=0; iGrid--)
	{
		if( m_pGrids->asGrid(iGrid)->Get_Variance() == 0.0 )
		{
			m_pGrids->Del_Item(iGrid);
		}
	}

	//-----------------------------------------------------
	if( m_pGrids->Get_Count() > 1 )
	{
		iField		= Parameters("FIELD")		->asInt();
		pPolygons	= Parameters("POLYGONS")	->asShapes();

		m_pClasses->Destroy();
		m_pClasses->Set_Name(_TL("Class Information"));

		m_pClasses->Add_Field(_TL("NR")			, TABLE_FIELDTYPE_Int);
		m_pClasses->Add_Field(_TL("IDENTIFIER")	, TABLE_FIELDTYPE_String);
		m_pClasses->Add_Field(_TL("ELEMENTS")	, TABLE_FIELDTYPE_Int);

		for(iGrid=0; iGrid<m_pGrids->Get_Count(); iGrid++)
		{
			m_pClasses->Add_Field(CSG_String::Format(_TL("MEAN_%02d")  , iGrid + 1), TABLE_FIELDTYPE_Double);
			m_pClasses->Add_Field(CSG_String::Format(_TL("STDDEV_%02d"), iGrid + 1), TABLE_FIELDTYPE_Double);
		}

		//-------------------------------------------------
		for(y=0, p.y=Get_YMin(); y<Get_NY() && Set_Progress(y); y++, p.y+=Get_Cellsize())
		{
			for(x=0, p.x=Get_XMin(); x<Get_NX(); x++, p.x+=Get_Cellsize())
			{
				bool	bNoData;

				for(iGrid=0, bNoData=false; iGrid<m_pGrids->Get_Count() && !bNoData; iGrid++)
				{
					if( m_pGrids->asGrid(iGrid)->is_NoData(x, y) )
					{
						bNoData	= true;
					}
				}

				//-----------------------------------------
				if( bNoData )
				{
					m_pResult->Set_NoData(x, y);
				}
				else
				{
					m_pResult->Set_Value(x, y, 0.0);

					for(iPolygon=0; iPolygon<pPolygons->Get_Count(); iPolygon++)
					{
						pPolygon	= (CSG_Shape_Polygon *)pPolygons->Get_Shape(iPolygon);

						if( pPolygon->is_Containing(p) && (pClass = Get_Class(pPolygon->Get_Record()->asString(iField))) != NULL )
						{
							pClass->Add_Value(CLASS_N, 1.0);

							for(iGrid=0; iGrid<m_pGrids->Get_Count(); iGrid++)
							{
								d	= GET_GRID_VALUE(x, y, iGrid);

								pClass->Add_Value(CLASS_M + 2 * iGrid, d);
								pClass->Add_Value(CLASS_S + 2 * iGrid, d * d);
							}
						}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -