Ejemplo 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 );
}
//---------------------------------------------------------
bool CSet_Grid_Georeference::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Parameter_Grid_List	*pGrids	= Parameters("GRIDS")->asGridList();

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

	//-----------------------------------------------------
	double	xMin, yMin, size;

	switch( Parameters("DEFINITION")->asInt() )
	{
	case 0:	// cellsize and lower left center coordinates
		size	= Parameters("SIZE")->asDouble();
		xMin	= Parameters("XMIN")->asDouble();
		yMin	= Parameters("YMIN")->asDouble();
		break;

	case 1:	// cellsize and lower left corner coordinates
		size	= Parameters("SIZE")->asDouble();
		xMin	= Parameters("XMIN")->asDouble() + size * 0.5;
		yMin	= Parameters("YMIN")->asDouble() + size * 0.5;
		break;

	case 2:	// cellsize and upper left center coordinates
		size	= Parameters("SIZE")->asDouble();
		xMin	= Parameters("XMIN")->asDouble();
		yMin	= Parameters("YMAX")->asDouble() - size * Get_NY();
		break;

	case 3:	// cellsize and upper left corner coordinates
		size	= Parameters("SIZE")->asDouble();
		xMin	= Parameters("XMIN")->asDouble() + size * 0.5;
		yMin	= Parameters("YMAX")->asDouble() - size * (0.5 + Get_NY());
		break;

	case 4:	// lower left and upper right center coordinates
		size	= (Parameters("XMAX")->asDouble() - Parameters("XMIN")->asDouble()) / Get_NX();
		xMin	= Parameters("XMIN")->asDouble();
		yMin	= Parameters("YMIN")->asDouble();
		break;

	case 5:	// lower left and upper right corner coordinates
		size	= (Parameters("XMAX")->asDouble() - Parameters("XMIN")->asDouble()) / (Get_NX() + 1);
		xMin	= Parameters("XMIN")->asDouble() + size * 0.5;
		yMin	= Parameters("YMIN")->asDouble() + size * 0.5;
		break;
	}

	//-----------------------------------------------------
	CSG_Grid_System	System;

	if( !System.Assign(size, xMin, yMin, Get_NX(), Get_NY()) )
	{
		return( false );
	}

	//-----------------------------------------------------
	Parameters("REFERENCED")->asGridList()->Del_Items();

	for(int i=0; i<pGrids->Get_Count() && Process_Get_Okay(); i++)
	{
		CSG_Grid	*pGrid	= pGrids->asGrid(i);
		CSG_Grid	*pReferenced	= SG_Create_Grid(System, pGrid->Get_Type());

		pReferenced->Set_Name(pGrid->Get_Name());
		pReferenced->Set_Unit(pGrid->Get_Unit());
		pReferenced->Set_Scaling(pGrid->Get_Scaling(), pGrid->Get_Offset());
		pReferenced->Set_NoData_Value_Range(pGrid->Get_NoData_Value(), pGrid->Get_NoData_hiValue());
		pReferenced->Get_MetaData  ()	= pGrid->Get_MetaData  ();
		pReferenced->Get_Projection()	= pGrid->Get_Projection();

		for(int y=0; y<Get_NY() && Set_Progress(y); y++)
		{
			#pragma omp parallel for
			for(int x=0; x<Get_NX(); x++)
			{
				pReferenced->Set_Value(x, y, pGrid->asDouble(x, y));
			}
		}

		Parameters("REFERENCED")->asGridList()->Add_Item(pReferenced);
	}

	//-----------------------------------------------------
	return( true );
}
Ejemplo n.º 3
0
//---------------------------------------------------------
bool CGCS_Grid_Longitude_Range::On_Execute(void)
{
	CSG_Parameter_Grid_List	*pInput		= Parameters("INPUT" )->asGridList();
	CSG_Parameter_Grid_List	*pOutput	= Parameters("OUTPUT")->asGridList();

	if( pInput->Get_Count() <= 0 )
	{
		Message_Dlg(_TL("nothing to do: no data in selection"));

		return( false );
	}

	pOutput->Del_Items();

	//-----------------------------------------------------
	int				xZero;
	CSG_Grid_System	Target;

	//-----------------------------------------------------
	if( Parameters("DIRECTION")->asInt() == 0 )	// 0 - 360 >> -180 - 180
	{
		if( Get_XMax() <= 180.0 )
		{
			Message_Add(_TL("Nothing to do. Raster is already within target range."));

			return( true );
		}
		else if( Get_XMin() >= 180.0 )
		{
			xZero	= 0;

			Target.Assign(Get_Cellsize(), Get_XMin() - 360.0, Get_YMin(), Get_NX(), Get_NY());
		}
		else if( Get_XMax() - 360.0 < Get_XMin() - Get_Cellsize() )
		{
			Error_Set(_TL("Nothing to do be done. Raster splitting is not supported."));

			return( false );
		}
		else
		{
			xZero	= (int)(0.5 + 180.0 / Get_Cellsize());

			Target.Assign(Get_Cellsize(), Get_XMin() - 180.0, Get_YMin(), Get_NX(), Get_NY());
		}
	}

	//-----------------------------------------------------
	else										// -180 - 180 >> 0 - 360
	{
		if( Get_XMin() >= 0.0 )
		{
			Message_Add(_TL("Nothing to do. Raster is already within target range."));

			return( true );
		}
		else if( Get_XMax() <= 0.0 )
		{
			xZero	= 0;

			Target.Assign(Get_Cellsize(), Get_XMin() + 360.0, Get_YMin(), Get_NX(), Get_NY());
		}
		else if( Get_XMin() + 360.0 > Get_XMax() + Get_Cellsize() )
		{
			Error_Set(_TL("Nothing to do be done. Raster splitting is not supported."));

			return( false );
		}
		else
		{
			xZero	= (int)(0.5 + 180.0 / Get_Cellsize());

			Target.Assign(Get_Cellsize(), Get_XMin() - 180.0, Get_YMin(), Get_NX(), Get_NY());
		}
	}

	//-----------------------------------------------------
	for(int i=0; i<pInput->Get_Count() && Process_Get_Okay(); i++)
	{
		CSG_Grid	*pIn	= pInput->asGrid(i);
		CSG_Grid	*pOut	= SG_Create_Grid(Target, pIn->Get_Type());

		pOut->Set_Name(pIn->Get_Name());
		pOut->Set_NoData_Value_Range(pIn->Get_NoData_Value(), pIn->Get_NoData_hiValue());
		pOut->Set_ZFactor(pIn->Get_ZFactor());

		pOutput->Add_Item(pOut);

		for(int y=0; y<Get_NY() && Set_Progress(y); y++)
		{
			for(int x=0, xx=xZero; x<Get_NX(); x++, xx++)
			{
				if( xx >= Get_NX() )
				{
					xx	= 0;
				}

				pOut->Set_Value(xx, y, pIn->asDouble(x, y));
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
Ejemplo n.º 4
0
bool CCreateGridSystem::On_Execute(void)		
{
	CSG_Grid					*pDummy;
	CSG_Shapes					*pShapes;
	CSG_Rect					extent;
	CSG_Parameter_Shapes_List	*pShapesList;
	CSG_Parameter_Grid_List		*pGridList;
	CSG_Grid_System				System;

	double		xMin, xMax, yMin, yMax, cellsize, offset_x, offset_y, xRange, yRange, n, initVal;
	int			NX, NY, m_extent, m_adjust, i;
	bool		useoff;


	xMin		= Parameters("XMIN")->asDouble();
	yMin		= Parameters("YMIN")->asDouble();
	xMax		= Parameters("XMAX")->asDouble();
	yMax		= Parameters("YMAX")->asDouble();
	NX			= Parameters("NX")->asInt();
	NY			= Parameters("NY")->asInt();
	cellsize	= Parameters("CELLSIZE")->asDouble();
	offset_x	= Parameters("XOFFSET")->asDouble();
	offset_y	= Parameters("YOFFSET")->asDouble();
	useoff		= Parameters("USEOFF")->asBool();
	m_extent	= Parameters("M_EXTENT")->asInt();
	m_adjust	= Parameters("ADJUST")->asInt();
    initVal     = Parameters("INIT")->asDouble();
	pShapesList	= Parameters("SHAPESLIST")->asShapesList();
	pGridList	= Parameters("GRIDLIST")->asGridList();


	if( useoff )
	{
		xMin += offset_x;
		yMin += offset_y;
		xMax += offset_x;
		yMax += offset_y;
	}

	switch( m_extent )
	{
	case 0:					// xMin, yMin, NX, NY

		System.Assign(cellsize, xMin, yMin, NX, NY);
		break;

	case 1:					// xMin, yMin, xMax, yMax

		if( xMin > xMax || yMin > yMax )
		{
			Message_Add(CSG_String::Format(_TL("\nError: Please verify your xMin, yMin, xMax, yMax settings!\n")));
			return false;
		}

		xRange = xMax - xMin;
		yRange = yMax - yMin;

		if( m_adjust == 0 )			// extent to cellsize
		{
			if( modf((xRange/cellsize), &n) != 0.0 )
			{
				NX = (int) floor(xRange / cellsize + 0.5);
				xMax = xMin + NX * cellsize;
			}
			
			if( modf((yRange/cellsize), &n) != 0.0 )
			{
				NY = (int) floor(yRange / cellsize + 0.5);
				yMax = yMin + NY * cellsize;
			}
		}
		else if( m_adjust == 1)		// cellsize to W-E extent
		{
			if( modf((xRange/cellsize), &n) != 0.0 )
			{
				NX = (int) (xRange/cellsize);
				cellsize = xRange/NX;
			}			
			if( modf((yRange/cellsize), &n) != 0.0 )
			{
				NY = (int) floor(yRange / cellsize + 0.5);
				yMax = yMin + NY * cellsize;
			}
		}
		else						// cellsize to S-N extent
		{
			if( modf((yRange/cellsize), &n) != 0.0 )
			{
				NY = (int) (yRange/cellsize);
				cellsize = yRange/NY;
			}	
			if( modf((xRange/cellsize), &n) != 0.0 )
			{
				NX = (int) floor(xRange / cellsize + 0.5);
				xMax = xMin + NX * cellsize;
			}
		}

		System.Assign(cellsize, xMin, yMin, xMax, yMax);
		break;

	case 2:			// Shape(s)

		if( pShapesList == NULL || pShapesList->Get_Count() == 0)
		{
			Message_Add(CSG_String::Format(_TL("\nError: the method Extent by Shape(s) requires shape(s) as input!\n")));
			return false;
		}

		for (i=0; i<pShapesList->Get_Count(); i++)
		{
			pShapes = pShapesList->asShapes(i);
			extent = pShapes->Get_Extent();

			if (i==0)
			{
				xMin = extent.Get_XMin();
				yMin = extent.Get_YMin();
				xMax = extent.Get_XMax();
				yMax = extent.Get_YMax();
			}
			else
			{
				xMin = (extent.Get_XMin() < xMin) ? extent.Get_XMin() : xMin;
				yMin = (extent.Get_YMin() < yMin) ? extent.Get_YMin() : yMin;
				xMax = (extent.Get_XMax() > xMax) ? extent.Get_XMax() : xMax;
				yMax = (extent.Get_YMax() > yMax) ? extent.Get_YMax() : yMax;
			}		
		}

		if( useoff )
		{
			xMin += offset_x;
			xMax += offset_x;
			yMin += offset_y;
			yMax += offset_y;
		}

		xRange = xMax - xMin;
		yRange = yMax - yMin;

		if( m_adjust == 0 )			// extent to cellsize
		{
			if( modf((xRange/cellsize), &n) != 0.0 )
			{
				NX = (int) floor(xRange / cellsize + 0.5);
				xMax = xMin + NX * cellsize;
			}
			
			if( modf((yRange/cellsize), &n) != 0.0 )
			{
				NY = (int) floor(yRange / cellsize + 0.5);
				yMax = yMin + NY * cellsize;
			}
		}
		else if( m_adjust == 1)		// cellsize to W-E extent
		{
			if( modf((xRange/cellsize), &n) != 0.0 )
			{
				NX = (int) (xRange/cellsize);
				cellsize = xRange/NX;
			}	
			if( modf((yRange/cellsize), &n) != 0.0 )
			{
				NY = (int) floor(yRange / cellsize + 0.5);
				yMax = yMin + NY * cellsize;
			}
		}
		else						// cellsize to S-N extent
		{
			if( modf((yRange/cellsize), &n) != 0.0 )
			{
				NY = (int) (yRange/cellsize);
				cellsize = yRange/NY;
			}
			if( modf((xRange/cellsize), &n) != 0.0 )
			{
				NX = (int) floor(xRange / cellsize + 0.5);
				xMax = xMin + NX * cellsize;
			}
		}

		System.Assign(cellsize, xMin, yMin, xMax, yMax);
		break;	

	case 3:				// Grid(s)

		if( pGridList == NULL || pGridList->Get_Count() == 0)
		{
			Message_Add(CSG_String::Format(_TL("\nError: the method Extent by Grid(s) requires grid(s) as input!\n")));
			return false;
		}
				
		for (i=0; i<pGridList->Get_Count(); i++)
		{
			pDummy = pGridList->asGrid(i);
			extent = pDummy->Get_Extent();

			if (i==0)
			{
				xMin = extent.Get_XMin();
				yMin = extent.Get_YMin();
				xMax = extent.Get_XMax();
				yMax = extent.Get_YMax();
			}
			else
			{
				xMin = (extent.Get_XMin() < xMin) ? extent.Get_XMin() : xMin;
				yMin = (extent.Get_YMin() < yMin) ? extent.Get_YMin() : yMin;
				xMax = (extent.Get_XMax() > xMax) ? extent.Get_XMax() : xMax;
				yMax = (extent.Get_YMax() > yMax) ? extent.Get_YMax() : yMax;
			}		
		}

		if( useoff )
		{
			xMin += offset_x;
			xMax += offset_x;
			yMin += offset_y;
			yMax += offset_y;
		}

		xRange = xMax - xMin;
		yRange = yMax - yMin;

		if( m_adjust == 0 )			// extent to cellsize
		{
			if( modf((xRange/cellsize), &n) != 0.0 )
			{
				NX = (int) floor(xRange / cellsize + 0.5);
				xMax = xMin + NX * cellsize;
			}
			
			if( modf((yRange/cellsize), &n) != 0.0 )
			{
				NY = (int) floor(yRange / cellsize + 0.5);
				yMax = yMin + NY * cellsize;
			}
		}
		else if( m_adjust == 1)		// cellsize to W-E extent
		{
			if( modf((xRange/cellsize), &n) != 0.0 )
			{
				NX = (int) (xRange/cellsize);
				cellsize = xRange/NX;
			}			
			if( modf((yRange/cellsize), &n) != 0.0 )
			{
				NY = (int) floor(yRange / cellsize + 0.5);
				yMax = yMin + NY * cellsize;
			}
		}
		else						// cellsize to S-N extent
		{
			if( modf((yRange/cellsize), &n) != 0.0 )
			{
				NY = (int) (yRange/cellsize);
				cellsize = yRange/NY;
			}			
			if( modf((xRange/cellsize), &n) != 0.0 )
			{
				NX = (int) floor(xRange / cellsize + 0.5);
				xMax = xMin + NX * cellsize;
			}
		}

		System.Assign(cellsize, xMin, yMin, xMax, yMax);
		break;
	}
		
	pDummy = SG_Create_Grid(System, SG_DATATYPE_Double);
	pDummy->Assign(initVal);
	pDummy->Set_Name(_TL("Dummy Grid"));
	Parameters("GRID")->Set_Value(pDummy);

	return (true);

}