
///////////////////////////////////////////////////////////
//                                                       //
//                         SAGA                          //
//                                                       //
//      System for Automated Geoscientific Analyses      //
//                                                       //
//                     Tool Library                      //
//                     table_tools                       //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//                   SelectByTheme.cpp                   //
//                                                       //
//                      Copyrights                       //
//                 Victor Olaya (c) 2004                 //
//                Olaf Conrad (c) 2011-26                //
//                                                       //
//-------------------------------------------------------//
//                                                       //
// 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, either version 2 of the     //
// License, or (at your option) any later version.       //
//                                                       //
// 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, see   //
// <http://www.gnu.org/licenses/>.                       //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//    e-mail:     oconrad@saga-gis.org                   //
//                                                       //
//    contact:    Olaf Conrad                            //
//                Institute of Geography                 //
//                University of Hamburg                  //
//                Germany                                //
//                                                       //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
#include "SelectByTheme.h"


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

//---------------------------------------------------------
CSelect_Location::CSelect_Location(void)
{
	Set_Name		(_TL("Select by Location..."));

	Set_Author		("V.Olaya (c) 2004, O.Conrad (c) 2011-26");

	Set_Description	(_TW(
		"Select by location."
	));

	//-----------------------------------------------------
	Parameters.Add_Shapes("",
		"SHAPES"   , _TL("Shapes to Select From"),
		_TL(""),
		PARAMETER_INPUT
	);

	Parameters.Add_Shapes("",
		"LOCATIONS", _TL("Locations"),
		_TL(""),
		PARAMETER_INPUT
	);

	Parameters.Add_Choice("",
		"CONDITION", _TL("Condition"), 
		_TL("Select shapes that fulfil this condition"),
		CSG_String::Format("%s|%s|%s|%s|%s",
			_TL("intersect"),
			_TL("are completely within"),
			_TL("completely contain"),
			_TL("have their centroid in"),
			_TL("contain the centeroid of")
		), 0
	);

	Parameters.Add_Choice("",
		"METHOD"   , _TL("Method"),
		_TL(""),
		CSG_String::Format("%s|%s|%s|%s",
			_TL("new selection"),
			_TL("add to current selection"),
			_TL("select from current selection"),
			_TL("remove from current selection")
		), 0
	);

	Parameters.Add_Choice("",
		"POSTJOB"  , _TL("Post Job"),
		_TL(""),
		CSG_String::Format("%s|%s|%s|%s",
			_TL("none"),
			_TL("copy"),
			_TL("move"),
			_TL("delete")
		), 0
	);

	Parameters.Add_Shapes    ("", "COPY"   , _TL("Copy"), _TL(""), PARAMETER_OUTPUT);
	Parameters.Add_PointCloud("", "COPY_PC", _TL("Copy"), _TL(""), PARAMETER_OUTPUT);
}


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

//---------------------------------------------------------
int CSelect_Location::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
{
	if( pParameter->Cmp_Identifier("SHAPES") || pParameter->Cmp_Identifier("POSTJOB") )
	{
		CSG_Parameter *pShapes = (*pParameters)("SHAPES"); int PostJob = (*pParameters)("POSTJOB")->asInt();

		pParameters->Set_Enabled("COPY"   , (PostJob == 1 || PostJob == 2) && pShapes->asDataObject() && pShapes->asDataObject()->asShapes    ());
		pParameters->Set_Enabled("COPY_PC", (PostJob == 1 || PostJob == 2) && pShapes->asDataObject() && pShapes->asDataObject()->asPointCloud());
	}

	return( CSG_Tool::On_Parameters_Enable(pParameters, pParameter) );
}


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

//---------------------------------------------------------
bool CSelect_Location::On_Execute(void)
{
	CSG_Shapes *pShapes = Parameters["SHAPES"].asShapes();

	if( pShapes->Get_Count() < 1 )
	{
		Error_Set(_TL("no shapes in input layer"));

		return( false );
	}

	//-----------------------------------------------------
	m_pLocations = Parameters["LOCATIONS"].asShapes();

	switch( m_Condition = Parameters["CONDITION"].asInt() )
	{
	case 0:	// intersect
		if( ((     pShapes->Get_Type() == SHAPE_TYPE_Point ||      pShapes->Get_Type() == SHAPE_TYPE_Points) && m_pLocations->Get_Type() != SHAPE_TYPE_Polygon)
		||	((m_pLocations->Get_Type() == SHAPE_TYPE_Point || m_pLocations->Get_Type() == SHAPE_TYPE_Points) &&      pShapes->Get_Type() != SHAPE_TYPE_Polygon) )
		{
			Error_Set(_TL("points can only intersect with polygons"));

			return( false );
		}
		break;

	case 1:	// are completely within
	case 3:	// have their centroid in
		if( m_pLocations->Get_Type() != SHAPE_TYPE_Polygon )
		{
			Error_Set(_TL("this operation requires locations to be of type polygon"));

			return( false );
		}
		break;

	case 2:	// completely contain
	case 4:	// contain the centroid of
		if( pShapes->Get_Type() != SHAPE_TYPE_Polygon )
		{
			Error_Set(_TL("this operation requires selectable shapes to be of type polygon"));

			return( false );
		}
		break;
	}

	//-----------------------------------------------------
	int Method = Parameters["METHOD"].asInt();

	for(sLong i=0; i<pShapes->Get_Count() && Set_Progress(i, pShapes->Get_Count()); i++)
	{
		CSG_Shape *pShape = pShapes->Get_Shape(i);

		switch( Method )
		{
		case 0:	// New selection
			if( ( pShape->is_Selected() && !Do_Select(pShape))
			||	(!pShape->is_Selected() &&  Do_Select(pShape)) )
			{
				pShapes->Select(i, true);
			}
			break;

		case 1:	// Add to current selection
			if( (!pShape->is_Selected() &&  Do_Select(pShape)) )
			{
				pShapes->Select(i, true);
			}
			break;

		case 2:	// Select from current selection
			if( ( pShape->is_Selected() && !Do_Select(pShape)) )
			{
				pShapes->Select(i, true);
			}
			break;

		case 3:	// Remove from current selection
			if( ( pShape->is_Selected() &&  Do_Select(pShape)) )
			{
				pShapes->Select(i, true);
			}
			break;
		}
	}

	//-----------------------------------------------------
	Message_Fmt("\n%s: %lld", _TL("selected shapes"), pShapes->Get_Selection_Count());

	if( Parameters["POSTJOB"].asInt() == 1 || Parameters["POSTJOB"].asInt() == 2 ) // copy / move
	{
		CSG_Shapes *pCopy = NULL;

		if( pShapes->asShapes() )
		{
			pCopy = Parameters["COPY"].asShapes(); pCopy->Create(pShapes->Get_Type(), NULL, pShapes, pShapes->Get_Vertex_Type());
		}
		else if( pShapes->asPointCloud() )
		{
			pCopy = Parameters["COPY_PC"].asPointCloud(); pCopy->asPointCloud()->Create(pShapes->asPointCloud());
		}

		if( pCopy )
		{
			pCopy->Fmt_Name("%s [%s]", pShapes->Get_Name(), _TL("Selection"));

			for(sLong i=0; i<pShapes->Get_Selection_Count() && Set_Progress(i, pShapes->Get_Selection_Count()); i++)
			{
				pCopy->Add_Shape(pShapes->Get_Selection(i));
			}
		}
	}

	if( Parameters["POSTJOB"].asInt() == 2 || Parameters["POSTJOB"].asInt() == 3 ) // move / delete
	{
		pShapes->Del_Selection();
	}

	DataObject_Update(pShapes);

	return( true );
}


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

//---------------------------------------------------------
bool CSelect_Location::Do_Select(CSG_Shape *pShape)
{
	for(sLong i=0; i<m_pLocations->Get_Count() && Process_Get_Okay(); i++)
	{
		CSG_Shape *pLocation = m_pLocations->Get_Shape(i);

		if( pShape->Intersects(pLocation->Get_Extent()) )
		{
			switch( m_Condition )
			{
			case 0: // intersect
				if( pLocation->Intersects(pShape) )
				{
					return( true );
				}
				break;

			case 1: // are completely within
				if( pLocation->Intersects(pShape) == INTERSECTION_Contains )
				{
					return( true );
				}
				break;

			case 2: // completely contain
				if( pShape->Intersects(pLocation) == INTERSECTION_Contains )
				{
					return( true );
				}
				break;

			case 3: // have their centroid in
				if( ((CSG_Shape_Polygon *)pLocation)->Contains(pShape->Get_Centroid()) )
				{
					return( true );
				}
				break;

			case 4: // contain the centroid of
				if( ((CSG_Shape_Polygon *)pShape)->Contains(pLocation->Get_Centroid()) )
				{
					return( true );
				}
				break;
			}
		}
	}

	return( false );
}


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

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