//---------------------------------------------------------
bool CSG_Grid_Pyramid::_Get_Next_Level(CSG_Grid *pGrid)
{
	if( (m_nMaxLevels <= 0 || m_nLevels < m_nMaxLevels) )
	{
		int		nx, ny;
		double	Cellsize;

		switch( m_Grow_Type )
		{
		case GRID_PYRAMID_Arithmetic:	Cellsize	= pGrid->Get_Cellsize() + m_Grow;	break;
		case GRID_PYRAMID_Geometric:	Cellsize	= pGrid->Get_Cellsize() * m_Grow;	break;
		default: Cellsize	= pGrid->Get_Cellsize() * m_Grow;	break;
		}

		nx	= (int)(1.5 + m_pGrid->Get_XRange() / Cellsize);	if( nx < 1 )	nx	= 1;
		ny	= (int)(1.5 + m_pGrid->Get_YRange() / Cellsize);	if( ny < 1 )	ny	= 1;

		if( nx > 1 || ny > 1 )
		{
			CSG_Grid	*pNext	= SG_Create_Grid(SG_DATATYPE_Float, nx, ny, Cellsize, pGrid->Get_XMin(), pGrid->Get_YMin());

			pNext->Set_NoData_Value(pGrid->Get_NoData_Value());
			pNext->Assign(pGrid);

			m_pLevels	= (CSG_Grid **)SG_Realloc(m_pLevels, (m_nLevels + 1) * sizeof(CSG_Grid *));
			m_pLevels[m_nLevels++]	= pNext;

			_Get_Next_Level(pNext);

			return( true );
		}
	}

	return( false );
}
//---------------------------------------------------------
bool CSG_Grid_Pyramid::_Get_Next_Level(CSG_Grid *pGrid, double Cellsize)
{
	if( (m_nMaxLevels <= 0 || m_nLevels < m_nMaxLevels) )
	{
		int		nx, ny;

		nx	= (int)(1.5 + m_pGrid->Get_XRange() / Cellsize);	if( nx < 1 )	nx	= 1;
		ny	= (int)(1.5 + m_pGrid->Get_YRange() / Cellsize);	if( ny < 1 )	ny	= 1;

		if( nx > 1 || ny > 1 )
		{
			CSG_Grid	*pNext	= SG_Create_Grid(SG_DATATYPE_Float, nx, ny, Cellsize, pGrid->Get_XMin(), pGrid->Get_YMin());

			pNext->Set_NoData_Value(pGrid->Get_NoData_Value());
			pNext->Assign(pGrid);

			m_pLevels	= (CSG_Grid **)SG_Realloc(m_pLevels, (m_nLevels + 1) * sizeof(CSG_Grid *));
			m_pLevels[m_nLevels++]	= pNext;

			_Get_Next_Level(pNext);

			return( true );
		}
	}

	return( false );
}
Example #3
0
//---------------------------------------------------------
bool CWatershed_Segmentation::Get_Borders(void)
{
    Process_Set_Text(_TL("Borders"));

    CSG_Grid	*pBorders	= SG_Create_Grid(SG_DATATYPE_Byte, Get_NX() + 2, Get_NY() + 2, Get_Cellsize(), Get_XMin() - 0.5 * Get_Cellsize(), Get_YMin() - 0.5 * Get_Cellsize());

    pBorders->Set_NoData_Value(0);

    Parameters("BORDERS")->Set_Value(pBorders);

    for(int y=0, yy=1; yy<Get_NY() && Set_Progress(yy); y++, yy++)
    {
        for(int x=0, xx=1; xx<Get_NX(); x++, xx++)
        {
            int		id	= m_pSegments->asInt(x, y);

            if( id != m_pSegments->asInt(xx,  y) )
            {
                pBorders->Set_Value(xx,  y, 1);
            }

            if( id != m_pSegments->asInt( x, yy) )
            {
                pBorders->Set_Value( x, yy, 1);
            }

            if( id != m_pSegments->asInt(xx, yy) )
            {
                pBorders->Set_Value(xx, yy, 1);
            }
        }
    }

    return( true );
}
//---------------------------------------------------------
bool CConvergence::On_Execute(void)
{
	bool		bGradient;
	int			Neighbours;
	CSG_Grid	*pConvergence;

	m_pDTM			= Parameters("ELEVATION")	->asGrid();
	pConvergence	= Parameters("RESULT")		->asGrid();
	Neighbours		= Parameters("NEIGHBOURS")	->asInt();
	bGradient		= Parameters("METHOD")		->asInt() == 1;

	DataObject_Set_Colors(pConvergence, 100, SG_COLORS_RED_GREY_BLUE, true);

	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(int x=0; x<Get_NX(); x++)
		{
			if( m_pDTM->is_InGrid(x, y) )
			{
				switch( Neighbours )
				{
				case 0: default:	pConvergence->Set_Value(x, y, Get_2x2(x, y, bGradient));	break;
				case 1:				pConvergence->Set_Value(x, y, Get_9x9(x, y, bGradient));	break;
				}
			}
			else
			{
				pConvergence->Set_NoData(x, y);
			}
		}
	}

	return( true );
}
Example #5
0
//---------------------------------------------------------
bool CGridding_Spline_Base::_Get_Grid(void)
{
	CSG_Grid	*pGrid		=  m_bGridPoints ? Parameters("GRIDPOINTS")->asGrid  () : NULL;
	CSG_Shapes	*pShapes	= !m_bGridPoints ? Parameters("SHAPES")    ->asShapes() : NULL;

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

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

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

	//-------------------------------------------------
	if( m_pGrid )
	{
		m_pGrid->Set_Name(CSG_String::Format(SG_T("%s (%s)"), m_bGridPoints ? pGrid->Get_Name() : pShapes->Get_Name(), Get_Name()));
		m_pGrid->Assign_NoData();
	}

	//-----------------------------------------------------
	return( m_pGrid != NULL );
}
//---------------------------------------------------------
bool CGrid_Volume::On_Execute(void)
{
	int			x, y, Method;
	double		Level, Volume, z;
	CSG_Grid		*pGrid;
	CSG_String	s;

	//-----------------------------------------------------
	pGrid	= Parameters("GRID")	->asGrid();
	Level	= Parameters("LEVEL")	->asDouble();
	Method	= Parameters("METHOD")	->asInt();

	//-----------------------------------------------------
	for(y=0, Volume=0.0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			if( !pGrid->is_NoData(x, y) )
			{
				z	= pGrid->asDouble(x, y) - Level;

				switch( Method )
				{
				case 0:	// Count Only Above Base Level
					if( z > 0.0 )
					{
						Volume	+= z;
					}
					break;

				case 1:	// Count Only Below Base Level
					if( z < 0.0 )
					{
						Volume	-= z;
					}
					break;

				case 2:	// Subtract Volumes Below Base Level
					Volume	+= z;
					break;

				case 3:	// Add Volumes Below Base Level
					Volume	+= fabs(z);
					break;
				}
			}
		}
	}

	//-----------------------------------------------------
	Volume	*= pGrid->Get_Cellarea();

	s.Printf(_TL("Volume: %f"), Volume);

	Message_Add(s);
	Message_Dlg(s, _TL("Grid Volume"));

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
void CD8_Flow_Analysis::Get_Direction(void)
{
	Process_Set_Text(_TL("Flow Direction"));

	m_pDir->Set_NoData_Value(-1);

	CSG_Grid	*pCon	= Parameters("CONNECTION")	->asGrid();

	if( pCon )
	{
		pCon->Assign(0.0);
	}

	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(int x=0, i, ix, iy; x<Get_NX(); x++)
		{
			if( (i = m_pDEM->Get_Gradient_NeighborDir(x, y)) >= 0 && m_pDEM->is_InGrid(ix = Get_xTo(i, x), iy = Get_yTo(i, y)) )
			{
				m_pDir->Set_Value(x, y, i);

				if( pCon )
				{
					pCon->Add_Value(ix, iy, 1);
				}
			}
			else
			{
				m_pDir->Set_NoData(x, y);
			}
		}
	}
}
//---------------------------------------------------------
bool CConstantGrid::On_Execute(void)
{
	//-----------------------------------------------------
	TSG_Data_Type	Type	= SG_DATATYPE_Float;

	switch( Parameters("TYPE")->asInt() )
	{
	case 0:	Type	= SG_DATATYPE_Bit   ;	break;
	case 1:	Type	= SG_DATATYPE_Byte  ;	break;
	case 2:	Type	= SG_DATATYPE_Char  ;	break;
	case 3:	Type	= SG_DATATYPE_Word  ;	break;
	case 4:	Type	= SG_DATATYPE_Short ;	break;
	case 5:	Type	= SG_DATATYPE_ULong ;	break;
	case 6:	Type	= SG_DATATYPE_Long  ;	break;
	case 7:	Type	= SG_DATATYPE_Float ;	break;
	case 8:	Type	= SG_DATATYPE_Double;	break;
	}

	//-----------------------------------------------------
	CSG_Grid	*pGrid	= m_Grid_Target.Get_Grid(Type);

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

	//-----------------------------------------------------
	pGrid->Set_Name(Parameters("NAME" )->asString());

	pGrid->Assign  (Parameters("CONST")->asDouble());

	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 CCost_Accumulated::Get_Destinations(CPoints &Points)
{
	Points.Clear();

	m_pAccumulated->Set_NoData_Value(-1.0); m_pAccumulated->Assign(-1.0);
	m_pAllocation ->Set_NoData_Value(-1.0); m_pAllocation ->Assign( 0.0);

	if( Parameters("DEST_TYPE")->asInt() == 0 )	// Point
	{
		CSG_Shapes	*pDestinations	= Parameters("DEST_POINTS")->asShapes();

		for(int i=0, x, y; i<pDestinations->Get_Count(); i++)
		{
			if( Get_System().Get_World_to_Grid(x, y, pDestinations->Get_Shape(i)->Get_Point(0)) && !m_pCost->is_NoData(x, y) )
			{
				Points.Add(x, y); m_pAllocation->Set_Value(x, y, Points.Get_Count()); m_pAccumulated->Set_Value(x, y, 0.0);
			}
		}
	}
	else										// Grid
	{
		CSG_Grid	*pDestinations	= Parameters("DEST_GRID")->asGrid();

		for(int y=0; y<Get_NY(); y++)	for(int x=0; x<Get_NX(); x++)
		{
			if( !pDestinations->is_NoData(x, y) && !m_pCost->is_NoData(x, y) )
			{
				Points.Add(x, y); m_pAllocation->Set_Value(x, y, Points.Get_Count()); m_pAccumulated->Set_Value(x, y, 0.0);
			}
		}
	}

	return( Points.Get_Count() > 0 );
}
//---------------------------------------------------------
bool CGrid_Division::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Grid	*pA	= Parameters("A")->asGrid();
	CSG_Grid	*pB	= Parameters("B")->asGrid();
	CSG_Grid	*pC	= Parameters("C")->asGrid();

	DataObject_Set_Colors(pC, 11, SG_COLORS_RED_GREY_BLUE);

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

	//-----------------------------------------------------
	return( true );
}
Example #12
0
//---------------------------------------------------------
double CGridding_Spline_MBA::BA_Get_Value(double x, double y, CSG_Grid &Phi)
{
	int		_x, _y, ix, iy;
	double	z	= 0.0, bx[4], by;

	if(	(_x = (int)x) >= 0 && _x < Phi.Get_NX() - 3
	&&	(_y = (int)y) >= 0 && _y < Phi.Get_NY() - 3 )
	{
		x	-= _x;
		y	-= _y;

		for(ix=0; ix<4; ix++)
		{
			bx[ix]	= BA_Get_B(ix, x);
		}

		for(iy=0; iy<4; iy++)
		{
			by	= BA_Get_B(iy, y);

			for(ix=0; ix<4; ix++)
			{
				z	+= by * bx[ix] * Phi.asDouble(_x + ix, _y + iy);
			}
		}
	}

	return( z );
}
//---------------------------------------------------------
bool CViGrA_Watershed::On_Execute(void)
{
	CSG_Grid	*pInput  = Parameters("INPUT" )->asGrid();
	CSG_Grid	*pOutput = Parameters("OUTPUT")->asGrid();

	//-----------------------------------------------------
	if( !Parameters("RGB")->asBool() )
	{
		vigra::FImage	Input, Output(Get_NX(), Get_NY());

		Copy_Grid_SAGA_to_VIGRA(*pInput, Input, true);

		Segmentation(Input, Output, Parameters("SCALE")->asDouble(), Parameters("EDGES")->asBool());

		Copy_Grid_VIGRA_to_SAGA(*pOutput, Output, false);
	}

	//-----------------------------------------------------
	else	// perform watershed segmentation on color image
	{
		vigra::BRGBImage	Input, Output(Get_NX(), Get_NY());

		Copy_RGBGrid_SAGA_to_VIGRA(*pInput, Input, true);

		Segmentation(Input, Output, Parameters("SCALE")->asDouble(), Parameters("EDGES")->asBool());

		Copy_RGBGrid_VIGRA_to_SAGA(*pOutput, Output, false);
	}

	//-----------------------------------------------------
	pOutput->Fmt_Name("%s [%s]", pInput->Get_Name(), Get_Name().c_str());

	return( true );
}
bool Cdodproperror::SaveGridsAsTIFF(CSG_Grid** grids, CSG_Strings paths)
{
	TSG_Data_Type Type;
	CSG_String FilePath;
	CSG_Grid* Grid;

	// SAVE TIFFS
	for (int i = 0; i < paths.Get_Count(); i++)
	{
		 FilePath = paths[i];
		 Grid = grids[i];
		 Type = Grid->Get_Type();

		if( !GDALDataSet.Open_Write(FilePath, GDALDriver, GDALOptions, Type, 1, *Get_System(), Projection) )
		{
			Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to open file for writing: "), FilePath.c_str()));
			return( false );
		}
		GDALDataSet.Write(0, Grid);
		if( !GDALDataSet.Close() )
		{
			Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to close file after writing: "), FilePath.c_str()));
			return( false );
		}
	}

	return true;
}
Example #15
0
//---------------------------------------------------------
bool CDVWK_SoilMoisture::On_Execute(void)
{
	int			Day, x, y, i, LandUseID;
	CSG_Grid	*pGrid;

	//-----------------------------------------------------
	if( pClimate->Get_Record_Count() > 0 )
	{
		pFK_mm		= Parameters("STA_FC")		->asGrid();
		FK_mm_Def	= Parameters("STA_FC_DEF")	->asDouble();

		pPWP_mm		= Parameters("STA_PWP")		->asGrid();
		PWP_mm_Def	= Parameters("STA_PWP_DEF")	->asDouble();

		pWi_mm		= Parameters("DYN_W")		->asGrid();
		DataObject_Set_Colors(pWi_mm, 100, SG_COLORS_YELLOW_BLUE);

		//-------------------------------------------------
		pLandUse	= SG_Create_Grid(pWi_mm, pCropCoeff->Get_Record_Count() < 127 ? SG_DATATYPE_Char : SG_DATATYPE_Int);
		pLandUse->Assign(Parameters("LANDUSE_DEF")->asInt());

		if( (pGrid = Parameters("LANDUSE")->asGrid()) != NULL )
		{
			for(y=0; y<Get_NY(); y++)
			{
				for(x=0; x<Get_NX(); x++)
				{
					LandUseID	= pGrid->asInt(x, y);

					for(i=0; i<pCropCoeff->Get_Record_Count(); i++)
					{
						if( LandUseID == pCropCoeff->Get_Record(i)->asInt(0) )
						{
							pLandUse->Set_Value(x, y, i);
							break;
						}
					}
				}
			}
		}

		//-------------------------------------------------
		DataObject_Update(pWi_mm, 0, pFK_mm ? pFK_mm->Get_ZMax() : FK_mm_Def, true);

		for(Day=0; Day<365 && Set_Progress(Day, 365); Day++)
		{
			Step_Day(Day);

			DataObject_Update(pWi_mm, true);
		}

		//-------------------------------------------------
		delete(pLandUse);

		return( true );
	}

	return( false );
}
Example #16
0
//---------------------------------------------------------
bool CFilter_Rank::On_Execute(void)
{
	int			x, y;
	double		Rank;
	CSG_Grid	*pResult;

	//-----------------------------------------------------
	m_pInput	= Parameters("INPUT" )->asGrid();
	pResult		= Parameters("RESULT")->asGrid();
	Rank		= Parameters("RANK"  )->asInt() / 100.0;

	//-----------------------------------------------------
	m_Kernel.Set_Radius(Parameters("RADIUS")->asInt(), Parameters("MODE")->asInt() == 0);

	//-----------------------------------------------------
	if( !pResult || pResult == m_pInput )
	{
		pResult	= SG_Create_Grid(m_pInput);
	}
	else
	{
		pResult->Set_Name(CSG_String::Format(SG_T("%s [%s: %.1f]"), m_pInput->Get_Name(), _TL("Rank"), 100.0 * Rank));

		pResult->Set_NoData_Value(m_pInput->Get_NoData_Value());
	}

	//-----------------------------------------------------
	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#pragma omp parallel private(x)
		for(x=0; x<Get_NX(); x++)
		{
			double	Value;

			if( Get_Value(x, y, Rank, Value) )
			{
				pResult->Set_Value(x, y, Value);
			}
			else
			{
				pResult->Set_NoData(x, y);
			}
		}
	}

	//-----------------------------------------------------
	if( !Parameters("RESULT")->asGrid() || Parameters("RESULT")->asGrid() == m_pInput )
	{
		m_pInput->Assign(pResult);

		delete(pResult);

		DataObject_Update(m_pInput);
	}

	m_Kernel.Destroy();

	return( true );
}
//---------------------------------------------------------
bool CGridding_Spline_MBA_Grid::_Get_Difference(CSG_Grid &Phi)
{
	int				xPoint, yPoint, nErrors;
	double			x, y, z, zMax, zMean;
	TSG_Point_Z	p;
	CSG_String		s;

	//-----------------------------------------------------
	for(yPoint=0, p.y=m_Points.Get_YMin(), zMax=0.0, nErrors=0, zMean=0.0; yPoint<m_Points.Get_NY() && Set_Progress(yPoint, m_Points.Get_NY()); yPoint++, p.y+=m_Points.Get_Cellsize())
	{
		for(xPoint=0, p.x=m_Points.Get_XMin(); xPoint<m_Points.Get_NX(); xPoint++, p.x+=m_Points.Get_Cellsize())
		{
			if( !m_Points.is_NoData(xPoint, yPoint) )
			{
				x	= (p.x - Phi.Get_XMin()) / Phi.Get_Cellsize();
				y	= (p.y - Phi.Get_YMin()) / Phi.Get_Cellsize();
				z	= m_Points.asDouble(xPoint, yPoint) - BA_Get_Value(x, y, Phi);

				m_Points.Set_Value(xPoint, yPoint, z);

				if( (z = fabs(z)) > m_Epsilon )
				{
					nErrors	++;
					zMean	+= fabs(z);

					if( fabs(z) > zMax )
					{
						zMax	= fabs(z);
					}
				}
				else
				{
				//	m_Points.Set_Value(xPoint, yPoint, 0.0);
					m_Points.Set_NoData(xPoint, yPoint);
				}
			}
		}
	}

	if( nErrors > 0 )
	{
		zMean	/= nErrors;
	}

	//-----------------------------------------------------
	int	i	= 1 + (int)(0.5 + log(Phi.Get_NX() - 4.0) / log(2.0));

	s.Printf(SG_T("%s:%d, %s:%d, %s:%f, %s:%f"),
		_TL("level"), i,
		_TL("error"), nErrors,
		_TL("max")	, zMax,
		_TL("mean")	, zMean
	);

	Process_Set_Text(s);
	Message_Add     (s);

	return( zMax >= m_Epsilon && i < m_Level_Max && Process_Get_Okay(false) );
}
Example #18
0
//---------------------------------------------------------
bool CFilter_LoG::On_Execute(void)
{
	CSG_Grid	*pResult;

	//-----------------------------------------------------
	m_pInput	= Parameters("INPUT")	->asGrid();
	pResult		= Parameters("RESULT")	->asGrid();

	//-----------------------------------------------------
	if( Initialise() )
	{
		if( !pResult || pResult == m_pInput )
		{
			pResult	= SG_Create_Grid(m_pInput);
		}
		else
		{
			pResult->Set_Name(CSG_String::Format(SG_T("%s [%s]"), m_pInput->Get_Name(), _TL("Laplace Filter")));

			pResult->Set_NoData_Value(m_pInput->Get_NoData_Value());
		}

		//-------------------------------------------------
		for(int y=0; y<Get_NY() && Set_Progress(y); y++)
		{
			for(int x=0; x<Get_NX(); x++)
			{
				if( m_pInput->is_InGrid(x, y) )
				{
					pResult->Set_Value(x, y, Get_Value(x, y));
				}
				else
				{
					pResult->Set_NoData(x, y);
				}
			}
		}

		//-------------------------------------------------
		if( !Parameters("RESULT")->asGrid() || Parameters("RESULT")->asGrid() == m_pInput )
		{
			m_pInput->Assign(pResult);

			delete(pResult);

			pResult	= m_pInput;
		}

		DataObject_Set_Colors(pResult, 100, SG_COLORS_BLACK_WHITE);

		m_Kernel.Destroy();

		return( true );
	}

	//-----------------------------------------------------
	return( false );
}
Example #19
0
//---------------------------------------------------------
bool CSRTM30_Import::On_Execute(void)
{
	char	x_sTile[9][5]	= {	"W180", "W140", "W100", "W060", "W020", "E020", "E060", "E100", "E140"	},
			y_sTile[3][4]	= {	"S10", "N40", "N90"	};

	double	dSize			= 30.0 / (60.0 * 60.0);

	//-----------------------------------------------------
	int			xTile, yTile;
	double		xMin, xMax, yMin, yMax;
	TSG_Rect	rOut, rTile;
	CSG_String	sTile;
	CSG_Grid	*pOut;

	//-----------------------------------------------------
	xMin		= Parameters("XMIN")->asInt();
	xMax		= Parameters("XMAX")->asInt();
	yMin		= Parameters("YMIN")->asInt();
	yMax		= Parameters("YMAX")->asInt();

	rOut.xMin	= (180 + xMin) / 40.0 * X_WIDTH;
	rOut.xMax	= rOut.xMin + (int)((xMax - xMin) / dSize);
	rOut.yMin	= ( 60 + yMin) / 50.0 * Y_WIDTH;
	rOut.yMax	= rOut.yMin + (int)((yMax - yMin) / dSize);

	//-----------------------------------------------------
	pOut		= SG_Create_Grid(SG_DATATYPE_Short,
					(int)(rOut.xMax - rOut.xMin),
					(int)(rOut.yMax - rOut.yMin),
					dSize,
					xMin + 0.5 * dSize,
					yMin + 0.5 * dSize
				);

	pOut->Set_NoData_Value(-9999);
	pOut->Assign_NoData();
	pOut->Set_Name(SG_T("SRTM30"));
	pOut->Get_Projection().Create(SG_T("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]"));

	//-----------------------------------------------------
	for(yTile=0, rTile.yMin=0, rTile.yMax=Y_WIDTH; yTile<3; yTile++, rTile.yMin+=Y_WIDTH, rTile.yMax+=Y_WIDTH)
	{
		for(xTile=0, rTile.xMin=0, rTile.xMax=X_WIDTH; xTile<9; xTile++, rTile.xMin+=X_WIDTH, rTile.xMax+=X_WIDTH)
		{
			sTile.Printf(SG_T("Tile: %s%s"), x_sTile[xTile], y_sTile[yTile]);
			Process_Set_Text(sTile);

			sTile.Printf(SG_T("%s%s%s.dem"), Parameters("PATH")->asString(), x_sTile[xTile], y_sTile[yTile]);
			Tile_Load(sTile, rTile, pOut, rOut);
		}
	}

	//-----------------------------------------------------
	Parameters("GRID")->Set_Value(pOut);

	return( true );
}
Example #20
0
//---------------------------------------------------------
bool CGrid_Colors_Fit::On_Execute(void)
{
	int		iColor;
	long	aC, bC;
	double	aZ, bZ, dColor, zMin, zRange;
	CSG_Colors	Colors_Old, Colors_New;
	CSG_Grid	*pGrid;

	pGrid	= Parameters("GRID")->asGrid();

	Colors_New.Set_Count(Parameters("COUNT")->asInt());

	switch( Parameters("SCALE")->asInt() )
	{
	case 0:	default:
		zMin	= pGrid->Get_ZMin();
		zRange	= pGrid->Get_ZMax() - zMin;
		break;

	case 1:
		zMin	= Parameters("RANGE")->asRange()->Get_LoVal();
		zRange	= Parameters("RANGE")->asRange()->Get_HiVal() - zMin;
		break;
	}

	DataObject_Get_Colors(pGrid, Colors_Old);

	if( Colors_Old.Get_Count() > 1 && pGrid->Get_ZRange() > 0.0 && zRange != 0.0 )
	{
		dColor	= 100.0 / Colors_Old.Get_Count();

		aZ		= 0.0;
		aC		= Colors_Old.Get_Color(0);

		for(iColor=1; iColor<Colors_Old.Get_Count()-1; iColor++)
		{
			bZ	= aZ;
			bC	= aC;
			aZ	= (pGrid->Get_Percentile(iColor * dColor) - zMin) / zRange;
			aC	= Colors_Old.Get_Color(iColor);
			_Set_Colors(Colors_New, bZ, bC, aZ, aC);
		}

		bZ	= aZ;
		bC	= aC;
		aZ	= 1.0;
		aC	= Colors_Old.Get_Color(Colors_Old.Get_Count() - 1);
		_Set_Colors(Colors_New, bZ, bC, aZ, aC);

		DataObject_Set_Colors	(pGrid, Colors_New);
		DataObject_Update		(pGrid, zMin, zMin + zRange);

		return( true );
	}

	return( false );
}
Example #21
0
//---------------------------------------------------------
bool CExercise_09::On_Execute(void)
{
	int			x, y;
	CSG_Grid	*pDTM;

	//-----------------------------------------------------
	// Get parameter settings...

	pDTM		= Parameters("ELEVATION")->asGrid();
	m_pArea		= Parameters("AREA"     )->asGrid();


	//-----------------------------------------------------
	// Initialisations...

	m_pArea		->Assign(0.0);
	m_pArea		->Set_Unit(SG_T("m\xc2\xb2"));
	DataObject_Set_Colors(m_pArea, 100, SG_COLORS_WHITE_BLUE);


	//-----------------------------------------------------
	// Save flow directions to temporary grid...

	m_pDir		= new CSG_Grid(pDTM, SG_DATATYPE_Char);	// this object has to be deleted later...

	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			m_pDir->Set_Value(x, y, pDTM->Get_Gradient_NeighborDir(x, y) % 8);
		}
	}


	//-------------------------------------------------
	// Execute calculation...

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


	//-----------------------------------------------------
	// Finalisations...

	delete(m_pDir);


	//-----------------------------------------------------
	// Return 'true' if everything went okay...

	return( true );
}
Example #22
0
//---------------------------------------------------------
bool CImport_Clip_Resample::Load_Grid(CSG_Grid *pImport)
{
	CSG_Grid_System	System	= pImport->Get_System();

	//-----------------------------------------------------
	const CSG_Rect	*pClip	= Parameters("CLIP")->asShapes() ? &Parameters("CLIP")->asShapes()->Get_Extent() : NULL;

	if( pClip )
	{
		if( !pClip->Intersects(System.Get_Extent()) )
		{
			return( false );
		}

		TSG_Rect	Extent	= System.Get_Extent();

		if( pClip->Get_XMin() > System.Get_XMin() )	Extent.xMin	= System.Fit_xto_Grid_System(pClip->Get_XMin());
		if( pClip->Get_XMax() < System.Get_XMax() )	Extent.xMax	= System.Fit_xto_Grid_System(pClip->Get_XMax());
		if( pClip->Get_YMin() > System.Get_YMin() )	Extent.yMin	= System.Fit_yto_Grid_System(pClip->Get_YMin());
		if( pClip->Get_YMax() < System.Get_YMax() )	Extent.yMax	= System.Fit_yto_Grid_System(pClip->Get_YMax());

		System.Assign(System.Get_Cellsize(), Extent);
	}

	//-----------------------------------------------------
	if( Parameters("RESAMPLE")->asBool() )
	{
		double	Cellsize	= Parameters("CELLSIZE")->asDouble();

		if( Cellsize > 0.0 && Cellsize != System.Get_Cellsize() )
		{
			System.Assign(Cellsize, System.Get_Extent());
		}
	}

	//-----------------------------------------------------
	if( Parameters("NODATA")->asBool() )
	{
		pImport->Set_NoData_Value(Parameters("NODATA_VAL")->asDouble());
	}

	//-----------------------------------------------------
	CSG_Grid	*pGrid	= SG_Create_Grid(System, Parameters("KEEP_TYPE")->asBool() ? pImport->Get_Type() : SG_DATATYPE_Float);

	if( pGrid )
	{
		pGrid->Assign  (pImport);
		pGrid->Set_Name(pImport->Get_Name());

		m_pGrids->Add_Item(pGrid);

		return( true );
	}

	return( false );
}
//---------------------------------------------------------
bool CGrid_Classify_Supervised::On_Execute(void)
{
	//-----------------------------------------------------
	if( !Get_Features() )
	{
		Error_Set(_TL("invalid features"));

		return( false );
	}

	//-----------------------------------------------------
	CSG_Classifier_Supervised	Classifier;

	if( !Set_Classifier(Classifier) )
	{
		return( false );
	}

	//-----------------------------------------------------
	CSG_Grid	*pClasses	= Parameters("CLASSES")->asGrid();
	CSG_Grid	*pQuality	= Parameters("QUALITY")->asGrid();

	pClasses->Set_NoData_Value(0);
	pClasses->Assign(0.0);

	//-----------------------------------------------------
	Process_Set_Text(_TL("prediction"));

	int	Method	= Parameters("METHOD")->asInt();

	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<Get_NX(); x++)
		{
			int			Class;
			double		Quality;
			CSG_Vector	Features(m_pFeatures->Get_Count());

			if( Get_Features(x, y, Features) && Classifier.Get_Class(Features, Class, Quality, Method) )
			{
				SG_GRID_PTR_SAFE_SET_VALUE(pClasses, x, y, 1 + Class);
				SG_GRID_PTR_SAFE_SET_VALUE(pQuality, x, y, Quality  );
			}
			else
			{
				SG_GRID_PTR_SAFE_SET_NODATA(pClasses, x, y);
				SG_GRID_PTR_SAFE_SET_NODATA(pQuality, x, y);
			}
		}
	}

	//-----------------------------------------------------
	return( Set_Classification(Classifier) );
}
//---------------------------------------------------------
bool CGrids_Product::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Parameter_Grid_List	*pGrids	= Parameters("GRIDS" )->asGridList();

	if( pGrids->Get_Count() < 1 )
	{
		Error_Set(_TL("no grid in list"));

		return( false );
	}

	//-----------------------------------------------------
	CSG_Grid	*pResult	= Parameters("RESULT")->asGrid();

	bool	bNoData	= Parameters("NODATA")->asBool();

	//-----------------------------------------------------
	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<Get_NX(); x++)
		{
			int		n	= 0;
			double	d	= 0.0;

			for(int i=0; i<pGrids->Get_Count(); i++)
			{
				if( pGrids->asGrid(i)->is_InGrid(x, y) )
				{
					if( n++ < 1 )
					{
						d 	 = pGrids->asGrid(i)->asDouble(x, y);
					}
					else
					{
						d	*= pGrids->asGrid(i)->asDouble(x, y);
					}
				}
			}

			if( bNoData ? n > 0 : n == pGrids->Get_Count() )
			{
				pResult->Set_Value(x, y, d);
			}
			else
			{
				pResult->Set_NoData(x, y);
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
Example #25
0
//---------------------------------------------------------
bool	Copy_RGBGrid_SAGA_to_VIGRA	(CSG_Grid &Grid, BRGBImage &Image, bool bCreate)
{
	if( bCreate )
	{
		Image.resize(Grid.Get_NX(), Grid.Get_NY());
	}

	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(SG_GET_R(Grid.asInt(x, y)), SG_GET_G(Grid.asInt(x, y)), SG_GET_B(Grid.asInt(x, y)));

			Image(x, y)	= rgb;
		}
	}

	SG_UI_Process_Set_Progress(0.0, 1.0);

	return( true );
}
//---------------------------------------------------------
CSG_String CPresence_Prediction::Get_Feature(int x, int y, int i)
{
	CSG_Grid	*pFeature	= m_Features[i].pGrid;

	if( m_nNumClasses > 1 && m_Features[i].bNumeric )
	{
		return( CSG_String::Format("%d", (int)(m_nNumClasses * (pFeature->asDouble(x, y) - pFeature->Get_Min()) / pFeature->Get_Range())) );
	}

	return( SG_Get_String(pFeature->asDouble(x, y), -2) );
}
Example #27
0
void	g_Add_Grid		(CSG_Grid *pGrid)
{
	if( g_pInterpreter )
	{
		CSG_Grid	*p	= SG_Create_Grid(*pGrid);

		p->Set_Name(pGrid->Get_Name());

		g_pInterpreter->Get_Parameters()->Get_Parameter("OUTPUT")->asGridList()->Add_Item(p);
	}
}
Example #28
0
//---------------------------------------------------------
bool CFlow_Distance::On_Execute(void)
{
	bool		bSeeds;
	int			x, y, Method;
	CSG_Grid	*pSeed;

	//-------------------------------------------------
	m_pDTM		= Parameters("ELEVATION")	->asGrid();
	pSeed		= Parameters("SEED")		->asGrid();
	m_pLength	= Parameters("LENGTH")		->asGrid();

	m_Converge	= Parameters("CONVERGENCE")	->asDouble();
	bSeeds		= Parameters("SEEDS_ONLY")	->asBool();
	Method		= Parameters("METHOD")		->asInt();

	m_pWeight	= SG_Create_Grid(m_pLength, SG_DATATYPE_Float);
	m_pWeight	->Assign(0.0);
	m_pLength	->Assign(0.0);

	//-------------------------------------------------
	for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
	{
		m_pDTM->Get_Sorted(n, x, y, true, false);

		if( pSeed && !pSeed->is_NoData(x, y) )
		{
			m_pLength->Set_Value(x, y, 0.0);
			m_pWeight->Set_Value(x, y, 0.0);
		}
		else if( m_pWeight->asDouble(x, y) > 0.0 )
		{
			m_pLength->Set_Value(x, y, m_pLength->asDouble(x, y) / m_pWeight->asDouble(x, y));
		}
		else if( bSeeds )
		{
			m_pLength->Set_NoData(x, y);

			continue;
		}

		switch( Method )
		{
		case 0:	Set_Length_D8	(x, y);	break;
		case 1:	Set_Length_MFD	(x, y);	break;
		}
	}

	//-------------------------------------------------
	delete(m_pWeight);

	DataObject_Set_Colors(m_pLength, 100, SG_COLORS_WHITE_BLUE);

	return( true );
}
//---------------------------------------------------------
bool CGrid_Histogram_Surface::Get_Lines(bool bRows)
{
	int			i, j, n_i, n_j;
	CSG_Table	Values;
	CSG_Grid	*pHist;

	//-----------------------------------------------------
	Parameters("HIST")->Set_Value(pHist	= SG_Create_Grid(m_pGrid));

	pHist->Set_NoData_Value_Range(
		m_pGrid->Get_NoData_Value(),
		m_pGrid->Get_NoData_hiValue()
	);

	n_i	= bRows ? Get_NX() : Get_NY();
	n_j	= bRows ? Get_NY() : Get_NX();

	Values.Add_Field(SG_T("Z"), SG_DATATYPE_Double);

	for(i=0; i<n_i; i++)
	{
		Values.Add_Record();
	}

	//-----------------------------------------------------
	for(j=0; j<n_j && Set_Progress(j, n_j); j++)
	{
		for(i=0; i<n_i; i++)
		{
			Values.Get_Record(i)->Set_Value(0, bRows ? m_pGrid->asDouble(i, j) : m_pGrid->asDouble(j, i));
		}

		Values.Set_Index(0, TABLE_INDEX_Ascending);

		for(i=0; i<n_i; i++)
		{
			int		k	= i % 2 ? i / 2 : n_i - 1 - i / 2;

			if( bRows )
			{
				pHist->Set_Value(k, j, Values.Get_Record_byIndex(i)->asDouble(0));
			}
			else
			{
				pHist->Set_Value(j, k, Values.Get_Record_byIndex(i)->asDouble(0));
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CTopographic_Openness::Get_Angles_Multi_Scale(int x, int y, CSG_Vector &Max, CSG_Vector &Min)
{
	if( m_pDEM->is_NoData(x, y) )
	{
		return( false );
	}

	double		z, d;
	TSG_Point	p, q;

	z	= m_pDEM->asDouble(x, y);
	p	= Get_System()->Get_Grid_to_World(x, y);

	//-----------------------------------------------------
	for(int iGrid=-1; iGrid<m_nLevels; iGrid++)
	{
		bool		bOkay	= false;
		CSG_Grid	*pGrid	= m_Pyramid.Get_Grid(iGrid);

		for(int i=0; i<m_Direction.Get_Count(); i++)
		{
			q.x	= p.x + pGrid->Get_Cellsize() * m_Direction[i].x;
			q.y	= p.y + pGrid->Get_Cellsize() * m_Direction[i].y;

			if( pGrid->Get_Value(q, d) )
			{
				d	= (d - z) / pGrid->Get_Cellsize();

				if( bOkay == false )
				{
					bOkay	= true;
					Max[i]	= Min[i]	= d;
				}
				else if( Max[i] < d )
				{
					Max[i]	= d;
				}
				else if( Min[i] > d )
				{
					Min[i]	= d;
				}
			}
		}

		if(0|| bOkay == false )
		{
			return( false );
		}
	}

	return( true );
}