Example #1
0
//---------------------------------------------------------
bool CGridding_Spline_MBA::_Get_Phi(CSG_Grid &Phi, double dCell, int nCells)
{
	Phi.Create	(SG_DATATYPE_Float, nCells + 4, nCells + 4, dCell, m_pGrid->Get_XMin(), m_pGrid->Get_YMin());
	BA_Get_Phi	(Phi);

	return( _Get_Difference(Phi) );
}
Example #2
0
//---------------------------------------------------------
bool	Copy_RGBGrid_VIGRA_to_SAGA	(CSG_Grid &Grid, BRGBImage &Image, bool bCreate)
{
	if( bCreate )
	{
		Grid.Create(Grid.Get_Type(), Image.width(), Image.height());
	}

	if( Grid.Get_NX() != Image.width() || Grid.Get_NY() != Image.height() )
	{
		return( false );
	}

	for(int y=0; y<Grid.Get_NY() && SG_UI_Process_Set_Progress(y, Grid.Get_NY()); y++)
	{
		for(int x=0; x<Grid.Get_NX(); x++)
		{
			RGBValue<unsigned char>	rgb	= Image(x, y);

			Grid.Set_Value(x, y, SG_GET_RGB(rgb.red(), rgb.green(), rgb.blue()));
		}
	}

	SG_UI_Process_Set_Progress(0.0, 1.0);

	return( true );
}
//---------------------------------------------------------
bool CGrid_Copy::On_Execute(void)
{
	CSG_Grid	*pGrid	= Parameters("GRID")->asGrid();
	CSG_Grid	*pCopy	= Parameters("COPY")->asGrid();

	return( pCopy->Create(*pGrid) );
}
//---------------------------------------------------------
bool CGrid_Mask::On_Execute(void)
{
	CSG_Grid	*pGrid	= Parameters("GRID")->asGrid();
	CSG_Grid	*pMask	= Parameters("MASK")->asGrid();

	if( !pGrid->is_Intersecting(pMask->Get_Extent()) )
	{
		Message_Add(_TL("no intersection with mask grid."));

		return( false );
	}

	//-----------------------------------------------------
	CSG_Grid	*pMasked	= Parameters("MASKED")->asGrid();

	if( pMasked && pMasked != pGrid )
	{
		pMasked->Create(*pGrid);
		pMasked->Fmt_Name("%s [%s]", pGrid->Get_Name(), _TL("masked"));

		pGrid	= pMasked;
	}

	//-----------------------------------------------------
	Process_Set_Text(_TL("masking..."));

	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		double	py	= Get_YMin() + y * Get_Cellsize();

		#pragma omp parallel for
		for(int x=0; x<Get_NX(); x++)
		{
			if( !pGrid->is_NoData(x, y) )
			{
				double	px	= Get_XMin() + x * Get_Cellsize();

				if( !pMask->is_InGrid_byPos(px, py) )
				{
					pGrid->Set_NoData(x, y);
				}
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CGrid_Invert::On_Execute(void)
{
	CSG_Grid	*pGrid	= Parameters("INVERSE")->asGrid();

	if( pGrid == NULL )
	{
		pGrid	= Parameters("GRID")->asGrid();
	}
	else if( pGrid != Parameters("GRID")->asGrid() )
	{
		pGrid->Create(*Parameters("GRID")->asGrid());

		pGrid->Fmt_Name("%s [%s]", pGrid->Get_Name(), _TL("Inverse"));
	}

	//-----------------------------------------------------
	double	zMin	= pGrid->Get_Min();
	double	zMax	= pGrid->Get_Max();

	for(int y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<Get_NX(); x++)
		{
			if( !pGrid->is_NoData(x, y) )
			{
				pGrid->Set_Value(x, y, zMax - (pGrid->asDouble(x, y) - zMin));
			}
		}
	}

	//-----------------------------------------------------
	if( pGrid == Parameters("GRID")->asGrid() )
	{
		DataObject_Update(pGrid);
	}

	return( true );
}
Example #6
0
//---------------------------------------------------------
bool CGridding_Spline_BA::On_Execute(void)
{
	bool	bResult	= false;
	int		nx, ny;
	double	d;
	CSG_Grid	Phi;

	if( Initialise(m_Points, true) )
	{
		d		= m_pGrid->Get_Cellsize() * Parameters("LEVEL")->asDouble();
		nx		= (int)((m_pGrid->Get_XRange()) / d);
		ny		= (int)((m_pGrid->Get_YRange()) / d);
		Phi.Create(SG_DATATYPE_Float, nx + 4, ny + 4, d, m_pGrid->Get_XMin(), m_pGrid->Get_YMin());

		BA_Get_Phi	(Phi);
		BA_Set_Grid	(Phi);

		bResult	= true;
	}

	m_Points.Clear();

	return( bResult );
}
//---------------------------------------------------------
bool CGrid_Value_Type::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Grid	*pOutput	= Parameters("OUTPUT")->asGrid();
	CSG_Grid	*pInput		= Parameters("INPUT" )->asGrid(), Input;

	if( pOutput == NULL || pOutput == pInput )
	{
		Input.Create(*pInput);
		pOutput	= pInput;
		pInput	= &Input;
	}

	//-----------------------------------------------------
	double	Offset	= Parameters("OFFSET")->asDouble();
	double	Scale	= Parameters("SCALE" )->asDouble();

	if( Scale == 0.0 )
	{
		Error_Set(_TL("scale factor must not equal zero"));

		return( false );
	}

	//-----------------------------------------------------
	switch( Parameters("TYPE")->asInt() )
	{
	default:
		Error_Set(_TL("undefined data type"));

		return( false );

	case 0:	pOutput->Create(*Get_System(), SG_DATATYPE_Bit   );	break;
	case 1:	pOutput->Create(*Get_System(), SG_DATATYPE_Byte  );	break;
	case 2:	pOutput->Create(*Get_System(), SG_DATATYPE_Char  );	break;
	case 3:	pOutput->Create(*Get_System(), SG_DATATYPE_Word  );	break;
	case 4:	pOutput->Create(*Get_System(), SG_DATATYPE_Short );	break;
	case 5:	pOutput->Create(*Get_System(), SG_DATATYPE_DWord );	break;
	case 6:	pOutput->Create(*Get_System(), SG_DATATYPE_Int   );	break;
	case 7:	pOutput->Create(*Get_System(), SG_DATATYPE_Float );	break;
	case 8:	pOutput->Create(*Get_System(), SG_DATATYPE_Double);	break;
	}

	pOutput->Set_Name       (pInput->Get_Name       ());
	pOutput->Set_Description(pInput->Get_Description());
	pOutput->Set_Unit       (pInput->Get_Unit       ());
	pOutput->Set_Scaling    (Scale, Offset);

	//-----------------------------------------------------
	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<Get_NX(); x++)
		{
			if( pInput->is_NoData(x, y) )
			{
				pOutput->Set_NoData(x, y);
			}
			else
			{
				pOutput->Set_Value(x, y, pInput->asDouble(x, y));
			}
		}
	}

	//-----------------------------------------------------
	if( pOutput == Parameters("INPUT")->asGrid() )
	{
		DataObject_Update(pOutput);
	}

	return( true );
}
//---------------------------------------------------------
bool CGrid_RGB_Composite::On_Execute(void)
{
	double		rMin, rRange, gMin, gRange, bMin, bRange, aMin, aRange;

	//-----------------------------------------------------
	CSG_Grid	*pR	= _Get_Grid(Parameters("R_GRID")->asGrid(), Parameters("R_METHOD")->asInt(), Parameters("R_RANGE")->asRange(), Parameters("R_PERCTL")->asRange(), Parameters("R_STDDEV")->asDouble(), rMin, rRange);
	CSG_Grid	*pG	= _Get_Grid(Parameters("G_GRID")->asGrid(), Parameters("G_METHOD")->asInt(), Parameters("G_RANGE")->asRange(), Parameters("G_PERCTL")->asRange(), Parameters("G_STDDEV")->asDouble(), gMin, gRange);
	CSG_Grid	*pB	= _Get_Grid(Parameters("B_GRID")->asGrid(), Parameters("B_METHOD")->asInt(), Parameters("B_RANGE")->asRange(), Parameters("B_PERCTL")->asRange(), Parameters("B_STDDEV")->asDouble(), bMin, bRange);
	CSG_Grid	*pA	= _Get_Grid(Parameters("A_GRID")->asGrid(), Parameters("A_METHOD")->asInt(), Parameters("A_RANGE")->asRange(), Parameters("A_PERCTL")->asRange(), Parameters("A_STDDEV")->asDouble(), aMin, aRange);

	//-----------------------------------------------------
	CSG_Grid	*pRGB	= Parameters("RGB")->asGrid();

	pRGB->Create(pRGB->Get_System(), SG_DATATYPE_Int);
	pRGB->Set_Name(_TL("Composite"));

	CSG_String	s;

	s	+= CSG_String(_TL("Red"  )) + ": " + pR->Get_Name() + "\n";
	s	+= CSG_String(_TL("Green")) + ": " + pG->Get_Name() + "\n";
	s	+= CSG_String(_TL("Blue" )) + ": " + pB->Get_Name() + "\n";

	if( pA )
	{
		s	+= CSG_String(_TL("Alpha")) + ": " + pA->Get_Name() + "\n";
	}

	pRGB->Set_Description(s);

	DataObject_Set_Colors   (pRGB, 100, SG_COLORS_BLACK_WHITE);
	DataObject_Set_Parameter(pRGB, "COLORS_TYPE", 5);	// Color Classification Type: RGB Coded Values

	//-----------------------------------------------------
	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<Get_NX(); x++)
		{
			if( pR->is_NoData(x, y) || pG->is_NoData(x, y) || pB->is_NoData(x, y) || (pA && pA->is_NoData(x, y)) )
			{
				pRGB->Set_NoData(x, y);
			}
			else
			{
				int	r	= (int)(rRange * (pR->asDouble(x, y) - rMin)); if( r > 255 ) r = 255; else if( r < 0 ) r = 0;
				int	g	= (int)(gRange * (pG->asDouble(x, y) - gMin)); if( g > 255 ) g = 255; else if( g < 0 ) g = 0;
				int	b	= (int)(bRange * (pB->asDouble(x, y) - bMin)); if( b > 255 ) b = 255; else if( b < 0 ) b = 0;

				if( pA )
				{
					int	a	= (int)(aRange * (pA->asDouble(x, y) - aMin)); if( a > 255 ) a = 255; else if( a < 0 ) a = 0;

					pRGB->Set_Value(x, y, SG_GET_RGBA(r, g, b, a));
				}
				else
				{
					pRGB->Set_Value(x, y, SG_GET_RGB (r, g, b));
				}
			}
		}
	}

	return( true );
}
//---------------------------------------------------------
bool CGrid_Mirror::On_Execute(void)
{
	CSG_Grid	*pGrid	= Parameters("MIRROR")->asGrid();

	if( pGrid == NULL )
	{
		pGrid	= Parameters("GRID")->asGrid();
	}
	else if( pGrid != Parameters("GRID")->asGrid() )
	{
		pGrid->Create(*Parameters("GRID")->asGrid());

		pGrid->Fmt_Name("%s [%s %s]", pGrid->Get_Name(), _TL("mirrored"), Parameters("METHOD")->asString());
	}

	//-----------------------------------------------------
	switch( Parameters("METHOD")->asInt() )
	{
	//-----------------------------------------------------
	case  0:	// vertically
		{
			for(int xa=0, xb=Get_NX()-1; xa<xb && SG_UI_Process_Set_Progress(xa, Get_NX()/2); xa++, xb--)
			{
				#pragma omp parallel for
				for(int y=0; y<Get_NY(); y++)
				{
					double	d             = pGrid->asDouble(xa, y);
					pGrid->Set_Value(xa, y, pGrid->asDouble(xb, y));
					pGrid->Set_Value(xb, y, d);
				}
			}
		}
		break;

	//-----------------------------------------------------
	case  1:	// horizontally
		{
			for(int ya=0, yb=Get_NY()-1; ya<yb && SG_UI_Process_Set_Progress(ya, Get_NY()/2); ya++, yb--)
			{
				#pragma omp parallel for
				for(int x=0; x<Get_NX(); x++)
				{
					double	d             = pGrid->asDouble(x, ya);
					pGrid->Set_Value(x, ya, pGrid->asDouble(x, yb));
					pGrid->Set_Value(x, yb, d);
				}
			}
		}
		break;

	//-----------------------------------------------------
	default:	// both
		{
			for(int ya=0, yb=Get_NY()-1; ya<=yb && SG_UI_Process_Set_Progress(ya, Get_NY()/2); ya++, yb--)
			{
				for(int xa=0, xb=Get_NX()-1; xa<=xb; xa++, xb--)
				{
					if( ya < yb && xa < xb )
					{
						double	d              = pGrid->asDouble(xa, ya);
						pGrid->Set_Value(xa, ya, pGrid->asDouble(xb, yb));
						pGrid->Set_Value(xb, yb, d);

						d                      = pGrid->asDouble(xa, yb);
						pGrid->Set_Value(xa, yb, pGrid->asDouble(xb, ya));
						pGrid->Set_Value(xb, ya, d);
					}
					else if( xa < xb )
					{
						double	d              = pGrid->asDouble(xa, ya);
						pGrid->Set_Value(xa, ya, pGrid->asDouble(xb, ya));
						pGrid->Set_Value(xb, ya, d);
					}
					else if( ya < yb )
					{
						double	d              = pGrid->asDouble(xa, ya);
						pGrid->Set_Value(xa, ya, pGrid->asDouble(xa, yb));
						pGrid->Set_Value(xa, yb, d);
					}
				}
			}
		}
		break;
	}

	//-----------------------------------------------------
	if( pGrid == Parameters("GRID")->asGrid() )
	{
		DataObject_Update(pGrid);
	}

	return( true );
}
//---------------------------------------------------------
CSG_Shape * CWatersheds_ext::Get_Basin(CSG_Grid *pBasins, CSG_Shapes *pPolygons)
{
	int			x, y, nEdges, Basin_ID;
	CSG_Grid	Edge;
	CSG_Shape	*pPolygon	= NULL;

	Basin_ID	= 1 + pPolygons->Get_Count();

	//-----------------------------------------------------
	Edge.Create(SG_DATATYPE_Char, 2 * Get_NX() + 1, 2 * Get_NY() + 1, 0.5 * Get_Cellsize(), Get_XMin() - 0.5 * Get_Cellsize(), Get_YMin() - 0.5 * Get_Cellsize());
	Edge.Set_NoData_Value(0);

	for(y=0, nEdges=0; y<Get_NY() && Process_Get_Okay(); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			if( pBasins->asInt(x, y) == Basin_ID )
			{
				for(int i=0; i<8; i+=2)
				{
					int	ix	= Get_xTo(i, x);
					int	iy	= Get_yTo(i, y);

					if( !is_InGrid(ix, iy) || pBasins->asInt(ix, iy) != Basin_ID )
					{
						ix	= 1 + 2 * x;
						iy	= 1 + 2 * y;

						Edge.Set_Value(               ix,                 iy ,  1);
						Edge.Set_Value(Get_xTo(i    , ix), Get_yTo(i    , iy), -1);
						Edge.Set_Value(Get_xTo(i - 1, ix), Get_yTo(i - 1, iy), -1);

						nEdges++;
					}
				}
			}
		}
	}

	//-----------------------------------------------------
	if( nEdges > 0 )
	{
		for(int yEdge=0; yEdge<Edge.Get_NY(); yEdge++)	for(int xEdge=0; xEdge<Edge.Get_NX(); xEdge++)
		{
			int	i	= 4;

			if( Edge.asInt(xEdge, yEdge) == 1 && Edge.asInt(Get_xTo(i, xEdge), Get_yTo(i, yEdge)) == -1 )
			{
				if( pPolygon == NULL )
				{
					pPolygon	= pPolygons->Add_Shape();
				}

				int	iPart	= pPolygon->Get_Part_Count();
				int	xFirst	= x	= Get_xTo(i, xEdge);
				int	yFirst	= y	= Get_yTo(i, yEdge);
				i	= i + 2;

				pPolygon	->Add_Point(Edge.Get_System().Get_Grid_to_World(x, y), iPart);

				do
				{
					int	ix	= Get_xTo(i + 2, x);
					int	iy	= Get_yTo(i + 2, y);

					if( Edge.is_InGrid(ix, iy) && Edge.asInt(ix, iy) == -1 )			// go right ?
					{
						pPolygon->Add_Point(Edge.Get_System().Get_Grid_to_World(x, y), iPart);

						i	= (i + 2) % 8;
					}
					else
					{
						if( Edge.asInt(ix, iy) == 1 )
						{
							Edge.Set_NoData(ix, iy);	// erase class id in right cells
						}

						ix	= Get_xTo(i, x);
						iy	= Get_yTo(i, y);

						if( Edge.is_InGrid(ix, iy) && Edge.asInt(ix, iy) == -1 )		// go ahead ?
						{
							// nop
						}
						else
						{
							ix	= Get_xTo(i + 6, x);
							iy	= Get_yTo(i + 6, y);

							if( Edge.is_InGrid(ix, iy) && Edge.asInt(ix, iy) == -1 )	// go left ?
							{
								pPolygon->Add_Point(Edge.Get_System().Get_Grid_to_World(x, y), iPart);

								i	= (i + 6) % 8;
							}
							else
							{
								return( false );
							}
						}
					}

					x	= ix;
					y	= iy;
				}
				while( x != xFirst || y != yFirst );

				pPolygon->Add_Point(Edge.Get_System().Get_Grid_to_World(x, y), iPart);
			}
		}
	}

	//-----------------------------------------------------
	return( pPolygon );
}
//---------------------------------------------------------
bool CGW_Multi_Regression_Grid::On_Execute(void)
{
	int		i;

	//-----------------------------------------------------
	CSG_Parameter_Grid_List	*pPredictors	= Parameters("PREDICTORS")->asGridList();

	if( !Initialize(Parameters("POINTS")->asShapes(), Parameters("DEPENDENT")->asInt(), pPredictors) )
	{
		Finalize();

		return( false );
	}

	//-----------------------------------------------------
	CSG_Grid	Quality;

	m_dimModel	= *Get_System();

	if( Parameters("RESOLUTION")->asInt() == 1 && Parameters("RESOLUTION_VAL")->asDouble() > Get_Cellsize() )
	{
		CSG_Rect	r(Get_System()->Get_Extent()); r.Inflate(0.5 * Parameters("RESOLUTION_VAL")->asDouble(), false);

		m_dimModel.Assign(Parameters("RESOLUTION_VAL")->asDouble(), r);

		Quality.Create(m_dimModel);
		m_pQuality	= &Quality;
	}
	else
	{
		m_pQuality	= Parameters("QUALITY")->asGrid();
	}

	//-----------------------------------------------------
	Process_Set_Text(_TL("upsetting model domain"));

	m_pPredictors	= (CSG_Grid **)SG_Calloc(m_nPredictors    , sizeof(CSG_Grid *));
	m_pModel		= (CSG_Grid **)SG_Calloc(m_nPredictors + 1, sizeof(CSG_Grid *));

	for(i=0; i<m_nPredictors; i++)
	{
		if( m_dimModel.Get_Cellsize() > Get_Cellsize() )	// scaling
		{
			m_pPredictors[i]	= SG_Create_Grid(m_dimModel);
			m_pPredictors[i]	->Assign(pPredictors->asGrid(i), GRID_INTERPOLATION_NearestNeighbour);	// GRID_INTERPOLATION_Mean_Cells
		}
		else
		{
			m_pPredictors[i]	= pPredictors->asGrid(i);
		}

		m_pModel     [i]	= SG_Create_Grid(m_dimModel);
		m_pModel     [i]	->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pPredictors->asGrid(i)->Get_Name(), _TL("Factor")));
	}

	m_pModel[m_nPredictors]	= SG_Create_Grid(m_dimModel);
	m_pModel[m_nPredictors]	->Set_Name(_TL("Intercept"));

	//-----------------------------------------------------
	Process_Set_Text(_TL("model creation"));

	bool	bResult	= Get_Model();

	//-----------------------------------------------------
	if( m_dimModel.Get_Cellsize() > Get_Cellsize() )	// scaling
	{
		for(i=0; i<m_nPredictors; i++)
		{
			delete(m_pPredictors[i]);

			m_pPredictors[i]	= pPredictors->asGrid(i);
		}
	}

	//-----------------------------------------------------
	if( bResult )
	{
		Process_Set_Text(_TL("model application"));

		bResult	= Set_Model();
	}

	//-----------------------------------------------------
	if( Parameters("MODEL_OUT")->asBool() )
	{
		CSG_Parameter_Grid_List	*pModel	= Parameters("MODEL")->asGridList();

		pModel->Del_Items();
		pModel->Add_Item(m_pModel[m_nPredictors]);

		for(i=0; i<m_nPredictors; i++)
		{
			pModel->Add_Item(m_pModel[i]);
		}
	}
	else
	{
		for(i=0; i<=m_nPredictors; i++)
		{
			delete(m_pModel[i]);
		}
	}

	SG_FREE_SAFE(m_pModel);
	SG_FREE_SAFE(m_pPredictors);

	Finalize();

	return( bResult );
}
Example #12
0
//---------------------------------------------------------
bool CTC_Classification::On_Execute(void)
{
	//-----------------------------------------------------
	m_pSlope		= Parameters("SLOPE"    )->asGrid();
	m_pConvexity	= Parameters("CONVEXITY")->asGrid();
	m_pTexture		= Parameters("TEXTURE"  )->asGrid();

	if( (!m_pSlope || !m_pConvexity || !m_pTexture) && !Parameters("DEM")->asGrid() )
	{
		return( false );
	}

	//-----------------------------------------------------
	CSG_Grid	Slope;

	if( !m_pSlope )
	{
		Slope.Create(*Get_System());	m_pSlope	= &Slope;

		CSG_Grid	*pDEM	= Parameters("DEM")->asGrid();

		for(int y=0; y<Get_NY() && Set_Progress(y); y++)
		{
			#pragma omp parallel for
			for(int x=0; x<Get_NX(); x++)
			{
				double	s, a;

				if( pDEM->Get_Gradient(x, y, s, a) )
				{
					Slope.Set_Value(x, y, s);
				}
				else
				{
					Slope.Set_NoData(x, y);
				}
			}
		}
	}

	//-----------------------------------------------------
	if( !m_pConvexity || Parameters("CONV_RECALC")->asBool() )
	{
		CTC_Convexity	c;

		c.Set_Parameter(      "DEM", Parameters("DEM"));
		c.Set_Parameter(    "SCALE", Parameters("CONV_SCALE"));
		c.Set_Parameter(   "KERNEL", Parameters("CONV_KERNEL"));
		c.Set_Parameter(     "TYPE", Parameters("CONV_TYPE"));
		c.Set_Parameter(  "EPSILON", Parameters("CONV_EPSILON"));
		c.Set_Parameter("CONVEXITY", m_pConvexity);

		if( !c.Execute() )
		{
			return( false );
		}

		Parameters("CONVEXITY")->Set_Value(m_pConvexity = c.Get_Parameters()->Get_Parameter("CONVEXITY")->asGrid());
	}

	//-----------------------------------------------------
	if( !m_pTexture || Parameters("TEXT_RECALC")->asBool() )
	{
		CTC_Texture	c;

		c.Set_Parameter(    "DEM", Parameters("DEM"));
		c.Set_Parameter(  "SCALE", Parameters("TEXT_SCALE"));
		c.Set_Parameter("EPSILON", Parameters("TEXT_EPSILON"));
		c.Set_Parameter("TEXTURE", m_pTexture);

		if( !c.Execute() )
		{
			return( false );
		}

		Parameters("TEXTURE")->Set_Value(m_pTexture = c.Get_Parameters()->Get_Parameter("TEXTURE")->asGrid());
	}

	//-----------------------------------------------------
	return( Get_Classes() );
}