//---------------------------------------------------------
bool CSelect_Location::Do_Select(CSG_Shape *pShape, int Condition)
{
	for(int 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( 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 );
}
Esempio n. 2
0
//---------------------------------------------------------
bool Cut_Shapes(CSG_Rect Extent, int Method, CSG_Shapes *pShapes, CSG_Shapes *pCut)
{
	if( pCut && pShapes && pShapes->is_Valid() && Extent.Intersects(pShapes->Get_Extent()) )
	{
		pCut->Create(
			pShapes->Get_Type(),
			CSG_String::Format(SG_T("%s [%s]"), pShapes->Get_Name(), _TL("Cut")),
			pShapes
		);

		for(int iShape=0; iShape<pShapes->Get_Count() && SG_UI_Process_Set_Progress(iShape, pShapes->Get_Count()); iShape++)
		{
			bool		bAdd;
			CSG_Shape	*pShape	= pShapes->Get_Shape(iShape);

			if( Method == 2 )	// center
			{
				bAdd	= pShapes->Get_Type() == SHAPE_TYPE_Polygon
						? Extent.Contains(((CSG_Shape_Polygon *)pShape)->Get_Centroid())
						: Extent.Contains(pShape->Get_Extent().Get_Center());
			}
			else				// completely contained, intersects
			{
				switch( pShape->Intersects(Extent) )
				{
				case INTERSECTION_Identical:
				case INTERSECTION_Contained:
					bAdd	= true;
					break;

				case INTERSECTION_Overlaps:
				case INTERSECTION_Contains:
					bAdd	= Method == 1;
					break;

				default:
					bAdd	= false;
					break;
				}
			}

			if( bAdd )
			{
				pCut->Add_Shape(pShape);
			}
		}

		return( pCut->Get_Count() > 0 );
	}

	return( false );
}
Esempio n. 3
0
//---------------------------------------------------------
bool CShapes2Grid::On_Execute(void)
{
	int		iField, iShape, iType;

	//-----------------------------------------------------
	m_pShapes		= Parameters("INPUT")		->asShapes();
	m_Method_Lines	= Parameters("LINE_TYPE")	->asInt();
	iField			= Parameters("FIELD")		->asInt();
	iType			= Parameters("GRID_TYPE")	->asInt();

	if( iField < 0 || iField >= m_pShapes->Get_Field_Count() || m_pShapes->Get_Field_Type(iField) == SG_DATATYPE_String )
	{
		iField		= -1;

		Message_Add(_TL("WARNING: selected attribute is not numeric; generating unique identifiers instead."));
	}

	//-----------------------------------------------------
	m_pGrid		= NULL;

	switch( Parameters("TARGET")->asInt() )
	{
	case 0:	// user defined...
		if( m_Grid_Target.Init_User(m_pShapes->Get_Extent()) && Dlg_Parameters("USER") )
		{
			m_pGrid	= m_Grid_Target.Get_User(Get_Grid_Type(iType));
		}
		break;

	case 1:	// grid...
		if( Dlg_Parameters("GRID") )
		{
			m_pGrid	= m_Grid_Target.Get_Grid(Get_Grid_Type(iType));
		}
		break;
	}

	if( m_pGrid == NULL )
	{
		return( false );
	}

	//-------------------------------------------------
	m_pGrid->Set_Name(CSG_String::Format(SG_T("%s [%s]"), m_pShapes->Get_Name(), iField < 0 ? _TL("ID") : m_pShapes->Get_Field_Name(iField)));
	m_pGrid->Assign_NoData();

	m_pLock	= m_pShapes->Get_Type() == SHAPE_TYPE_Point ? NULL : SG_Create_Grid(m_pGrid, SG_DATATYPE_Byte);

	//-----------------------------------------------------
	for(iShape=0, m_Lock_ID=1; iShape<m_pShapes->Get_Count() && Set_Progress(iShape, m_pShapes->Get_Count()); iShape++, m_Lock_ID++)
	{
		CSG_Shape	*pShape	= m_pShapes->Get_Shape(iShape);

		if( m_pShapes->Get_Selection_Count() <= 0 || pShape->is_Selected() )
		{
			if( iField < 0 || !pShape->is_NoData(iField) )
			{
				m_Value	= iField < 0 ? iShape + 1 : pShape->asDouble(iField);

				if( pShape->Intersects(m_pGrid->Get_Extent().m_rect) )
				{
					switch( m_pShapes->Get_Type() )
					{
					case SHAPE_TYPE_Point:
					case SHAPE_TYPE_Points:		Set_Points	(pShape);	break;
					case SHAPE_TYPE_Line:		Set_Line	(pShape);	break;
					case SHAPE_TYPE_Polygon:	Set_Polygon	(pShape);	break;
					}
				}
			}
		}
	}

	//-----------------------------------------------------
	if( m_pLock )
	{
		delete(m_pLock);
	}

	return( true );
}
Esempio n. 4
0
//---------------------------------------------------------
bool CPolygon_Line_Intersection::Get_Intersection(CSG_Shape_Polygon *pPolygon)
{
	CSG_Network	Network;

	for(int iLine=0; iLine<m_pLines->Get_Count(); iLine++)
	{
		CSG_Shape	*pLine	= m_pLines->Get_Shape(iLine);

		if( pLine->Intersects(pPolygon) )
		{
			Network.Add_Shape(pLine);
		}
	}

	if( Network.Get_Edges().Get_Count() == 0 )
	{
		return( false );
	}

	Network.Add_Shape(pPolygon);
	Network.Update();
	Network.Remove_End_Nodes();

	//-----------------------------------------------------
	int			iEdge, iPolygon;
	CSG_Shapes	Intersection(SHAPE_TYPE_Polygon);

	Intersection.Add_Field(SG_T("ID"), SG_DATATYPE_Int);

	for(iEdge=0; iEdge<Network.Get_Edges().Get_Count(); iEdge++)
	{
		CSG_Shape	*pEdge	= Network.Get_Edges().Get_Shape(iEdge);

		if( pEdge->asInt(3) == SHAPE_TYPE_Polygon )
		{
			Trace_Polygon(Intersection.Add_Shape(), Network, iEdge);
		}
		else if( pPolygon->Contains(pEdge->Get_Point(0)) && pPolygon->Contains(pEdge->Get_Point(pEdge->Get_Point_Count(0) - 1)) )
		{
			Trace_Polygon(Intersection.Add_Shape(), Network, iEdge);
			Trace_Polygon(Intersection.Add_Shape(), Network, iEdge);
		}
	}

	//-----------------------------------------------------
	for(iPolygon=0; iPolygon<Intersection.Get_Count(); iPolygon++)	// 1. outer rings
	{
		CSG_Shape	*pIntersect	= Intersection.Get_Shape(iPolygon);

		if( pIntersect->Get_Point_Count() > 0 && ((CSG_Shape_Polygon *)pIntersect)->is_Clockwise(0) == true )
		{
			pIntersect->Set_Value(0, m_pIntersection->Get_Count());

			((CSG_Table_Record *)m_pIntersection->Add_Shape(pIntersect, SHAPE_COPY_GEOM))->Assign(pPolygon);
		}
	}

	for(iPolygon=0; iPolygon<Intersection.Get_Count(); iPolygon++)	// 2. inner rings
	{
		CSG_Shape	*pIntersect	= Intersection.Get_Shape(iPolygon);

		if( pIntersect->Get_Point_Count() > 0 && ((CSG_Shape_Polygon *)pIntersect)->is_Clockwise(0) == false )
		{
			for(int j=0; j<Intersection.Get_Count(); j++)
			{
				if( ((CSG_Shape_Polygon *)Intersection.Get_Shape(j))->Contains(pIntersect->Get_Point(0)) )
				{
					CSG_Shape	*pShape	= m_pIntersection->Get_Shape(Intersection[j].asInt(0));

					for(int iPoint=0, iPart=pShape->Get_Part_Count(); iPoint<pIntersect->Get_Point_Count(0); iPoint++)
					{
						pShape->Add_Point(pIntersect->Get_Point(iPoint), iPart);
					}

					break;
				}
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
Esempio n. 5
0
//---------------------------------------------------------
bool CShapes2Grid::On_Execute(void)
{
	//-----------------------------------------------------
	m_pShapes			= Parameters("INPUT"    )->asShapes();

	m_Method_Lines		= Parameters("LINE_TYPE")->asInt();
	m_Method_Polygon	= Parameters("POLY_TYPE")->asInt();
	m_Method_Multi		= Parameters("MULTIPLE" )->asInt();

	if( m_pShapes->Get_Type() == SHAPE_TYPE_Polygon && m_Method_Polygon == 1 )	// all cells intersected have to be marked
	{
		m_Method_Lines	= 1;	// thick, each cell crossed by polygon boundary will be marked additionally
	}

	//-----------------------------------------------------
	int		iField;

	switch( Parameters("OUTPUT")->asInt() )
	{
	case 0:	iField	= -1;	break;
	case 1:	iField	= -2;	break;
	case 2:
		if( (iField = Parameters("FIELD")->asInt()) < 0 || !SG_Data_Type_is_Numeric(m_pShapes->Get_Field_Type(iField)) )
		{
			iField		= -2;

			Message_Add(_TL("WARNING: selected attribute is not numeric; generating unique identifiers instead."));
		}
		break;
	}

	//-----------------------------------------------------
	m_Grid_Target.Cmd_Update(m_pShapes);	// if called from saga_cmd

	if( (m_pGrid = m_Grid_Target.Get_Grid("GRID", Get_Grid_Type(Parameters("GRID_TYPE")->asInt()))) == NULL )
	{
		return( false );
	}

	if( iField < 0 )
	{
		m_pGrid->Set_NoData_Value(0.0);
	}

	m_pGrid->Set_Name(CSG_String::Format("%s [%s]", m_pShapes->Get_Name(), iField < 0 ? _TL("ID") : m_pShapes->Get_Field_Name(iField)));
	m_pGrid->Assign_NoData();

	//-------------------------------------------------
	m_pCount	= m_Grid_Target.Get_Grid("COUNT", m_pShapes->Get_Count() < 256 ? SG_DATATYPE_Byte : SG_DATATYPE_Word);

	if( m_pCount == NULL )
	{
		m_Count.Create(m_pGrid->Get_System(), SG_DATATYPE_Word);

		m_pCount	= &m_Count;
	}

	m_pCount->Set_Name(CSG_String::Format("%s [%s]", m_pShapes->Get_Name(), _TL("Count")));
	m_pCount->Set_NoData_Value(0.0);
	m_pCount->Assign(0.0);

	//-----------------------------------------------------
	for(int iShape=0; iShape<m_pShapes->Get_Count() && Set_Progress(iShape, m_pShapes->Get_Count()); iShape++)
	{
		CSG_Shape	*pShape	= m_pShapes->Get_Shape(iShape);

		if( m_pShapes->Get_Selection_Count() <= 0 || pShape->is_Selected() )
		{
			if( iField < 0 || !pShape->is_NoData(iField) )
			{
				m_Value	= iField >= 0 ? pShape->asDouble(iField) : iField == -2 ? iShape + 1 : 1;

				if( pShape->Intersects(m_pGrid->Get_Extent()) )
				{
					switch( m_pShapes->Get_Type() )
					{
					case SHAPE_TYPE_Point:	case SHAPE_TYPE_Points:
						Set_Points	(pShape);
						break;

					case SHAPE_TYPE_Line:
						Set_Line	(pShape);
						break;

					case SHAPE_TYPE_Polygon:
						Set_Polygon	(pShape);

						if( m_Method_Polygon == 1 )	// all cells intersected have to be marked
						{
							Set_Line(pShape);	// thick, each cell crossed by polygon boundary will be marked additionally
						}
						break;
					}
				}
			}
		}
	}

	//-----------------------------------------------------
	if( m_Method_Multi == 4 )	// mean
	{
		for(int y=0; y<m_pGrid->Get_NY() && Set_Progress(y, m_pGrid->Get_NY()); y++)
		{
			for(int x=0; x<m_pGrid->Get_NX(); x++)
			{
				if( m_pCount->asInt(x, y) > 1 )
				{
					m_pGrid->Mul_Value(x, y, 1.0 / m_pCount->asDouble(x, y));
				}
			}
		}
	}

	//-----------------------------------------------------
	m_Count.Destroy();

	return( true );
}