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

📄 channelnetwork.cpp

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

///////////////////////////////////////////////////////////
//                                                       //
//                         SAGA                          //
//                                                       //
//      System for Automated Geoscientific Analyses      //
//                                                       //
//                    Module Library:                    //
//                      ta_channels                      //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//                  ChannelNetwork.cpp                   //
//                                                       //
//                 Copyright (C) 2003 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 "ChannelNetwork.h"


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

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

	//-----------------------------------------------------
	Set_Name(_TL("Channel Network"));

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

	Set_Description	(_TW(
		"This module derives a channel network based on gridded digital elevation data.\n"
		"Use the initiation options to determine under which conditions channels shall start.\n"
		"\n")
	);


	//-----------------------------------------------------
	// Input...

	Parameters.Add_Grid(
		NULL	, "ELEVATION"	, _TL("Elevation"),
		_TL("A grid that contains elevation data."),
		PARAMETER_INPUT
	);

	Parameters.Add_Grid(
		NULL	, "SINKROUTE"	, _TL("Flow Direction"),
		_TW(
		"An optional grid that provides information about flow directions. \n"
		"Values between 1 to 8 force the flow of a cell to be given to one its adjacent neighbor cells "
		"(1->NE, 2->E, 3->SE, 4->S, 5->SW, 6->W, 7->NW, 8->N). "
		"In case of other values the algorithm will use its own routing scheme. \n"
		"This option is in particular useful to supply the algorithm with routes that lead the flow through closed depression. "),
		PARAMETER_INPUT_OPTIONAL
	);


	//-----------------------------------------------------
	// Output...

	Parameters.Add_Grid(
		NULL	, "CHNLNTWRK"	, _TL("Channel Network"),
		_TW("If a cell is part of a channel its value equals the channel order. "
		"Otherwise the cell is marked as no-data."),
		PARAMETER_OUTPUT
	);

	Parameters.Add_Grid(
		NULL	, "CHNLROUTE"	, _TL("Channel Direction"),
		_TW("If a cell is part of a channel then its value shows the flow direction of the channel "
		"(1->NE, 2->E, 3->SE, 4->S, 5->SW, 6->W, 7->NW, 8->N). "
		"Otherwise the cell is marked as no-data."),
		PARAMETER_OUTPUT
	);

	Parameters.Add_Shapes(
		NULL	, "SHAPES"		, _TL("Channel Network"),
		_TL("This shapes layer will contain the resulting channel network in vector format (lines)."),
		PARAMETER_OUTPUT		, SHAPE_TYPE_Line
	);


	//-----------------------------------------------------
	// Initiation...

	pNode	= Parameters.Add_Grid(
		NULL	, "INIT_GRID"	, _TL("Initiation Grid"),
		_TW("Dependent on the chosen 'Initiation Type' and 'Initiation Threshold' "
		"the values of this grid control where a channel is initiated."),
		PARAMETER_INPUT
	);

	Parameters.Add_Choice(
		pNode	, "INIT_METHOD"	, _TL("Initiation Type"),
		_TL("Options:\n - Less than\n - Equals\n - Greater than\nControls under which condition a channel is initiated."),

		CSG_String::Format(SG_T("%s|%s|%s|"),
			_TL("Less than"),
			_TL("Equals"),
			_TL("Greater than")
		), 2
	);

	Parameters.Add_Value(
		pNode	, "INIT_VALUE"	, _TL("Initiation Threshold"),
		_TL("Dependent on the chosen 'Initiation Grid' and 'Initiation Type' this value controls under which condition a channel is initiated."),
		PARAMETER_TYPE_Double	, 0.0
	);

	//-----------------------------------------------------
	pNode	= Parameters.Add_Grid(
		NULL	, "DIV_GRID"	, _TL("Divergence"),
		_TL("Tracing: Convergence"),
		PARAMETER_INPUT_OPTIONAL
	);

	Parameters.Add_Value(
		pNode	, "DIV_CELLS"	, _TL("Tracing: Max. Divergence"),
		_TL("Tracing: Stop after x cells with divergent flow"),
		PARAMETER_TYPE_Int		, 5, 1, true
	);

	//-----------------------------------------------------
	Parameters.Add_Grid(
		NULL	, "TRACE_WEIGHT", _TL("Tracing: Weight"),
		_TL("Tracing: Weight"),
		PARAMETER_INPUT_OPTIONAL
	);

	Parameters.Add_Value(
		NULL	, "MINLEN"		, _TL("Min. Segment Length"),
		_TL("Minimum Segment Length (Cells)"),
		PARAMETER_TYPE_Int		, 10
	);
}

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


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

//---------------------------------------------------------
bool CChannelNetwork::On_Execute(void)
{
	int		x, y, ID, Trace_Method, Init_Method;
	long	n;
	double	Init_Threshold;
	CSG_Grid	*Trace_pRoute, *Trace_pWeight, *Init_pGrid;


	//-----------------------------------------------------
	pDTM				= Parameters("ELEVATION")	->asGrid();
	pConvergence		= Parameters("DIV_GRID")	->asGrid();

	pChannels			= Parameters("CHNLNTWRK")	->asGrid();
	pChannelRoute		= Parameters("CHNLROUTE")	->asGrid();
	pShapes				= Parameters("SHAPES")		->asShapes();

	minLength			= Parameters("MINLEN")		->asInt();

	maxDivCells			= Parameters("DIV_GRID")->asGrid() ? Parameters("DIV_CELLS")->asInt() : -1;


	//-----------------------------------------------------
	// 1. Flow Direction...

	Process_Set_Text(_TL("Channel Network: Pass 1"));

	pChannels->Assign();

	Trace_pRoute		= Parameters("SINKROUTE")	->asGrid();
	Trace_pWeight		= Parameters("TRACE_WEIGHT")->asGrid();
	Trace_Method		= Trace_pWeight ? 1 : 0;

	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			if( Trace_pRoute && (ID = Trace_pRoute->asChar(x, y)) >= 1 && ID <= 8 )
			{
				pChannels->Set_Value(x, y, ID);
			}
			else
			{
				switch( Trace_Method )
				{
				default:
					Set_Route_Standard(x, y);
					break;

				case 1:
					Set_Route_Weighted(x, y, Trace_pWeight, 0.0);
					break;
				}
			}
		}
	}


	//-----------------------------------------------------
	// 2. Initiation...

	Process_Set_Text(_TL("Channel Network: Pass 2"));

	pStart				= SG_Create_Grid(pDTM, GRID_TYPE_Char);
	Init_pGrid			= Parameters("INIT_GRID")	->asGrid();
	Init_Method			= Parameters("INIT_METHOD")	->asInt();
	Init_Threshold		= Parameters("INIT_VALUE")	->asDouble();

	for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
	{
		switch( Init_Method )
		{
		case 0:
			if( Init_pGrid->asDouble(n) <= Init_Threshold )
				pStart->Set_Value(n, 1);
			break;

		case 1:
			if( Init_pGrid->asDouble(n) == Init_Threshold )
				pStart->Set_Value(n, 1);
			break;

		case 2:
			if( Init_pGrid->asDouble(n) >= Init_Threshold )
				pStart->Set_Value(n, 1);
			break;
		}
	}


	//-----------------------------------------------------
	// 3. Trace Channel Routes...

	Process_Set_Text(_TL("Channel Network: Pass 3"));

	pChannelRoute->Assign();

	Direction			= NULL;
	Direction_Buffer	= 0;

	for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
	{
		pDTM->Get_Sorted(n,x,y);

		Set_Channel_Route(x,y);
	}

	if( Direction )
	{
		SG_Free( Direction );
	}

	pChannels->Assign();

	delete(pStart);


	//-----------------------------------------------------
	Process_Set_Text(_TL("Channel Network: Pass 4"));

	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			Set_Channel_Order(x,y);
		}
	}


	//-----------------------------------------------------
	Process_Set_Text(_TL("Channel Network: Pass 5"));

	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			Set_Channel_Mouth(x,y);
		}
	}


	//-----------------------------------------------------
	if( pShapes )
	{
		Process_Set_Text(_TL("Channel Network: Pass 6"));

		pShapes->Create(SHAPE_TYPE_Line, _TL("Channel Network"));

		pShapes->Get_Table().Add_Field("SegmentID"	,TABLE_FIELDTYPE_Int);
		pShapes->Get_Table().Add_Field("Order"		,TABLE_FIELDTYPE_Int);
		pShapes->Get_Table().Add_Field("Length"		,TABLE_FIELDTYPE_Double);

		Lock_Create();

		for(y=0, ID=1; y<Get_NY() && Set_Progress(y); y++)
		{
			for(x=0; x<Get_NX(); x++)
			{
				Set_Vector(x, y, ID++);
			}
		}

		Lock_Destroy();
	}


	//-----------------------------------------------------
	for(n=0; n<Get_NCells(); n++)
	{
		if( pChannels->asInt(n) == 0 )
		{
			pChannels->Set_NoData(n);
			pChannelRoute->Set_NoData(n);
		}
	}


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


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

//---------------------------------------------------------
void CChannelNetwork::Set_Route_Standard(int x, int y)
{
	int		i, ix, iy, iMin;

	double	z, dz, dzMin;

	z		= pDTM->asDouble(x,y);
	iMin	= 0;

	for(i=1; i<=8; i++)
	{
		ix		= Get_xTo(i,x);
		iy		= Get_yTo(i,y);

		if( !pDTM->is_InGrid(ix,iy) )

⌨️ 快捷键说明

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