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

📄 polygon_intersection.cpp

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

///////////////////////////////////////////////////////////
//                                                       //
//                         SAGA                          //
//                                                       //
//      System for Automated Geoscientific Analyses      //
//                                                       //
//                    Module Library:                    //
//                    shapes_polygons                    //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//                Polygon_Intersection.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 "Polygon_Intersection.h"
#include "Polygon_Intersection_GPC.h"


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

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

	//-----------------------------------------------------
	Set_Name(_TL("Polygon Intersection"));

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

	Set_Description	(_TW(
		"Polygon_Intersection of polygon shapes. Based on the GPC (General Polygon Clipper, version 2.31) code of Alan Murta.")
	);

	//-----------------------------------------------------
	pNode	= Parameters.Add_Shapes(
		NULL	, "SHAPES_A"	, _TL("Layer A"),
		_TL(""),
		PARAMETER_INPUT
	);

	pNode	= Parameters.Add_Shapes(
		NULL	, "SHAPES_B"	, _TL("Layer B"),
		_TL(""),
		PARAMETER_INPUT
	);

	//-----------------------------------------------------
	pNode	= Parameters.Add_Shapes(
		NULL	, "RESULT"		, _TL("Polygon Intersection"),
		_TL(""),
		PARAMETER_OUTPUT
	);

	//-----------------------------------------------------
	pNode	= Parameters.Add_Choice(
		NULL	, "METHOD"		, _TL("Method"),
		_TL(""),

		CSG_String::Format(SG_T("%s|%s|%s|"),
			_TL("Complete Intersection"),
			_TL("Intersection"),
			_TL("Difference (A - B)")
		), 0
	);

	pNode	= Parameters.Add_Value(
		NULL	, "SPLITPARTS"	, _TL("Split Parts"),
		_TL("Set true if you want multipart polygons to become separate polygons."),
		PARAMETER_TYPE_Bool, true
	);
}

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


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

//---------------------------------------------------------
bool CPolygon_Intersection::On_Execute(void)
{
	CSG_String	sName;
	CSG_Shapes		*pShapes_A, *pShapes_B, *pShapes_AB;

	pShapes_A	= Parameters("SHAPES_A")->asShapes();
	pShapes_B	= Parameters("SHAPES_B")->asShapes();

	if(	pShapes_A->Get_Type() == SHAPE_TYPE_Polygon
	&&	pShapes_B->Get_Type() == SHAPE_TYPE_Polygon )
	{
		bSplitParts	= Parameters("SPLITPARTS")->asBool();

		//-------------------------------------------------
		switch( Parameters("METHOD")->asInt() )
		{
		//-------------------------------------------------
		case 0: case 1: default:	// Intersection...
			sName.Printf(SG_T("%s [%s / %s]"), _TL("Intersection"), pShapes_A->Get_Name(), pShapes_B->Get_Name());

			pShapes_AB	= Parameters("RESULT")->asShapes();
			pShapes_AB->Create(SHAPE_TYPE_Polygon, sName);
			pShapes_AB->Get_Table().Add_Field("ID"		, TABLE_FIELDTYPE_Int);
			pShapes_AB->Get_Table().Add_Field("ID_A"	, TABLE_FIELDTYPE_Int);
			pShapes_AB->Get_Table().Add_Field("ID_B"	, TABLE_FIELDTYPE_Int);

			Get_Polygon_Intersection(pShapes_A, pShapes_B, pShapes_AB);

			if( Parameters("METHOD")->asInt() == 0 )	// Complete Intersection...
			{
				Get_Difference	(pShapes_A, pShapes_B, pShapes_AB, 1);
				Get_Difference	(pShapes_B, pShapes_A, pShapes_AB, 2);
			}
			break;

		//-------------------------------------------------
		case 2:						// Difference...
			sName.Printf(SG_T("%s [%s / %s]"), _TL("Difference"), pShapes_A->Get_Name(), pShapes_B->Get_Name());

			pShapes_AB	= Parameters("RESULT")->asShapes();
			pShapes_AB->Create(SHAPE_TYPE_Polygon, sName);
			pShapes_AB->Get_Table().Add_Field("ID"		, TABLE_FIELDTYPE_Int);
			pShapes_AB->Get_Table().Add_Field("ID_A"	, TABLE_FIELDTYPE_Int);

			Get_Difference	(pShapes_A, pShapes_B, pShapes_AB, 0);
			break;
		}

		//-------------------------------------------------
		return( pShapes_AB->Get_Count() > 0 );
	}

	return( false );
}


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

//---------------------------------------------------------
bool CPolygon_Intersection::Get_Polygon_Intersection(CSG_Shapes *pShapes_A, CSG_Shapes *pShapes_B, CSG_Shapes *pShapes_AB)
{
	int		iShape_A, iShape_B, nResult;
	CSG_Shape	*pShape_A, *pShape_B, *pShape_AB;
	CSG_Shapes	Intersect;

	Intersect.Create(SHAPE_TYPE_Polygon);
	pShape_A	= Intersect.Add_Shape();
	pShape_B	= Intersect.Add_Shape();
	pShape_AB	= Intersect.Add_Shape();

	nResult		= 0;

	for(iShape_A=0; iShape_A<pShapes_A->Get_Count() && Set_Progress(iShape_A, pShapes_A->Get_Count()); iShape_A++)
	{
		pShapes_B->Select(pShapes_A->Get_Shape(iShape_A)->Get_Extent().m_rect);

		if( pShapes_B->Get_Selection_Count() > 0 )
		{
			pShape_A->Assign(pShapes_A->Get_Shape(iShape_A));

			for(iShape_B=0; iShape_B<pShapes_B->Get_Selection_Count(); iShape_B++)
			{
				pShape_B->Assign(pShapes_B->Get_Selection(iShape_B));

				if( GPC_Polygon_Intersection(pShape_A, pShape_B, pShape_AB) )
				{
					nResult++;

					Add_Polygon(pShapes_AB, pShape_AB, iShape_A + 1, iShape_B + 1);
				}
			}
		}
	}

	return( nResult > 0 );
}

//---------------------------------------------------------
bool CPolygon_Intersection::Get_Difference(CSG_Shapes *pShapes_A, CSG_Shapes *pShapes_B, CSG_Shapes *pShapes_AB, int bTargetID)
{
	int		iShape_A, iShape_B, nResult;
	CSG_Shape	*pShape_A, *pShape_B, *pShape_AB;
	CSG_Shapes	Intersect;

	Intersect.Create(SHAPE_TYPE_Polygon);
	pShape_A	= Intersect.Add_Shape();
	pShape_B	= Intersect.Add_Shape();
	pShape_AB	= Intersect.Add_Shape();

	nResult		= 0;

	for(iShape_A=0; iShape_A<pShapes_A->Get_Count() && Set_Progress(iShape_A, pShapes_A->Get_Count()); iShape_A++)
	{
		pShape_A->Assign(pShapes_A->Get_Shape(iShape_A));

		pShapes_B->Select(pShapes_A->Get_Shape(iShape_A)->Get_Extent().m_rect);

		if( pShapes_B->Get_Selection_Count() > 0 )
		{
			for(iShape_B=0; iShape_B<pShapes_B->Get_Selection_Count(); iShape_B++)
			{
				pShape_B->Assign(pShapes_B->Get_Selection(iShape_B));

				if( GPC_Difference(pShape_A, pShape_B, pShape_AB) )
				{
					pShape_A->Assign(pShape_AB, false);
				}
			}
		}

		if( pShape_A->is_Valid() )
		{
			nResult++;

			switch( bTargetID )
			{

⌨️ 快捷键说明

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