//---------------------------------------------------------
bool CTopographic_Correction::Get_Model(void)
{
	//-----------------------------------------------------
	m_pOriginal		= Parameters("ORIGINAL")	->asGrid();
	m_pCorrected	= Parameters("CORRECTED")	->asGrid();

	m_pCorrected	->Set_Name(CSG_String::Format(SG_T("%s [%s]"), m_pOriginal->Get_Name(), _TL("Topographic Correction")));

	m_Method		= Parameters("METHOD")		->asInt();

	m_Minnaert		= Parameters("MINNAERT")	->asDouble();

	switch( Parameters("MAXVALUE")->asInt() )
	{
	default:	m_maxValue	=   255;	break;
	case  1:	m_maxValue	= 65535;	break;
	}

	switch( m_Method )
	{
	//-----------------------------------------------------
	case 5:	// C Correction
		{
			Process_Set_Text(_TL("Regression Analysis"));

			CSG_Regression	R;

			sLong n		= Parameters("MAXCELLS")->asInt();
			int	nStep	= Get_NCells() < n ? 1 : (int)(Get_NCells() / n);

			for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n+=nStep)
			{
				R.Add_Values(m_pOriginal->asDouble(n), m_Illumination.asDouble(n));
			}

			if( !R.Calculate() || !R.Get_Constant() )
			{
				return( false );
			}

			m_C	= R.Get_Coefficient() / R.Get_Constant();

			Message_Add(R.asString());
		}
		break;

	//-----------------------------------------------------
	case 6:	// Normalization (after Civco, modified by Law & Nichol)
		{
			m_C	= 1.0;
		}
		break;
	}

	//-----------------------------------------------------
	return( true );
}
Esempio n. 2
0
//---------------------------------------------------------
bool CSG_Grid::Assign(double Value)
{
	if( is_Valid() )
	{
		if( Value == 0.0 && m_Memory_Type == GRID_MEMORY_Normal )
		{
			for(int n=0, m=_Get_nLineBytes(); n<Get_NY(); n++)
			{
				memset(m_Values[n], 0, m);
			}
		}
		else
		{
			for(int n=0; n<Get_NCells(); n++)
			{
				Set_Value(n, Value);
			}
		}

		//-------------------------------------------------
		Get_History().Destroy();
		Get_History().Add_Child(SG_T("GRID_OPERATION"), Value)->Add_Property(SG_T("NAME"), LNG("Assign"));

		//-------------------------------------------------
		m_zStats.Invalidate();

		Set_Update_Flag(false);

		return( true );
	}

	return( false );
}
Esempio n. 3
0
//---------------------------------------------------------
bool CGrid_Gaps_Spline_Fill::On_Execute(void)
{
	//-----------------------------------------------------
	m_pGrid			= Parameters("CLOSED")		->asGrid();
	m_pMask			= Parameters("MASK")		->asGrid();
	m_nGapCells_Max	= Parameters("MAXGAPCELLS")	->asInt();
	m_nPoints_Max	= Parameters("MAXPOINTS")	->asInt();
	m_nPoints_Local	= Parameters("LOCALPOINTS")	->asInt();
	m_bExtended		= Parameters("EXTENDED")	->asBool();
	m_Neighbours	= Parameters("NEIGHBOURS")	->asInt() == 0 ? 2 : 1;
	m_Radius		= Parameters("RADIUS")		->asDouble();
	m_Relaxation	= Parameters("RELAXATION")	->asDouble();

	if( m_pGrid == NULL )
	{
		m_pGrid	= Parameters("GRID")->asGrid();

		Parameters("CLOSED")->Set_Value(m_pGrid);
	}
	else if( m_pGrid != Parameters("GRID")->asGrid() )
	{
		m_pGrid->Assign(Parameters("GRID")->asGrid());

		m_pGrid->Set_Name(CSG_String::Format(SG_T("%s [%s]"), Parameters("GRID")->asGrid()->Get_Name(), _TL("no gaps")));
	}

	if( m_nGapCells_Max == 0 )
	{
		m_nGapCells_Max	= Get_NCells();
	}

	if( m_nPoints_Local > m_nPoints_Max )
	{
		m_nPoints_Local	= m_nPoints_Max;
	}

	//-----------------------------------------------------
	m_Gaps.Create(*Get_System(), SG_DATATYPE_Int);
	m_Gaps.Assign(0.0);
	m_nGaps	= 0;

	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(int x=0; x<Get_NX(); x++)
		{
			if( is_Gap(x, y) && m_Gaps.asInt(x, y) == 0 )
			{
				Close_Gap(x, y);
			}
		}
	}

	//-----------------------------------------------------
	m_Stack		.Clear();
	m_GapCells	.Clear();
	m_Gaps		.Destroy();
	m_Spline	.Destroy();

	return( true );
}
Esempio n. 4
0
//---------------------------------------------------------
// Runs on module execution.
//---------------------------------------------------------
bool CExercise_04::On_Execute(void)
{
	CSG_Grid *grid, *result, *accumulation;

	grid = Parameters("GRID")->asGrid();
	result = Parameters("RESULT")->asGrid();
	accumulation = Parameters("ACC")->asGrid();

	accumulation->Assign(1.0);

	for (int i = 0; i < Get_NCells() && Set_Progress_NCells(i); i++)
	{
		int x, y;
		if (grid->Get_Sorted(i, x, y))
		{
			int direction = Get_Flow_Direction(grid, x, y);

			if (direction > -1)
			{
				result->Set_Value(x, y, direction);

				int destX = accumulation->Get_System().Get_xTo(direction, x);
				int destY = accumulation->Get_System().Get_yTo(direction, y);

				accumulation->Set_Value(destX, destY, accumulation->asDouble(destX, destY) + accumulation->asDouble(x, y));
			}
		}
	}

	result->Set_Name("Flow Direction Map");

	return true;
}
//---------------------------------------------------------
void CHillslope_Evolution_FTCS::Set_Difference(void)
{
	CSG_Grid	*pDiff	= Parameters("DIFF")->asGrid();

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

		#pragma omp parallel for
		for(int i=0; i<Get_NCells(); i++)
		{
			if( pDEM->is_NoData(i) )
			{
				pDiff->Set_NoData(i);
			}
			else
			{
				pDiff->Set_Value(i, m_pDEM->asDouble(i) - pDEM->asDouble(i));
			}
		}

		if( Parameters("UPDATE")->asBool() )
		{
			DataObject_Update(pDiff, SG_UI_DATAOBJECT_SHOW);
		}
	}
}
Esempio n. 6
0
//---------------------------------------------------------
bool CViGrA_Morphology::On_Execute(void)
{
	bool		bRescale;
	int			Type, Radius;
	double		Rank;
	CSG_Grid	*pInput, *pOutput, Rescaled;

	pInput		= Parameters("INPUT")	->asGrid();
	pOutput		= Parameters("OUTPUT")	->asGrid();
	Type		= Parameters("TYPE")	->asInt();
	Radius		= Parameters("RADIUS")	->asInt();
	Rank		= Parameters("RANK")	->asDouble();
	bRescale	= Parameters("RESCALE")	->asBool();

	//-----------------------------------------------------
	if( bRescale )
	{
		Rescaled.Create(*Get_System(), SG_DATATYPE_Byte);

		for(sLong i=0; i<Get_NCells() && Set_Progress_NCells(i); i++)
		{
			Rescaled.Set_Value(i, 0.5 + (pInput->asDouble(i) - pInput->Get_ZMin()) * 255.0 / pInput->Get_ZRange());
		}

		pInput	= &Rescaled;
	}

	//-----------------------------------------------------
	vigra::BImage	Input, Output(Get_NX(), Get_NY());

	Copy_Grid_SAGA_to_VIGRA(*pInput, Input, true);

	switch( Type )
	{
	case 0:	// Dilation
		discDilation		(srcImageRange(Input), destImage(Output), Radius);
		break;

	case 1:	// Erosion
		discErosion			(srcImageRange(Input), destImage(Output), Radius);
		break;

	case 2:	// Median
		discMedian			(srcImageRange(Input), destImage(Output), Radius);
		break;

	case 3:	// User defined rank
		discRankOrderFilter	(srcImageRange(Input), destImage(Output), Radius, Rank);
		break;
	}

	//-----------------------------------------------------
	Copy_Grid_VIGRA_to_SAGA(*pOutput, Output, false);

	pOutput->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pInput->Get_Name(), Get_Name().c_str()));

	return( true );
}
Esempio n. 7
0
//---------------------------------------------------------
bool CSADO_SolarRadiation::Get_Shade(double Decline, double Azimuth)
{
	//-----------------------------------------------------
	m_Shade.Assign(0.0);

	if( !m_bBending )
	{
		int		i, x, y;
		double	dx, dy, dz;

		Get_Shade_Params(Decline, Azimuth, dx, dy, dz);

		for(i=0; i<Get_NCells() && Set_Progress_NCells(i); i++)
		{
			if( m_pDEM->Get_Sorted(i, x, y) && !Get_Shade_Complete(x, y) )
			{
				Set_Shade(x, y, dx, dy, dz);
			}
		}
	}

	//-----------------------------------------------------
	else
	{
		int		i, x, y, iLock;

		for(i=0, iLock=1; i<Get_NCells() && Set_Progress_NCells(i); i++, iLock++)
		{
			if( m_pDEM->Get_Sorted(i, x, y) && !Get_Shade_Complete(x, y) )
			{
				if( iLock >= 255 )
					iLock	= 1;

				if( iLock == 1 )
					Lock_Create();

				Set_Shade_Bended(x, y, iLock);
			}
		}
	}

	return( true );
}
Esempio n. 8
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 CMelton_Ruggedness::On_Execute(void)
{
	CSG_Grid	*pDEM, *pArea, *pMRN, *pZMax;

	//-----------------------------------------------------
	pDEM		= Parameters("DEM" )->asGrid();
	pArea		= Parameters("AREA")->asGrid();
	pZMax		= Parameters("ZMAX")->asGrid();
	pMRN		= Parameters("MRN" )->asGrid();

	if( !pDEM->Set_Index() )
	{
		Error_Set(_TL("index creation failed"));

		return( false );
	}

	pArea->Set_NoData_Value(0.0);
	pArea->Assign_NoData();
	pZMax->Assign_NoData();
	pMRN ->Assign_NoData();

	//-------------------------------------------------
	for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
	{
		int		x, y, i, ix, iy;

		if( pDEM->Get_Sorted(n, x, y, true, true) )
		{
			pArea->Add_Value(x, y, Get_Cellsize());

			if( pZMax->is_NoData(x, y) )
			{
				pZMax->Set_Value(x, y, pDEM->asDouble(x, y));
			}

			if( (i = pDEM->Get_Gradient_NeighborDir(x, y, true)) >= 0 && Get_System().Get_Neighbor_Pos(i, x, y, ix, iy) )
			{
				pArea->Add_Value(ix, iy, pArea->asDouble(x, y));

				if( pZMax->is_NoData(ix, iy) || pZMax->asDouble(ix, iy) < pZMax->asDouble(x, y) )
				{
					pZMax->Set_Value(ix, iy, pZMax->asDouble(x, y));
				}
			}

			pMRN->Set_Value(x, y, (pZMax->asDouble(x, y) - pDEM->asDouble(x, y)) / sqrt(pArea->asDouble(x, y)));
		}
	}

	//-----------------------------------------------------
	return( true );
}
Esempio n. 10
0
//---------------------------------------------------------
bool CFlow_by_Slope::On_Execute(void)
{
	m_Slope_Min	= Parameters("SLOPE_MIN")->asDouble() * M_DEG_TO_RAD;
	m_Slope_Max = Parameters("SLOPE_MAX")->asDouble() * M_DEG_TO_RAD;

	if( m_Slope_Max <= 0.0 )
	{
		Error_Set(_TL("slope threshold must not be zero!"));

		return( false );
	}

	if( Parameters("B_FLOW")->asBool() )
	{
		m_Flow_Min	= Parameters("T_FLOW")->asRange()->Get_LoVal() * Get_Cellarea();
		m_Flow_Max	= Parameters("T_FLOW")->asRange()->Get_HiVal() * Get_Cellarea();
	}
	else
	{
		m_Flow_Min	= m_Flow_Max	= 0.0;
	}

	//-----------------------------------------------------
	m_pDEM		= Parameters("DEM"   )->asGrid();
	m_pFlow		= Parameters("FLOW"  )->asGrid();

	m_pFlow->Assign(Get_Cellarea());

	if( Parameters("WEIGHT")->asGrid() )
	{
		m_pFlow->Multiply(*Parameters("WEIGHT")->asGrid());
	}

	DataObject_Set_Colors(m_pFlow, 11, SG_COLORS_WHITE_BLUE, false);

	//-----------------------------------------------------
	for(sLong i=0; i<Get_NCells() && Set_Progress_NCells(i); i++)
	{
		int	x, y;

		if( !m_pDEM->Get_Sorted(i, x, y, true) || m_pDEM->is_NoData(x, y) )
		{
			m_pFlow->Set_NoData(x, y);
		}
		else
		{
			Set_Area(x, y);
		}
	}

	//-----------------------------------------------------
	return( true );
}
Esempio n. 11
0
//---------------------------------------------------------
bool CSlopeLength::On_Execute(void)
{
	int		x, y;

	//-----------------------------------------------------
	m_pDEM		= Parameters("DEM"   )->asGrid();
	m_pLength	= Parameters("LENGTH")->asGrid();

	if( !m_pDEM->Set_Index() )
	{
		Error_Set(_TL("index creation failed"));

		return( false );
	}

	//-----------------------------------------------------
	m_Slope.Create(*Get_System());

	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#pragma omp parallel for private(x)
		for(x=0; x<Get_NX(); x++)
		{
			double	Slope, Aspect;

			if( m_pDEM->Get_Gradient(x, y, Slope, Aspect) )
			{
				m_Slope   .Set_Value(x, y, Slope);
				m_pLength->Set_Value(x, y, 0.0);
			}
			else
			{
				m_Slope   .Set_NoData(x, y);
				m_pLength->Set_NoData(x, y);
			}
		}
	}

	//-----------------------------------------------------
	for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
	{
		if( m_pDEM->Get_Sorted(n, x, y) )
		{
			Get_Length(x, y);
		}
	}

	//-----------------------------------------------------
	m_Slope.Destroy();

	return( true );
}
Esempio n. 12
0
//---------------------------------------------------------
bool CDiffuse_Pollution_Risk::Set_Flow(void)
{
	Process_Set_Text(_TL("initialization"));

	CSG_Grid	*pWeight	= Parameters("WEIGHT")->asGrid  ();
	double		  Weight	= Parameters("WEIGHT")->asDouble();

	CSG_Grid	*pRain		= Parameters("RAIN"  )->asGrid  ();
	double		  Rain		= Parameters("RAIN"  )->asDouble();

	m_FlowDir.Create(*Get_System(), SG_DATATYPE_Char);
	m_RainAcc.Create(*Get_System());
	m_TWI    .Create(*Get_System());

	for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
	{
		int		x, y;

		if( !m_pDEM->Get_Sorted(n, x, y, true) || (pRain && pRain->is_NoData(x, y)) || !Set_Flow(x, y, pRain ? pRain->asDouble(x, y) : Rain) )
		{
			m_FlowDir     .Set_NoData(x, y);
			m_RainAcc     .Set_NoData(x, y);
			m_TWI         .Set_NoData(x, y);
			m_pRisk_Point->Set_NoData(x, y);
		}
		else
		{
			double	s, a;

			m_pDEM->Get_Gradient(x, y, s, a);
			
			s	= tan(s);											// tangens of slope
			a	= (fabs(sin(a)) + fabs(cos(a))) * Get_Cellsize();	// flow width

			double	SCA	= m_RainAcc.asDouble(x, y) / a;				// rain * specific catchment area

			m_TWI.Set_Value(x, y, log(SCA / (s < M_ALMOST_ZERO ? M_ALMOST_ZERO : s)));

			if( pWeight && pWeight->is_NoData(x, y) )
			{
				m_pRisk_Point->Set_NoData(x, y);
			}
			else
			{
				m_pRisk_Point->Set_Value(x, y, SCA * s * (pWeight ? pWeight->asDouble(x, y) : Weight));	// Point Scale Risk Calculation according to Milledge et al. 2012
			}
		}
	}

	return( true );
}
Esempio n. 13
0
//---------------------------------------------------------
void CExercise_14::Find_Channels(void)
{
	int		n, x, y;

	for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
	{
		if( m_pDTM->Get_Sorted(n, x, y, true) && m_pChnl->asInt(x, y) == SPRING )
		{
			m_pChnl	->Set_Value(x, y, SPRING);

			Find_Channels(x, y);
		}
	}
}
Esempio n. 14
0
//---------------------------------------------------------
// Calculation according to Milledge et al. 2012
//---------------------------------------------------------
bool CDiffuse_Pollution_Risk::Get_Risk_Diffuse(void)
{
	Process_Set_Text(_TL("Difuse Pollution Risk"));

	m_pRisk_Diffuse->Assign(0.0);

	//-----------------------------------------------------
	for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
	{
		int		x, y;

		if( !m_pDEM->Get_Sorted(n, x, y, true) || m_pDelivery->is_NoData(x, y) || m_pRisk_Point->is_NoData(x, y) || m_RainAcc.asDouble(x, y) <= 0.0 )
		{
			m_pRisk_Diffuse->Set_NoData(x, y);
		}
		else
		{
			double d[8], Risk;

			m_pRisk_Point->Mul_Value(x, y, m_pDelivery->asDouble(x, y));				// locational risk = generation risk * connection risk
			Risk	= m_pRisk_Diffuse->asDouble(x, y) + m_pRisk_Point->asDouble(x, y);	// risk load = sum of upslope locational risk
			m_pRisk_Diffuse->Set_Value(x, y, Risk / m_RainAcc.asDouble(x, y));			// risk concentration = risk load / sum of upslope rain

			if( m_bSingle )
			{
				int		i	= !m_FlowDir.is_NoData(x, y) ? m_FlowDir.asInt(x, y) : -1;

				if( i > 0 && m_pDEM->is_InGrid(Get_xTo(i, x), Get_yTo(i, y)) )
				{
					m_pRisk_Diffuse->Add_Value(Get_xTo(i, x), Get_yTo(i, y), Risk);
				}
			}
			else if( Get_Flow_Proportions(x, y, d) )
			{
				for(int i=0; i<8; i++)
				{
					if( d[i] > 0.0 )
					{
						m_pRisk_Diffuse->Add_Value(Get_xTo(i, x), Get_yTo(i, y), Risk * d[i]);
					}
				}
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CTIN_From_Grid_Specific_Points::Get_FlowDirection2(CSG_Grid *pResult, CSG_Grid *pGrid, int Threshold)
{
	CSG_Grid	Grid(*pGrid), Result(*pResult);

	Get_FlowDirection(pResult, &Grid, -1, Threshold);
	Grid.Invert();
	Get_FlowDirection(&Result, &Grid, -1, Threshold);

	for(sLong n=0; n<Get_NCells(); n++)
	{
		if( Result.asInt(n) > 0 )
		{
			pResult->Set_Value(n, 1);
		}
	}

	return( true );
}
Esempio n. 16
0
//---------------------------------------------------------
bool CWatersheds::On_Execute(void)
{
	int			x, y, nCells, nCells_Min, nBasins;
	sLong		n;
	CSG_Grid	*pDTM, *pSeed, *pRoute;

	//-----------------------------------------------------
	pDTM		= Parameters("ELEVATION")->asGrid();
	pSeed		= Parameters("CHANNELS" )->asGrid();
	pRoute		= Parameters("SINKROUTE")->asGrid();
	nCells_Min	= Parameters("MINSIZE"  )->asInt();
	m_pBasins	= Parameters("BASINS"   )->asGrid();

	m_pBasins->Set_NoData_Value(NO_BASIN);
	m_pBasins->Assign_NoData();

	if( !pDTM->Set_Index() )
	{
		Error_Set(_TL("index creation failed"));

		return( false );
	}

	m_Direction.Create(m_pBasins, SG_DATATYPE_Char);

	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			if( pDTM->is_NoData(x, y) )
			{
				m_Direction.Set_NoData(x, y);
			}
			else
			{
				if( !pRoute || (n = pRoute->asChar(x, y)) <= 0 )
				{
					n	= pDTM->Get_Gradient_NeighborDir(x, y);
				}

				m_Direction.Set_Value(x, y, (int)(n < 0 ? -1 : (n + 4) % 8));
			}
		}
	}

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

		if( !pSeed->is_NoData(x, y) && pSeed->asInt(x, y) < 0 )
		{
			m_nBasins++;

			if( (nCells = Get_Basin(x, y)) < nCells_Min )
			{
				nBasins		= m_nBasins - 1;
				m_nBasins	= NO_BASIN;
				Get_Basin(x, y);
				m_nBasins	= nBasins;
			}
		}
	}

	//-----------------------------------------------------
	m_Direction.Destroy();

	return( true );
}
Esempio n. 17
0
//---------------------------------------------------------
bool CChange_Detection::On_Execute(void)
{
	bool		bNoChange;
	int			iInitial, iFinal;
	CSG_Matrix	Identity;
	CSG_Table	Initial, Final, *pChanges;
	CSG_Grid	*pInitial, *pFinal, *pChange;

	//-----------------------------------------------------
	pInitial	= Parameters("INITIAL")	->asGrid();
	pFinal		= Parameters("FINAL")	->asGrid();
	pChange		= Parameters("CHANGE")	->asGrid();
	pChanges	= Parameters("CHANGES")	->asTable();
	bNoChange	= Parameters("NOCHANGE")->asBool();

	if( !Get_Classes(Initial, pInitial, true) )
	{
		Error_Set(_TL("no class definitions for initial state"));

		return( false );
	}

	if( !Get_Classes(Final, pFinal, false) )
	{
		Error_Set(_TL("no class definitions for final state"));

		return( false );
	}

	if( !Get_Changes(Initial, Final, pChanges, Identity) )
	{
		return( false );
	}

	//-----------------------------------------------------
	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(int x=0; x<Get_NX(); x++)
		{
			iInitial	= Get_Class(Initial, pInitial->asDouble(x, y));
			iFinal		= Get_Class(Final  , pFinal  ->asDouble(x, y));

			if( bNoChange || !Identity[iInitial][iFinal] )
			{
				pChanges->Get_Record(iInitial)->Add_Value(1 + iFinal, 1);

				pChange->Set_Value(x, y, (pChanges->Get_Field_Count() - 1) * iInitial + iFinal);
			}
			else
			{
				pChange->Set_Value(x, y, -1);
			}
		}
	}

	//-----------------------------------------------------
	CSG_Parameters	P;

	if( DataObject_Get_Parameters(pChange, P) && P("COLORS_TYPE") && P("LUT") )
	{
		CSG_Table	*pLUT	= P("LUT")->asTable();

		CSG_Colors	cRandom(pChanges->Get_Count());

		cRandom.Random();

		pLUT->Del_Records();

		for(iInitial=0; iInitial<pChanges->Get_Count(); iInitial++)
		{
			CSG_Colors	cRamp(pChanges->Get_Field_Count() - 1);

			cRamp.Set_Ramp(cRandom[iInitial], cRandom[iInitial]);
			cRamp.Set_Ramp_Brighness(225, 50);

			for(iFinal=0; iFinal<pChanges->Get_Field_Count()-1; iFinal++)
			{
				if( pChanges->Get_Record(iInitial)->asInt(1 + iFinal) > 0 )
				{
					CSG_Table_Record	*pClass	= pLUT->Add_Record();

					pClass->Set_Value(0, cRamp.Get_Color(iFinal));
					pClass->Set_Value(1, CSG_String::Format(SG_T("%s >> %s"), pChanges->Get_Record(iInitial)->asString(0), pChanges->Get_Field_Name(1 + iFinal)));
					pClass->Set_Value(3, (pChanges->Get_Field_Count() - 1) * iInitial + iFinal);
					pClass->Set_Value(4, (pChanges->Get_Field_Count() - 1) * iInitial + iFinal);
				}
			}
		}

		P("COLORS_TYPE")->Set_Value(1);	// Color Classification Type: Lookup Table

		DataObject_Set_Parameters(pChange, P);
	}

	//-----------------------------------------------------
	double	Factor;

	switch( Parameters("OUTPUT")->asInt() )
	{
	default:	Factor	= 1.0;						break;	// cells
	case 1:		Factor	= 100.0 / Get_NCells();		break;	// percent
	case 2:		Factor	= M_SQR(Get_Cellsize());	break;	// area
	}

	if( Factor != 1.0 )
	{
		for(iInitial=0; iInitial<pChanges->Get_Count(); iInitial++)
		{
			for(iFinal=0; iFinal<pChanges->Get_Field_Count()-1; iFinal++)
			{
				pChanges->Get_Record(iInitial)->Mul_Value(1 + iFinal, Factor);
			}
		}
	}

	//-----------------------------------------------------
	pChanges	->Set_Name(CSG_String::Format(SG_T("%s [%s >> %s]"), _TL("Changes"), pInitial->Get_Name(), pFinal->Get_Name()));

	pChange		->Set_Name(CSG_String::Format(SG_T("%s [%s >> %s]"), _TL("Changes"), pInitial->Get_Name(), pFinal->Get_Name()));
	pChange		->Set_NoData_Value(-1);

	return( true );
}
Esempio n. 18
0
//---------------------------------------------------------
void CFlow::Finalize(void)
{
	long	n;
	double	z, CellSize, Catch, Contour, dContour, G, H;

	//-----------------------------------------------------
	CellSize	= Get_Cellsize() * Get_Cellsize();
	Contour		= 1.0;
	dContour	= 0.02 * M_PI * sqrt(CellSize / M_PI);

	//-----------------------------------------------------
	for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
	{
		if( pDTM->is_NoData(n) )
		{
			if( pCatch )
			{
				pCatch			->Set_NoData(n);
			}

			if( pCatch_Height )
			{
				pCatch_Height	->Set_NoData(n);
			}

			if( pCatch_Slope )
			{
				pCatch_Slope	->Set_NoData(n);
			}

			if( pCatch_Aspect )
			{
				pCatch_Aspect	->Set_NoData(n);
			}

			if( pFlowPath )
			{
				pFlowPath		->Set_NoData(n);
			}
		}
		else
		{
			z		= pDTM->asDouble(n);

			//---------------------------------------------
			Catch	= 1.0 / pCatch->asDouble(n);

			if( pCatch_Height )
			{
				pCatch_Height	->Set_Value(n, Catch * pCatch_Height->asDouble(n) - z);
			}

			if( pCatch_Slope )
			{
				pCatch_Slope	->Mul_Value(n, Catch);
			}

			if( pFlowPath )
			{
				pFlowPath		->Mul_Value(n, Catch);
			}

			//---------------------------------------------
			Catch	= CellSize / Catch;

			if( pCatch )
			{
				pCatch			->Set_Value(n, Catch);
			}

			//---------------------------------------------
			if( pCatch_Aspect && pCatch_AspectY )
			{
				G	= pCatch_Aspect	->asDouble(n);
				H	= pCatch_AspectY->asDouble(n);

				pCatch_Aspect	->Set_Value(n, G ? fmod(M_PI_270 + atan2(H, G), M_PI_360) : (H > 0 ? M_PI_270 : (H < 0 ? M_PI_090 : -1)));
			}
		}
	}

	//-----------------------------------------------------
	if( pCatch_AspectY )
	{
		delete(pCatch_AspectY);
		pCatch_AspectY	= NULL;
	}
}
Esempio n. 19
0
//---------------------------------------------------------
bool CFlow::On_Execute(void)
{
	bool	bResult	= false;
	long	n;
	double	d;

	//-------------------------------------------------
	pDTM			= Parameters("ELEVATION")	->asGrid();
	pRoute			= Parameters("SINKROUTE")	->asGrid();
	pWeight			= Parameters("WEIGHT")		->asGrid();

	pCatch			= Parameters("CAREA")		->asGrid();
	DataObject_Set_Colors(pCatch, 100, SG_COLORS_WHITE_BLUE);

	pCatch_Height	= NULL;
	pCatch_Slope	= NULL;
	pCatch_Aspect	= NULL;
	pFlowPath		= NULL;

	Step			= Parameters("STEP")		->asInt();

	//-------------------------------------------------
	On_Initialize();

	//-------------------------------------------------
	if( pCatch )
	{
		pCatch			->Assign(0.0);
	}

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

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

	if( pCatch_Aspect )
	{
		pCatch_Aspect	->Assign(0.0);
		pCatch_AspectY	 = SG_Create_Grid(pCatch_Aspect);
	}

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

	//-------------------------------------------------
	if( bPoint )
	{
		bPoint	= false;

		if( is_InGrid(xPoint, yPoint) )
		{
			Calculate(xPoint, yPoint);

			On_Finalize();

			for(n=0; n<Get_NCells(); n++)
			{
				d	= pCatch->asDouble(n);
				pCatch->Set_Value(n, 100.0 * d);
			}

			bResult		= true;
		}
	}
	else
	{
		pCatch_Height	= Parameters("CHEIGHT")	->asGrid();
		pCatch_Slope	= Parameters("CSLOPE")	->asGrid();

		Calculate();

		On_Finalize();
		Finalize();

		bResult			= true;
	}

	//-------------------------------------------------
	return( bResult );
}
Esempio n. 20
0
//---------------------------------------------------------
bool CWatershed_Segmentation::Get_Segments(void)
{
    Process_Set_Text(_TL("Segments"));

    double	Threshold	= Parameters("THRESHOLD")->asDouble();
    int		Join		= Threshold > 0.0 ? Parameters("JOIN")->asInt() : 0;

    //-----------------------------------------------------
    for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
    {
        int		x, y, i, ID, iID;

        if( m_pGrid->Get_Sorted(n, x, y, m_bDown) && (i = m_Dir.asInt(x, y)) >= 0 )
        {
            m_pSegments->Set_Value(x, y, ID = m_pSegments->asInt(Get_xTo(i, x), Get_yTo(i, y)));

            if( Join != 0 && ID >= 0 )
            {
                double	z	= m_pGrid->asDouble(x, y);

                for(int i=0; i<8; i++)
                {
                    int	ix	= Get_xTo(i, x);
                    int	iy	= Get_yTo(i, y);

                    if( m_pSegments->is_InGrid(ix, iy) && (iID = m_pSegments->asInt(ix, iy)) >= 0 )	// Border < 0, Segment >= 0
                    {
                        if( ID != iID )
                        {
                            bool	bJoin;

                            if( Join == 1 )
                            {
                                bJoin	=  (Threshold >= fabs(m_pSeeds->Get_Shape(iID)->asDouble(SEED_Z) - z))
                                           || (Threshold >= fabs(m_pSeeds->Get_Shape( ID)->asDouble(SEED_Z) - z));
                            }
                            else
                            {
                                bJoin	=  Threshold >= fabs(m_pSeeds->Get_Shape(iID)->asDouble(SEED_Z) - m_pSeeds->Get_Shape(ID)->asDouble(SEED_Z));
                            }

                            if( bJoin )
                            {
                                if(	(m_bDown == true  && m_pSeeds->Get_Shape(iID)->asDouble(SEED_Z) < m_pSeeds->Get_Shape(ID)->asDouble(SEED_Z))
                                        ||	(m_bDown == false && m_pSeeds->Get_Shape(iID)->asDouble(SEED_Z) > m_pSeeds->Get_Shape(ID)->asDouble(SEED_Z)) )
                                {
                                    Segment_Change(iID, ID);
                                }
                                else
                                {
                                    Segment_Change(ID, iID);

                                    ID	= iID;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    return( true );
}
Esempio n. 21
0
//---------------------------------------------------------
bool CGrid_Cluster_Analysis::_On_Execute(void)
{
	int						i, j, *nMembers, nCluster, nElements;
	double					*Variances, **Centroids, SP;
	CSG_Grid				**Grids, *pCluster;
	CSG_Parameter_Grid_List	*pGrids;

	//-----------------------------------------------------
	pGrids		= Parameters("GRIDS")	->asGridList();
	pCluster	= Parameters("CLUSTER")	->asGrid();
	nCluster	= Parameters("NCLUSTER")->asInt();

	if( pGrids->Get_Count() < 1 )
	{
		return( false );
	}

	//-----------------------------------------------------
	Grids		= (CSG_Grid **)SG_Malloc(pGrids->Get_Count() * sizeof(CSG_Grid *));

	if( Parameters("NORMALISE")->asBool() )
	{
		for(i=0; i<pGrids->Get_Count(); i++)
		{
			Grids[i]	= SG_Create_Grid(pGrids->asGrid(i), SG_DATATYPE_Float);
			Grids[i]	->Assign(pGrids->asGrid(i));
			Grids[i]	->Standardise();
		}
	}
	else
	{
		for(i=0; i<pGrids->Get_Count(); i++)
		{
			Grids[i]	= pGrids->asGrid(i);
		}
	}

	pCluster->Set_NoData_Value(-1.0);
	pCluster->Assign_NoData();

	nMembers	= (int     *)SG_Malloc(nCluster * sizeof(int));
	Variances	= (double  *)SG_Malloc(nCluster * sizeof(double));
	Centroids	= (double **)SG_Malloc(nCluster * sizeof(double *));

	for(i=0; i<nCluster; i++)
	{
		Centroids[i]	= (double  *)SG_Malloc(pGrids->Get_Count() * sizeof(double));
	}

	//-------------------------------------------------
	switch( Parameters("METHOD")->asInt() )
	{
	case 0:		SP	= _MinimumDistance	(Grids, pGrids->Get_Count(), pCluster, nCluster, nMembers, Variances, Centroids, nElements = Get_NCells());	break;
	case 1:		SP	= _HillClimbing		(Grids, pGrids->Get_Count(), pCluster, nCluster, nMembers, Variances, Centroids, nElements = Get_NCells());	break;
	case 2:		SP	= _MinimumDistance	(Grids, pGrids->Get_Count(), pCluster, nCluster, nMembers, Variances, Centroids, nElements = Get_NCells());
				SP	= _HillClimbing		(Grids, pGrids->Get_Count(), pCluster, nCluster, nMembers, Variances, Centroids, nElements = Get_NCells());	break;
	}

	//-------------------------------------------------
	if( Parameters("NORMALISE")->asBool() )
	{
		for(i=0; i<pGrids->Get_Count(); i++)
		{
			delete(Grids[i]);

			for(j=0; j<nCluster; j++)
			{
				Centroids[j][i]	= pGrids->asGrid(i)->Get_StdDev() * Centroids[j][i] + pGrids->asGrid(i)->Get_Mean();
			}
		}
	}

	//-------------------------------------------------
	Save_LUT(pCluster);

	//-------------------------------------------------
	int					iCluster, iFeature;
	CSG_String			s;
	CSG_Table_Record	*pRecord;
	CSG_Table			*pTable;

	pTable	= Parameters("STATISTICS")->asTable();

	pTable->Destroy();
	pTable->Set_Name(_TL("Cluster Analysis"));

	pTable->Add_Field(_TL("ClusterID")	, SG_DATATYPE_Int);
	pTable->Add_Field(_TL("Elements")	, SG_DATATYPE_Int);
	pTable->Add_Field(_TL("Std.Dev.")	, SG_DATATYPE_Double);

	s.Printf(SG_T("\n%s:\t%ld \n%s:\t%d \n%s:\t%d \n%s:\t%f\n\n%s\t%s\t%s"),
		_TL("Number of Elements")	, nElements,
		_TL("Number of Variables")	, pGrids->Get_Count(),
		_TL("Number of Clusters")	, nCluster,
		_TL("Standard Deviation")	, sqrt(SP),
		_TL("Cluster"), _TL("Elements"), _TL("Std.Dev.")
	);

	for(iFeature=0; iFeature<pGrids->Get_Count(); iFeature++)
	{
		s	+= CSG_String::Format(SG_T("\t%s"), pGrids->asGrid(iFeature)->Get_Name());

		pTable->Add_Field(pGrids->asGrid(iFeature)->Get_Name(), SG_DATATYPE_Double);
	}

	Message_Add(s);

	for(iCluster=0; iCluster<nCluster; iCluster++)
	{
		Variances[iCluster]	= nMembers[iCluster] ? Variances[iCluster] / nMembers[iCluster] : 0.0;

		s.Printf(SG_T("\n%d\t%d\t%f"), iCluster, nMembers[iCluster], sqrt(Variances[iCluster]));

		pRecord	= pTable->Add_Record();
		pRecord->Set_Value(0, iCluster);
		pRecord->Set_Value(1, nMembers[iCluster]);
		pRecord->Set_Value(2, sqrt(Variances[iCluster]));

		for(iFeature=0; iFeature<pGrids->Get_Count(); iFeature++)
		{
			double	Centroid	= Centroids[iCluster][iFeature];

			if( Parameters("NORMALISE")->asBool() )
			{
				Centroid	= pGrids->asGrid(iFeature)->Get_Mean() + Centroid * pGrids->asGrid(iFeature)->Get_StdDev();
			}

			s	+= CSG_String::Format(SG_T("\t%f"), Centroid);

			pRecord->Set_Value(iFeature + 3, Centroid);
		}

		Message_Add(s, false);
	}

	//-------------------------------------------------
	for(i=0; i<nCluster; i++)
	{
		SG_Free(Centroids[i]);
	}

	SG_Free(Centroids);
	SG_Free(Variances);
	SG_Free(nMembers);
	SG_Free(Grids);

	return( true );
}
Esempio n. 22
0
//---------------------------------------------------------
bool CChannelNetwork::On_Execute(void)
{
	int		x, y, ID, Trace_Method, Init_Method;
	long	n;
	double	Init_Threshold;
	CSG_Grid	*Trace_pRoute, *Trace_pWeight, *Init_pGrid;


	//-----------------------------------------------------
	pDTM				= Parameters("ELEVATION")	->asGrid();
	pConvergence		= Parameters("DIV_GRID")	->asGrid();

	pChannels			= Parameters("CHNLNTWRK")	->asGrid();
	pChannelRoute		= Parameters("CHNLROUTE")	->asGrid();
	pShapes				= Parameters("SHAPES")		->asShapes();

	minLength			= Parameters("MINLEN")		->asInt();

	maxDivCells			= Parameters("DIV_GRID")->asGrid() ? Parameters("DIV_CELLS")->asInt() : -1;


	//-----------------------------------------------------
	// 1. Flow Direction...

	Process_Set_Text(_TL("Channel Network: Pass 1"));

	pChannels->Assign();

	Trace_pRoute		= Parameters("SINKROUTE")	->asGrid();
	Trace_pWeight		= Parameters("TRACE_WEIGHT")->asGrid();
	Trace_Method		= Trace_pWeight ? 1 : 0;

	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			if( Trace_pRoute && (ID = Trace_pRoute->asChar(x, y)) >= 1 && ID <= 8 )
			{
				pChannels->Set_Value(x, y, ID);
			}
			else
			{
				switch( Trace_Method )
				{
				default:
					Set_Route_Standard(x, y);
					break;

				case 1:
					Set_Route_Weighted(x, y, Trace_pWeight, 0.0);
					break;
				}
			}
		}
	}


	//-----------------------------------------------------
	// 2. Initiation...

	Process_Set_Text(_TL("Channel Network: Pass 2"));

	pStart				= SG_Create_Grid(pDTM, SG_DATATYPE_Char);
	Init_pGrid			= Parameters("INIT_GRID")	->asGrid();
	Init_Method			= Parameters("INIT_METHOD")	->asInt();
	Init_Threshold		= Parameters("INIT_VALUE")	->asDouble();

	for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
	{
		switch( Init_Method )
		{
		case 0:
			if( Init_pGrid->asDouble(n) <= Init_Threshold )
				pStart->Set_Value(n, 1);
			break;

		case 1:
			if( Init_pGrid->asDouble(n) == Init_Threshold )
				pStart->Set_Value(n, 1);
			break;

		case 2:
			if( Init_pGrid->asDouble(n) >= Init_Threshold )
				pStart->Set_Value(n, 1);
			break;
		}
	}


	//-----------------------------------------------------
	// 3. Trace Channel Routes...

	Process_Set_Text(_TL("Channel Network: Pass 3"));

	pChannelRoute->Assign();

	Direction			= NULL;
	Direction_Buffer	= 0;

	for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
	{
		if( pDTM->Get_Sorted(n,x,y) )
		{
			Set_Channel_Route(x,y);
		}
	}

	if( Direction )
	{
		SG_Free( Direction );
	}

	pChannels->Assign();

	delete(pStart);


	//-----------------------------------------------------
	Process_Set_Text(_TL("Channel Network: Pass 4"));

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


	//-----------------------------------------------------
	Process_Set_Text(_TL("Channel Network: Pass 5"));

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


	//-----------------------------------------------------
	if( pShapes )
	{
		Process_Set_Text(_TL("Channel Network: Pass 6"));

		pShapes->Create(SHAPE_TYPE_Line, _TL("Channel Network"));

		pShapes->Add_Field("SegmentID"	,SG_DATATYPE_Int);
		pShapes->Add_Field("Order"		,SG_DATATYPE_Int);
		pShapes->Add_Field("Length"		,SG_DATATYPE_Double);

		Lock_Create();

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

		Lock_Destroy();
	}


	//-----------------------------------------------------
	for(n=0; n<Get_NCells(); n++)
	{
		if( pChannels->asInt(n) == 0 )
		{
			pChannels->Set_NoData(n);
			pChannelRoute->Set_NoData(n);
		}
	}


	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CGrid_Proximity::On_Execute(void)
{
	int				x, y;
	double			z, d;
	TSG_Point		p;
	CSG_Grid		*pFeatures, *pDistance, *pDirection, *pAllocation;
	CSG_PRQuadTree	Search;

	//-----------------------------------------------------
	pFeatures	= Parameters("FEATURES")	->asGrid();
	pDistance	= Parameters("DISTANCE")	->asGrid();
	pDirection	= Parameters("DIRECTION")	->asGrid();
	pAllocation	= Parameters("ALLOCATION")	->asGrid();

	//-----------------------------------------------------
	Process_Set_Text(_TL("preparing distance calculation..."));

	Search.Create(CSG_Rect(-1, -1, Get_NX(), Get_NY()));

	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			if( pFeatures->is_NoData(x, y) )
			{
				pDistance->Set_Value(x, y, -1.0);
			}
			else
			{
				pDistance->Set_Value(x, y,  0.0);

				if( pDirection )
				{
					pDirection->Set_NoData(x, y);
				}

				if( pAllocation )
				{
					pAllocation->Set_Value(x, y, pFeatures->asDouble(x, y));
				}

				//-----------------------------------------
				bool	bBorder	= false;

				for(int i=0; i<8 && !bBorder; i++)
				{
					int	ix	= Get_xTo(i, x);
					int	iy	= Get_yTo(i, y);

					if( is_InGrid(ix, iy) && pFeatures->is_NoData(ix, iy) )
					{
						bBorder	= true;
					}
				}

				if( bBorder )
				{
					Search.Add_Point(x, y, pFeatures->asDouble(x, y));
				}
			}
		}
	}

	if( !Search.is_Okay() || Search.Get_Point_Count() <= 0 || Search.Get_Point_Count() >= Get_NCells() )
	{
		Message_Add(_TL("no features to buffer."));

		return( false );
	}

	//-----------------------------------------------------
	Process_Set_Text(_TL("performing distance calculation..."));

	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			if( pDistance->asDouble(x, y) < 0.0 && Search.Get_Nearest_Point(x, y, p, z, d) )
			{
				pDistance->Set_Value(x, y, d * Get_Cellsize());

				if( pDirection )
				{
					if( d > 0.0 )
					{
						pDirection->Set_Value(x, y, SG_Get_Angle_Of_Direction(x, y, p.x, p.y) * M_RAD_TO_DEG);
					}
					else
					{
						pDirection->Set_NoData(x, y);
					}
				}

				if( pAllocation )
				{
					pAllocation->Set_Value(x, y, z);
				}
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
Esempio n. 24
0
//---------------------------------------------------------
bool CChange_Detection::Get_Classes(CSG_Table &Classes, CSG_Grid *pGrid, bool bInitial)
{
	CSG_Table	*pClasses;

	Classes.Destroy();

	Classes.Add_Field(_TL("NAME")	, SG_DATATYPE_String);
	Classes.Add_Field(_TL("MIN")	, SG_DATATYPE_Double);
	Classes.Add_Field(_TL("MAX")	, SG_DATATYPE_Double);

	//-----------------------------------------------------
	if( (pClasses = Parameters(bInitial ? "INI_LUT" : "FIN_LUT")->asTable()) != NULL )
	{
		int	fNam	= Parameters(bInitial ? "INI_LUT_NAM" : "FIN_LUT_NAM")->asInt();
		int	fMin	= Parameters(bInitial ? "INI_LUT_MIN" : "FIN_LUT_MIN")->asInt();
		int	fMax	= Parameters(bInitial ? "INI_LUT_MAX" : "FIN_LUT_MAX")->asInt();

		if( fNam < 0 || fNam >= pClasses->Get_Field_Count() )	{	fNam	= fMin;	}
		if( fMax < 0 || fMax >= pClasses->Get_Field_Count() )	{	fMax	= fMin;	}

		for(int iClass=0; iClass<pClasses->Get_Count(); iClass++)
		{
			CSG_Table_Record	*pClass	= Classes.Add_Record();

			pClass->Set_Value(CLASS_NAM, pClasses->Get_Record(iClass)->asString(fNam));
			pClass->Set_Value(CLASS_MIN, pClasses->Get_Record(iClass)->asDouble(fMin));
			pClass->Set_Value(CLASS_MAX, pClasses->Get_Record(iClass)->asDouble(fMax));
		}
	}

	//-----------------------------------------------------
	else if( DataObject_Get_Parameter(pGrid, "LUT") )
	{
		pClasses	= DataObject_Get_Parameter(pGrid, "LUT")->asTable();

		for(int iClass=0; iClass<pClasses->Get_Count(); iClass++)
		{
			CSG_Table_Record	*pClass	= Classes.Add_Record();

			pClass->Set_Value(CLASS_NAM, pClasses->Get_Record(iClass)->asString(1));
			pClass->Set_Value(CLASS_MIN, pClasses->Get_Record(iClass)->asDouble(3));
			pClass->Set_Value(CLASS_MAX, pClasses->Get_Record(iClass)->asDouble(4));
		}
	}

	//-----------------------------------------------------
	else
	{
		double	z;

		for(sLong i=0; i<Get_NCells() && Set_Progress_NCells(i); i++)
		{
			double iz	= pGrid->asDouble(pGrid->Get_Sorted(i, false, false));

			if( i == 0 || iz != z )
			{
				CSG_Table_Record	*pClass	= Classes.Add_Record();

				pClass->Set_Value(CLASS_NAM, z = iz);
				pClass->Set_Value(CLASS_MIN, z);
				pClass->Set_Value(CLASS_MAX, z);
			}
		}
	}

	//-----------------------------------------------------
	return( Classes.Get_Count() > 0 );
}
//---------------------------------------------------------
bool CGrid_Aspect_Slope_Map::On_Execute(void)
{
	CSG_Grid	*pAspect, *pSlope, *pAspectSlope;
	CSG_Table	*pLUT;

	int						iAspectCount	= 9;
	static const double		AspectBreaks[]	= {0.000, 0.393, 1.178, 1.963, 2.749, 3.534, 4.320, 5.105, 5.890, 6.283};
	static const int		AspectClass[]	= {1, 2, 3, 4, 5, 6, 7, 8, 1};

	int						iSlopeCount		= 4;
	static const double		SlopeBreaks[]	= {0.000, 0.087, 0.349, 0.698, 1.571};
	static const int		SlopeClass[]	= {10, 20, 30, 40};


	pAspect			= Parameters("ASPECT")->asGrid();
	pSlope			= Parameters("SLOPE")->asGrid();
	pAspectSlope	= Parameters("ASPECT_SLOPE")->asGrid();
	pLUT			= Parameters("LUT")->asTable();


	//-----------------------------------------------------
	if( pLUT == NULL )
		pLUT = new CSG_Table();
	else
		pLUT->Destroy();

	pLUT->Set_Name(SG_T("LUT_Aspect-Slope"));

	pLUT->Add_Field(SG_T("COLOR"),			SG_DATATYPE_Int);
	pLUT->Add_Field(SG_T("NAME"),			SG_DATATYPE_String);
	pLUT->Add_Field(SG_T("DESCRIPTION"),	SG_DATATYPE_String);
	pLUT->Add_Field(SG_T("MINIMUM"),		SG_DATATYPE_Int);
	pLUT->Add_Field(SG_T("MAXIMUM"),		SG_DATATYPE_Int);

	for(int i=0; i<25; i++)
	{
		CSG_Table_Record	*pRecord = pLUT->Add_Record();

		pRecord->Set_Value(0, LUT_COLOR[i]);
		pRecord->Set_Value(1, LUT_NAME[i]);
		pRecord->Set_Value(2, SG_T(""));
		pRecord->Set_Value(3, LUT_BREAK[i]);
		pRecord->Set_Value(4, LUT_BREAK[i+1]);
	}


	//-----------------------------------------------------
	#pragma omp parallel for
	for(sLong n=0; n<Get_NCells(); n++)
	{
		int		iAspectClass, iSlopeClass;

		if( pAspect->is_NoData(n) || pSlope->is_NoData(n) )
		{
			pAspectSlope->Set_NoData(n);
		}
		else
		{
			iAspectClass	= Get_Class(pAspect->asDouble(n), iAspectCount, AspectBreaks, AspectClass);
			iSlopeClass		= Get_Class(pSlope->asDouble(n), iSlopeCount, SlopeBreaks, SlopeClass);

			pAspectSlope->Set_Value(n, iAspectClass + iSlopeClass);
		}
	}


	//-----------------------------------------------------
	CSG_Parameters	Parms;

	if( DataObject_Get_Parameters(pAspectSlope, Parms) && Parms("COLORS_TYPE") && Parms("LUT") )
	{
		Parms("LUT")->asTable()->Assign(pLUT);
		Parms("COLORS_TYPE")->Set_Value(1);

		DataObject_Set_Parameters(pAspectSlope, Parms);
	}

	if( Parameters("LUT")->asTable() == NULL )
	{
		delete pLUT;
	}


	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CGridsFromTableAndGrid::On_Execute(void)
{
	int						iField, iRecord, iAttribute, nAttributes, *Attribute;
	sLong					iCell, jCell;
	CSG_Parameter_Grid_List	*pGrids;
	CSG_Grid				*pClasses;
	CSG_Table				*pTable;

	//-----------------------------------------------------
	pClasses	= Parameters("CLASSES" )->asGrid();
	pGrids		= Parameters("GRIDS"   )->asGridList();
	pTable		= Parameters("TABLE"   )->asTable();
	iField		= Parameters("ID_FIELD")->asInt();

	pGrids->Del_Items();

	if( !pClasses->Set_Index() )
	{
		Error_Set(_TL("index creation failed"));

		return( false );
	}

	//-----------------------------------------------------
	if( pTable->Get_Field_Count() == 0 || pTable->Get_Count() == 0 )
	{
		Message_Add(_TL("selected table contains no valid records"));

		return( false );
	}

	//-----------------------------------------------------
	if( !pTable->Set_Index(iField, TABLE_INDEX_Ascending) )
	{
		Message_Add(_TL("failed to create index for table"));

		return( false );
	}

	//-----------------------------------------------------
	Attribute	= new int[pTable->Get_Field_Count()];

	for(iAttribute=0, nAttributes=0; iAttribute<pTable->Get_Field_Count(); iAttribute++)
	{
		if( iAttribute != iField && pTable->Get_Field_Type(iAttribute) != SG_DATATYPE_String )
		{
			Attribute[nAttributes++]	= iAttribute;

			CSG_Grid	*pGrid	= SG_Create_Grid(*Get_System());

			pGrid->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pClasses->Get_Name(), pTable->Get_Field_Name(iAttribute)));

			pGrids->Add_Item(pGrid);
		}
	}

	if( nAttributes == 0 )
	{
		delete[](Attribute);

		Message_Add(_TL("selected table does not have numeric attributes"));

		return( false );
	}

	//-----------------------------------------------------
	CSG_Table_Record	*pRecord	= pTable->Get_Record_byIndex(0);

	for(iCell=0, iRecord=0; iCell<Get_NCells() && pRecord && Set_Progress_NCells(iCell); iCell++)
	{
		if( pClasses->Get_Sorted(iCell, jCell, false, true) )
		{
			double	valClass	= pClasses->asDouble(jCell);

			while( pRecord && pRecord->asDouble(iField) < valClass )
			{
				pRecord	= pTable->Get_Record_byIndex(++iRecord);
			}

			if( !pRecord || pRecord->asDouble(iField) > valClass )
			{
				for(iAttribute=0; iAttribute<nAttributes; iAttribute++)
				{
					pGrids->asGrid(iAttribute)->Set_NoData(jCell);
				}
			}
			else
			{
				for(iAttribute=0; iAttribute<nAttributes; iAttribute++)
				{
					pGrids->asGrid(iAttribute)->Set_Value(jCell, pRecord->asDouble(Attribute[iAttribute]));
				}
			}
		}
	}

	//-----------------------------------------------------
	delete[](Attribute);

	return true;
}
//---------------------------------------------------------
bool CChannelNetwork_Distance::On_Execute(void)
{
	CSG_Grid	*pChannels;

	//-----------------------------------------------------
	m_pDEM		= Parameters("ELEVATION")->asGrid();
	m_pRoute	= Parameters("ROUTE"    )->asGrid();
	pChannels	= Parameters("CHANNELS" )->asGrid();

	m_pDistance	= Parameters("DISTANCE" )->asGrid();
	m_pDistVert	= Parameters("DISTVERT" )->asGrid();
	m_pDistHorz	= Parameters("DISTHORZ" )->asGrid();

	m_pTime		= Parameters("TIME"     )->asGrid();
	m_pSDR		= Parameters("SDR"      )->asGrid();

	m_Flow_B	= Parameters("FLOW_B"   )->asDouble();
	m_Flow_K	= Parameters("FLOW_K"   )->asDouble();
	m_Flow_R	= Parameters("FLOW_R"   )->asDouble();
	m_pFlow_K	= Parameters("FLOW_K"   )->asGrid();
	m_pFlow_R	= Parameters("FLOW_R"   )->asGrid();

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

	//-----------------------------------------------------
	if( m_pDistance )	m_pDistance->Assign_NoData();
	if( m_pDistVert )	m_pDistVert->Assign_NoData();
	if( m_pDistHorz )	m_pDistHorz->Assign_NoData();
	if( m_pTime     )	m_pTime    ->Assign_NoData();
	if( m_pSDR      )	m_pSDR     ->Assign_NoData();

	switch( Method )
	{
	default:	Initialize_D8 ();	break;
	case  1:	Initialize_MFD();	break;
	}

	m_pDEM->Set_Index(true);

	//-----------------------------------------------------
	for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
	{
		int		x, y;

		if( m_pDEM->Get_Sorted(n, x, y, false, true) && !(pChannels->is_NoData(x, y) && m_pDistance->is_NoData(x, y)) )
		{
			if( !pChannels->is_NoData(x, y) )
			{
				if( m_pDistance )	m_pDistance->Set_Value(x, y, 0.0);
				if( m_pDistVert )	m_pDistVert->Set_Value(x, y, 0.0);
				if( m_pDistHorz )	m_pDistHorz->Set_Value(x, y, 0.0);
				if( m_pTime     )	m_pTime    ->Set_Value(x, y, 0.0);
				if( m_pSDR      )	m_pSDR     ->Set_Value(x, y, 0.0);
				if( m_pFields   )	m_pPasses  ->Set_Value(x, y, 0.0);
			}

			switch( Method )
			{
			default:	Execute_D8 (x, y);	break;
			case  1:	Execute_MFD(x, y);	break;
			}
		}
	}

	//-----------------------------------------------------
	m_Dir.Destroy();

	for(int i=0; i<=8; i++)
	{
		m_Flow[i].Destroy();
	}

	//-----------------------------------------------------
	return( true );
}
Esempio n. 28
0
//---------------------------------------------------------
bool CRGA_Basic::On_Execute(void)
{
	bool		bRefresh;
	int			x, y, i, Segment;
	CSG_Grid	*pSeeds;

	//-----------------------------------------------------
	m_pSegments		= Parameters("SEGMENTS"  )->asGrid();
	m_pFeatures		= Parameters("FEATURES"  )->asGridList();
	m_nFeatures		= m_pFeatures->Get_Count();

	pSeeds			= Parameters("SEEDS"     )->asGrid();
	m_pSeeds		= Parameters("TABLE"     )->asTable();

	m_pSimilarity	= Parameters("SIMILARITY")->asGrid();

	m_dNeighbour	= Parameters("NEIGHBOUR" )->asInt() == 0 ? 2 : 1;

	m_Var_1			= SG_Get_Square(Parameters("SIG_1")->asDouble());
	m_Var_2			= SG_Get_Square(Parameters("SIG_2")->asDouble());
	m_Threshold		= Parameters("THRESHOLD" )->asDouble();

	m_bNormalize	= Parameters("NORMALIZE" )->asBool();

	m_Method		= Parameters("METHOD"    )->asInt();
	bRefresh		= Parameters("REFRESH"   )->asBool();

	//-----------------------------------------------------
	m_pSegments		->Assign(-1);
	m_pSegments		->Set_NoData_Value(-1);

	m_pSimilarity	->Assign(-1);
	m_pSimilarity	->Set_NoData_Value(-1);

	//-----------------------------------------------------
	m_pSeeds->Destroy();

	m_pSeeds->Add_Field(_TL("ID"  ), SG_DATATYPE_Int);
	m_pSeeds->Add_Field(_TL("AREA"), SG_DATATYPE_Double);
	m_pSeeds->Add_Field(_TL("X"   ), SG_DATATYPE_Double);
	m_pSeeds->Add_Field(_TL("Y"   ), SG_DATATYPE_Double);

	for(i=0; i<m_pFeatures->Get_Count(); i++)
	{
		m_pSeeds->Add_Field(m_pFeatures->asGrid(i)->Get_Name(), SG_DATATYPE_Double);
	}

	m_Candidates.Create(Parameters("LEAFSIZE")->asInt());

	//-----------------------------------------------------
	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			if( !pSeeds->is_NoData(x, y) )
			{
				CSG_Table_Record	*pRec	= m_pSeeds->Add_Record();

				pRec->Set_Value(0, m_pSeeds->Get_Count() - 1);
				pRec->Set_Value(SEEDFIELD_X, x);
				pRec->Set_Value(SEEDFIELD_Y, y);

				for(i=0; i<m_pFeatures->Get_Count(); i++)
				{
					pRec->Set_Value(SEEDFIELD_Z + i, Get_Feature(x, y, i));
				}

				m_pSimilarity->Set_Value(x, y, 1.0);

				Add_To_Segment(x, y, m_pSeeds->Get_Count() - 1);
			}
		}
	}

	//-----------------------------------------------------
	if( m_pSeeds->Get_Count() > 1 )
	{
		sLong	n	= 0;

		while( n++ < Get_NCells() && Set_Progress_NCells(n) && Get_Next_Candidate(x, y, Segment) )
		{
			Add_To_Segment(x, y, Segment);

			if( bRefresh && (n % Get_NX()) == 0 )
			{
				DataObject_Update(m_pSegments, 0, m_pSeeds->Get_Count());

				Process_Set_Text(CSG_String::Format(SG_T("%.2f"), 100.0 * m_Candidates.Get_Count() / Get_NCells()));
			}
		}

		m_Candidates.Destroy();

		return( true );
	}

	//-----------------------------------------------------
	m_Candidates.Destroy();

	return( false );
}
Esempio n. 29
0
//---------------------------------------------------------
void CFlow_RecursiveUp::On_Create(void)
{
	int		x, y, Method;

	double	*p;

	//-----------------------------------------------------
	On_Destroy();

	Flow	= (double ***)SG_Malloc(    Get_NY    () * sizeof(double **));
	p		= (double   *)SG_Malloc(8 * Get_NCells() * sizeof(double   ));

	for(y=0; y<Get_NY(); y++)
	{
		Flow[y]	= (double **)SG_Malloc( Get_NX    () * sizeof(double  *));

		for(x=0; x<Get_NX(); x++, p+=8)
		{
			Flow[y][x]	= p;
		}
	}

	//-----------------------------------------------------
	Lock_Create();

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

	memset(Flow[0][0], 0, 8 * Get_NCells() * sizeof(double) );

	for(y=0; y<Get_NY(); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			if( pRoute && pRoute->asChar(x,y) > 0 )
			{
				Flow[y][x][pRoute->asChar(x,y) % 8]	= 1.0;
			}
			else
			{
				switch( Method )
				{
					case 0:
						Set_D8(x,y);
						break;

					case 1:
						Set_Rho8(x,y);
						break;

					case 2:
						Set_DInf(x,y);
						break;

					case 3:
						Set_MFD(x,y);
						break;
				}
			}
		}
	}
}
Esempio n. 30
0
//---------------------------------------------------------
bool CGrid_Cluster_Analysis::On_Execute(void)
{
	if( Parameters("OLDVERSION")->asBool() )	{	return( _On_Execute() );	}

	//-----------------------------------------------------
	bool					bNormalize;
	int						iFeature;
	sLong					iElement, nElements;
	CSG_Cluster_Analysis	Analysis;
	CSG_Grid				*pCluster;
	CSG_Parameter_Grid_List	*pGrids;

	//-----------------------------------------------------
	pGrids		= Parameters("GRIDS"    )->asGridList();
	pCluster	= Parameters("CLUSTER"  )->asGrid();
	bNormalize	= Parameters("NORMALISE")->asBool();

	if( !Analysis.Create(pGrids->Get_Count()) )
	{
		return( false );
	}

	//-----------------------------------------------------
	pCluster->Set_NoData_Value(0.0);

	for(iElement=0, nElements=0; iElement<Get_NCells() && Set_Progress_NCells(iElement); iElement++)
	{
		bool	bNoData		= false;

		for(iFeature=0; iFeature<pGrids->Get_Count() && !bNoData; iFeature++)
		{
			if( pGrids->asGrid(iFeature)->is_NoData(iElement) )
			{
				bNoData	= true;
			}
		}

		if( bNoData || !Analysis.Add_Element() )
		{
			pCluster->Set_Value(iElement, 0);
		}
		else
		{
			pCluster->Set_Value(iElement, 1);

			for(iFeature=0; iFeature<pGrids->Get_Count(); iFeature++)
			{
				double	d	= pGrids->asGrid(iFeature)->asDouble(iElement);

				if( bNormalize )
				{
					d	= (d - pGrids->asGrid(iFeature)->Get_Mean()) / pGrids->asGrid(iFeature)->Get_StdDev();
				}

				Analysis.Set_Feature(nElements, iFeature, d);
			}

			nElements++;
		}
	}

	if( nElements <= 1 )
	{
		return( false );
	}

	//-----------------------------------------------------
	bool	bResult	= Analysis.Execute(
		Parameters("METHOD"  )->asInt(),
		Parameters("NCLUSTER")->asInt(),
		Parameters("MAXITER" )->asInt()
	);

	for(iElement=0, nElements=0; iElement<Get_NCells(); iElement++)
	{
		Set_Progress_NCells(iElement);

		if( !pCluster->is_NoData(iElement) )
		{
			pCluster->Set_Value(iElement, 1 + Analysis.Get_Cluster(nElements++));
		}
	}

	Save_Statistics(pGrids, bNormalize, Analysis);

	Save_LUT(pCluster);

	return( bResult );
}