//---------------------------------------------------------
bool CFilter_Resample::On_Execute(void)
{
	double		Cellsize;
	CSG_Grid	*pGrid, *pLoPass, *pHiPass;

	//-----------------------------------------------------
	pGrid		= Parameters("GRID"  )->asGrid();
	pLoPass		= Parameters("LOPASS")->asGrid();
	pHiPass		= Parameters("HIPASS")->asGrid();
	Cellsize	= Parameters("SCALE" )->asDouble() * Get_Cellsize();

	//-----------------------------------------------------
	if( Cellsize > 0.5 * SG_Get_Length(Get_System()->Get_XRange(), Get_System()->Get_YRange()) )
	{
		Error_Set(_TL("resampling cell size is too large"));

		return( false );
	}

	//-----------------------------------------------------
	CSG_Grid	Grid(CSG_Grid_System(Cellsize, Get_XMin(), Get_YMin(), Get_XMax(), Get_YMax()), SG_DATATYPE_Float);

	Grid.Assign(pGrid, GRID_RESAMPLING_Mean_Cells);

	//-----------------------------------------------------
	pLoPass->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pGrid->Get_Name(), _TL("Low Pass")));
	pHiPass->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pGrid->Get_Name(), _TL("High Pass")));

	CSG_Colors	Colors;

	DataObject_Get_Colors(pGrid  , Colors);
	DataObject_Set_Colors(pLoPass, Colors);
	DataObject_Set_Colors(pHiPass, 11, SG_COLORS_RED_GREY_BLUE);

	//-----------------------------------------------------
	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++)
		{
			double	z, px	= Get_XMin() + x * Get_Cellsize();

			if( !pGrid->is_NoData(x, y) && Grid.Get_Value(px, py, z) )
			{
				pLoPass->Set_Value(x, y, z);
				pHiPass->Set_Value(x, y, pGrid->asDouble(x, y) - z);
			}
			else
			{
				pLoPass->Set_NoData(x, y);
				pHiPass->Set_NoData(x, y);
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
Esempio n. 2
0
//---------------------------------------------------------
inline double CKernel_Density::Get_Kernel(double dx, double dy)
{
	double	d	= SG_Get_Length(dx, dy);

	if( d >= m_dRadius )
	{
		return( 0.0 );
	}

	d	/= m_dRadius;

	switch( m_Kernel )
	{
	default:
	case 0:	// quartic kernel
		return( (3.0 / (M_PI * m_dRadius*m_dRadius)) * SG_Get_Square(1.0 - d*d) );

	case 1:	// gaussian kernel
		d	*= 2.0;
		return( exp(-0.5 * d*d) );

	case 2:	// exponential
		d	*= 2.0;
		return( exp(-d) );

	case 3:	// inverse distance
		return( pow(1.0 + d, -1.0) );
	}
}
//---------------------------------------------------------
void CVariogram_Dialog::Set_Variogram(void)
{
	double	lagDist	= m_Settings("LAGDIST")->asDouble();
	double	maxDist	= m_Settings("MAXDIST")->asDouble();

	if( lagDist > 0.0 )
	{
		double	Diagonal	= SG_Get_Length(m_pPoints->Get_Extent().Get_XRange(), m_pPoints->Get_Extent().Get_YRange());	// bounding box's diagonal

		if( maxDist <= 0.0 || maxDist > Diagonal )
		{
			m_Settings("MAXDIST")->Set_Value(maxDist = Diagonal);
		}

		CSG_Variogram::Calculate(m_pPoints, m_Attribute, m_bLog, m_pVariogram,
			1 + (int)(0.5 + maxDist / lagDist), maxDist, m_Settings("SKIP")->asInt()
		);

		m_pDistance->Set_Range(0.0, m_pVariogram->Get_Maximum(CSG_Variogram::FIELD_DISTANCE));
		m_pDistance->Set_Value(m_pVariogram->Get_Maximum(CSG_Variogram::FIELD_DISTANCE));

		m_pDiagram->Set_Variogram();

		Set_Model();
	}
}
//---------------------------------------------------------
bool CGWR_Grid_Downscaling::Get_Model(void)
{
	//-----------------------------------------------------
	m_pQuality		= Parameters("QUALITY"  )->asGrid();
	m_pQuality		->Set_Name(CSG_String::Format(SG_T("%s [%s, %s]"), m_pDependent->Get_Name(), _TL("GWR"), _TL("Quality")));

	m_pResiduals	= Parameters("RESIDUALS")->asGrid();
	m_pResiduals	->Set_Name(CSG_String::Format(SG_T("%s [%s, %s]"), m_pDependent->Get_Name(), _TL("GWR"), _TL("Residuals")));

	//-----------------------------------------------------
	m_Search.Get_Weighting().Set_Parameters(&Parameters);

	m_Search.Set_Radius(Parameters("SEARCH_RANGE")->asInt() == 0
		? Parameters("SEARCH_RADIUS")->asInt() : 1 + (int)(SG_Get_Length(m_pDependent->Get_NX(), m_pDependent->Get_NY()))
	);

	//-----------------------------------------------------
	CSG_Grid_System	System(m_pDependent->Get_System());

	for(int y=0; y<System.Get_NY() && Set_Progress(y, System.Get_NY()); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<System.Get_NX(); x++)
		{
			if( !Get_Regression(x, y) )
			{
				m_pQuality  ->Set_NoData(x, y);
				m_pResiduals->Set_NoData(x, y);

				for(int i=0; i<=m_nPredictors; i++)
				{
					m_pModel[i]->Set_NoData(x, y);
				}
			}
		}
	}

	//-----------------------------------------------------
	m_Search.Destroy();

	return( true );
}
Esempio n. 5
0
//---------------------------------------------------------
// find_weight() Function to find the weightings matrix for the
//               observed cell values.
//               Uses an inverse distance function that can be
//               calibrated with an exponent (0= no decay,
//               1=linear decay, 2=squared distance decay etc.)
//               V.1.1, Jo Wood, 11th May, 1995.
//---------------------------------------------------------
bool CParam_Scale::Get_Weights(void)
{
	if( (m_Radius = Parameters("SIZE")->asInt()) < 1 || !m_Weights.Create(1 + 2 * m_Radius, 1 + 2 * m_Radius) )
	{
		return( false );
	}

	double	Exponent	= Parameters("EXPONENT")->asDouble();

	// Find inverse distance of all cells to centre.
	for(int y=0; y<m_Weights.Get_NY(); y++)
	{
		for(int x=0; x<m_Weights.Get_NX(); x++)
		{
			m_Weights[y][x]	= 1.0 / pow(1.0 + SG_Get_Length(m_Radius - x, m_Radius - y), Exponent);
		}
	}

	return( true );
}
//---------------------------------------------------------
bool CWKSP_Shapes::_Edit_Move(bool bToggle)
{
	if( m_Edit_pShape )
	{
		if( bToggle )
		{
			switch( m_Edit_Mode )
			{
			default:
				break;

			//---------------------------------------------
			case EDIT_SHAPE_MODE_Normal:
				m_Edit_Mode	= EDIT_SHAPE_MODE_Move;

				if( m_Edit_Shapes.Get_Count() > 1 )
				{
					m_Edit_Shapes.Get_Shape(1)->Del_Parts();
				}
				else
				{
					m_Edit_Shapes.Add_Shape();
				}

				return( true );

			//---------------------------------------------
			case EDIT_SHAPE_MODE_Move:
				m_Edit_Mode	= EDIT_SHAPE_MODE_Normal;

				m_Edit_Shapes.Del_Shape(1);

				return( true );
			}
		}

		//-------------------------------------------------
		else // if( !bToggle )
		{
			if( m_Edit_Shapes.Get_Count() > 1 && m_Edit_Shapes.Get_Shape(1)->Get_Point_Count() > 1 )
			{
				CSG_Point	Move	= CSG_Point(m_Edit_Shapes.Get_Shape(1)->Get_Point(1))
									- CSG_Point(m_Edit_Shapes.Get_Shape(1)->Get_Point(0));

				m_Edit_Shapes.Get_Shape(1)->Del_Parts();

				if( SG_Get_Length(Move.Get_X(), Move.Get_Y()) > 0.0 )
				{
					for(int iPart=0; iPart<m_Edit_pShape->Get_Part_Count(); iPart++)
					{
						for(int iPoint=0; iPoint<m_Edit_pShape->Get_Point_Count(iPart); iPoint++)
						{
							m_Edit_pShape->Set_Point(Move + m_Edit_pShape->Get_Point(iPoint, iPart), iPoint, iPart);
						}
					}

					Update_Views(false);

					return( true );
				}
			}
		}
	}

	return( false );
}
//---------------------------------------------------------
bool CGSPoints_Semi_Variances::On_Execute(void)
{
	int					i, j, k, n, nDistances, nSkip, Attribute;
	double				zi, zj, zMean, v, c, maxDistance, lagDistance;
	TSG_Point			Pt_i, Pt_j;
	CSG_Vector			Count, Variance, Covariance;
	CSG_Table_Record	*pRecord;
	CSG_Table			*pTable;
	CSG_Shape			*pPoint;
	CSG_Shapes			*pPoints;

	//-----------------------------------------------------
	pPoints		= Parameters("POINTS")		->asShapes();
	pTable		= Parameters("RESULT")		->asTable();
	Attribute	= Parameters("FIELD")		->asInt();
	nSkip		= Parameters("NSKIP")		->asInt();
	maxDistance	= Parameters("DISTMAX")		->asDouble();
	nDistances	= Parameters("DISTCOUNT")	->asInt();

	if( maxDistance <= 0.0 )
	{
		maxDistance	= SG_Get_Length(pPoints->Get_Extent().Get_XRange(), pPoints->Get_Extent().Get_YRange());
	}

	lagDistance	= maxDistance / nDistances;

	zMean		= pPoints->Get_Mean(Attribute);

	Count		.Create(nDistances);
	Variance	.Create(nDistances);
	Covariance	.Create(nDistances);

	//-----------------------------------------------------
	for(i=0, n=0; i<pPoints->Get_Count() && Set_Progress(n, SG_Get_Square(pPoints->Get_Count()/nSkip)/2); i+=nSkip)
	{
		pPoint	= pPoints->Get_Shape(i);

		if( !pPoint->is_NoData(Attribute) )
		{
			Pt_i	= pPoint->Get_Point(0);
			zi		= pPoint->asDouble(Attribute);

			for(j=i+nSkip; j<pPoints->Get_Count(); j+=nSkip, n++)
			{
				pPoint	= pPoints->Get_Shape(j);

				if( !pPoint->is_NoData(Attribute) )
				{
					Pt_j	= pPoint->Get_Point(0);
					k		= (int)(SG_Get_Distance(Pt_i, Pt_j) / lagDistance);

					if( k < nDistances )
					{
						zj	= pPoint->asDouble(Attribute);

						v	= SG_Get_Square(zi - zj);
						c	= (zi - zMean) * (zj - zMean);

						Count	  [k]	++;
						Variance  [k]	+= v;
						Covariance[k]	+= c;
					}
				}
			}
		}
	}

	//-----------------------------------------------------
	pTable->Destroy();
	pTable->Set_Name(CSG_String::Format(SG_T("%s [%s: %s]"), pPoints->Get_Name(), _TL("Variogram"), pPoints->Get_Field_Name(Attribute)));
	pTable->Add_Field(_TL("Class")		, SG_DATATYPE_Int);		// FIELD_CLASSNR
	pTable->Add_Field(_TL("Distance")	, SG_DATATYPE_Double);	// FIELD_DISTANCE
	pTable->Add_Field(_TL("Count")		, SG_DATATYPE_Int);		// FIELD_COUNT
	pTable->Add_Field(_TL("Variance")	, SG_DATATYPE_Double);	// FIELD_VARIANCE
	pTable->Add_Field(_TL("Cum.Var.")	, SG_DATATYPE_Double);	// FIELD_VARCUMUL
	pTable->Add_Field(_TL("Covariance")	, SG_DATATYPE_Double);	// FIELD_COVARIANCE
	pTable->Add_Field(_TL("Cum.Covar.")	, SG_DATATYPE_Double);	// FIELD_COVARCUMUL

	for(i=0, v=0.0, c=0.0, n=0; i<nDistances; i++)
	{
		if( Count[i] > 0 )
		{
			n	+= (int)Count[i];
			v	+= Variance  [i];
			c	+= Covariance[i];

			pRecord	= pTable->Add_Record();
			pRecord->Set_Value(FIELD_CLASSNR	, (i + 1));
			pRecord->Set_Value(FIELD_DISTANCE	, (i + 1) * lagDistance);
			pRecord->Set_Value(FIELD_COUNT		, Count[i]);
			pRecord->Set_Value(FIELD_VARIANCE	, 0.5 * Variance  [i] / Count[i]);
			pRecord->Set_Value(FIELD_VARCUMUL	, 0.5 * v / n);
			pRecord->Set_Value(FIELD_COVARIANCE	, 1.0 * Covariance[i] / Count[i]);
			pRecord->Set_Value(FIELD_COVARCUMUL	, 1.0 * c / n);
		}
	}

	return( true );
}
Esempio n. 8
0
//---------------------------------------------------------
bool CMRVBF::On_Execute(void)
{
	bool		bUpdate;
	int			Level;
	double		T_Slope, Resolution, max_Resolution;
	CSG_Grid	*pDEM, *pMRVBF, *pMRRTF, CF, VF, RF, DEM, Slope, Pctl;

	//-----------------------------------------------------
	pDEM			= Parameters("DEM")			->asGrid();
	pMRVBF			= Parameters("MRVBF")		->asGrid();
	pMRRTF			= Parameters("MRRTF")		->asGrid();

	T_Slope			= Parameters("T_SLOPE")		->asDouble();

	m_T_Pctl_V		= Parameters("T_PCTL_V")	->asDouble();
	m_T_Pctl_R		= Parameters("T_PCTL_R")	->asDouble();

	m_P_Slope		= Parameters("P_SLOPE")		->asDouble();
	m_P_Pctl		= Parameters("P_PCTL")		->asDouble();

	bUpdate			= Parameters("UPDATE")		->asBool();

	max_Resolution	= Parameters("MAX_RES")		->asDouble() / 100.0;
	Resolution		= SG_Get_Length(Get_System()->Get_XRange(), Get_System()->Get_YRange());
	max_Resolution	= max_Resolution * Resolution;

	//-----------------------------------------------------
	if( 1 )
	{
	//	DataObject_Set_Colors(pMRVBF, 100, SG_COLORS_WHITE_BLUE		, false);
		DataObject_Set_Colors(pMRVBF, 100, SG_COLORS_RED_GREY_BLUE	, false);

	//	DataObject_Set_Colors(pMRRTF, 100, SG_COLORS_RED_GREY_BLUE	, true);
		DataObject_Set_Colors(pMRRTF, 100, SG_COLORS_WHITE_RED		, false);

		CSG_Grid	CF, VF, RF, DEM, Slopes, Percentiles;

		VF.Create(*Get_System(), SG_DATATYPE_Float);
		RF.Create(*Get_System(), SG_DATATYPE_Float);
		CF.Create(*Get_System(), SG_DATATYPE_Float);
		CF.Assign(1.0);

		DEM.Create(*pDEM);

		//-------------------------------------------------
		Level		= 1;
		Resolution	= Get_Cellsize();

		Process_Set_Text(CSG_String::Format(SG_T("%d. %s"), Level, _TL("step")));
		Message_Add(CSG_String::Format(SG_T("%s: %d, %s: %.2f, %s %.2f"), _TL("step"), Level, _TL("resolution"), Resolution, _TL("threshold slope"), T_Slope));

		Get_Slopes		(&DEM, &Slopes);
		Get_Percentiles	(&DEM, &Percentiles, 3);
		Get_Flatness	(&Slopes, &Percentiles, &CF, pMRVBF, pMRRTF, T_Slope);
		UPDATE_VIEWS	(true);

		//-------------------------------------------------
		T_Slope		/= 2.0;
		Level++;

		Process_Set_Text(CSG_String::Format(SG_T("%d. %s"), Level, _TL("step")));
		Message_Add(CSG_String::Format(SG_T("%s: %d, %s: %.2f, %s %.2f"), _TL("step"), Level, _TL("resolution"), Resolution, _TL("threshold slope"), T_Slope));

		Get_Percentiles	(&DEM, &Percentiles, 6);
		Get_Flatness	(&Slopes, &Percentiles, &CF, &VF, &RF, T_Slope);
		Get_MRVBF		(Level, pMRVBF, &VF, pMRRTF, &RF);
		UPDATE_VIEWS	(false);

		//-------------------------------------------------
		while( Process_Get_Okay(false) && Resolution < max_Resolution )
		{
			Resolution	*= 3.0;
			T_Slope		/= 2.0;
			Level++;

			Process_Set_Text(CSG_String::Format(SG_T("%d. %s"), Level, _TL("step")));
			Message_Add(CSG_String::Format(SG_T("%s: %d, %s: %.2f, %s %.2f"), _TL("step"), Level, _TL("resolution"), Resolution, _TL("threshold slope"), T_Slope));

			Get_Values		(&DEM, &Slopes, &Percentiles, Resolution);
			Get_Flatness	(&Slopes, &Percentiles, &CF, &VF, &RF, T_Slope);
			Get_MRVBF		(Level, pMRVBF, &VF, pMRRTF, &RF);
			UPDATE_VIEWS	(false);
		}

		if( Parameters("CLASSIFY")->asBool() )
		{
			Get_Classified(pMRVBF);
			Get_Classified(pMRRTF);
		}

		return( true );
	}

	return( false );
}
//---------------------------------------------------------
bool CSG_Variogram::Calculate(CSG_Shapes *pPoints, int Attribute, bool bLog, CSG_Table *pVariogram, int nClasses, double maxDistance, int nSkip)
{
	int					i, j, k, n;
	double				z, lagDistance;
	TSG_Point			p;
	CSG_Vector			Count, Variance;
	CSG_Table_Record	*pRecord;
	CSG_Shape			*pPoint;

	//-----------------------------------------------------
	if( nSkip < 1 )
	{
		nSkip		= 1;
	}

	if( maxDistance <= 0.0 || maxDistance > SG_Get_Length(pPoints->Get_Extent().Get_XRange(), pPoints->Get_Extent().Get_YRange()) )
	{
		maxDistance	= SG_Get_Length(pPoints->Get_Extent().Get_XRange(), pPoints->Get_Extent().Get_YRange());	// bounding box' diagonal
	}

	lagDistance	= maxDistance / nClasses;

	Count		.Create(nClasses);
	Variance	.Create(nClasses);

	//-----------------------------------------------------
	for(i=0, n=0; i<pPoints->Get_Count()-nSkip && SG_UI_Process_Set_Progress(n, SG_Get_Square(pPoints->Get_Count()/nSkip)/2); i+=nSkip)
	{
		pPoint	= pPoints->Get_Shape(i);

		if( !pPoint->is_NoData(Attribute) )
		{
			p	= pPoint->Get_Point(0);
			z	= bLog ? log(pPoint->asDouble(Attribute)) : pPoint->asDouble(Attribute);

			for(j=i+nSkip; j<pPoints->Get_Count(); j+=nSkip, n++)
			{
				pPoint	= pPoints->Get_Shape(j);

				if( !pPoint->is_NoData(Attribute) )
				{
					k		= (int)(SG_Get_Distance(p, pPoint->Get_Point(0)) / lagDistance);

					if( k < nClasses )
					{
						Count	[k]	++;
						Variance[k]	+= SG_Get_Square((bLog ? log(pPoint->asDouble(Attribute)) : pPoint->asDouble(Attribute)) - z);
					}
				}
			}
		}
	}

	//-----------------------------------------------------
	pVariogram->Destroy();

	pVariogram->Set_Name(CSG_String::Format(SG_T("%s [%s]"), _TL("Variogram"), pPoints->Get_Name()));

	pVariogram->Add_Field(_TL("Class")		, SG_DATATYPE_Int);		// FIELD_CLASS
	pVariogram->Add_Field(_TL("Distance")	, SG_DATATYPE_Double);	// FIELD_DISTANCE
	pVariogram->Add_Field(_TL("Count")		, SG_DATATYPE_Int);		// FIELD_COUNT
	pVariogram->Add_Field(_TL("Variance")	, SG_DATATYPE_Double);	// FIELD_VAR_EXP
	pVariogram->Add_Field(_TL("Var.cum.")	, SG_DATATYPE_Double);	// FIELD_VAR_CUM
	pVariogram->Add_Field(_TL("Model")		, SG_DATATYPE_Double);	// FIELD_VAR_MODEL

	for(i=0, z=0.0, n=0; i<nClasses; i++)
	{
		if( Count[i] > 0 )
		{
			n	+= (int)Count[i];
			z	+= Variance[i];

			pRecord	= pVariogram->Add_Record();
			pRecord->Set_Value(FIELD_CLASS		, (i + 1));
			pRecord->Set_Value(FIELD_DISTANCE	, (i + 1) * lagDistance);
			pRecord->Set_Value(FIELD_COUNT		, Count[i]);
			pRecord->Set_Value(FIELD_VAR_EXP	, 0.5 * Variance[i] / Count[i]);
			pRecord->Set_Value(FIELD_VAR_CUM	, 0.5 * z / n);
		}
	}

	//-----------------------------------------------------
	return( SG_UI_Process_Get_Okay() );
}
Esempio n. 10
0
//---------------------------------------------------------
bool CTC_Parameter_Base::Get_Parameter(CSG_Grid *pValues, CSG_Grid *pParameter)
{
	DataObject_Set_Colors(pParameter, 10, SG_COLORS_RED_GREY_BLUE, true);

	//-----------------------------------------------------
	if( Parameters("METHOD")->asInt() == 0 )
	{
		m_Kernel.Get_Weighting().Set_Parameters(&Parameters);
		m_Kernel.Get_Weighting().Set_BandWidth(Parameters("SCALE")->asDouble() * m_Kernel.Get_Weighting().Get_BandWidth());
		m_Kernel.Set_Radius(Parameters("SCALE")->asDouble());

		for(int y=0; y<Get_NY() && Set_Progress(y); y++)
		{
			#pragma omp parallel for
			for(int x=0; x<Get_NX(); x++)
			{
				if( pValues->is_NoData(x, y) )
				{
					pParameter->Set_NoData(x, y);
				}
				else
				{
					double	d, w, nTotal = 0.0, nValid = 0.0;

					for(int i=0, ix, iy; i<m_Kernel.Get_Count(); i++)
					{
						if( m_Kernel.Get_Values(i, ix = x, iy = y, d, w, true) && pValues->is_InGrid(ix, iy) )
						{
							nTotal	+= w;

							if( pValues->asInt(ix, iy) != 0 )
							{
								nValid	+= w;
							}
						}
					}

					pParameter->Set_Value(x, y, nTotal > 0.0 ? 100.0 * nValid / nTotal : 0.0);	// make percentage
				}
			}
		}

		m_Kernel.Destroy();
	}

	//-----------------------------------------------------
	else
	{
		double		Cellsize	= Parameters("SCALE")->asInt() * Get_Cellsize();

		if( Cellsize > 0.5 * SG_Get_Length(Get_System()->Get_XRange(), Get_System()->Get_YRange()) )
		{
			Error_Set(_TL("resampling cell size is too large"));

			return( false );
		}

		CSG_Grid	Values(CSG_Grid_System(Cellsize, Get_XMin(), Get_YMin(), Get_XMax(), Get_YMax()), SG_DATATYPE_Float);

		Values.Assign(pValues, GRID_RESAMPLING_Mean_Cells);

		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++)
			{
				double	z, px	= Get_XMin() + x * Get_Cellsize();

				if( pValues->is_NoData(x, y) || !Values.Get_Value(px, py, z, GRID_RESAMPLING_BSpline) )
				{
					pParameter->Set_NoData(x, y);
				}
				else
				{
					pParameter->Set_Value(x, y, 100.0 * z);	// make percentage
				}
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}