//---------------------------------------------------------
double CSG_Variogram::Get_Lag_Distance(CSG_Shapes *pPoints, int Method, int nSkip)
{
	if( Method == 0 )
	{
		CSG_PRQuadTree			QT(pPoints, 0);
		CSG_Simple_Statistics	s;

		double	x, y, z;

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

		for(int iPoint=0; iPoint<pPoints->Get_Count() && ::SG_UI_Process_Set_Progress(iPoint, pPoints->Get_Count()); iPoint+=nSkip)
		{
			TSG_Point	p	= pPoints->Get_Shape(iPoint)->Get_Point(0);

			if( QT.Select_Nearest_Points(p.x, p.y, 2) && QT.Get_Selected_Point(1, x, y, z) && (x != p.x || y != p.y) )
			{
				s.Add_Value(SG_Get_Distance(x, y, p.x, p.y));
			}
		}

		if( s.Get_Count() > 0 && s.Get_Mean() > 0.0 )
		{
			return( s.Get_Mean() );
		}
	}

	return( 0.25 * sqrt((pPoints->Get_Extent().Get_XRange() * pPoints->Get_Extent().Get_YRange()) / pPoints->Get_Count()) );
}
//---------------------------------------------------------
bool CInterpolation_AngularDistance::Get_Value(double x, double y, double &z)
{
	int		i, j, n;

	if( (n = m_Search.Set_Location(x, y)) <= 0 )
	{
		return( false );
	}

	//-----------------------------------------------------
	CSG_Vector	X(n), Y(n), D(n), W(n), Z(n);

	for(i=0; i<n; i++)
	{
		m_Search.Get_Point(i, X[i], Y[i], Z[i]);

		D[i]	= SG_Get_Distance(x, y, X[i], Y[i]);
		W[i]	= m_Weighting.Get_Weight(D[i]);

		if( D[i] <= 0.0 )
		{
			z	= Z[i];

			return( true );
		}
	}

	//-----------------------------------------------------
	CSG_Simple_Statistics	s;

	for(i=0; i<n; i++)
	{
		double	w	= 0.0, t	= 0.0;

		for(j=0; j<n; j++)
		{
			if( j != i )
			{
				t	+= W[j] * (1.0 - ((x - X[i]) * (x - X[j]) + (y - Y[i]) * (y - Y[j])) / (D[i] * D[j]));
				w	+= W[j];
			}
		}

		s.Add_Value(Z[i], W[i] * (1.0 + t / w));
	}

	//-----------------------------------------------------
	z	= s.Get_Mean();

	return( true );
}
//---------------------------------------------------------
bool CGSPoints_Distances::On_Execute(void)
{
    //-----------------------------------------------------
    CSG_Shapes	*pPoints	= Parameters("POINTS")	->asShapes();
    CSG_Table	*pTable		= Parameters("TABLE")	->asTable();

    //-----------------------------------------------------
    CSG_PRQuadTree			QT(pPoints, 0);
    CSG_Simple_Statistics	s;

    double	x, y, z;

    for(int iPoint=0; iPoint<pPoints->Get_Count() && Set_Progress(iPoint, pPoints->Get_Count()); iPoint++)
    {
        TSG_Point	p	= pPoints->Get_Shape(iPoint)->Get_Point(0);

        if( QT.Select_Nearest_Points(p.x, p.y, 2) && QT.Get_Selected_Point(1, x, y, z) && (x != p.x || y != p.y) )
        {
            s.Add_Value(SG_Get_Distance(x, y, p.x, p.y));
        }
    }

    //-----------------------------------------------------
    if( s.Get_Count() > 0 )
    {
        CSG_Table_Record	*pRecord;

        pTable->Destroy();
        pTable->Set_Name(CSG_String::Format(SG_T("%s [%s]"), _TL("Minimum Distance Analysis"), pPoints->Get_Name()));

        pTable->Add_Field(SG_T("NAME")	, SG_DATATYPE_String);
        pTable->Add_Field(SG_T("VALUE")	, SG_DATATYPE_Double);

        SET_VALUE(_TL("Mean Average")		, s.Get_Mean());
        SET_VALUE(_TL("Minimum")			, s.Get_Minimum());
        SET_VALUE(_TL("Maximum")			, s.Get_Maximum());
        SET_VALUE(_TL("Standard Deviation")	, s.Get_StdDev());
        SET_VALUE(_TL("Duplicates")			, pPoints->Get_Count() - s.Get_Count());

        DataObject_Update(pTable, SG_UI_DATAOBJECT_SHOW);

        return( true );
    }

    Message_Dlg(_TL("not enough observations"));

    return( false );
}
//---------------------------------------------------------
bool CShape_Index::Get_Diameters_Feret(CSG_Shape_Polygon *pPolygon, int Field, double dAngle)
{
	CSG_Simple_Statistics	F;

	double	maxDir, maxF90, minDir, minF90;

	TSG_Point	C	= pPolygon->Get_Centroid();	// not really necessary

	for(double Direction=0.0; Direction<M_PI_090; Direction+=dAngle)
	{
		double	sin_a	= sin(Direction);
		double	cos_a	= cos(Direction);

		CSG_Simple_Statistics	sx, sy;

		for(int iPart=0; iPart<pPolygon->Get_Part_Count(); iPart++)
		{
			for(int iPoint=0; iPoint<pPolygon->Get_Point_Count(iPart); iPoint++)
			{
				TSG_Point	P	= pPolygon->Get_Point(iPoint, iPart);

				P.x	-= C.x;	P.y	-= C.y;	// not really necessary

				sx	+= P.x * cos_a - P.y * sin_a;
				sy	+= P.x * sin_a + P.y * cos_a;
			}
		}

		#define CHECK_DIR(s, s90, d)	{ if( !F.Get_Count() ) { minDir = maxDir = d; minF90 = maxF90 = s90.Get_Range(); }\
			else if( F.Get_Minimum() > s.Get_Range() ) { minDir = d; minF90 = s90.Get_Range(); }\
			else if( F.Get_Maximum() < s.Get_Range() ) { maxDir = d; maxF90 = s90.Get_Range(); } F += s.Get_Range(); }

		CHECK_DIR(sx, sy, Direction + M_PI_090);
		CHECK_DIR(sy, sx, Direction           );
	}

	pPolygon->Set_Value(Field + 0, F.Get_Maximum());
	pPolygon->Set_Value(Field + 1, maxDir * M_RAD_TO_DEG);
	pPolygon->Set_Value(Field + 2, F.Get_Minimum());
	pPolygon->Set_Value(Field + 3, minDir * M_RAD_TO_DEG);
	pPolygon->Set_Value(Field + 4, F.Get_Mean());
	pPolygon->Set_Value(Field + 5, maxF90);
	pPolygon->Set_Value(Field + 6, minF90);
	pPolygon->Set_Value(Field + 7, pow(3 * F.Get_Minimum() * F.Get_Maximum(), 1. / 3.));

	return( true );
}
Esempio n. 5
0
//---------------------------------------------------------
bool CWindeffect_Correction::Get_Data(int x, int y, CSG_Matrix &Data, CSG_Simple_Statistics &statsObserved)
{
	for(int i=0; i<m_Kernel.Get_Count(); i++)
	{
		int	ix	= m_Kernel.Get_X(i, x);
		int	iy	= m_Kernel.Get_Y(i, y);

		if( m_pBoundary->is_InGrid(ix, iy) && m_pWind->is_InGrid(ix, iy) && m_pObserved->is_InGrid(ix, iy) )
		{
			CSG_Vector	d(2);

			d[BND]	= m_pBoundary->asDouble(ix, iy);
			d[WND]	= m_pWind    ->asDouble(ix, iy);

			Data.Add_Row(d);

			statsObserved  += m_pObserved->asDouble(ix, iy);
		}
	}

	return( statsObserved.Get_Count() >= 5 );
}
//---------------------------------------------------------
bool CGPS_Track_Aggregation::Set_Statistic(CSG_Table_Record *pAggregate, CSG_Simple_Statistics &Statistic, CSG_Simple_Statistics &Time, int nDropped, bool bVerbose)
{
	if( pAggregate )
	{
		pAggregate	->Set_Value(AGG_PARM   , Statistic.Get_Mean());
		pAggregate	->Set_Value(AGG_TIME   , Time     .Get_Mean());

		if( bVerbose )
		{
			pAggregate	->Set_Value(AGG_MIN    , Statistic.Get_Minimum());
			pAggregate	->Set_Value(AGG_MAX    , Statistic.Get_Maximum());
			pAggregate	->Set_Value(AGG_RANGE  , Statistic.Get_Range  ());
			pAggregate	->Set_Value(AGG_STDDEV , Statistic.Get_StdDev ());
			pAggregate	->Set_Value(AGG_COUNT  , Statistic.Get_Count  ());
			pAggregate	->Set_Value(AGG_DTIME  , Time     .Get_Range  ());
			pAggregate	->Set_Value(AGG_DROPPED, nDropped);
		}

		return( true );
	}

	return( false );
}
//---------------------------------------------------------
bool CMultiBand_Variation::Get_Variation(int x, int y)
{
	if( !m_Mask.is_NoData(x, y) )
	{
		int			iBand, iCell, ix, iy;
		double		iDistance, iWeight, Weights, Distance;
		CSG_Vector	Centroid(m_pBands->Get_Count());

		//-------------------------------------------------
		for(iCell=0, Weights=0.0; iCell<m_Cells.Get_Count(); iCell++)
		{
			if( m_Cells.Get_Values(iCell, ix = x, iy = y, iDistance, iWeight, true) && m_Mask.is_InGrid(ix, iy) )
			{
				for(iBand=0; iBand<m_pBands->Get_Count(); iBand++)
				{
					Centroid[iBand]	+= iWeight * m_pBands->asGrid(iBand)->asDouble(ix, iy);
				}

				Weights			+= iWeight;
			}
		}

		//-------------------------------------------------
		if( Weights > 0.0 )
		{
			CSG_Simple_Statistics	s;

			Centroid	*= 1.0 / Weights;

			for(iCell=0; iCell<m_Cells.Get_Count(); iCell++)
			{
				if( m_Cells.Get_Values(iCell, ix = x, iy = y, iDistance, iWeight, true) && m_Mask.is_InGrid(ix, iy) )
				{
					for(iBand=0, Distance=0.0; iBand<m_pBands->Get_Count(); iBand++)
					{
						Distance	+= SG_Get_Square(Centroid[iBand] - m_pBands->asGrid(iBand)->asDouble(ix, iy));
					}

					s.Add_Value(sqrt(Distance), iWeight);

					if( ix == x && iy == y )
					{
						if( m_pDiff )	m_pDiff->Set_Value(x, y, sqrt(Distance));
					}
				}
			}

			if( m_pMean   )	m_pMean  ->Set_Value(x, y, s.Get_Mean());
			if( m_pStdDev )	m_pStdDev->Set_Value(x, y, s.Get_StdDev());

			return( true );
		}
	}

	//-----------------------------------------------------
	if( m_pMean     )	m_pMean		->Set_NoData(x, y);
	if( m_pStdDev   )	m_pStdDev	->Set_NoData(x, y);
	if( m_pDiff     )	m_pDiff		->Set_NoData(x, y);

	return( false );
}
//---------------------------------------------------------
bool CGSGrid_Residuals::Get_Statistics(int x, int y, bool bCenter)
{
	if( m_pGrid->is_InGrid(x, y) )
	{
		int		i, ix, iy, nLower;
		double	z, iz, id, iw;

		CSG_Simple_Statistics	Statistics;

		for(i=0, nLower=0, z=m_pGrid->asDouble(x, y); i<m_Cells.Get_Count(); i++)
		{
			if( m_Cells.Get_Values(i, ix = x, iy = y, id, iw, true) && (bCenter || id > 0.0) && m_pGrid->is_InGrid(ix, iy) )
			{
				Statistics.Add_Value(iz = m_pGrid->asDouble(ix, iy), iw);

				if( z > iz )
				{
					nLower++;
				}
			}
		}

		//-------------------------------------------------
		if( Statistics.Get_Weights() > 0.0 )
		{
			m_pMean		->Set_Value(x, y, Statistics.Get_Mean());
			m_pDiff		->Set_Value(x, y, z - Statistics.Get_Mean());
			m_pStdDev	->Set_Value(x, y, Statistics.Get_StdDev());
			m_pRange	->Set_Value(x, y, Statistics.Get_Range());
			m_pMin		->Set_Value(x, y, Statistics.Get_Minimum());
			m_pMax		->Set_Value(x, y, Statistics.Get_Maximum());
			m_pDevMean	->Set_Value(x, y, Statistics.Get_StdDev() > 0.0 ? ((z - Statistics.Get_Mean()) / Statistics.Get_StdDev()) : 0.0);
			m_pPercent	->Set_Value(x, y, 100.0 * nLower / (double)Statistics.Get_Count());

			return( true );
		}
	}

	//-----------------------------------------------------
	m_pMean		->Set_NoData(x, y);
	m_pDiff		->Set_NoData(x, y);
	m_pStdDev	->Set_NoData(x, y);
	m_pRange	->Set_NoData(x, y);
	m_pMin		->Set_NoData(x, y);
	m_pMax		->Set_NoData(x, y);
	m_pDevMean	->Set_NoData(x, y);
	m_pPercent	->Set_NoData(x, y);

	return( false );
}
Esempio n. 9
0
//---------------------------------------------------------
bool CGSGrid_Statistics::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Parameter_Grid_List	*pGrids	= Parameters("GRIDS")->asGridList();

	if( pGrids->Get_Count() <= 1 )
	{
		Error_Set(_TL("no grids in selection"));

		return( false );
	}

	//-----------------------------------------------------
	CSG_Grid	*pMean			= Parameters("MEAN"    )->asGrid();
	CSG_Grid	*pMin			= Parameters("MIN"     )->asGrid();
	CSG_Grid	*pMax			= Parameters("MAX"     )->asGrid();
	CSG_Grid	*pRange			= Parameters("RANGE"   )->asGrid();
	CSG_Grid	*pSum			= Parameters("SUM"     )->asGrid();
	CSG_Grid	*pVar			= Parameters("VAR"     )->asGrid();
	CSG_Grid	*pStdDev		= Parameters("STDDEV"  )->asGrid();
	CSG_Grid	*pStdDevLo		= Parameters("STDDEVLO")->asGrid();
	CSG_Grid	*pStdDevHi		= Parameters("STDDEVHI")->asGrid();
	CSG_Grid	*pPercentile	= Parameters("PCTL"    )->asGrid();

	if( !pMean && !pMin && !pMax && !pRange && !pSum && !pVar && !pStdDev && !pStdDevLo && !pStdDevHi && !pPercentile )
	{
		Error_Set(_TL("no parameter output specified"));

		return( false );
	}

	double	dRank	= Parameters("PCTL_VAL")->asDouble() / 100.0;

	//-----------------------------------------------------
	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<Get_NX(); x++)
		{
			CSG_Table				Values;
			CSG_Simple_Statistics	s;

			for(int i=0; i<pGrids->Get_Count(); i++)
			{
				if( !pGrids->asGrid(i)->is_NoData(x, y) )
				{
					double	z	= pGrids->asGrid(i)->asDouble(x, y);

					s.Add_Value(z);

					if( pPercentile )
					{
						if( Values.Get_Field_Count() == 0 )
						{
							Values.Add_Field("Z", SG_DATATYPE_Double);
						}

						Values.Add_Record()->Set_Value(0, z);
					}
				}
			}

			//-----------------------------------------
			if( s.Get_Count() <= 0 )
			{
				if( pMean       )	pMean		->Set_NoData(x, y);
				if( pMin        )	pMin		->Set_NoData(x, y);
				if( pMax        )	pMax		->Set_NoData(x, y);
				if( pRange      )	pRange		->Set_NoData(x, y);
				if( pSum        )	pSum		->Set_NoData(x, y);
				if( pVar        )	pVar		->Set_NoData(x, y);
				if( pStdDev     )	pStdDev		->Set_NoData(x, y);
				if( pStdDevLo   )	pStdDevLo	->Set_NoData(x, y);
				if( pStdDevHi   )	pStdDevHi	->Set_NoData(x, y);
				if( pPercentile )	pPercentile	->Set_NoData(x, y);
			}
			else
			{
				if( pMean       )	pMean		->Set_Value(x, y, s.Get_Mean());
				if( pMin        )	pMin		->Set_Value(x, y, s.Get_Minimum());
				if( pMax        )	pMax		->Set_Value(x, y, s.Get_Maximum());
				if( pRange      )	pRange		->Set_Value(x, y, s.Get_Range());
				if( pSum        )	pSum		->Set_Value(x, y, s.Get_Sum());
				if( pVar        )	pVar		->Set_Value(x, y, s.Get_Variance());
				if( pStdDev     )	pStdDev		->Set_Value(x, y, s.Get_StdDev());
				if( pStdDevLo   )	pStdDevLo	->Set_Value(x, y, s.Get_Mean() - s.Get_StdDev());
				if( pStdDevHi   )	pStdDevHi	->Set_Value(x, y, s.Get_Mean() + s.Get_StdDev());
				if( pPercentile )
				{
					Values.Set_Index(0, TABLE_INDEX_Ascending);

					pPercentile->Set_Value(x, y, Values.Get_Record_byIndex((int)(dRank * s.Get_Count()))->asDouble(0));
				}
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CGrid_Values_AddTo_Shapes::On_Execute(void)
{
	CSG_Parameter_Grid_List	*pGrids;
	CSG_Shapes				*pShapes;

	//-----------------------------------------------------
	pShapes	= Parameters("RESULT")->asShapes();
	pGrids	= Parameters("GRIDS" )->asGridList();

	//-----------------------------------------------------
	if( pGrids->Get_Count() <= 0 )
	{
		return( false );
	}

	//-----------------------------------------------------
	if( pShapes == NULL )
	{
		pShapes		= Parameters("SHAPES")->asShapes();
	}
	else if( pShapes != Parameters("SHAPES")->asShapes() )
	{
		pShapes->Create(*Parameters("SHAPES")->asShapes());
	}

	//-----------------------------------------------------
	switch( Parameters("RESAMPLING")->asInt() )
	{
	default:	m_Resampling	= GRID_RESAMPLING_NearestNeighbour;	break;
	case  1:	m_Resampling	= GRID_RESAMPLING_Bilinear;			break;
	case  2:	m_Resampling	= GRID_RESAMPLING_BicubicSpline;	break;
	case  3:	m_Resampling	= GRID_RESAMPLING_BSpline;			break;
	}

	//-----------------------------------------------------
	for(int iGrid=0; iGrid<pGrids->Get_Count(); iGrid++)
	{
		CSG_Grid	*pGrid	= pGrids->asGrid(iGrid);

		int	Field	= pShapes->Get_Field_Count();

		pShapes->Add_Field(pGrid->Get_Name(), SG_DATATYPE_Double);

		for(int iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++)
		{
			CSG_Simple_Statistics	Statistics;

			CSG_Shape	*pShape	= pShapes->Get_Shape(iShape);

			if( pShape->Get_Extent().Intersects(pGrid->Get_Extent()) )
			{
				switch( pShapes->Get_Type() )
				{
				case SHAPE_TYPE_Point: default:
				case SHAPE_TYPE_Points:		Get_Data_Point  (Statistics, pShape, pGrid);	break;
				case SHAPE_TYPE_Line:		Get_Data_Line   (Statistics, pShape, pGrid);	break;
				case SHAPE_TYPE_Polygon:	Get_Data_Polygon(Statistics, pShape, pGrid);	break;
				}
			}

			if( Statistics.Get_Count() > 0 )
			{
				pShape->Set_Value(Field, Statistics.Get_Mean());
			}
			else
			{
				pShape->Set_NoData(Field);
			}
		}
	}

	//-----------------------------------------------------
	if( pShapes == Parameters("SHAPES")->asShapes() )
	{
		DataObject_Update(pShapes);
	}

	return( true );
}