Exemple #1
0
//---------------------------------------------------------
bool	Copy_RGBGrid_SAGA_to_VIGRA	(CSG_Grid &Grid, BRGBImage &Image, bool bCreate)
{
	if( bCreate )
	{
		Image.resize(Grid.Get_NX(), Grid.Get_NY());
	}

	if( Grid.Get_NX() != Image.width() || Grid.Get_NY() != Image.height() )
	{
		return( false );
	}

	for(int y=0; y<Grid.Get_NY() && SG_UI_Process_Set_Progress(y, Grid.Get_NY()); y++)
	{
		for(int x=0; x<Grid.Get_NX(); x++)
		{
			RGBValue<unsigned char>	rgb(SG_GET_R(Grid.asInt(x, y)), SG_GET_G(Grid.asInt(x, y)), SG_GET_B(Grid.asInt(x, y)));

			Image(x, y)	= rgb;
		}
	}

	SG_UI_Process_Set_Progress(0.0, 1.0);

	return( true );
}
Exemple #2
0
//---------------------------------------------------------
bool	Copy_RGBGrid_VIGRA_to_SAGA	(CSG_Grid &Grid, BRGBImage &Image, bool bCreate)
{
	if( bCreate )
	{
		Grid.Create(Grid.Get_Type(), Image.width(), Image.height());
	}

	if( Grid.Get_NX() != Image.width() || Grid.Get_NY() != Image.height() )
	{
		return( false );
	}

	for(int y=0; y<Grid.Get_NY() && SG_UI_Process_Set_Progress(y, Grid.Get_NY()); y++)
	{
		for(int x=0; x<Grid.Get_NX(); x++)
		{
			RGBValue<unsigned char>	rgb	= Image(x, y);

			Grid.Set_Value(x, y, SG_GET_RGB(rgb.red(), rgb.green(), rgb.blue()));
		}
	}

	SG_UI_Process_Set_Progress(0.0, 1.0);

	return( true );
}
Exemple #3
0
//---------------------------------------------------------
bool CGrid_Plotter::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Formula	Formula;
	
	if( !Formula.Set_Formula(Parameters("FORMULA")->asString()) )
	{
		CSG_String	Message;

		if( !Formula.Get_Error(Message) )
		{
			Message	= _TL("unknown errror parsing formula");
		}

		Error_Set(Message);

		return( false );
	}

	//-----------------------------------------------------
	CSG_Grid	*pFunction	= m_Grid_Target.Get_Grid("FUNCTION");

	if( !pFunction )
	{
		Error_Set(_TL("could not create target grid"));

		return( false );
	}

	//-----------------------------------------------------
	double xMin		= Parameters("X_RANGE")->asRange()->Get_LoVal();
	double xRange	= Parameters("X_RANGE")->asRange()->Get_HiVal() - xMin;

	double yMin		= Parameters("Y_RANGE")->asRange()->Get_LoVal();
	double yRange	= Parameters("Y_RANGE")->asRange()->Get_HiVal() - yMin;

	//-----------------------------------------------------
	for(int y=0; y<pFunction->Get_NY() && Set_Progress(y); y++)
	{
		double	py	= yMin + yRange * (y / (double)pFunction->Get_NY());

		#pragma omp parallel for
		for(int x=0; x<pFunction->Get_NX(); x++)
		{
			double	px	= xMin + xRange * (x / (double)pFunction->Get_NX());

			pFunction->Set_Value(x, y, Formula.Get_Value(SG_T("xy"), px, py));
		}
	}

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CGeoref_Grid::Get_Target_Extent(CSG_Rect &Extent, bool bEdge)
{
	if( Parameters("METHOD")->asInt() == GEOREF_Triangulation )	// triangulation
	{
		return( m_Engine.Get_Reference_Extent(Extent) );
	}

	//-----------------------------------------------------
	CSG_Grid	*pGrid	= Parameters("GRID")->asGrid();

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

	//-----------------------------------------------------
	if( bEdge )
	{
		for(int y=0; y<pGrid->Get_NY(); y++)
		{
			Add_Target_Extent(Extent, pGrid->Get_XMin(), pGrid->Get_System().Get_yGrid_to_World(y));
			Add_Target_Extent(Extent, pGrid->Get_XMax(), pGrid->Get_System().Get_yGrid_to_World(y));
		}

		for(int x=0; x<pGrid->Get_NX(); x++)
		{
			Add_Target_Extent(Extent, pGrid->Get_System().Get_xGrid_to_World(x), pGrid->Get_YMin());
			Add_Target_Extent(Extent, pGrid->Get_System().Get_xGrid_to_World(x), pGrid->Get_YMax());
		}
	}

	//-----------------------------------------------------
	else
	{
		for(int y=0; y<pGrid->Get_NY() && Set_Progress(y, pGrid->Get_NY()); y++)
		{
			for(int x=0; x<pGrid->Get_NX(); x++)
			{
				if( !pGrid->is_NoData(x, y) )
				{
					TSG_Point	p	= pGrid->Get_System().Get_Grid_to_World(x, y);

					Add_Target_Extent(Extent, p.x, p.y);
				}
			}
		}
	}

	return( is_Progress() && Extent.Get_XRange() > 0.0 && Extent.Get_YRange() > 0.0 );
}
Exemple #5
0
//---------------------------------------------------------
double CGridding_Spline_MBA::BA_Get_Value(double x, double y, CSG_Grid &Phi)
{
	int		_x, _y, ix, iy;
	double	z	= 0.0, bx[4], by;

	if(	(_x = (int)x) >= 0 && _x < Phi.Get_NX() - 3
	&&	(_y = (int)y) >= 0 && _y < Phi.Get_NY() - 3 )
	{
		x	-= _x;
		y	-= _y;

		for(ix=0; ix<4; ix++)
		{
			bx[ix]	= BA_Get_B(ix, x);
		}

		for(iy=0; iy<4; iy++)
		{
			by	= BA_Get_B(iy, y);

			for(ix=0; ix<4; ix++)
			{
				z	+= by * bx[ix] * Phi.asDouble(_x + ix, _y + iy);
			}
		}
	}

	return( z );
}
//---------------------------------------------------------
bool CGridding_Spline_MBA_Grid::_Get_Difference(CSG_Grid &Phi)
{
	int				xPoint, yPoint, nErrors;
	double			x, y, z, zMax, zMean;
	TSG_Point_Z	p;
	CSG_String		s;

	//-----------------------------------------------------
	for(yPoint=0, p.y=m_Points.Get_YMin(), zMax=0.0, nErrors=0, zMean=0.0; yPoint<m_Points.Get_NY() && Set_Progress(yPoint, m_Points.Get_NY()); yPoint++, p.y+=m_Points.Get_Cellsize())
	{
		for(xPoint=0, p.x=m_Points.Get_XMin(); xPoint<m_Points.Get_NX(); xPoint++, p.x+=m_Points.Get_Cellsize())
		{
			if( !m_Points.is_NoData(xPoint, yPoint) )
			{
				x	= (p.x - Phi.Get_XMin()) / Phi.Get_Cellsize();
				y	= (p.y - Phi.Get_YMin()) / Phi.Get_Cellsize();
				z	= m_Points.asDouble(xPoint, yPoint) - BA_Get_Value(x, y, Phi);

				m_Points.Set_Value(xPoint, yPoint, z);

				if( (z = fabs(z)) > m_Epsilon )
				{
					nErrors	++;
					zMean	+= fabs(z);

					if( fabs(z) > zMax )
					{
						zMax	= fabs(z);
					}
				}
				else
				{
				//	m_Points.Set_Value(xPoint, yPoint, 0.0);
					m_Points.Set_NoData(xPoint, yPoint);
				}
			}
		}
	}

	if( nErrors > 0 )
	{
		zMean	/= nErrors;
	}

	//-----------------------------------------------------
	int	i	= 1 + (int)(0.5 + log(Phi.Get_NX() - 4.0) / log(2.0));

	s.Printf(SG_T("%s:%d, %s:%d, %s:%f, %s:%f"),
		_TL("level"), i,
		_TL("error"), nErrors,
		_TL("max")	, zMax,
		_TL("mean")	, zMean
	);

	Process_Set_Text(s);
	Message_Add     (s);

	return( zMax >= m_Epsilon && i < m_Level_Max && Process_Get_Okay(false) );
}
//---------------------------------------------------------
bool CPanSharp_Brovey::On_Execute(void)
{
	//-----------------------------------------------------
	TSG_Grid_Resampling	Resampling	= Get_Resampling(Parameters("RESAMPLING")->asInt());

	//-----------------------------------------------------
	CSG_Grid	*pPan	= Parameters("PAN")->asGrid();

	//-----------------------------------------------------
	Process_Set_Text("%s: %s ...", _TL("Resampling"), Parameters("R")->asGrid()->Get_Name());
	CSG_Grid	*pR	= Parameters("R_SHARP")->asGrid();
	pR->Assign  (Parameters("R")->asGrid(), Resampling);
	pR->Set_Name(Parameters("R")->asGrid()->Get_Name());

	Process_Set_Text("%s: %s ...", _TL("Resampling"), Parameters("G")->asGrid()->Get_Name());
	CSG_Grid	*pG	= Parameters("G_SHARP")->asGrid();
	pG->Assign  (Parameters("G")->asGrid(), Resampling);
	pG->Set_Name(Parameters("G")->asGrid()->Get_Name());

	Process_Set_Text("%s: %s ...", _TL("Resampling"), Parameters("B")->asGrid()->Get_Name());
	CSG_Grid	*pB	= Parameters("B_SHARP")->asGrid();
	pB->Assign  (Parameters("B")->asGrid(), Resampling);
	pB->Set_Name(Parameters("B")->asGrid()->Get_Name());

	//-----------------------------------------------------
	Process_Set_Text(_TL("Sharpening"));

	for(int y=0; y<pPan->Get_NY() && Set_Progress(y, pPan->Get_NY()); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<pPan->Get_NX(); x++)
		{
			if( !pPan->is_NoData(x, y) && !pR->is_NoData(x, y) && !pG->is_NoData(x, y) && !pB->is_NoData(x, y) )
			{
				double	k	= (pR->asDouble(x, y) + pG->asDouble(x, y) + pB->asDouble(x, y));

				if( k != 0.0 )
				{
					k	= pPan->asDouble(x, y) / k;
				}

				pR->Mul_Value(x, y, k);
				pG->Mul_Value(x, y, k);
				pB->Mul_Value(x, y, k);
			}
			else
			{
				pR->Set_NoData(x, y);
				pG->Set_NoData(x, y);
				pB->Set_NoData(x, y);
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CCRS_Transform_Grid::Transform(CSG_Parameter_Grid_List *pGrids, CSG_Shapes *pPoints)
{
	if( !pPoints || !pGrids || pGrids->Get_Count() < 1 )
	{
		return( false );
	}

	CSG_Grid	*pGrid	= pGrids->asGrid(0);

	if( !m_Projector.Set_Source(pGrid->Get_Projection()) )
	{
		return( false );
	}

	int			x, y, i;
	TSG_Point	Point;

	pPoints->Create(SHAPE_TYPE_Point, _TL("Points"));
	pPoints->Get_Projection()	= m_Projector.Get_Target();

	for(i=0; i<pGrids->Get_Count(); i++)
	{
		pPoints->Add_Field(pGrids->asGrid(i)->Get_Name(), pGrids->asGrid(i)->Get_Type());
	}

	for(y=0, Point.y=pGrid->Get_YMin(); y<pGrid->Get_NY() && Set_Progress(y, pGrid->Get_NY()); y++, Point.y+=pGrid->Get_Cellsize())
	{
		for(x=0, Point.x=pGrid->Get_XMin(); x<pGrid->Get_NX(); x++, Point.x+=pGrid->Get_Cellsize())
		{
			TSG_Point	Point_Transformed	= Point;

			if( m_Projector.Get_Projection(Point_Transformed) )
			{
				CSG_Shape	*pPoint	= pPoints->Add_Shape();

				pPoint->Add_Point(Point_Transformed);

				for(i=0; i<pGrids->Get_Count(); i++)
				{
					if( !pGrids->asGrid(i)->is_NoData(x, y) )
					{
						pPoint->Set_Value(i, pGrids->asGrid(i)->asDouble(x, y));
					}
					else
					{
						pPoint->Set_NoData(i);
					}
				}
			}
		}
	}

	return( true );
}
Exemple #9
0
//---------------------------------------------------------
bool CPROJ4_Grid::Set_Shapes(CSG_Parameter_Grid_List *pSources, CSG_Shapes *pTarget)
{
	int			x, y, i;
	double		z;
	TSG_Point	Pt_Source, Pt_Target;
	CSG_Grid	*pSource;
	CSG_Shape	*pShape;

	if( pSources && pSources->Get_Count() > 0 && pTarget )
	{
		pSource	= pSources->asGrid(0);

		pTarget->Create(SHAPE_TYPE_Point, CSG_String::Format(SG_T("%s [%s]"), pSource->Get_Name(), Get_Proj_Name().c_str()));

		for(i=0; i<pSources->Get_Count(); i++)
		{
			pTarget->Add_Field(pSources->asGrid(i)->Get_Name(), pSources->asGrid(i)->Get_Type());
		}

		for(y=0, Pt_Source.y=pSource->Get_YMin(); y<pSource->Get_NY() && Set_Progress(y, pSource->Get_NY()); y++, Pt_Source.y+=pSource->Get_Cellsize())
		{
			for(x=0, Pt_Source.x=pSource->Get_XMin(); x<pSource->Get_NX(); x++, Pt_Source.x+=pSource->Get_Cellsize())
			{
				if( !pSource->is_NoData(x, y) )
				{
					Pt_Target	= Pt_Source;

					if( Get_Converted(Pt_Target) )
					{
						pShape	= pTarget->Add_Shape();
						pShape->Add_Point(Pt_Target);

						for(i=0; i<pSources->Get_Count(); i++)
						{
							if( pSources->asGrid(i)->Get_Value(Pt_Source, z, m_Interpolation) )
							{
								pShape->Set_Value(i, z);
							}
							else
							{
								pShape->Set_NoData(i);
							}
						}
					}
				}
			}
		}

		return( true );
	}

	return( false );
}
//---------------------------------------------------------
bool CGridding_Spline_Base::_Get_Points(CSG_Points_Z &Points, bool bInGridOnly)
{
	Points.Clear();

	if( m_bGridPoints )
	{
		int			x, y;
		TSG_Point	p;
		CSG_Grid	*pGrid	= Parameters("GRIDPOINTS")	->asGrid();

		for(y=0, p.y=pGrid->Get_YMin(); y<pGrid->Get_NY() && Set_Progress(y, pGrid->Get_NY()); y++, p.y+=pGrid->Get_Cellsize())
		{
			for(x=0, p.x=pGrid->Get_XMin(); x<pGrid->Get_NX(); x++, p.x+=pGrid->Get_Cellsize())
			{
				if( !pGrid->is_NoData(x, y) && (!bInGridOnly || m_pGrid->is_InGrid_byPos(p)) )
				{
					Points.Add(p.x, p.y, pGrid->asDouble(x, y));
				}
			}
		}
	}
	else
	{
		CSG_Shapes	*pShapes	= Parameters("SHAPES")	->asShapes();
		int			zField		= Parameters("FIELD")	->asInt();

		for(int iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++)
		{
			CSG_Shape	*pShape	= pShapes->Get_Shape(iShape);

			if( !pShape->is_NoData(zField) )
			{
				double		zValue	= pShape->asDouble(zField);

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

						if( !bInGridOnly || m_pGrid->is_InGrid_byPos(p) )
						{
							Points.Add(p.x, p.y, zValue);
						}
					}
				}
			}
		}
	}

	return( Points.Get_Count() >= 3 );
}
Exemple #11
0
//---------------------------------------------------------
bool CGridding_Spline_MBA::_Get_Difference(CSG_Grid &Phi)
{
	int			i, nErrors;
	double		x, y, z, zMax, zMean;
	CSG_String	s;

	//-----------------------------------------------------
	for(i=0, zMax=0.0, nErrors=0, zMean=0.0; i<m_Points.Get_Count(); i++)
	{
		x	= (m_Points[i].x - Phi.Get_XMin()) / Phi.Get_Cellsize();
		y	= (m_Points[i].y - Phi.Get_YMin()) / Phi.Get_Cellsize();
		z	= (m_Points[i].z	= m_Points[i].z - BA_Get_Value(x, y, Phi));

		if( (z = fabs(z)) > m_Epsilon )
		{
			nErrors	++;
			zMean	+= fabs(z);

			if( fabs(z) > zMax )
			{
				zMax	= fabs(z);
			}
		}
		else
		{
			m_Points[i].z	 = 0.0;
		}
	}

	if( nErrors > 0 )
	{
		zMean	/= nErrors;
	}

	//-----------------------------------------------------
	i	= 1 + (int)(0.5 + log(Phi.Get_NX() - 4.0) / log(2.0));

	s.Printf(SG_T("%s:%d, %s:%d, %s:%f, %s:%f"),
		_TL("level"), i,
		_TL("error"), nErrors,
		_TL("max")	, zMax,
		_TL("mean")	, zMean
	);

	Process_Set_Text(s);
	Message_Add     (s);

	return( zMax >= m_Epsilon && i < m_Level_Max && Process_Get_Okay(false) );
}
Exemple #12
0
//---------------------------------------------------------
bool CCRS_Transform_Grid::Set_Target_System(CSG_Parameters *pParameters, int Resolution, bool bEdges)
{
	if( !pParameters || !pParameters->Get_Parameter("SOURCE") || !pParameters->Get_Parameter("CRS_PROJ4") )
	{
		return( false );
	}

	CSG_Grid	*pGrid	= m_bList
		? pParameters->Get_Parameter("SOURCE")->asGridList()->asGrid(0)
		: pParameters->Get_Parameter("SOURCE")->asGrid();

	if( !pGrid || !pGrid->is_Valid() || !pGrid->Get_Projection().is_Okay()
	||  !m_Projector.Set_Target(CSG_Projection(pParameters->Get_Parameter("CRS_PROJ4")->asString(), SG_PROJ_FMT_Proj4))
	||  !m_Projector.Get_Target().is_Okay()
	||  !m_Projector.Set_Source(pGrid->Get_Projection()) )
	{
		return( false );
	}

	//-----------------------------------------------------
	int			x, y;
	TSG_Rect	Extent;

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

	Get_MinMax(Extent, pGrid->Get_XMin(), pGrid->Get_YMin());
	Get_MinMax(Extent, pGrid->Get_XMax(), pGrid->Get_YMin());
	Get_MinMax(Extent, pGrid->Get_XMin(), pGrid->Get_YMax());
	Get_MinMax(Extent, pGrid->Get_XMax(), pGrid->Get_YMax());

	//-----------------------------------------------------
	if( bEdges )	// edges
	{
		double	d;

		int	yStep	= 1 + pGrid->Get_NY() / Resolution;

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

		int	xStep	= 1 + pGrid->Get_NX() / Resolution;

		for(x=0, d=pGrid->Get_XMin(); x<pGrid->Get_NX(); x+=xStep, d+=xStep*pGrid->Get_Cellsize())
		{
			Get_MinMax(Extent, d, pGrid->Get_YMin());
			Get_MinMax(Extent, d, pGrid->Get_YMax());
		}
	}

	//-----------------------------------------------------
	else			// all cells
	{
		TSG_Point	p;

		int	xStep	= 1 + pGrid->Get_NX() / Resolution;
		int	yStep	= 1 + pGrid->Get_NY() / Resolution;

		for(y=0, p.y=pGrid->Get_YMin(); y<pGrid->Get_NY(); y+=yStep, p.y+=yStep*pGrid->Get_Cellsize())
		{
			for(x=0, p.x=pGrid->Get_XMin(); x<pGrid->Get_NX(); x+=xStep, p.x+=xStep*pGrid->Get_Cellsize())
			{
				Get_MinMax(Extent, p.x, p.y);
			}
		}
	}

	return(	Extent.xMin < Extent.xMax && Extent.yMin < Extent.yMax
		&&	m_Grid_Target.Set_User_Defined(pParameters, Extent, pGrid->Get_NY())
		&&  m_Grid_Target.Get_System().is_Valid()
	);
}
//---------------------------------------------------------
bool CPanSharp_IHS::On_Execute(void)
{
	//-----------------------------------------------------
	TSG_Grid_Resampling	Resampling	= Get_Resampling(Parameters("RESAMPLING")->asInt());

	//-----------------------------------------------------
	int			y;

	CSG_Grid	*pPan	= Parameters("PAN")->asGrid();

	//-----------------------------------------------------
	Process_Set_Text("%s: %s ...", _TL("Resampling"), Parameters("R")->asGrid()->Get_Name());
	CSG_Grid	*pR	= Parameters("R_SHARP")->asGrid();
	pR->Assign  (Parameters("R")->asGrid(), Resampling);
	pR->Set_Name(Parameters("R")->asGrid()->Get_Name());

	Process_Set_Text("%s: %s ...", _TL("Resampling"), Parameters("G")->asGrid()->Get_Name());
	CSG_Grid	*pG	= Parameters("G_SHARP")->asGrid();
	pG->Assign  (Parameters("G")->asGrid(), Resampling);
	pG->Set_Name(Parameters("G")->asGrid()->Get_Name());

	Process_Set_Text("%s: %s ...", _TL("Resampling"), Parameters("B")->asGrid()->Get_Name());
	CSG_Grid	*pB	= Parameters("B_SHARP")->asGrid();
	pB->Assign  (Parameters("B")->asGrid(), Resampling);
	pB->Set_Name(Parameters("B")->asGrid()->Get_Name());

	//-----------------------------------------------------
	Process_Set_Text(_TL("RGB to IHS"));

	double	rMin	= pR->Get_Min(),	rRange	= pR->Get_Range();
	double	gMin	= pG->Get_Min(),	gRange	= pG->Get_Range();
	double	bMin	= pB->Get_Min(),	bRange	= pB->Get_Range();

	for(y=0; y<pPan->Get_NY() && Set_Progress(y, pPan->Get_NY()); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<pPan->Get_NX(); x++)
		{
			bool	bNoData	= true;

			if( pPan->is_NoData(x, y) || pR->is_NoData(x, y) || pG->is_NoData(x, y) || pB->is_NoData(x, y) )
			{
				pR->Set_NoData(x, y);
				pG->Set_NoData(x, y);
				pB->Set_NoData(x, y);
			}
			else
			{
				double	r	= (pR->asDouble(x, y) - rMin) / rRange;	if( r < 0.0 ) r = 0.0; else if( r > 1.0 ) r = 1.0;
				double	g	= (pG->asDouble(x, y) - gMin) / gRange;	if( g < 0.0 ) g = 0.0; else if( g > 1.0 ) g = 1.0;
				double	b	= (pB->asDouble(x, y) - bMin) / bRange;	if( b < 0.0 ) b = 0.0; else if( b > 1.0 ) b = 1.0;

				double	h, s, i	= r + g + b;
				
				if( i <= 0.0 )
				{
					h	= 0.0;
					s	= 0.0;
				}
				else
				{
					if( r == g && g == b )			{	h	= 0.0;	}
					else if( b < r && b < g )		{	h	= (g - b) / (i - 3 * b)    ;	}
					else if( r < g && r < b )		{	h	= (b - r) / (i - 3 * r) + 1;	}
					else							{	h	= (r - g) / (i - 3 * g) + 2;	}

					if     ( 0.0 <= h && h < 1.0 )	{	s	= (i - 3 * b) / i;	}
					else if( 1.0 <= h && h < 2.0 )	{	s	= (i - 3 * r) / i;	}
					else							{	s	= (i - 3 * g) / i;	}
				}

				pR->Set_Value(x, y, i);
				pG->Set_Value(x, y, s);
				pB->Set_Value(x, y, h);
			}
		}
	}

	//-----------------------------------------------------
	double	Offset_Pan, Offset, Scale;

	if( Parameters("PAN_MATCH")->asInt() == 0 )
	{
		Offset_Pan	= pPan->Get_Min();
		Offset		= pR->Get_Min();
		Scale		= pR->Get_Range() / pPan->Get_Range();
	}
	else
	{
		Offset_Pan	= pPan->Get_Mean();
		Offset		= pR->Get_Mean();
		Scale		= pR->Get_StdDev() / pPan->Get_StdDev();
	}

	//-----------------------------------------------------
	Process_Set_Text(_TL("IHS to RGB"));

	for(y=0; y<pPan->Get_NY() && Set_Progress(y, pPan->Get_NY()); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<pPan->Get_NX(); x++)
		{
			if( !pR->is_NoData(x, y) )
			{
				double	i	= Offset + Scale * (pPan->asDouble(x, y) - Offset_Pan);
				double	s	= pG  ->asDouble(x, y);
				double	h	= pB  ->asDouble(x, y);

				double	r, g, b;

				if     ( 0.0 <= h && h < 1.0 )
				{
					r	= i * (1 + 2 * s - 3 * s * h) / 3;
					g	= i * (1 -     s + 3 * s * h) / 3;
					b	= i * (1 -     s            ) / 3;
				}
				else if( 1.0 <= h && h < 2.0 )
				{
					r	= i * (1 -     s                  ) / 3;
					g	= i * (1 + 2 * s - 3 * s * (h - 1)) / 3;
					b	= i * (1 -     s + 3 * s * (h - 1)) / 3;
				}
				else
				{
					r	= i * (1 -     s + 3 * s * (h - 2)) / 3;
					g	= i * (1 -     s                  ) / 3;
					b	= i * (1 + 2 * s - 3 * s * (h - 2)) / 3;
				}

				pR->Set_Value(x, y, rMin + r * rRange);
				pG->Set_Value(x, y, gMin + g * gRange);
				pB->Set_Value(x, y, bMin + b * bRange);
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
Exemple #14
0
//---------------------------------------------------------
bool CGrid_Import::On_Execute(void)
{
	bool				bTransform;
	int					x, y, yy, Method;
	double				ax, ay, dx, dy, rx, ry, xMin, yMin, Cellsize;
	CSG_Colors			Colors;
	CSG_String			fImage, fWorld, Name;
	CSG_Grid			*pImage;
	CSG_File			Stream;
	wxImage				Image;
	wxImageHistogram	Histogram;

	//-----------------------------------------------------
	fImage	= Parameters("FILE")	->asString();
	Method	= Parameters("METHOD")	->asInt();

	Name	= SG_File_Get_Name(fImage, false);

	//-----------------------------------------------------
	wxImageHandler	*pImgHandler = NULL;

	if( !SG_UI_Get_Window_Main() )
	{
		CSG_String	fName = SG_File_Get_Name(fImage, true);

		if( SG_File_Cmp_Extension(fName, SG_T("jpg")) )
			pImgHandler = new wxJPEGHandler;
		else if( SG_File_Cmp_Extension(fName, SG_T("pcx")) )
			pImgHandler = new wxPCXHandler;
		else if( SG_File_Cmp_Extension(fName, SG_T("tif")) )
			pImgHandler = new wxTIFFHandler;
		else if( SG_File_Cmp_Extension(fName, SG_T("gif")) )
			pImgHandler = new wxGIFHandler;
		else if( SG_File_Cmp_Extension(fName, SG_T("pnm")) )
			pImgHandler = new wxPNMHandler;
		else if( SG_File_Cmp_Extension(fName, SG_T("xpm")) )
			pImgHandler = new wxXPMHandler;
#ifdef _SAGA_MSW
		else if( SG_File_Cmp_Extension(fName, SG_T("bmp")) )
			pImgHandler = new wxBMPHandler;
#endif
		else // if( SG_File_Cmp_Extension(fName, SG_T("png")) )
			pImgHandler = new wxPNGHandler;

		wxImage::AddHandler(pImgHandler);
	}

	if( !Image.LoadFile(fImage.c_str()) )
	{
		return( false );
	}

	//-----------------------------------------------------
	     if( SG_File_Cmp_Extension(fImage, SG_T("bmp")) )
	{
		fWorld	= SG_File_Make_Path(NULL, fImage, SG_T("bpw"));
	}
	else if( SG_File_Cmp_Extension(fImage, SG_T("jpg")) )
	{
		fWorld	= SG_File_Make_Path(NULL, fImage, SG_T("jgw"));
	}
	else if( SG_File_Cmp_Extension(fImage, SG_T("png")) )
	{
		fWorld	= SG_File_Make_Path(NULL, fImage, SG_T("pgw"));
	}
	else if( SG_File_Cmp_Extension(fImage, SG_T("tif")) )
	{
		fWorld	= SG_File_Make_Path(NULL, fImage, SG_T("tfw"));
	}
	else
	{
		fWorld	= SG_File_Make_Path(NULL, fImage, SG_T("world"));
	}

	bTransform	= false;
	xMin		= 0.0;
	yMin		= 0.0;
	Cellsize	= 1.0;

	if(	Stream.Open(fWorld, SG_FILE_R, false) && fscanf(Stream.Get_Stream(), "%lf %lf %lf %lf %lf %lf ", &dx, &ry, &rx, &dy, &ax, &ay) == 6 )
	{
		if( dx != -dy || rx != 0.0 || ry != 0.0 )
		{
			bTransform	= true;
		}
		else
		{
			xMin		= ax;
			yMin		= ay + dy * (Image.GetHeight() - 1);
			Cellsize	= dx;
		}
	}


	//-----------------------------------------------------
	// color look-up table...

	if( Method == 0 && (yy = Image.ComputeHistogram(Histogram)) <= 256 )
	{
		Colors.Set_Count(yy);

		for(wxImageHistogram::iterator i=Histogram.begin(); i!=Histogram.end(); ++i)
		{
			Colors.Set_Color(i->second.index, SG_GET_R(i->first), SG_GET_G(i->first), SG_GET_B(i->first));
		}

		pImage	= SG_Create_Grid(yy <= 2 ? SG_DATATYPE_Bit : SG_DATATYPE_Byte, Image.GetWidth(), Image.GetHeight(), Cellsize, xMin, yMin);

		for(y=0; y<pImage->Get_NY() && Set_Progress(y, pImage->Get_NY()); y++)
		{
			yy	= bTransform ? y : pImage->Get_NY() - 1 - y;

			for(x=0; x<pImage->Get_NX(); x++)
			{
				pImage->Set_Value(x, y, Histogram[SG_GET_RGB(Image.GetRed(x, yy), Image.GetGreen(x, yy), Image.GetBlue(x, yy))].index);
			}
		}

		if( bTransform )
		{
			Set_Transformation(&pImage, ax, ay, dx, dy, rx, ry);
		}

		pImage->Set_Name(Name);
		pImage->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj")));
		Parameters("OUT_GRID")->Set_Value(pImage);
		DataObject_Set_Colors(pImage, Colors);
		DataObject_Update(pImage, 0, Colors.Get_Count() - 1);
	}

	//-----------------------------------------------------
	else	// true color...
	{
		pImage	= SG_Create_Grid(SG_DATATYPE_Int, Image.GetWidth(), Image.GetHeight(), Cellsize, xMin, yMin);
		pImage	->Set_Name(Name);

		for(y=0; y<pImage->Get_NY() && Set_Progress(y, pImage->Get_NY()); y++)
		{
			yy	= bTransform ? y : pImage->Get_NY() - 1 - y;

			for(x=0; x<pImage->Get_NX(); x++)
			{
				pImage->Set_Value(x, y, SG_GET_RGB(Image.GetRed(x, yy), Image.GetGreen(x, yy), Image.GetBlue(x, yy)));
			}
		}

		if( bTransform )
		{
			Set_Transformation(&pImage, ax, ay, dx, dy, rx, ry);
		}

		//-------------------------------------------------
		if( Method != 1 )	// true color...
		{
			pImage->Get_Projection().Load(fImage, SG_PROJ_FMT_WKT);
			pImage->Set_Name(Name);
			pImage->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj")));
			Parameters("OUT_GRID")->Set_Value(pImage);
			DataObject_Set_Colors(pImage, 100, SG_COLORS_BLACK_WHITE);
			DataObject_Set_Parameter(pImage, "COLORS_TYPE", 6);	// Color Classification Type: RGB
		}

		//-------------------------------------------------
		else				// split channels...
		{
			CSG_Grid	*pR, *pG, *pB;

			pR	= SG_Create_Grid(pImage->Get_System(), SG_DATATYPE_Byte);
			pG	= SG_Create_Grid(pImage->Get_System(), SG_DATATYPE_Byte);
			pB	= SG_Create_Grid(pImage->Get_System(), SG_DATATYPE_Byte);

			for(y=0; y<pImage->Get_NY() && Set_Progress(y, pImage->Get_NY()); y++)
			{
				for(x=0; x<pImage->Get_NX(); x++)
				{
					pR->Set_Value(x, y, SG_GET_R(pImage->asInt(x, y)));
					pG->Set_Value(x, y, SG_GET_G(pImage->asInt(x, y)));
					pB->Set_Value(x, y, SG_GET_B(pImage->asInt(x, y)));
				}
			}

			pR->Get_Projection().Load(fImage, SG_PROJ_FMT_WKT);
			pG->Get_Projection().Load(fImage, SG_PROJ_FMT_WKT);
			pB->Get_Projection().Load(fImage, SG_PROJ_FMT_WKT);

			pR->Set_Name(CSG_String::Format(SG_T("%s [R]"), Name.c_str()));
			pG->Set_Name(CSG_String::Format(SG_T("%s [G]"), Name.c_str()));
			pB->Set_Name(CSG_String::Format(SG_T("%s [B]"), Name.c_str()));

			pR->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj")));
			pG->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj")));
			pB->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj")));

			Parameters("OUT_RED")	->Set_Value(pR);
			Parameters("OUT_GREEN")	->Set_Value(pG);
			Parameters("OUT_BLUE")	->Set_Value(pB);

			DataObject_Set_Colors(pR, 100, SG_COLORS_BLACK_RED);
			DataObject_Set_Colors(pG, 100, SG_COLORS_BLACK_GREEN);
			DataObject_Set_Colors(pB, 100, SG_COLORS_BLACK_BLUE);
		}
	}

	//-----------------------------------------------------
	if( !SG_UI_Get_Window_Main() && pImgHandler != NULL)
	{
		wxImage::RemoveHandler(pImgHandler->GetName());
	}

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CGrid_Merge::On_Execute(void)
{
	//-----------------------------------------------------
	if( !Initialize() )
	{
		return( false );
	}

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

		Set_Weight(pGrid);

		Get_Match(i > 0 ? pGrid : NULL);

		int	ax	= (int)((pGrid->Get_XMin() - m_pMosaic->Get_XMin()) / m_pMosaic->Get_Cellsize());
		int	ay	= (int)((pGrid->Get_YMin() - m_pMosaic->Get_YMin()) / m_pMosaic->Get_Cellsize());

		//-------------------------------------------------
		if(	is_Aligned(pGrid) )
		{
			Process_Set_Text(CSG_String::Format("[%d/%d] %s: %s", i + 1, m_pGrids->Get_Count(), _TL("copying"), pGrid->Get_Name()));

			int	nx	= pGrid->Get_NX(); if( nx > m_pMosaic->Get_NX() - ax )	nx	= m_pMosaic->Get_NX() - ax;
			int	ny	= pGrid->Get_NY(); if( ny > m_pMosaic->Get_NY() - ay )	ny	= m_pMosaic->Get_NY() - ay;

			for(int y=0; y<ny && Set_Progress(y, ny); y++)
			{
				if( ay + y >= 0 )
				{
					#pragma omp parallel for
					for(int x=0; x<nx; x++)
					{
						if( ax + x >= 0 && !pGrid->is_NoData(x, y) )
						{
							Set_Value(ax + x, ay + y, pGrid->asDouble(x, y), Get_Weight(x, y));
						}
					}
				}
			}
		}

		//-------------------------------------------------
		else
		{
			Process_Set_Text(CSG_String::Format("[%d/%d] %s: %s", i + 1, m_pGrids->Get_Count(), _TL("resampling"), pGrid->Get_Name()));

			if( ax < 0 )	ax	= 0;
			if( ay < 0 )	ay	= 0;

			int	nx	= 1 + m_pMosaic->Get_System().Get_xWorld_to_Grid(pGrid->Get_XMax()); if( nx > m_pMosaic->Get_NX() )	nx	= m_pMosaic->Get_NX();
			int	ny	= 1 + m_pMosaic->Get_System().Get_yWorld_to_Grid(pGrid->Get_YMax()); if( ny > m_pMosaic->Get_NY() )	ny	= m_pMosaic->Get_NY();

			for(int y=ay; y<ny && Set_Progress(y-ay, ny-ay); y++)
			{
				double	py	= m_pMosaic->Get_YMin() + y * m_pMosaic->Get_Cellsize();

				#pragma omp parallel for
				for(int x=ax; x<nx; x++)
				{
					double	px	= m_pMosaic->Get_XMin() + x * m_pMosaic->Get_Cellsize();

					Set_Value(x, y, pGrid, px, py);
				}
			}
		}
	}

	//-----------------------------------------------------
	if( m_Overlap == 4 )	// mean
	{
		for(int y=0; y<m_pMosaic->Get_NY() && Set_Progress(y, m_pMosaic->Get_NY()); y++)
		{
			#pragma omp parallel for
			for(int x=0; x<m_pMosaic->Get_NX(); x++)
			{
				double	w	= m_Weights.asDouble(x, y);

				if( w > 0.0 )
				{
					m_pMosaic->Mul_Value(x, y, 1.0 / w);
				}
			}
		}
	}

	//-----------------------------------------------------
	m_Weight .Destroy();
	m_Weights.Destroy();

	return( true );
}
Exemple #16
0
//---------------------------------------------------------
bool CSurfer_Import::On_Execute(void)
{
	int			x, y, NX, NY;
	short		sValue;
	long		lValue;
	float		*fLine;
	double		*dLine, dValue, DX, DY, xMin, yMin;
	FILE		*Stream;
	CSG_String	fName;
	CSG_Grid	*pGrid;

	//-----------------------------------------------------
	pGrid	= NULL;
	fName	= Parameters("FILE")->asString();

	//-----------------------------------------------------
	if( fName.Length() > 0 && (Stream = fopen(fName.b_str(), "rb")) != NULL )
	{
		fread(&lValue, 1, sizeof(long), Stream);

		//-------------------------------------------------
		// Surfer 7: Binary...

		if( !strncmp((char *)&lValue, "DSRB", 4) )
		{
			fread(&lValue, 1, sizeof(long)	, Stream);			// SectionSize...
			fread(&lValue, 1, sizeof(long)	, Stream);			// Version
			fread(&lValue, 1, sizeof(long)	, Stream);

			if( lValue == 0x44495247 )							// Grid-Header...
			{
				fread(&lValue	, 1, sizeof(long)	, Stream);	// SectionSize...

				fread(&lValue	, 1, sizeof(long)	, Stream);	// NX...
				NY		= (int)lValue;
				fread(&lValue	, 1, sizeof(long)	, Stream);	// NY...
				NX		= (int)lValue;

				fread(&xMin		, 1, sizeof(double)	, Stream);	// xMin...
				fread(&yMin		, 1, sizeof(double)	, Stream);	// yMin...

				fread(&DX		, 1, sizeof(double)	, Stream);	// DX...
				fread(&DY		, 1, sizeof(double)	, Stream);	// DY...

				fread(&dValue	, 1, sizeof(double)	, Stream);	// zMin...
				fread(&dValue	, 1, sizeof(double)	, Stream);	// zMax...

				fread(&dValue	, 1, sizeof(double)	, Stream);	// Rotation (unused)...
				fread(&dValue	, 1, sizeof(double)	, Stream);	// Blank Value...
				fread(&lValue	, 1, sizeof(long)	, Stream);	// ???...

				if( lValue == 0x41544144 )						// Load Binary Double...
				{
					fread(&lValue, 1, sizeof(long)	, Stream);	// SectionSize...

					//-------------------------------------
					if( !feof(Stream) && (pGrid = SG_Create_Grid(SG_DATATYPE_Double, NX, NY, DX, xMin, yMin)) != NULL )
					{
						dLine	= (double *)SG_Malloc(pGrid->Get_NX() * sizeof(double));

						for(y=0; y<pGrid->Get_NY() && !feof(Stream) && Set_Progress(y, pGrid->Get_NY()); y++)
						{
							fread(dLine, pGrid->Get_NX(), sizeof(double), Stream);

							for(x=0; x<pGrid->Get_NX(); x++)
							{
								pGrid->Set_Value(x, y, dLine[x]);
							}
						}

						SG_Free(dLine);
					}
				}
			}
		}

		//-------------------------------------------------
		// Surfer 6: Binary...

		else if( !strncmp((char *)&lValue, "DSBB", 4) )
		{
			fread(&sValue	, 1, sizeof(short)	, Stream);
			NX		= sValue;
			fread(&sValue	, 1, sizeof(short)	, Stream);
			NY		= sValue;

			fread(&xMin		, 1, sizeof(double)	, Stream);
			fread(&dValue	, 1, sizeof(double)	, Stream);	// XMax
			DX		= (dValue - xMin) / (NX - 1.0);

			fread(&yMin		, 1, sizeof(double)	, Stream);
			fread(&dValue	, 1, sizeof(double)	, Stream);	// YMax...
			DY		= (dValue - yMin) / (NY - 1.0);

			fread(&dValue	, 1, sizeof(double)	, Stream);	// ZMin...
			fread(&dValue	, 1, sizeof(double)	, Stream);	// ZMax...

			//---------------------------------------------
			if( !feof(Stream) && (pGrid = SG_Create_Grid(SG_DATATYPE_Float, NX, NY, DX, xMin, yMin)) != NULL )
			{
				fLine	= (float *)SG_Malloc(pGrid->Get_NX() * sizeof(float));

				for(y=0; y<pGrid->Get_NY() && !feof(Stream) && Set_Progress(y, pGrid->Get_NY()); y++)
				{
					fread(fLine, pGrid->Get_NX(), sizeof(float), Stream);

					for(x=0; x<pGrid->Get_NX(); x++)
					{
						pGrid->Set_Value(x, y, fLine[x]);
					}
				}

				SG_Free(fLine);
			}
		}

		//-------------------------------------------------
		// Surfer 6: ASCII...

		else if( !strncmp((char *)&lValue, "DSAA", 4) )
		{
			fscanf(Stream, "%d %d"	, &NX	, &NY);

			fscanf(Stream, "%lf %lf", &xMin	, &dValue);
			DX		= (dValue - xMin) / (NX - 1.0);

			fscanf(Stream, "%lf %lf", &yMin	, &dValue);
			DY		= (dValue - yMin) / (NY - 1.0);

			fscanf(Stream, "%lf %lf", &dValue, &dValue);

			//---------------------------------------------
			if( !feof(Stream) && (pGrid = SG_Create_Grid(SG_DATATYPE_Float, NX, NY, DX, xMin, yMin)) != NULL )
			{
				for(y=0; y<pGrid->Get_NY() && !feof(Stream) && Set_Progress(y, pGrid->Get_NY()); y++)
				{
					for(x=0; x<pGrid->Get_NX(); x++)
					{
						fscanf(Stream, "%lf", &dValue);

						pGrid->Set_Value(x, y, dValue);
					}
				}
			}
		}

		fclose(Stream);
	}

	//-----------------------------------------------------
	if( pGrid )
	{
		pGrid->Set_Name(Parameters("FILE")->asString());
		pGrid->Set_NoData_Value(Parameters("NODATA")->asInt() == 0 ? NODATAVALUE : Parameters("NODATA_VAL")->asDouble());

		Parameters("GRID")->Set_Value(pGrid);
	}

	return( pGrid != NULL );
}
Exemple #17
0
//---------------------------------------------------------
bool CGridding_Spline_MBA::BA_Get_Phi(CSG_Grid &Phi)
{
	int		iPoint, _x, _y, ix, iy;
	double	x, y, z, dx, dy, wxy, wy, SW2, W[4][4];
	CSG_Grid	Delta;

	//-----------------------------------------------------
	Phi		.Assign(0.0);
	Delta	.Create(Phi.Get_System());

	//-----------------------------------------------------
	for(iPoint=0; iPoint<m_Points.Get_Count() && Set_Progress(iPoint, m_Points.Get_Count()); iPoint++)
	{
		x	= (m_Points[iPoint].x - Phi.Get_XMin()) / Phi.Get_Cellsize();
		y	= (m_Points[iPoint].y - Phi.Get_YMin()) / Phi.Get_Cellsize();
		z	=  m_Points[iPoint].z;

		if(	(_x = (int)x) >= 0 && _x < Phi.Get_NX() - 3
		&&	(_y = (int)y) >= 0 && _y < Phi.Get_NY() - 3 )
		{
			dx	= x - _x;
			dy	= y - _y;

			for(iy=0, SW2=0.0; iy<4; iy++)	// compute W[k,l] and Sum[a=0-3, b=0-3](W²[a,b])
			{
				wy	= BA_Get_B(iy, dy);

				for(ix=0; ix<4; ix++)
				{
					wxy	= W[iy][ix]	= wy * BA_Get_B(ix, dx);

					SW2	+= wxy*wxy;
				}
			}

			for(iy=0; iy<4; iy++)
			{
				for(ix=0; ix<4; ix++)
				{
					wxy	= W[iy][ix];

					Delta.Add_Value(_x + ix, _y + iy, wxy*wxy * ((wxy * z) / SW2));	// Numerator
					Phi  .Add_Value(_x + ix, _y + iy, wxy*wxy);						// Denominator
				}
			}
		}
	}

	//-----------------------------------------------------
	for(iy=0; iy<Phi.Get_NY(); iy++)
	{
		for(ix=0; ix<Phi.Get_NX(); ix++)
		{
			if( (z = Phi.asDouble(ix, iy)) != 0.0 )
			{
				Phi.Set_Value(ix, iy, Delta.asDouble(ix, iy) / z);
			}
		}
	}

	//-----------------------------------------------------
	return( true );
}
Exemple #18
0
//---------------------------------------------------------
bool CWRF_Import::Load(const CSG_String &File)
{
	//-----------------------------------------------------
	// 00001-00600.00001-00600
	// 01234567890123456789012

	CSG_String	Name	= SG_File_Get_Name(File, true);

	if( Name.Length() != 23 || Name[5] != SG_T('-') || Name[11] != SG_T('.') || Name[17] != SG_T('-') )
	{
		Error_Set(_TL("invalid geogrid file name"));

		return( false );
	}

	int	xOffset	= Name.asInt() - 1;
	int	yOffset	= Name.AfterFirst(SG_T('.')).asInt() - 1;

	//-----------------------------------------------------
	CSG_File	Stream;

	if( !Stream.Open(File, SG_FILE_R) )
	{
		Error_Set(_TL("data file could not be openend"));

		return( false );
	}

	//-----------------------------------------------------
	TSG_Data_Type	Type;

	switch( m_Index.m_WORDSIZE )
	{
	default:
		Error_Set(_TL("invalid word size"));

		return( false );

	case 1:	Type = m_Index.m_SIGNED == false ? SG_DATATYPE_Byte  : SG_DATATYPE_Char;  break;
	case 2:	Type = m_Index.m_SIGNED == false ? SG_DATATYPE_Word  : SG_DATATYPE_Short; break;
	case 4:	Type = m_Index.m_SIGNED == false ? SG_DATATYPE_DWord : SG_DATATYPE_Int;   break;
	}

	//-----------------------------------------------------
	char	*pLine, *pValue;
	int		x, y, nBytes_Line;

	nBytes_Line	= (m_Index.m_TILE_X + 2 * m_Index.m_TILE_BDR) * m_Index.m_WORDSIZE;
	pLine		= (char *)SG_Malloc(nBytes_Line);

	//-----------------------------------------------------
	for(int z=m_Index.m_TILE_Z_START; z<=m_Index.m_TILE_Z_END && !Stream.is_EOF() && Process_Get_Okay(); z++)
	{
		CSG_Grid	*pGrid	= SG_Create_Grid(
			Type,
			m_Index.m_TILE_X + 2 * m_Index.m_TILE_BDR,
			m_Index.m_TILE_Y + 2 * m_Index.m_TILE_BDR,
			m_Index.m_DX,
			m_Index.m_KNOWN_LON + (xOffset - m_Index.m_TILE_BDR) * m_Index.m_DX,
			m_Index.m_KNOWN_LAT + (yOffset - m_Index.m_TILE_BDR) * m_Index.m_DY
		);

		pGrid->Set_Name			(CSG_String::Format(SG_T("%s_%02d"), SG_File_Get_Name(File, false).c_str(), z));
		pGrid->Set_Description	(m_Index.m_DESCRIPTION);
		pGrid->Set_Unit			(m_Index.m_UNITS);
		pGrid->Set_NoData_Value	(m_Index.m_MISSING_VALUE);
		pGrid->Set_Scaling		(m_Index.m_SCALE_FACTOR);

		Parameters("GRIDS")->asGridList()->Add_Item(pGrid);

		//-------------------------------------------------
		for(y=0; y<pGrid->Get_NY() && !Stream.is_EOF() && Set_Progress(y, pGrid->Get_NY()); y++)
		{
			int	yy	= m_Index.m_ROW_ORDER == VAL_TOP_BOTTOM ? pGrid->Get_NY() - 1 - y : y;

			Stream.Read(pLine, sizeof(char), nBytes_Line);

			for(x=0, pValue=pLine; x<pGrid->Get_NX(); x++, pValue+=m_Index.m_WORDSIZE)
			{
				if( m_Index.m_ENDIAN == VAL_ENDIAN_BIG )
				{
					SG_Swap_Bytes(pValue, m_Index.m_WORDSIZE);
				}

				switch( pGrid->Get_Type() )
				{
				case SG_DATATYPE_Byte:		pGrid->Set_Value(x, yy, *(unsigned char  *)pValue);	break;	// 1 Byte Integer (unsigned)
				case SG_DATATYPE_Char:		pGrid->Set_Value(x, yy, *(signed char    *)pValue);	break;	// 1 Byte Integer (signed)
				case SG_DATATYPE_Word:		pGrid->Set_Value(x, yy, *(unsigned short *)pValue);	break;	// 2 Byte Integer (unsigned)
				case SG_DATATYPE_Short:		pGrid->Set_Value(x, yy, *(signed short   *)pValue);	break;	// 2 Byte Integer (signed)
				case SG_DATATYPE_DWord:		pGrid->Set_Value(x, yy, *(unsigned int   *)pValue);	break;	// 4 Byte Integer (unsigned)
				case SG_DATATYPE_Int:		pGrid->Set_Value(x, yy, *(signed int     *)pValue);	break;	// 4 Byte Integer (signed)
				}
			}
		}
	}

	//-----------------------------------------------------
	SG_Free(pLine);

	return( true );
}
Exemple #19
0
//---------------------------------------------------------
bool CWRF_Export::Save(const CSG_String &Directory, CSG_Parameter_Grid_List *pGrids)
{
	//-----------------------------------------------------
	// 00001-00600.00001-00600
	// 01234567890123456789012

	int	xOffset	= m_Index.m_TILE_BDR + (int)(0.5 + (Get_XMin() - m_Index.m_KNOWN_LON) / Get_Cellsize());
	int	yOffset	= m_Index.m_TILE_BDR + (int)(0.5 + (Get_YMin() - m_Index.m_KNOWN_LAT) / Get_Cellsize());

	CSG_String	Name	= SG_File_Get_Name(Directory, true);

	Name.Printf(SG_T("%05d-%05d.%05d-%05d"), xOffset + 1, xOffset + m_Index.m_TILE_X, yOffset + 1, yOffset + m_Index.m_TILE_Y);

	//-----------------------------------------------------
	CSG_File	Stream;

	if( !Stream.Open(SG_File_Make_Path(Directory, Name), SG_FILE_W) )
	{
		Error_Set(_TL("data file could not be openend"));

		return( false );
	}

	//-----------------------------------------------------
	char	*pLine, *pValue;
	int		x, y, nBytes_Line;

	nBytes_Line	= Get_NX() * m_Index.m_WORDSIZE;
	pLine		= (char *)SG_Malloc(nBytes_Line);

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

		//-------------------------------------------------
		for(y=0; y<pGrid->Get_NY() && !Stream.is_EOF() && Set_Progress(y, pGrid->Get_NY()); y++)
		{
			int	yy	= m_Index.m_ROW_ORDER == VAL_TOP_BOTTOM ? pGrid->Get_NY() - 1 - y : y;

			for(x=0, pValue=pLine; x<pGrid->Get_NX(); x++, pValue+=m_Index.m_WORDSIZE)
			{
				if( m_Index.m_SIGNED )
				{
					switch( m_Index.m_WORDSIZE )
					{
					case 1:	*((signed char    *)pValue)	= (signed char   )pGrid->asInt(x, yy);	break;
					case 2:	*((signed short   *)pValue)	= (signed short  )pGrid->asInt(x, yy);	break;
					case 4:	*((signed int     *)pValue)	= (signed int    )pGrid->asInt(x, yy);	break;
					}
				}
				else
				{
					switch( m_Index.m_WORDSIZE )
					{
					case 1:	*((unsigned char  *)pValue)	= (unsigned char )pGrid->asInt(x, yy);	break;
					case 2:	*((unsigned short *)pValue)	= (unsigned short)pGrid->asInt(x, yy);	break;
					case 4:	*((unsigned int   *)pValue)	= (unsigned int  )pGrid->asInt(x, yy);	break;
					}
				}

				if( m_Index.m_ENDIAN == VAL_ENDIAN_BIG )
				{
					SG_Swap_Bytes(pValue, m_Index.m_WORDSIZE);
				}
			}

			Stream.Write(pLine, sizeof(char), nBytes_Line);
		}
	}

	//-----------------------------------------------------
	SG_Free(pLine);

	return( true );
}
//---------------------------------------------------------
CSG_Grid * CLandsat_Import::Get_Band(const CSG_String &File)
{
	CSG_Data_Manager	tmpMgr;

	if( !tmpMgr.Add(File) || !tmpMgr.Get_Grid_System(0) || !tmpMgr.Get_Grid_System(0)->Get(0) )
	{
		Error_Set(CSG_String::Format(SG_T("%s: %s"), _TL("could not load file"), File.c_str()));

		return( NULL );
	}

	tmpMgr.Get_Grid_System(0)->Get(0)->Set_NoData_Value(0);	// landsat 8 pretends to use a value of 65535 (2^16 - 1)

	CSG_Grid	*pBand	= NULL;

	//-----------------------------------------------------
	if( !tmpMgr.Get_Grid_System(0)->Get(0)->Get_Projection().is_Okay() )
	{
		// undefined coordinate system, nothing to do be done further...
	}

	//-----------------------------------------------------
	else if( Parameters("PROJECTION")->asInt() == 2 )	// Geographic Coordinates
	{
		pBand	= Get_Projection((CSG_Grid *)tmpMgr.Get_Grid_System(0)->Get(0), "+proj=longlat +ellps=WGS84 +datum=WGS84");
	}

	//-----------------------------------------------------
	else												// UTM
	{
		CSG_Grid	*pTmp	= (CSG_Grid *)tmpMgr.Get_Grid_System(0)->Get(0);

		CSG_String	Projection	= pTmp->Get_Projection().Get_Proj4();

		if( Projection.Find("+proj=utm") >= 0
		&&  (  (Projection.Find("+south") >= 0 && Parameters("PROJECTION")->asInt() == 0)
		    || (Projection.Find("+south") <  0 && Parameters("PROJECTION")->asInt() == 1))
		&&  (pBand = SG_Create_Grid(pTmp->Get_Type(), pTmp->Get_NX(), pTmp->Get_NY(), pTmp->Get_Cellsize(),
				pTmp->Get_XMin(), pTmp->Get_YMin() + (Parameters("PROJECTION")->asInt() == 1 ? 10000000 : -10000000)
			)) != NULL )
		{
			if( Parameters("PROJECTION")->asInt() == 1 )
				Projection.Append (" +south");
			else
				Projection.Replace(" +south", "");

			pBand->Get_Projection().Create(Projection, SG_PROJ_FMT_Proj4);

			pBand->Set_Name              (pTmp->Get_Name());
			pBand->Set_Description       (pTmp->Get_Description());
			pBand->Set_NoData_Value_Range(pTmp->Get_NoData_Value(), pTmp->Get_NoData_hiValue());
			pBand->Set_Scaling           (pTmp->Get_Scaling(), pTmp->Get_Offset());

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

	//-----------------------------------------------------
	if( !pBand )
	{
		pBand	= (CSG_Grid *)tmpMgr.Get_Grid_System(0)->Get(0);

		tmpMgr.Delete(tmpMgr.Get_Grid_System(0)->Get(0), true);	// make permanent, detach from temporary data manager
	}

	return( pBand );
}
//---------------------------------------------------------
bool CSurfer_Import::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_String	File	= Parameters("FILE")->asString();

	FILE	*Stream	 = fopen(File.b_str(), "rb");

	if( !Stream )
	{
		Error_Set(_TL("failed to open file"));

		return( false );
	}

	CSG_Grid	*pGrid	= NULL;

	char	Id[4];	fread(Id, 1, 4 * sizeof(char), Stream);

	//-----------------------------------------------------
	if( !strncmp(Id, "DSRB", 4) )	// Surfer 7: Binary...
	{
		long	lValue, nx, ny;
		double	dValue, dx, dy, xmin, ymin;

		fread(&lValue, 1, sizeof(long), Stream);		// SectionSize...
		fread(&lValue, 1, sizeof(long), Stream);		// Version
		fread(&lValue, 1, sizeof(long), Stream);

		if( lValue == 0x44495247 )						// Grid-Header...
		{
			fread(&lValue, 1, sizeof(long  ), Stream);	// SectionSize...
			fread(&ny    , 1, sizeof(long  ), Stream);
			fread(&nx    , 1, sizeof(long  ), Stream);
			fread(&xmin  , 1, sizeof(double), Stream);
			fread(&ymin  , 1, sizeof(double), Stream);
			fread(&dx    , 1, sizeof(double), Stream);
			fread(&dy    , 1, sizeof(double), Stream);
			fread(&dValue, 1, sizeof(double), Stream);
			fread(&dValue, 1, sizeof(double), Stream);
			fread(&dValue, 1, sizeof(double), Stream);	// Rotation (unused)...
			fread(&dValue, 1, sizeof(double), Stream);	// Blank Value...
			fread(&lValue, 1, sizeof(long  ), Stream);	// ???...

			if( lValue == 0x41544144 )	// Load Binary Double...
			{
				fread(&lValue, 1, sizeof(long), Stream);	// SectionSize...

				//-----------------------------------------
				if( !feof(Stream) && (pGrid = SG_Create_Grid(SG_DATATYPE_Double, nx, ny, dx, xmin, ymin)) != NULL )
				{
					double	*Line	= (double *)SG_Malloc(pGrid->Get_NX() * sizeof(double));

					for(int y=0; y<pGrid->Get_NY() && !feof(Stream) && Set_Progress(y, pGrid->Get_NY()); y++)
					{
						fread(Line, pGrid->Get_NX(), sizeof(double), Stream);

						for(int x=0; x<pGrid->Get_NX(); x++)
						{
							pGrid->Set_Value(x, y, Line[x]);
						}
					}

					SG_Free(Line);
				}
			}
		}
	}

	//-----------------------------------------------------
	else if( !strncmp(Id, "DSBB", 4) )	// Surfer 6: Binary...
	{
		short	nx, ny;
		double	dValue, dx, dy, xmin, ymin;

		fread(&nx    , 1, sizeof(short ), Stream);
		fread(&ny    , 1, sizeof(short ), Stream);
		fread(&xmin  , 1, sizeof(double), Stream);
		fread(&dx    , 1, sizeof(double), Stream);	dx	= (dx - xmin) / (nx - 1.0);	// XMax
		fread(&ymin  , 1, sizeof(double), Stream);
		fread(&dy    , 1, sizeof(double), Stream);	dy	= (dy - ymin) / (ny - 1.0);	// YMax...
		fread(&dValue, 1, sizeof(double), Stream);	// ZMin...
		fread(&dValue, 1, sizeof(double), Stream);	// ZMax...

		//-------------------------------------------------
		if( !feof(Stream) && (pGrid = SG_Create_Grid(SG_DATATYPE_Float, nx, ny, dx, xmin, ymin)) != NULL )
		{
			float	*Line	= (float *)SG_Malloc(pGrid->Get_NX() * sizeof(float));

			for(int y=0; y<pGrid->Get_NY() && !feof(Stream) && Set_Progress(y, pGrid->Get_NY()); y++)
			{
				fread(Line, pGrid->Get_NX(), sizeof(float), Stream);

				for(int x=0; x<pGrid->Get_NX(); x++)
				{
					pGrid->Set_Value(x, y, Line[x]);
				}
			}

			SG_Free(Line);
		}
	}

	//-----------------------------------------------------
	else if( !strncmp(Id, "DSAA", 4) )	// Surfer 6: ASCII...
	{
		int		nx, ny;
		double	dx, dy, xmin, ymin, dValue;

		fscanf(Stream, "%d  %d" , &nx    , &ny    );
		fscanf(Stream, "%lf %lf", &xmin	 , &dx    );	dx	= (dx - xmin) / (nx - 1.0);
		fscanf(Stream, "%lf %lf", &ymin	 , &dy    );	dy	= (dy - ymin) / (ny - 1.0);
		fscanf(Stream, "%lf %lf", &dValue, &dValue);

		//-------------------------------------------------
		if( !feof(Stream) && (pGrid = SG_Create_Grid(SG_DATATYPE_Float, nx, ny, dx, xmin, ymin)) != NULL )
		{
			for(int y=0; y<pGrid->Get_NY() && !feof(Stream) && Set_Progress(y, pGrid->Get_NY()); y++)
			{
				for(int x=0; x<pGrid->Get_NX(); x++)
				{
					fscanf(Stream, "%lf", &dValue);

					pGrid->Set_Value(x, y, dValue);
				}
			}
		}
	}

	//-----------------------------------------------------
	fclose(Stream);

	if( pGrid )
	{
		pGrid->Set_Name(SG_File_Get_Name(File, false));

		pGrid->Set_NoData_Value(Parameters("NODATA")->asInt() == 0 ? NODATAVALUE : Parameters("NODATA_VAL")->asDouble());

		Parameters("GRID")->Set_Value(pGrid);

		return( true );
	}

	return( false );
}
//---------------------------------------------------------
bool CWMS_Import::Get_Map(wxHTTP *pServer, const CSG_String &Directory, CWMS_Capabilities &Cap)
{
	bool	bResult	= false;

	int				i, n;
	CSG_Rect		r(Cap.m_Extent);
	CSG_Parameters	p;

	//-----------------------------------------------------
//	if( Cap.m_MaxWidth  > 2 && NX > Cap.m_MaxWidth  )	NX	= Cap.m_MaxWidth;
//	if( Cap.m_MaxHeight > 2 && NY > Cap.m_MaxHeight )	NY	= Cap.m_MaxHeight;

	p.Add_Range	(NULL	, "X_RANGE"	, _TL("X Range")	, _TL(""), r.Get_XMin(), r.Get_XMax(), r.Get_XMin(), r.Get_XRange() > 0.0, r.Get_XMax(), r.Get_XRange() > 0.0);
	p.Add_Range	(NULL	, "Y_RANGE"	, _TL("Y Range")	, _TL(""), r.Get_YMin(), r.Get_YMax(), r.Get_YMin(), r.Get_YRange() > 0.0, r.Get_YMax(), r.Get_YRange() > 0.0);

	p.Add_Value	(NULL	, "CELLSIZE", _TL("Cellsize")	, _TL(""), PARAMETER_TYPE_Double, r.Get_XRange() / 2001.0, 0.0, true);

	p.Add_Choice(NULL	, "FORMAT"	, _TL("Format")		, _TL(""), Cap.m_Formats);
	p.Add_Choice(NULL	, "PROJ"	, _TL("Projections"), _TL(""), Cap.m_Projections);

	CSG_Parameter	*pNode	= p("FORMAT");
	for(i=0; i<pNode->asChoice()->Get_Count(); i++)
	{
		CSG_String	s(pNode->asChoice()->Get_Item(i));
		if( !s.CmpNoCase(SG_T("image/png")) )
			pNode->Set_Value(i);
	}

	for(i=0; i<Cap.m_Layers_Name.Get_Count(); i++)
	{
		p.Add_Value(NULL, Cap.m_Layers_Name[i], Cap.m_Layers_Title[i], "", PARAMETER_TYPE_Bool, false);
	}

	//-----------------------------------------------------
	if( pServer && Dlg_Parameters(&p, _TL("WMS Import")) )
	{
		int			NX, NY;
		double		Cellsize;
		CSG_String	Layers, Format;

		//-------------------------------------------------
		r.Assign(
			p("X_RANGE")->asRange()->Get_LoVal(),
			p("Y_RANGE")->asRange()->Get_LoVal(),
			p("X_RANGE")->asRange()->Get_HiVal(),
			p("Y_RANGE")->asRange()->Get_HiVal()
		);

		Cellsize	= p("CELLSIZE")	->asDouble();

		NX			= 1 + (int)(r.Get_XRange() / Cellsize);
		NY			= 1 + (int)(r.Get_YRange() / Cellsize);

		//-------------------------------------------------
		Layers.Clear();

		for(i=0, n=0; i<Cap.m_Layers_Name.Get_Count(); i++)
		{
			if( p(Cap.m_Layers_Name[i])->asBool() )
			{
				if( n++ > 0 )	Layers	+= ",";

				Layers	+= Cap.m_Layers_Name[i];
			}
		}

		if( n == 0 )
		{
			return( false );
		}

		//-------------------------------------------------
		wxBitmapType	tFormat;

		Format	= p("FORMAT")->asString();

		if(      Format.Contains(SG_T("image/gif" )) )	tFormat	= wxBITMAP_TYPE_GIF ;
		else if( Format.Contains(SG_T("image/jpeg")) )	tFormat	= wxBITMAP_TYPE_JPEG;
		else if( Format.Contains(SG_T("image/png" )) )	tFormat	= wxBITMAP_TYPE_PNG ;
		else if( Format.Contains(SG_T("image/wbmp")) )	tFormat	= wxBITMAP_TYPE_BMP ;
		else if( Format.Contains(SG_T("image/bmp" )) )	tFormat	= wxBITMAP_TYPE_BMP ;
		else if( Format.Contains(SG_T("image/tiff")) )	tFormat	= wxBITMAP_TYPE_TIF ;
		else if( Format.Contains(SG_T("GIF"       )) )	tFormat	= wxBITMAP_TYPE_GIF ;
		else if( Format.Contains(SG_T("JPEG"      )) )	tFormat	= wxBITMAP_TYPE_JPEG;
		else if( Format.Contains(SG_T("PNG"       )) )	tFormat	= wxBITMAP_TYPE_PNG ;
		else
		{
			return( false );
		}

		//-------------------------------------------------
		CSG_String	sRequest(Directory);

		sRequest	+= SG_T("?SERVICE=WMS");
		sRequest	+= SG_T("&VERSION=")	+ Cap.m_Version;
		sRequest	+= SG_T("&REQUEST=GetMap");

		sRequest	+= SG_T("&LAYERS=")		+ Layers;

		if( Cap.m_Projections.Length() > 0 )
			sRequest	+= CSG_String(S_SRS(Cap.m_Version)) + p("PROJ")->asString();

		sRequest	+= SG_T("&FORMAT=")		+ Format;

		sRequest	+= CSG_String::Format(SG_T("&WIDTH=%d&HEIGHT=%d"), NX, NY);
		sRequest	+= CSG_String::Format(SG_T("&BBOX=%f,%f,%f,%f"), r.m_rect.xMin, r.m_rect.yMin, r.m_rect.xMax, r.m_rect.yMax);

		Message_Add(sRequest, true);

		//-------------------------------------------------
		wxInputStream	*pStream;

		if( (pStream = pServer->GetInputStream(sRequest.c_str())) == NULL )
		{
			Message_Add(_TL("could not open GetMap stream"));
		}
		else
		{
			wxImage	Image;

			if( Image.LoadFile(*pStream, tFormat) == false )
			{
				Message_Add(_TL("could not read image"));

				CSG_String	s	= SG_T("\n");

				pStream->SeekI(0, wxFromStart);

				while( !pStream->Eof() )
				{
					s	+= (char)pStream->GetC();
				}

				Message_Add(s);
			}
			else
			{
				CSG_Grid	*pGrid	= SG_Create_Grid(SG_DATATYPE_Int, Image.GetWidth(), Image.GetHeight(), Cellsize, r.m_rect.xMin, r.m_rect.yMin);

				for(int y=0, yy=pGrid->Get_NY()-1; y<pGrid->Get_NY() && Set_Progress(y, pGrid->Get_NY()); y++, yy--)
				{
					for(int x=0; x<pGrid->Get_NX(); x++)
					{
						pGrid->Set_Value(x, y, SG_GET_RGB(Image.GetRed(x, yy), Image.GetGreen(x, yy), Image.GetBlue(x, yy)));
					}
				}

				//-----------------------------------------
				pGrid->Set_Name(Cap.m_Title);
				Parameters("MAP")->Set_Value(pGrid);
				DataObject_Set_Colors(pGrid, 100, SG_COLORS_BLACK_WHITE);

				CSG_Parameters	Parms;

				if( DataObject_Get_Parameters(pGrid, Parms) && Parms("COLORS_TYPE") )
				{
					Parms("COLORS_TYPE")->Set_Value(3);	// Color Classification Type: RGB

					DataObject_Set_Parameters(pGrid, Parms);
				}

				bResult	= true;
			}

			delete(pStream);
		}
	}

	return( bResult );
}
//---------------------------------------------------------
bool CSurfer_Export::On_Execute(void)
{
	const char	ID_BINARY[]	= "DSBB";

	FILE		*Stream;

	CSG_Grid	*pGrid	= Parameters("GRID")->asGrid();

	CSG_String	File	= Parameters("FILE")->asString();

	bool		bNoData	= Parameters("NODATA")->asBool();

	switch( Parameters("FORMAT")->asInt() )
	{
	//-----------------------------------------------------
	case 0:	// Surfer 6 - Binary...

		if( (Stream = fopen(File.b_str(), "wb")) != NULL )
		{
			short	sValue;
			double	dValue;

			fwrite(ID_BINARY, 4, sizeof(char), Stream);

			sValue	= (short)pGrid->Get_NX  (); fwrite(&sValue, 1, sizeof(short ), Stream);
			sValue	= (short)pGrid->Get_NY  (); fwrite(&sValue, 1, sizeof(short ), Stream);
			dValue	=        pGrid->Get_XMin(); fwrite(&dValue, 1, sizeof(double), Stream);
			dValue	=        pGrid->Get_XMax(); fwrite(&dValue, 1, sizeof(double), Stream);
			dValue	=        pGrid->Get_YMin(); fwrite(&dValue, 1, sizeof(double), Stream);
			dValue	=        pGrid->Get_YMax(); fwrite(&dValue, 1, sizeof(double), Stream);
			dValue	=        pGrid->Get_Min (); fwrite(&dValue, 1, sizeof(double), Stream);
			dValue	=        pGrid->Get_Max (); fwrite(&dValue, 1, sizeof(double), Stream);

			//---------------------------------------------
			float	*fLine	= (float *)SG_Malloc(pGrid->Get_NX() * sizeof(float));

			for(int y=0; y<pGrid->Get_NY() && Set_Progress(y, pGrid->Get_NY()); y++)
			{
				for(int x=0; x<pGrid->Get_NX(); x++)
				{
					fLine[x]	= bNoData && pGrid->is_NoData(x, y) ? NODATAVALUE : pGrid->asFloat(x, y);
				}

				fwrite(fLine, pGrid->Get_NX(), sizeof(float), Stream);
			}

			SG_Free(fLine);

			fclose(Stream);

			return( true );
		}
		break;

	//-----------------------------------------------------
	case 1:	// Surfer - ASCII...

		if( (Stream = fopen(File.b_str(), "w")) != NULL )
		{
			fprintf(Stream, "DSAA\n");
			fprintf(Stream, "%d %d\n", pGrid->Get_NX  (), pGrid->Get_NY  ());
			fprintf(Stream, "%f %f\n", pGrid->Get_XMin(), pGrid->Get_XMax());
			fprintf(Stream, "%f %f\n", pGrid->Get_YMin(), pGrid->Get_YMax());
			fprintf(Stream, "%f %f\n", pGrid->Get_Min (), pGrid->Get_Max ());

			//---------------------------------------------
			for(int y=0; y<pGrid->Get_NY() && Set_Progress(y, pGrid->Get_NY()); y++)
			{
				for(int x=0; x<pGrid->Get_NX(); x++)
				{
					fprintf(Stream, "%f ", bNoData && pGrid->is_NoData(x, y) ? NODATAVALUE : pGrid->asFloat(x, y));
				}

				fprintf(Stream, "\n");
			}

			fclose(Stream);

			return( true );
		}
		break;
	}

	//-----------------------------------------------------
	return( false );
}
//---------------------------------------------------------
CSG_Shape * CWatersheds_ext::Get_Basin(CSG_Grid *pBasins, CSG_Shapes *pPolygons)
{
	int			x, y, nEdges, Basin_ID;
	CSG_Grid	Edge;
	CSG_Shape	*pPolygon	= NULL;

	Basin_ID	= 1 + pPolygons->Get_Count();

	//-----------------------------------------------------
	Edge.Create(SG_DATATYPE_Char, 2 * Get_NX() + 1, 2 * Get_NY() + 1, 0.5 * Get_Cellsize(), Get_XMin() - 0.5 * Get_Cellsize(), Get_YMin() - 0.5 * Get_Cellsize());
	Edge.Set_NoData_Value(0);

	for(y=0, nEdges=0; y<Get_NY() && Process_Get_Okay(); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			if( pBasins->asInt(x, y) == Basin_ID )
			{
				for(int i=0; i<8; i+=2)
				{
					int	ix	= Get_xTo(i, x);
					int	iy	= Get_yTo(i, y);

					if( !is_InGrid(ix, iy) || pBasins->asInt(ix, iy) != Basin_ID )
					{
						ix	= 1 + 2 * x;
						iy	= 1 + 2 * y;

						Edge.Set_Value(               ix,                 iy ,  1);
						Edge.Set_Value(Get_xTo(i    , ix), Get_yTo(i    , iy), -1);
						Edge.Set_Value(Get_xTo(i - 1, ix), Get_yTo(i - 1, iy), -1);

						nEdges++;
					}
				}
			}
		}
	}

	//-----------------------------------------------------
	if( nEdges > 0 )
	{
		for(int yEdge=0; yEdge<Edge.Get_NY(); yEdge++)	for(int xEdge=0; xEdge<Edge.Get_NX(); xEdge++)
		{
			int	i	= 4;

			if( Edge.asInt(xEdge, yEdge) == 1 && Edge.asInt(Get_xTo(i, xEdge), Get_yTo(i, yEdge)) == -1 )
			{
				if( pPolygon == NULL )
				{
					pPolygon	= pPolygons->Add_Shape();
				}

				int	iPart	= pPolygon->Get_Part_Count();
				int	xFirst	= x	= Get_xTo(i, xEdge);
				int	yFirst	= y	= Get_yTo(i, yEdge);
				i	= i + 2;

				pPolygon	->Add_Point(Edge.Get_System().Get_Grid_to_World(x, y), iPart);

				do
				{
					int	ix	= Get_xTo(i + 2, x);
					int	iy	= Get_yTo(i + 2, y);

					if( Edge.is_InGrid(ix, iy) && Edge.asInt(ix, iy) == -1 )			// go right ?
					{
						pPolygon->Add_Point(Edge.Get_System().Get_Grid_to_World(x, y), iPart);

						i	= (i + 2) % 8;
					}
					else
					{
						if( Edge.asInt(ix, iy) == 1 )
						{
							Edge.Set_NoData(ix, iy);	// erase class id in right cells
						}

						ix	= Get_xTo(i, x);
						iy	= Get_yTo(i, y);

						if( Edge.is_InGrid(ix, iy) && Edge.asInt(ix, iy) == -1 )		// go ahead ?
						{
							// nop
						}
						else
						{
							ix	= Get_xTo(i + 6, x);
							iy	= Get_yTo(i + 6, y);

							if( Edge.is_InGrid(ix, iy) && Edge.asInt(ix, iy) == -1 )	// go left ?
							{
								pPolygon->Add_Point(Edge.Get_System().Get_Grid_to_World(x, y), iPart);

								i	= (i + 6) % 8;
							}
							else
							{
								return( false );
							}
						}
					}

					x	= ix;
					y	= iy;
				}
				while( x != xFirst || y != yFirst );

				pPolygon->Add_Point(Edge.Get_System().Get_Grid_to_World(x, y), iPart);
			}
		}
	}

	//-----------------------------------------------------
	return( pPolygon );
}