Esempio n. 1
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 );
}
Esempio n. 2
0
//---------------------------------------------------------
bool CPROJ4_Grid::Set_Grids(CSG_Parameter_Grid_List *pSources, CSG_Parameter_Grid_List *pTargets)
{
	if( !pSources || pSources->Get_Count() < 1 || !pTargets || pTargets->Get_Count() != pSources->Get_Count() || !Set_Inverse() )
	{
		return( false );
	}

	//-------------------------------------------------
	int				x, y, i;
	double			z;
	TSG_Point		Pt_Source, Pt_Target;
	CSG_Grid_System	System;
	CSG_Grid		*pX, *pY;

	System	= pTargets->asGrid(0)->Get_System();

	Init_XY(System, &pX, &pY);

	//-------------------------------------------------
	for(y=0, Pt_Target.y=System.Get_YMin(); y<System.Get_NY() && Set_Progress(y, System.Get_NY()); y++, Pt_Target.y+=System.Get_Cellsize())
	{
		for(x=0, Pt_Target.x=System.Get_XMin(); x<System.Get_NX(); x++, Pt_Target.x+=System.Get_Cellsize())
		{
			Pt_Source	= Pt_Target;

			if( Get_Converted(Pt_Source) )
			{
				if( pX )	pX->Set_Value(x, y, Pt_Source.x);
				if( pY )	pY->Set_Value(x, y, Pt_Source.y);

				for(i=0; i<pSources->Get_Count(); i++)
				{
					if( pSources->asGrid(i)->Get_Value(Pt_Source, z, m_Interpolation) )
					{
						pTargets->asGrid(i)->Set_Value(x, y, z);
					}
				}
			}
		}
	}

	return( true );
}
//---------------------------------------------------------
bool CCRS_Transform_Grid::Get_Target_System(const CSG_Grid_System &System, bool bEdge)
{
	int			x, y, Resolution;
	TSG_Rect	Extent;

	Extent.xMin	= Extent.yMin	= 1.0;
	Extent.xMax	= Extent.yMax	= 0.0;

	Get_MinMax(Extent, System.Get_XMin(), System.Get_YMin());
	Get_MinMax(Extent, System.Get_XMax(), System.Get_YMin());
	Get_MinMax(Extent, System.Get_XMin(), System.Get_YMax());
	Get_MinMax(Extent, System.Get_XMax(), System.Get_YMax());

	Resolution	= 256;

	switch( 1 )
	{
	case 1:	// edges
		{
			double	d;

			int	yStep	= 1 + System.Get_NY() / Resolution;

			for(y=0, d=System.Get_YMin(); y<System.Get_NY(); y+=yStep, d+=yStep*System.Get_Cellsize())
			{
				Get_MinMax(Extent, System.Get_XMin(), d);
				Get_MinMax(Extent, System.Get_XMax(), d);
			}

			int	xStep	= 1 + System.Get_NX() / Resolution;

			for(x=0, d=System.Get_XMin(); x<System.Get_NX(); x+=xStep, d+=xStep*System.Get_Cellsize())
			{
				Get_MinMax(Extent, d, System.Get_YMin());
				Get_MinMax(Extent, d, System.Get_YMax());
			}
		}
		break;

	case 2:	// all cells
		{
			TSG_Point	p;

			int	xStep	= 1 + System.Get_NX() / Resolution;
			int	yStep	= 1 + System.Get_NY() / Resolution;

			for(y=0, p.y=System.Get_YMin(); y<System.Get_NY() && Set_Progress(y, System.Get_NY()); y+=yStep, p.y+=yStep*System.Get_Cellsize())
			{
				for(x=0, p.x=System.Get_XMin(); x<System.Get_NX(); x+=xStep, p.x+=xStep*System.Get_Cellsize())
				{
					Get_MinMax(Extent, p.x, p.y);
				}
			}
		}
		break;
	}

	return(	is_Progress() && Extent.xMin < Extent.xMax && Extent.yMin < Extent.yMax
		&&	m_Grid_Target.Init_User(Extent, System.Get_NY())
		&&	Dlg_Parameters("GET_USER")
	);
}
//---------------------------------------------------------
bool CCRS_Transform_Grid::Transform(CSG_Parameter_Grid_List *pSources, CSG_Parameter_Grid_List *pTargets, const CSG_Grid_System &Target_System)
{
	if( !m_Projector.Set_Inverse(true) || !pTargets || !pSources || pSources->Get_Count() < 1 )
	{
		return( false );
	}

	//-----------------------------------------------------
	CSG_Grid	*pX, *pY;

	if( !Parameters("CREATE_XY")->asBool() )
	{
		pX	= pY	= NULL;
	}
	else
	{
		Parameters("OUT_X")->Set_Value(pX	= SG_Create_Grid(Target_System, SG_DATATYPE_Float));
		pX->Assign_NoData();
		pX->Set_Name(_TL("X-Coordinate"));
		pX->Get_Projection().Create(m_Projector.Get_Target());

		Parameters("OUT_Y")->Set_Value(pY	= SG_Create_Grid(Target_System, SG_DATATYPE_Float));
		pY->Assign_NoData();
		pY->Set_Name(_TL("Y-Coordinate"));
		pY->Get_Projection().Create(m_Projector.Get_Target());
	}

	//-----------------------------------------------------
	bool	bGeogCS_Adjust	= m_Projector.Get_Source().Get_Type() == SG_PROJ_TYPE_CS_Geographic && pSources->asGrid(0)->Get_System().Get_XMax() > 180.0;

	Set_Target_Area(pSources->asGrid(0)->Get_System(), Target_System);

	//-----------------------------------------------------
	int	i, n	= pTargets->Get_Count();

	for(i=0; i<pSources->Get_Count(); i++)
	{
		CSG_Grid	*pSource	= pSources->asGrid(i);
		CSG_Grid	*pTarget	= SG_Create_Grid(Target_System, m_Interpolation == 0 ? pSource->Get_Type() : SG_DATATYPE_Float);

		if( pTarget )
		{
			pTarget->Set_NoData_Value_Range	(pSource->Get_NoData_Value(), pSource->Get_NoData_hiValue());
			pTarget->Set_ZFactor			(pSource->Get_ZFactor());
			pTarget->Set_Name				(CSG_String::Format(SG_T("%s"), pSource->Get_Name()));
			pTarget->Set_Unit				(pSource->Get_Unit());
			pTarget->Assign_NoData();
			pTarget->Get_Projection().Create(m_Projector.Get_Target());

			pTargets->Add_Item(pTarget);
		}
	}

	//-------------------------------------------------
	for(int y=0; y<Target_System.Get_NY() && Set_Progress(y, Target_System.Get_NY()); y++)
	{
		double	yTarget	= Target_System.Get_YMin() + y * Target_System.Get_Cellsize();

		#pragma omp parallel for private(i)
		for(int x=0; x<Target_System.Get_NX(); x++)
		{
			double	z, ySource, xSource	= Target_System.Get_XMin() + x * Target_System.Get_Cellsize();

			if( is_In_Target_Area(x, y) && m_Projector.Get_Projection(xSource, ySource = yTarget) )
			{
				if( pX )	pX->Set_Value(x, y, xSource);
				if( pY )	pY->Set_Value(x, y, ySource);

				if( bGeogCS_Adjust && xSource < 0.0 )
				{
					xSource	+= 360.0;
				}

				for(i=0; i<pTargets->Get_Count(); i++)
				{
					if( pSources->asGrid(i)->Get_Value(xSource, ySource, z, m_Interpolation) )
					{
						pTargets->asGrid(n + i)->Set_Value(x, y, z);
					}
				}
			}
		}
	}

	//-----------------------------------------------------
	m_Target_Area.Destroy();

	return( true );
}
Esempio n. 5
0
//---------------------------------------------------------
bool CCRS_Transform_Grid::Transform(CSG_Parameter_Grid_List *pSources, CSG_Parameter_Grid_List *pTargets, const CSG_Grid_System &Target_System)
{
	if( !m_Projector.Set_Inverse(true) || !pTargets || !pSources || pSources->Get_Count() < 1 )
	{
		return( false );
	}

	//-----------------------------------------------------
	CSG_Grid	*pX, *pY;

	if( (pX = m_Grid_Target.Get_Grid("OUT_X")) != NULL )
	{
		pX->Assign_NoData();
		pX->Set_Name(_TL("X Coordinates"));
		pX->Get_Projection().Create(m_Projector.Get_Target());
	}

	if( (pY = m_Grid_Target.Get_Grid("OUT_Y")) != NULL )
	{
		pY->Assign_NoData();
		pY->Set_Name(_TL("Y Coordinates"));
		pY->Get_Projection().Create(m_Projector.Get_Target());
	}

	//-----------------------------------------------------
	bool	bGeogCS_Adjust	= m_Projector.Get_Source().Get_Type() == SG_PROJ_TYPE_CS_Geographic && pSources->asGrid(0)->Get_System().Get_XMax() > 180.0;

	Set_Target_Area(pSources->asGrid(0)->Get_System(), Target_System);

	bool	bKeepType	= m_Resampling == GRID_RESAMPLING_NearestNeighbour || Parameters("KEEP_TYPE")->asBool();

	//-----------------------------------------------------
	int	i, n	= pTargets->Get_Count();

	for(i=0; i<pSources->Get_Count(); i++)
	{
		CSG_Grid	*pSource	= pSources->asGrid(i);
		CSG_Grid	*pTarget	= SG_Create_Grid(Target_System, bKeepType ? pSource->Get_Type() : SG_DATATYPE_Float);

		if( pTarget )
		{
			pTargets->Add_Item(pTarget);

			pTarget->Set_NoData_Value_Range (pSource->Get_NoData_Value(), pSource->Get_NoData_hiValue());
			pTarget->Set_Scaling            (pSource->Get_Scaling(), pSource->Get_Offset());
			pTarget->Set_Name               (pSource->Get_Name());
			pTarget->Set_Unit               (pSource->Get_Unit());
			pTarget->Get_Projection().Create(m_Projector.Get_Target());
			pTarget->Assign_NoData();

			CSG_Parameters Parms; if( DataObject_Get_Parameters(pSource, Parms) ) { DataObject_Add(pTarget); DataObject_Set_Parameters(pTarget, Parms); }
		}
	}

	//-------------------------------------------------
	for(int y=0; y<Target_System.Get_NY() && Set_Progress(y, Target_System.Get_NY()); y++)
	{
		double	yTarget	= Target_System.Get_YMin() + y * Target_System.Get_Cellsize();

	//	#pragma omp parallel for private(i)
		for(int x=0; x<Target_System.Get_NX(); x++)
		{
			double	z, ySource, xSource	= Target_System.Get_XMin() + x * Target_System.Get_Cellsize();

			if( is_In_Target_Area(x, y) && m_Projector.Get_Projection(xSource, ySource = yTarget) )
			{
				if( bGeogCS_Adjust )
				{
					if( xSource < 0.0 )
					{
						xSource	+= 360.0;
					}
					else if( xSource >= 360.0 )
					{
						xSource	-= 360.0;
					}
				}

				if( pX ) pX->Set_Value(x, y, xSource);
				if( pY ) pY->Set_Value(x, y, ySource);

				for(i=0; i<pSources->Get_Count(); i++)
				{
					if( pSources->asGrid(i)->Get_Value(xSource, ySource, z, m_Resampling, false, true) )
					{
						pTargets->asGrid(n + i)->Set_Value(x, y, z);
					}
				}
			}
		}
	}

	//-----------------------------------------------------
	m_Target_Area.Destroy();

	return( true );
}