예제 #1
0
//---------------------------------------------------------
bool CKinWav_D8::Gauges_Initialise(void)
{
	if( m_pGauges_Flow != NULL )
	{
		if( m_pGauges == NULL )
		{
			DataObject_Add(m_pGauges = SG_Create_Shapes(SHAPE_TYPE_Point, _TL("Gauges")));

			Parameters("GAUGES")->Set_Value(m_pGauges);

			m_pGauges->Add_Field(_TL("ID"), SG_DATATYPE_Int);

			for(int y=0; y<Get_NY() && Set_Progress(y); y++)
			{
				for(int x=0; x<Get_NX(); x++)
				{
					bool	bBorder	= false;
					bool	bLowest	= true;

					for(int i=0; i<8; i++)
					{
						int	ix	= Get_xTo(i, x);
						int	iy	= Get_yTo(i, y);

						if( !m_pDEM->is_InGrid(ix, iy) )
						{
							bBorder	= true;
						}
						else if( m_pDEM->asDouble(ix, iy) < m_pDEM->asDouble(x, y) )
						{
							bLowest	= false;
						}
					}

					if( bLowest && bBorder )
					{
						CSG_Shape	*pGauge	= m_pGauges->Add_Shape();

						pGauge->Add_Point(Get_System()->Get_Grid_to_World(x, y));
						pGauge->Set_Value(0, m_pGauges->Get_Count() + 1);
					}
				}
			}
		}

		m_pGauges_Flow->Destroy();
		m_pGauges_Flow->Set_Name(_TL("Outlet Hydrographs"));
		m_pGauges_Flow->Add_Field("TIME", SG_DATATYPE_Double);

		for(int i=0; i<m_pGauges->Get_Count(); i++)
		{
			m_pGauges_Flow->Add_Field(CSG_String::Format(SG_T("GAUGE_%02d"), i + 1), SG_DATATYPE_Double);
		}

		return( true );
	}

	return( false );
}
예제 #2
0
bool CGrid_Aggregate::On_Execute(void)
{
	int x,y;
	int x2,y2;
	int i,j;
	int iNX, iNY;
	int iSize = Parameters("SIZE")->asInt();
	int iMethod = Parameters("METHOD")->asInt();
	double dMin,dMax;
	double dSum;
	double dValue;

	iNX = (int) (Get_NX() / iSize);
	iNY = (int) (Get_NY() / iSize);

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

	CSG_Grid *pOutput = SG_Create_Grid(pGrid->Get_Type(), iNX, iNY, pGrid->Get_Cellsize() * iSize, 
					pGrid->Get_XMin(), pGrid->Get_YMin());

	pOutput->Set_Name(pGrid->Get_Name());

	for (y = 0, y2 = 0; y2 < iNY; y+=iSize, y2++){
		for (x = 0, x2 = 0; x2 < iNX; x+=iSize, x2++){
			dMax = dMin = pGrid->asDouble(x,y);
			dSum = 0;
			for (i = 0; i < iSize; i++){
				for (j = 0; j < iSize; j++){
					dValue = pGrid->asDouble(x+i,y+j);
					if (dValue > dMax){
						dMax = dValue;
					}//if
					if (dValue < dMin){
						dMin = dValue;
					}//if
					dSum += dValue;
				}//for
			}//for
			switch (iMethod){
			case SUM:
				pOutput->Set_Value(x2,y2,dSum);
				break;
			case MIN:
				pOutput->Set_Value(x2,y2,dMin);
				break;
			case MAX:
				pOutput->Set_Value(x2,y2,dMax);
				break;
			default:
				break;
			}
		}//for
	}//for

	DataObject_Add(pOutput);

	return true;

}
예제 #3
0
bool CCreateChartLayer::On_Execute(void){

	
	CSG_Shapes *pInput;
	int i=0;
	int iType;	
	int iSizeField;
	
	if (GetExtraParameters()){

		iSizeField = Parameters("SIZE")->asInt();
		m_fMaxSize = (float) Parameters("MAXSIZE")->asDouble();
		m_fMinSize = (float) Parameters("MINSIZE")->asDouble();

		if (m_fMinSize > m_fMaxSize){
			m_fMinSize = m_fMaxSize;
		}//if

		iType = Parameters("TYPE")->asInt();
		pInput = Parameters("INPUT")->asShapes();
		m_fMaxValue = pInput->Get_Maximum(iSizeField);
		m_fMinValue = pInput->Get_Minimum(iSizeField);

		if (iType == TYPE_PIE){		
			m_pOutput = SG_Create_Shapes(SHAPE_TYPE_Polygon, _TL("Chart (sectors):"));				
		}//if
		else{
			m_pOutput = SG_Create_Shapes(SHAPE_TYPE_Polygon, _TL("Chart (bars):"));
		}//else

		m_pOutput->Add_Field(_TL("Field (ID)"), SG_DATATYPE_Int);
		m_pOutput->Add_Field(_TL("Field (Name)"), SG_DATATYPE_String);


		for (i = 0; i < pInput->Get_Count(); i++){
			if (iType == TYPE_PIE){
		//		AddPieChart(pInput->Get_Shape(i),m_pOutput->Get_Type());
				AddPieChart(pInput->Get_Shape(i),pInput->Get_Type());
			}//if
			else{
		//		AddBarChart(pInput->Get_Shape(i),m_pOutput->Get_Type());
				AddBarChart(pInput->Get_Shape(i),pInput->Get_Type());
			}//else
		}//for
		
		DataObject_Add(m_pOutput, false);

		delete [] m_bIncludeParam;
		
		return true;

	}//if

	delete [] m_bIncludeParam;

	return false;

}//method
예제 #4
0
//---------------------------------------------------------
bool CLife::On_Execute(void)
{
	//-----------------------------------------------------
	m_pLife	= m_Grid_Target.Get_Grid("LIFE", SG_DATATYPE_Byte);

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

		return( false );
	}

	//-----------------------------------------------------
	m_nColors	= Parameters("FADECOLOR")->asInt();

	for(int y=0; y<m_pLife->Get_NY(); y++)
	{
		for(int x=0; x<m_pLife->Get_NX(); x++)
		{
			m_pLife->Set_Value(x, y, CSG_Random::Get_Uniform(0, 100) < 50 ? 0 : m_nColors);
		}
	}

	//-----------------------------------------------------
	m_pLife->Set_Name(_TL("Conway's Game of Life"));
	m_pLife->Set_NoData_Value(-1);

	DataObject_Add       (m_pLife);
	DataObject_Set_Colors(m_pLife, 11, SG_COLORS_WHITE_BLUE);
	DataObject_Update    (m_pLife, 0, m_nColors, SG_UI_DATAOBJECT_SHOW);

	//-----------------------------------------------------
	int		i;

	m_Count.Create(m_pLife->Get_System(), SG_DATATYPE_Byte);

	for(i=1; Process_Get_Okay(true) && Next_Cycle(i > m_nColors); i++)
	{
		Process_Set_Text(CSG_String::Format("%s: %d", _TL("Life Cycle"), i));

		DataObject_Update(m_pLife, 0, m_nColors);
	}

	m_Count.Destroy();

	//-----------------------------------------------------
	if( is_Progress() )
	{
		Message_Add(CSG_String::Format("\n%s %d %s\n", _TL("Dead after"), i, _TL("Life Cycles")), false);
	}

	return( true );
}
//---------------------------------------------------------
void CGrid_3D_Image::_Set_Grid(void)
{
	T3DPoint	*a, *b, *c, *d, p[3];

	//-----------------------------------------------------
	a	= (T3DPoint *)SG_Malloc(sizeof(T3DPoint) *  Get_NX());
	b	= (T3DPoint *)SG_Malloc(sizeof(T3DPoint) *  Get_NX());
	c	= (T3DPoint *)SG_Malloc(sizeof(T3DPoint) * (Get_NX() - 1));

	//-----------------------------------------------------
	_Get_Line(0, b);

	for(int y=1; y<Get_NY() && Set_Progress(y); y++)
	{
		d	= a;
		a	= b;
		b	= d;

		_Get_Line(y, b);
		_Get_Line(a, b, c);

		for(int ax=0, bx=1; bx<Get_NX(); ax++, bx++)
		{
			DRAW_TRIANGLE(a[ax], a[bx], c[ax]);
			DRAW_TRIANGLE(b[ax], b[bx], c[ax]);
			DRAW_TRIANGLE(a[ax], b[ax], c[ax]);
			DRAW_TRIANGLE(a[bx], b[bx], c[ax]);
		}
	}

	//-----------------------------------------------------
	SG_Free(a);
	SG_Free(b);
	SG_Free(c);

	//-----------------------------------------------------
	DataObject_Add(m_pRGB_Z);
	DataObject_Add(m_pRGB);
	DataObject_Set_Colors(m_pRGB, 100, SG_COLORS_BLACK_WHITE);
}
//---------------------------------------------------------
bool CLandsat_Import::On_Execute(void)
{
	CSG_Strings	Files;

	if( !Parameters("FILES")->asFilePath()->Get_FilePaths(Files) || Files.Get_Count() <= 0 )
	{
		return( false );
	}

	//-----------------------------------------------------
	CSG_Parameter_Grid_List	*pBands	= Parameters("BANDS")->asGridList();

	pBands->Del_Items();

	for(int i=0; i<Files.Get_Count(); i++)
	{
		Message_Add(CSG_String::Format(SG_T("%s: %s"), _TL("loading"), SG_File_Get_Name(Files[i], false).c_str()));

		CSG_Grid	*pBand	= Get_Band(Files[i]);

		if( pBand )
		{
			pBands->Add_Item(pBand);

			DataObject_Add(pBand);
			DataObject_Set_Colors(pBand, 11, SG_COLORS_BLACK_WHITE);
		}
	}

	//-----------------------------------------------------
	if( Parameters("SHOW_RGB")->is_Enabled() && Parameters("SHOW_RGB")->asBool() )
	{
		CSG_Grid	*pR	= pBands->asGrid(Parameters("SHOW_R")->asInt());
		CSG_Grid	*pG	= pBands->asGrid(Parameters("SHOW_G")->asInt());
		CSG_Grid	*pB	= pBands->asGrid(Parameters("SHOW_B")->asInt());

		if( pR && pG && pB )
		{
			DataObject_Set_Parameter(pR, "COLORS_TYPE" , 5);	// _TL("RGB Overlay")	// CLASSIFY_OVERLAY
			DataObject_Set_Parameter(pR, "OVERLAY_MODE", 0);	// _TL("red=this, green=1, blue=2")
			DataObject_Set_Parameter(pR, "OVERLAY_G"   , pG);
			DataObject_Set_Parameter(pR, "OVERLAY_B"   , pB);

			DataObject_Update(pR, true);
		}
	}

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
void CGrid_3D_Image::_Set_Shapes(CSG_Shapes *pInput)
{
	int			iShape, iPart, iPoint;
	double		x, y, z, dx, dy;
	T3DPoint	p;
	TSG_Point	Point;
	CSG_Shape		*pShape;
	CSG_Shapes		*pOutput;

	if( pInput && pInput->is_Valid() )
	{
		Process_Set_Text("%s \"%s\"", _TL("Project"), pInput->Get_Name());

		pOutput	= SG_Create_Shapes(*pInput);
		dx		= (double)Get_NX() / Get_System().Get_XRange();
		dy		= (double)Get_NY() / Get_System().Get_YRange();

		for(iShape=0; iShape<pOutput->Get_Count() && Set_Progress(iShape, pOutput->Get_Count()); iShape++)
		{
			pShape	= pOutput->Get_Shape(iShape);

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

					x		= dx * (Point.x - Get_XMin());
					y		= dy * (Point.y - Get_YMin());
					z		= m_pDEM->is_InGrid((int)x, (int)y, true) ? m_pDEM->asDouble((int)x, (int)y) : 0.0;

					_Get_Position(x, y, z, p);

					pShape->Set_Point(p.x, p.y, iPoint, iPart);
				}
			}
		}

		DataObject_Add(pOutput);
	}
}
예제 #8
0
bool CCropToData::On_Execute(void) {

    //CSG_Grid* pGrid = Parameters("GRID")->asGrid();
    CSG_Grid* pCroppedGrid;
    CSG_Grid** pGrids;
    int iGrids;
    int iMinX = 1000000;
    int iMaxX = 0;
    int iMinY = 1000000;
    int iMaxY = 0;
    double dMinX, dMinY;
    int iNX;
    int iNY;
    int i;
    int x,y;

    if (Parameters("INPUT")->asInt() <= 0) {
        Message_Add(_TL("No grids selected"));
        return (false);
    }//if

    iGrids		= Parameters("INPUT")->asInt();
    pGrids		=(CSG_Grid **)Parameters("INPUT")->asPointer();

    for (i = 0 ; i < iGrids ; i++) {
        if (pGrids[i]->is_Compatible(pGrids[0]->Get_System())) {
            for(y=0; y<Get_NY() && Set_Progress(y); y++) {
                for(x=0; x<Get_NX(); x++) {
                    if (!pGrids[i]->is_NoData(x,y)) {
                        if (x<iMinX) {
                            iMinX = x;
                        }//if
                        if (x>iMaxX) {
                            iMaxX = x;
                        }//if
                        if (y<iMinY) {
                            iMinY = y;
                        }//if
                        if (y>iMaxY) {
                            iMaxY = y;
                        }//if
                    }//if
                }//for
            }//for
        }//if
    }//for
    iNX = iMaxX-iMinX+1;
    iNY = iMaxY-iMinY+1;
    dMinX = pGrids[0]->Get_XMin() + iMinX * pGrids[0]->Get_Cellsize();
    dMinY = pGrids[0]->Get_YMin() + iMinY * pGrids[0]->Get_Cellsize();

    if (iNX != pGrids[0]->Get_NX() || iNY != pGrids[0]->Get_NY()) {
        for (i = 0 ; i < iGrids ; i++) {
            if (pGrids[i]->is_Compatible(pGrids[0]->Get_System())) {
                pCroppedGrid = new CSG_Grid(pGrids[i]->Get_Type(), iNX, iNY, pGrids[i]->Get_Cellsize(), dMinX, dMinY);
                pCroppedGrid->Assign(pGrids[i], GRID_INTERPOLATION_BSpline);
                DataObject_Add(pCroppedGrid);
            }//if
        }//for
    }//if

    return true;

}//method
예제 #9
0
//---------------------------------------------------------
bool CWator::On_Execute(void)
{
	//-----------------------------------------------------
	m_pWator	= m_Grid_Target.Get_Grid("GRID", SG_DATATYPE_Byte);

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

		return( false );
	}

	//-----------------------------------------------------
	m_pWator->Set_Name(_TL("Wa-Tor"));
	m_pWator->Set_NoData_Value(-1);

	CSG_Colors	Colors(3);

	Colors.Set_Color(0, SG_COLOR_BLACK);
	Colors.Set_Color(1, SG_COLOR_GREEN);
	Colors.Set_Color(2, SG_COLOR_RED  );

	DataObject_Add       (m_pWator);
	DataObject_Set_Colors(m_pWator, Colors);
	DataObject_Update    (m_pWator, 0, 2, SG_UI_DATAOBJECT_SHOW);

	//-----------------------------------------------------
	if( Parameters("REFRESH")->asBool() )
	{
		double	Fish_perc	= Parameters("INIT_FISH" )->asDouble();
		double	Shark_perc	= Parameters("INIT_SHARK")->asDouble() + Fish_perc;

		#pragma omp parallel for
		for(int y=0; y<m_pWator->Get_NY(); y++)
		{
			for(int x=0; x<m_pWator->Get_NX(); x++)
			{
				double	perc	= CSG_Random::Get_Uniform(0, 100);

				if( perc <= Fish_perc )
				{
					m_pWator->Set_Value(x, y, FISH);
				}
				else if( perc <= Shark_perc )
				{
					m_pWator->Set_Value(x, y, SHARK);
				}
				else
				{
					m_pWator->Set_Value(x, y, 0);
				}
			}
		}
	}

	//-----------------------------------------------------
	CSG_Table	*pTable	= Parameters("TABLE")->asTable();

	pTable->Destroy();
	pTable->Set_Name(_TL("Wa-Tor"));

	pTable->Add_Field("Cycle" , SG_DATATYPE_Int);
	pTable->Add_Field("Fishes", SG_DATATYPE_Int);
	pTable->Add_Field("Sharks", SG_DATATYPE_Int);

	//-----------------------------------------------------
	m_Fish_Birth	= Parameters("FISH_BIRTH"  )->asInt();
	m_Shark_Birth	= Parameters("SHARK_BIRTH" )->asInt();
	m_Shark_Starve	= Parameters("SHARK_STARVE")->asInt();

	m_Next  .Create(m_pWator, SG_DATATYPE_Byte);
	m_Age   .Create(m_pWator, SG_DATATYPE_Byte);
	m_Starve.Create(m_pWator, SG_DATATYPE_Byte);

	#pragma omp parallel for
	for(int y=0; y<m_pWator->Get_NY(); y++)
	{
		for(int x=0; x<m_pWator->Get_NX(); x++)
		{
			switch( m_pWator->asByte(x, y) )
			{
			case FISH:
				m_Age   .Set_Value(x, y, CSG_Random::Get_Uniform(0.0, m_Fish_Birth  ));
				break;

			case SHARK:
				m_Age   .Set_Value(x, y, CSG_Random::Get_Uniform(0.0, m_Shark_Birth ));
				m_Starve.Set_Value(x, y, CSG_Random::Get_Uniform(0.0, m_Shark_Starve));
				break;
			}
		}
	}

	//-----------------------------------------------------
	int		i;

	SG_UI_Progress_Lock(true);

	for(i=1; Process_Get_Okay(true) && Next_Cycle(); i++)
	{
		Process_Set_Text("%s: %d", _TL("Life Cycle"), i);

		CSG_Table_Record	*pRecord	= pTable->Add_Record();

		pRecord->Set_Value(0, i);
		pRecord->Set_Value(1, m_nFishes);
		pRecord->Set_Value(2, m_nSharks);

		DataObject_Update(m_pWator, 0, 2);
		DataObject_Update(pTable);
	}

	SG_UI_Progress_Lock(false);

	//-----------------------------------------------------
	m_Next  .Destroy();
	m_Age   .Destroy();
	m_Starve.Destroy();

	if( is_Progress() )
	{
		Message_Fmt("\n%s %d %s", _TL("Dead after"), i, _TL("Life Cycles"));
	}

	return( true );
}
//---------------------------------------------------------
bool CPointCloud_From_Text_File::On_Execute(void)
{
	CSG_String				fileName;
	int						iField, iType;
	CSG_String				Name, Types, s;
	CSG_PointCloud			*pPoints;
	CSG_Parameters			P;
	CSG_Parameter			*pNode;
	int						xField, yField, zField, nAttribs;
	bool					bSkipHeader;
	char					fieldSep;
	std::vector<int>		vCol;
	std::ifstream			tabStream;
    std::string				tabLine;
	double					lines;
	long					cntPt, cntInvalid;
	double					x, y, z;


	//-----------------------------------------------------
	fileName	= Parameters("FILE")		->asString();
	xField		= Parameters("XFIELD")		->asInt() - 1;
	yField		= Parameters("YFIELD")		->asInt() - 1;
	zField		= Parameters("ZFIELD")		->asInt() - 1;
	bSkipHeader	= Parameters("SKIP_HEADER")	->asBool();

	switch (Parameters("FIELDSEP")->asInt())
    {
	default:
    case 0: fieldSep = '\t';	break;
    case 1: fieldSep = ' ';		break;
    case 2: fieldSep = ',';		break;
    }

    pPoints	= SG_Create_PointCloud();
    pPoints->Create();
    pPoints->Set_Name(SG_File_Get_Name(fileName, false));
    Parameters("POINTS")->Set_Value(pPoints);
    DataObject_Add(pPoints);


    //-----------------------------------------------------
    if (SG_UI_Get_Window_Main())
    {
        nAttribs	= Parameters("ATTRIBS")		->asInt();

        Types.Printf(SG_T("%s|%s|%s|%s|%s|"),
            _TL("1 byte integer"),
            _TL("2 byte integer"),
            _TL("4 byte integer"),
            _TL("4 byte floating point"),
            _TL("8 byte floating point")
        );

        P.Set_Name(_TL("Attribute Field Properties"));

        for(iField=1; iField<=nAttribs; iField++)
        {
            s.Printf(SG_T("NODE_%03d") , iField);
            pNode	= P.Add_Node(NULL, s, CSG_String::Format(SG_T("%d. %s"), iField, _TL("Field")), _TL(""));

            s.Printf(SG_T("FIELD_%03d"), iField);
            P.Add_String(pNode, s, _TL("Name"), _TL(""), s);

            s.Printf(SG_T("COLUMN_%03d"), iField);
            P.Add_Value(pNode, s, _TL("Attribute is Column ..."), _TL(""), PARAMETER_TYPE_Int, iField+3, 1, true);

            s.Printf(SG_T("TYPE_%03d") , iField);
            P.Add_Choice(pNode, s, _TL("Type"), _TL(""), Types, 3);
        }

        //-----------------------------------------------------
        if( nAttribs > 0 )
        {
            if( Dlg_Parameters(&P, _TL("Field Properties")) )
            {
                for(iField=0; iField<nAttribs; iField++)
                {

                    Name		 = P(CSG_String::Format(SG_T("FIELD_%03d" ), iField + 1).c_str())->asString();
                    iType		 = P(CSG_String::Format(SG_T("TYPE_%03d"  ), iField + 1).c_str())->asInt();
                    vCol.push_back(P(CSG_String::Format(SG_T("COLUMN_%03d"), iField + 1).c_str())->asInt() - 1);

                    pPoints->Add_Field(Name, Get_Data_Type(iType));
                }
            }
            else
                return( false );
        }
    }
    else // CMD
	{
		CSG_String		    sFields, sNames, sTypes;
		CSG_String		    token;
		int				    iValue;
		std::vector<int>	vTypes;


		sFields		= Parameters("FIELDS")->asString();
		sNames		= Parameters("FIELDNAMES")->asString();
		sTypes	    = Parameters("FIELDTYPES")->asString();

		CSG_String_Tokenizer   tkz_fields(sFields, ";", SG_TOKEN_STRTOK);

		while( tkz_fields.Has_More_Tokens() )
		{
			token	= tkz_fields.Get_Next_Token();

			if( token.Length() == 0 )
				break;

			if( !token.asInt(iValue) )
			{
				SG_UI_Msg_Add_Error(_TL("Error parsing attribute fields: can't convert to number"));
				return( false );
			}

			iValue	-= 1;

			if( iValue < 1)
			{
				SG_UI_Msg_Add_Error(_TL("Error parsing attribute fields: field index out of range"));
				return( false );
			}
			else
				vCol.push_back(iValue);
		}

		CSG_String_Tokenizer   tkz_datatypes(sTypes, ";", SG_TOKEN_STRTOK);

		while( tkz_datatypes.Has_More_Tokens() )
		{
			token	= tkz_datatypes.Get_Next_Token();

			if( token.Length() == 0 )
				break;

			if( !token.asInt(iValue) )
			{
				SG_UI_Msg_Add_Error(_TL("Error parsing field type: can't convert to number"));
				return( false );
			}

			vTypes.push_back(iValue);
		}

		CSG_String_Tokenizer   tkz_datanames(sNames, ";", SG_TOKEN_STRTOK);

        int                 iter = 0;

		while( tkz_datanames.Has_More_Tokens() )
		{
			token	= tkz_datanames.Get_Next_Token();

			if( token.Length() == 0 )
				break;

			pPoints->Add_Field(token, Get_Data_Type(vTypes.at(iter)));

			iter++;
		}

		if( vCol.size() != vTypes.size() || (int)vCol.size() != iter )
		{
		    SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Number of arguments for attribute fields (%d), names (%d) and types (%d) do not match!"), vCol.size(), iter, vTypes.size()));
            return( false );
		}
	}


	// open input stream
    //---------------------------------------------------------
    tabStream.open(fileName.b_str(), std::ifstream::in);
    if( !tabStream )
    {
        SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unable to open input file!")));
        return (false);
    }

	tabStream.seekg(0, std::ios::end);	// get length of file
    lines = (double)tabStream.tellg();
    tabStream.seekg(0, std::ios::beg);

    std::getline(tabStream, tabLine);	// as a workaround we assume the number of lines from the length of the first line
    lines = lines / (double)tabStream.tellg();

	if( !bSkipHeader )
    {
        tabStream.clear();                      // let's forget we may have reached the EOF
        tabStream.seekg(0, std::ios::beg);      // and rewind to the beginning
    }


	// import
    //---------------------------------------------------------
	cntPt = cntInvalid = 0;

	SG_UI_Process_Set_Text(CSG_String::Format(_TL("Importing data ...")));

    while( std::getline(tabStream, tabLine) )
    {
        std::istringstream stream(tabLine);
        std::vector<std::string> tabCols;
        std::string tabEntry;

        if( cntPt%10000 == 0 )
		{
            SG_UI_Process_Set_Progress((double)cntPt, lines);
		}
        cntPt++;

        while( std::getline(stream, tabEntry, fieldSep) )      // read every column in this line and fill vector
        {
            if (tabEntry.length() == 0)
                continue;
            tabCols.push_back(tabEntry);
        }

        if ((int)tabCols.size() < (vCol.size() + 3) )
        {
            SG_UI_Msg_Add(CSG_String::Format(_TL("WARNING: Skipping misformatted line: %d!"), cntPt), true);
            cntInvalid++;
            continue;
        }

        //parse line tokens
		std::vector<double> fieldValues;
		fieldValues.resize(vCol.size());

		x = strtod(tabCols[xField].c_str(), NULL);
        y = strtod(tabCols[yField].c_str(), NULL);
        z = strtod(tabCols[zField].c_str(), NULL);

		for( int i=0; i<(int)vCol.size(); i++ )
		{
			fieldValues[i] = strtod(tabCols.at(vCol.at(i)).c_str(), NULL);
		}

		pPoints->Add_Point(x, y, z);

		for( int i=0; i<(int)vCol.size(); i++ )
		{
			pPoints->Set_Attribute(i, fieldValues[i]);
		}
    }

	// finalize
    //---------------------------------------------------------
	tabStream.close();

	CSG_Parameters	sParms;
	DataObject_Get_Parameters(pPoints, sParms);
	if (sParms("METRIC_ATTRIB")	&& sParms("COLORS_TYPE") && sParms("METRIC_COLORS")
		&& sParms("METRIC_ZRANGE") && sParms("DISPLAY_VALUE_AGGREGATE"))
	{
		sParms("DISPLAY_VALUE_AGGREGATE")->Set_Value(3);		// highest z
		sParms("COLORS_TYPE")->Set_Value(2);                    // graduated color
		sParms("METRIC_COLORS")->asColors()->Set_Count(255);    // number of colors
		sParms("METRIC_ATTRIB")->Set_Value(2);					// z attrib
		sParms("METRIC_ZRANGE")->asRange()->Set_Range(pPoints->Get_Minimum(2),pPoints->Get_Maximum(2));
		DataObject_Set_Parameters(pPoints, sParms);
		DataObject_Update(pPoints);
	}

	if (cntInvalid > 0)
        SG_UI_Msg_Add(CSG_String::Format(SG_T("%s: %d %s"), _TL("WARNING"), cntInvalid, _TL("invalid points have been skipped")), true);

    SG_UI_Msg_Add(CSG_String::Format(SG_T("%d %s"), (cntPt-cntInvalid), _TL("points have been imported with success")), true);

	return( true );
}
예제 #11
0
//---------------------------------------------------------
bool CAtlas_BNA_Import::On_Execute(void)
{
	bool		bOk;
	int			iPoint, nPoints;
	double		x, y;
	FILE		*Stream;
	CSG_String	FileName, sLine, sName1, sName2;
	CSG_Shape	*pShape;
	CSG_Shapes	*pPoints, *pLines, *pPolygons;

	//-----------------------------------------------------
	FileName	= Parameters("FILE")	->asString();

	//-----------------------------------------------------
	if( (Stream = fopen(FileName.b_str(), "r")) != NULL )
	{
		pPoints		= SG_Create_Shapes(SHAPE_TYPE_Point  , SG_File_Get_Name(FileName, false));
		pPoints		->Add_Field("NAME1"	, SG_DATATYPE_String);
		pPoints		->Add_Field("NAME2"	, SG_DATATYPE_String);

		pLines		= SG_Create_Shapes(SHAPE_TYPE_Line   , SG_File_Get_Name(FileName, false));
		pLines		->Add_Field("NAME1"	, SG_DATATYPE_String);
		pLines		->Add_Field("NAME2"	, SG_DATATYPE_String);

		pPolygons	= SG_Create_Shapes(SHAPE_TYPE_Polygon, SG_File_Get_Name(FileName, false));
		pPolygons	->Add_Field("NAME1"	, SG_DATATYPE_String);
		pPolygons	->Add_Field("NAME2"	, SG_DATATYPE_String);

		//-------------------------------------------------
		bOk		= true;

		while( bOk && SG_Read_Line(Stream, sLine) && Process_Get_Okay(true) )
		{
			sName1	= sLine.AfterFirst('\"').BeforeFirst('\"');
			sName2	= sLine.BeforeLast('\"').AfterLast('\"');
			sLine	= sLine.AfterLast('\"');	if( sLine.Find(',', true) >= 0 )	sLine	= sLine.AfterLast(',');

			nPoints	= sLine.asInt();

			if( nPoints == 1 )
			{
				pShape	= pPoints	->Add_Shape();
			}
			else if( nPoints < 0 )
			{
				pShape	= pLines	->Add_Shape();
				nPoints	= -nPoints;
			}
			else if( nPoints > 2 )
			{
				pShape	= pPolygons	->Add_Shape();
			}
			else
			{
				bOk		= false;
			}

			if( bOk )
			{
				pShape->Set_Value(0, sName1);
				pShape->Set_Value(1, sName2);

				for(iPoint=0; iPoint<nPoints && bOk; iPoint++)
				{
					if( (bOk = SG_Read_Line(Stream, sLine)) == true )
					{
						SG_SSCANF(sLine, SG_T("%lf %lf"), &x, &y);
						pShape->Add_Point(x, y);
					}
				}
			}
		}

		fclose(Stream);

		//-------------------------------------------------
		bOk		= false;

		if( pPoints->is_Valid() && pPoints->Get_Count() > 0 )
		{
			bOk		= true;
			DataObject_Add(pPoints);
		}
		else
		{
			delete(pPoints);
		}

		if( pLines->is_Valid() && pLines->Get_Count() > 0 )
		{
			bOk		= true;
			DataObject_Add(pLines);
		}
		else
		{
			delete(pLines);
		}

		if( pPolygons->is_Valid() && pPolygons->Get_Count() > 0 )
		{
			bOk		= true;
			DataObject_Add(pPolygons);
		}
		else
		{
			delete(pPolygons);
		}

		return( bOk );
	}

	//-----------------------------------------------------
	return( false );
}
//---------------------------------------------------------
bool CWatersheds_ext::On_Execute(void)
{
	int			x, y;
	CSG_Grid	*pBasins, *pSubBasins, Inflows;
	CSG_Shapes	*pHeads, *pMouths, *pVBasins, *pVSubBasins;

	m_pDEM		= Parameters("DEM")			->asGrid(); 
	m_pChannels	= Parameters("CHANNELS")	->asGrid();
	pBasins		= Parameters("BASINS")		->asGrid();
	pSubBasins	= Parameters("SUBBASINS")	->asGrid();
	pVBasins	= Parameters("V_BASINS")	->asShapes();
	pVSubBasins	= Parameters("V_SUBBASINS")	->asShapes();
	pHeads		= Parameters("HEADS")		->asShapes();
	pMouths		= Parameters("MOUTHS")		->asShapes();

	//-----------------------------------------------------
	Inflows		.Create(*Get_System(), SG_DATATYPE_Char);

	m_Direction	.Create(*Get_System(), SG_DATATYPE_Char);
	m_Direction	.Set_NoData_Value(-1);

	m_Distance	.Create(*Get_System(), SG_DATATYPE_Float);
	m_Distance	.Set_NoData_Value(-1);
	m_Distance	.Assign_NoData();

	pBasins		->Assign(0.0);
	pBasins		->Set_NoData_Value(0.0);

	pSubBasins	->Assign(0.0);
	pSubBasins	->Set_NoData_Value(0.0);

	pHeads		->Create(SHAPE_TYPE_Point	, _TL("Heads"));
	pHeads		->Add_Field("ID"			, SG_DATATYPE_Int);
	pHeads		->Add_Field("MAIN_ID"		, SG_DATATYPE_Int);
	pHeads		->Add_Field("ELEVATION"		, SG_DATATYPE_Double);
	pHeads		->Add_Field("DISTANCE"		, SG_DATATYPE_Double);

	pMouths		->Create(SHAPE_TYPE_Point	, _TL("Mouths"));
	pMouths		->Add_Field("ID"			, SG_DATATYPE_Int);
	pMouths		->Add_Field("MAIN_ID"		, SG_DATATYPE_Int);
	pMouths		->Add_Field("ELEVATION"		, SG_DATATYPE_Double);

	pVBasins	->Create(SHAPE_TYPE_Polygon	, _TL("Basins"));
	BASIN_ADD_FIELDS(pVBasins);

	pVSubBasins	->Create(SHAPE_TYPE_Polygon	, _TL("Subbasins"));
	BASIN_ADD_FIELDS(pVSubBasins);

	//-----------------------------------------------------
	Process_Set_Text(_TL("flow directions..."));

	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			int	Direction	= -1;

			if( m_pDEM->is_InGrid(x, y) )
			{
				double	dMax		= 0.0;

				for(int i=0; i<8; i++)
				{
					int	ix	= Get_xTo(i, x);
					int	iy	= Get_yTo(i, y);

					if( m_pDEM->is_InGrid(ix, iy) && !m_pChannels->is_NoData(ix, iy) )
					{
						double	dz	= (m_pDEM->asDouble(x, y) - m_pDEM->asDouble(ix, iy)) / Get_Length(i);

						if( dMax < dz )
						{
							dMax		= dz;
							Direction	= i;
						}
					}
				}

				if( !m_pChannels->is_NoData(x, y) )
				{
					if( Direction >= 0 )
					{
						int	ix	= Get_xTo(Direction, x);
						int	iy	= Get_yTo(Direction, y);

						if( m_pDEM->is_InGrid(ix, iy) )
						{
							Inflows.Add_Value(ix, iy, 1);
						}
					}
				}
				else if( Direction < 0 )
				{
					Direction	= m_pDEM->Get_Gradient_NeighborDir(x, y);
				}
			}

			m_Direction.Set_Value(x, y, Direction);
		}
	}

	//-----------------------------------------------------
	Process_Set_Text(_TL("main basins..."));

	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			if( m_pChannels->is_InGrid(x, y) && is_Outlet(x, y) )
			{
				Get_Basin(pBasins, pVBasins, x, y, -1);

				CSG_Shape	*pMouth	= pMouths->Add_Shape();

				pMouth->Add_Point(Get_System()->Get_Grid_to_World(x, y));

				pMouth->Set_Value(0, pVBasins->Get_Count());	// ID
				pMouth->Set_Value(1, pVBasins->Get_Count());	// MAIN_ID
				pMouth->Set_Value(2, m_pDEM->asDouble(x, y));	// ELEVATION
			}
		}
	}

	if( Parameters("DISTANCE")->asBool() )
	{
		m_Distance.Set_Name(_TL("Flow Distance"));

		DataObject_Add(SG_Create_Grid(m_Distance));
	}

	//-----------------------------------------------------
	Process_Set_Text(_TL("heads and mouths..."));

	for(y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(x=0; x<Get_NX(); x++)
		{
			if( m_pChannels->is_InGrid(x, y) )
			{
				//-----------------------------------------
				if( Inflows.asInt(x, y) > 1 )		// mouth, linking channels of subcatchments
				{
					CSG_Shape	*pMouth	= pMouths->Add_Shape();

					pMouth->Add_Point(Get_System()->Get_Grid_to_World(x, y));

					pMouth->Set_Value(0, pMouths->Get_Count());		// ID
					pMouth->Set_Value(1, pBasins->asDouble(x, y));	// MAIN_ID
					pMouth->Set_Value(2, m_pDEM->asDouble(x, y));	// ELEVATION
				}

				//-----------------------------------------
				else if( Inflows.asInt(x, y) == 0 )	// head
				{
					CSG_Shape	*pHead	= pHeads->Add_Shape();

					pHead->Add_Point(Get_System()->Get_Grid_to_World(x, y));

					pHead->Set_Value(0, pHeads->Get_Count() + 1);
					pHead->Set_Value(1, m_Distance.asDouble(x, y));
				}
			}
		}
	}

	//-----------------------------------------------------
	Process_Set_Text(_TL("subbasins..."));

	pMouths->Set_Index(1, TABLE_INDEX_Ascending, 2, TABLE_INDEX_Descending);

	for(int iMouth=0; iMouth<pMouths->Get_Count() && Set_Progress(iMouth, pMouths->Get_Count()); iMouth++)
	{
		CSG_Shape	*pMouth	= pMouths->Get_Shape_byIndex(iMouth);

		if( Get_System()->Get_World_to_Grid(x, y, pMouth->Get_Point(0)) )
		{
			if( pMouth->asInt(0) == pMouth->asInt(1) )
			{
				Get_Basin(pSubBasins, pVSubBasins, x, y, pMouth->asInt(1));
			}
			else
			{
				for(int i=0; i<8; i++)
				{
					int	ix	= Get_xFrom(i, x);
					int	iy	= Get_yFrom(i, y);

					if( m_pChannels->is_InGrid(ix, iy) && m_Direction.asInt(ix, iy) == i )
					{
						Get_Basin(pSubBasins, pVSubBasins, ix, iy, pMouth->asInt(1));
					}
				}
			}
		}
	}

	if( Parameters("DISTANCE")->asBool() )
	{
		m_Distance.Set_Name(_TL("Subbasin Flow Distance"));

		DataObject_Add(SG_Create_Grid(m_Distance));
	}

	//-----------------------------------------------------
	m_Distance	.Destroy();
	m_Direction	.Destroy();

	return( true );
}
예제 #13
0
//---------------------------------------------------------
bool CLAS_Import::On_Execute(void)
{
	CSG_Parameter_PointCloud_List	*pPointsList;
	bool			bValidity;
	CSG_Strings		Files;
	int				RGBrange;
	int				cntInvalid = 0;


	bValidity		= Parameters("VALID")->asBool();
	RGBrange		= Parameters("RGB_RANGE")->asInt();
	
	//-----------------------------------------------------
	if( !Parameters("FILES")->asFilePath()->Get_FilePaths(Files) )
	{
		return( false );
	}

	//-----------------------------------------------------
	pPointsList	= Parameters("POINTS")->asPointCloudList();
	pPointsList	->Del_Items();

	for(int i=0; i<Files.Get_Count(); i++)
	{
		SG_UI_Msg_Add(CSG_String::Format(_TL("Parsing %s ... "), SG_File_Get_Name(Files[i], true).c_str()), true);

		std::ifstream   ifs;

		ifs.open(Files[i].b_str(), std::ios::in | std::ios::binary);
		if( !ifs )
		{
			SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unable to open LAS file!")));
			continue;
		}

		//-----------------------------------------------------
		// Check if LAS version is supported
		liblas::LASReader *pReader;
		try {
			pReader = new liblas::LASReader(ifs);
		}
		catch(std::exception &e) {
			SG_UI_Msg_Add_Error(CSG_String::Format(_TL("LAS header exception: %s"), e.what()));
			ifs.close();
			return( false );
		}
		catch(...) {
			SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unknown LAS header exception!")));
			ifs.close();
			return( false );
		}
	
		delete (pReader);
		ifs.clear();
		//-----------------------------------------------------


		liblas::LASReader reader(ifs);

		liblas::LASHeader const& header = reader.GetHeader();


		//-----------------------------------------------------
		int		nFields, iField[VAR_Count];

		CSG_PointCloud	*pPoints	= SG_Create_PointCloud();
		pPoints->Set_Name(SG_File_Get_Name(Files[i], false));

		nFields		= 3;

		ADD_FIELD("T", VAR_T, _TL("gps-time")							, SG_DATATYPE_Double);	// SG_DATATYPE_Long
		ADD_FIELD("i", VAR_i, _TL("intensity")							, SG_DATATYPE_Float);	// SG_DATATYPE_Word
		ADD_FIELD("a", VAR_a, _TL("scan angle")							, SG_DATATYPE_Float);	// SG_DATATYPE_Byte
		ADD_FIELD("r", VAR_r, _TL("number of the return")				, SG_DATATYPE_Int);
		ADD_FIELD("c", VAR_c, _TL("classification")						, SG_DATATYPE_Int);		// SG_DATATYPE_Byte
		ADD_FIELD("u", VAR_u, _TL("user data")							, SG_DATATYPE_Double);	// SG_DATATYPE_Byte
		ADD_FIELD("n", VAR_n, _TL("number of returns of given pulse")	, SG_DATATYPE_Int);
		ADD_FIELD("R", VAR_R, _TL("red channel color")					, SG_DATATYPE_Int);		// SG_DATATYPE_Word
		ADD_FIELD("G", VAR_G, _TL("green channel color")				, SG_DATATYPE_Int);
		ADD_FIELD("B", VAR_B, _TL("blue channel color")					, SG_DATATYPE_Int);
		ADD_FIELD("e", VAR_e, _TL("edge of flight line flag")			, SG_DATATYPE_Char);
		ADD_FIELD("d", VAR_d, _TL("direction of scan flag")				, SG_DATATYPE_Char);
		ADD_FIELD("p", VAR_p, _TL("point source ID")					, SG_DATATYPE_Int);		// SG_DATATYPE_Word
		ADD_FIELD("C", VAR_C, _TL("rgb color")							, SG_DATATYPE_Int);

		//-----------------------------------------------------
		int		iPoint	= 0;

		try {
			while( reader.ReadNextPoint() )
			{
				if (iPoint % 100000)
					SG_UI_Process_Set_Progress(iPoint, header.GetPointRecordsCount()); 

				liblas::LASPoint const& point = reader.GetPoint();

				if( bValidity )
				{
					if( !point.IsValid() )
					{
						cntInvalid++;
						continue;
					}
				}

				pPoints->Add_Point(point.GetX(), point.GetY(), point.GetZ());

				if( iField[VAR_T] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_T], point.GetTime());
				if( iField[VAR_i] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_i], point.GetIntensity());
				if( iField[VAR_a] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_a], point.GetScanAngleRank());
				if( iField[VAR_r] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_r], point.GetReturnNumber());
				if( iField[VAR_c] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_c], point.GetClassification());
				if( iField[VAR_u] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_u], point.GetUserData());
				if( iField[VAR_n] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_n], point.GetNumberOfReturns());
				if( iField[VAR_R] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_R], point.GetColor().GetRed());
				if( iField[VAR_G] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_G], point.GetColor().GetGreen());
				if( iField[VAR_B] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_B], point.GetColor().GetBlue());
				if( iField[VAR_e] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_e], point.GetFlightLineEdge());
				if( iField[VAR_d] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_d], point.GetScanDirection());
				if( iField[VAR_p] > 0 )	pPoints->Set_Value(iPoint, iField[VAR_p], point.GetPointSourceID());
				if( iField[VAR_C] > 0 )
				{
					double	r, g, b;
					r = point.GetColor().GetRed();
					g = point.GetColor().GetGreen();
					b = point.GetColor().GetBlue();

					if (RGBrange == 0)		// 16 bit
					{
						r = r / 65535 * 255;
						g = g / 65535 * 255;
						b = b / 65535 * 255;
					}
			
					pPoints->Set_Value(iPoint, iField[VAR_C], SG_GET_RGB(r, g, b));
				}

				iPoint++;
			}
		}
		catch(std::exception &e) {
			SG_UI_Msg_Add_Error(CSG_String::Format(_TL("LAS reader exception: %s"), e.what()));
			ifs.close();
			return( false );
		}
		catch(...) {
			SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unknown LAS reader exception!")));
			ifs.close();
			return( false );
		}

		ifs.close();

		pPointsList->Add_Item(pPoints);

		DataObject_Add(pPoints);

		//-----------------------------------------------------
		CSG_Parameters		sParms;

		DataObject_Get_Parameters(pPoints, sParms);

		if (sParms("METRIC_ATTRIB")	&& sParms("COLORS_TYPE") && sParms("METRIC_COLORS")
			&& sParms("METRIC_ZRANGE") && sParms("DISPLAY_VALUE_AGGREGATE"))
		{
			sParms("DISPLAY_VALUE_AGGREGATE")->Set_Value(3);		// highest z
			sParms("COLORS_TYPE")->Set_Value(2);                    // graduated color
			sParms("METRIC_COLORS")->asColors()->Set_Count(255);    // number of colors
			sParms("METRIC_ATTRIB")->Set_Value(2);					// z attrib
			sParms("METRIC_ZRANGE")->asRange()->Set_Range(pPoints->Get_Minimum(2),pPoints->Get_Maximum(2));
		}

		DataObject_Set_Parameters(pPoints, sParms);

		SG_UI_Msg_Add(_TL("okay"), false);
	}

	//-----------------------------------------------------
	if( bValidity && cntInvalid > 0 )
		SG_UI_Msg_Add(CSG_String::Format(_TL("WARNING: %d invalid points skipped!"), cntInvalid), true);

	return( true );
}
예제 #14
0
bool CGPX2SHP::On_Execute(void) {

    CSG_String sCmd;
    CSG_String sFile = Parameters("FILE")->asString();
    CSG_String sBasePath = Parameters("BASEPATH")->asString();
    CSG_String sShapefile;
    bool bWaypoints = Parameters("WAYPOINTS")->asBool();
    bool bTrackpoints = Parameters("TRACKPOINTS")->asBool();
    bool bRoutes = Parameters("ROUTES")->asBool();
    bool bAdd = Parameters("ADD")->asBool();
    CSG_Shapes *pShapes;

    sCmd = sBasePath + SG_T("\\gpx2shp ");

    if (bWaypoints) {
        sCmd += SG_T("-w ");
    }//if
    if (bTrackpoints) {
        sCmd += SG_T("-t ");
    }//if
    if (bRoutes) {
        sCmd += SG_T("-r ");
    }//if

    sCmd += sFile;

    system(sCmd.b_str());

    if( bAdd )
    {
        CSG_String	sDir(SG_File_Get_Path(sFile)), sName(SG_File_Get_Name(sFile, false));

        //-------------------------------------------------
        sFile	= SG_File_Make_Path(sDir, sName + SG_T("_wpt"), SG_T("shp"));
        pShapes	= SG_Create_Shapes(sFile);

        if( pShapes->is_Valid() )
            DataObject_Add(pShapes, false);
        else
            delete(pShapes);

        //-------------------------------------------------
        sFile	= SG_File_Make_Path(sDir, sName + SG_T("_trk"), SG_T("shp"));
        pShapes	= SG_Create_Shapes(sFile);

        if( pShapes->is_Valid() )
            DataObject_Add(pShapes, false);
        else
            delete(pShapes);

        //-------------------------------------------------
        sFile	= SG_File_Make_Path(sDir, sName + SG_T("_rte"), SG_T("shp"));
        pShapes	= SG_Create_Shapes(sFile);

        if( pShapes->is_Valid() )
            DataObject_Add(pShapes, false);
        else
            delete(pShapes);
    }//if

    return true;

}//method
예제 #15
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 );
}
예제 #16
0
//---------------------------------------------------------
bool CGeoref_Grid::Get_Conversion(void)
{
	//-----------------------------------------------------
	CSG_Rect	Extent;

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

	//-----------------------------------------------------
	if( !Get_Target_Extent(Extent, true) ||  !m_Grid_Target.Set_User_Defined(Get_Parameters("TARGET"), Extent, pSource->Get_NY()) )
	{
		Error_Set(_TL("failed to estimate target extent"));

		return( false );
	}

	if( !Dlg_Parameters("TARGET") )
	{
		return( false );
	}

	//-----------------------------------------------------
	TSG_Grid_Resampling	Resampling;

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

	CSG_Grid	*pReferenced	= m_Grid_Target.Get_Grid(Resampling == GRID_RESAMPLING_NearestNeighbour ? pSource->Get_Type() : SG_DATATYPE_Float);

	if( !pReferenced )
	{
		Error_Set(_TL("failed to initialize target grid"));

		return( false );
	}

	//-----------------------------------------------------
	if( !Set_Grid(pSource, pReferenced, Resampling) )
	{
		Error_Set(_TL("failed to project target grid"));

		return( false );
	}

	//-----------------------------------------------------
	CSG_Parameters  P;

	if( DataObject_Get_Parameters(pSource, P) )
	{
		DataObject_Add(pReferenced);

		DataObject_Set_Parameters(pReferenced, P);
	}

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CGrid_Value_Replace::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Grid	*pGrid	= Parameters("OUTPUT")->asGrid();

	if( !pGrid || pGrid == Parameters("INPUT")->asGrid() )
	{
		pGrid	= Parameters("INPUT")->asGrid();
	}
	else
	{
		pGrid->Assign(Parameters("INPUT")->asGrid());

		DataObject_Set_Parameters(pGrid, Parameters("INPUT")->asGrid());

		pGrid->Fmt_Name("%s [%s]", Parameters("INPUT")->asGrid()->Get_Name(), _TL("Changed"));
	}

	//-----------------------------------------------------
	int		Method	= Parameters("METHOD")->asInt();

	CSG_Table	LUT;

	switch( Method )
	{
	default:	LUT.Create(*Parameters("IDENTITY")->asTable());	break;
	case  1:	LUT.Create(*Parameters("RANGE"   )->asTable());	break;
	case  2:	LUT.Create( Parameters("RANGE"   )->asTable());
		if( SG_UI_Get_Window_Main()	// gui only
		&&  DataObject_Get_Parameter(Parameters("GRID" )->asGrid(), "LUT")
		&&  DataObject_Get_Parameter(Parameters("INPUT")->asGrid(), "LUT") )
		{
			CSG_Table	LUTs[2];

			LUTs[0].Create(*DataObject_Get_Parameter(Parameters("GRID" )->asGrid(), "LUT")->asTable());
			LUTs[1].Create(*DataObject_Get_Parameter(Parameters("INPUT")->asGrid(), "LUT")->asTable());

			for(int i=0; i<LUTs[0].Get_Count(); i++)
			{
				CSG_String	Name	= LUTs[0][i].asString(1);

				for(int j=LUTs[1].Get_Count()-1; j>=0; j--)
				{
					if( !Name.Cmp(LUTs[1][j].asString(1)) )
					{
						CSG_Table_Record	*pReplace	= LUT.Add_Record();

						pReplace->Set_Value(0, LUTs[0][i].asDouble(3));
						pReplace->Set_Value(1, LUTs[1][j].asDouble(3));
						pReplace->Set_Value(2, LUTs[1][j].asDouble(4));

						LUTs[1].Del_Record(j);

						break;
					}
				}
			}

			for(int j=0; j<LUTs[1].Get_Count(); j++)
			{
				LUTs[0].Add_Record(LUTs[1].Get_Record(j));
			}

			DataObject_Add(pGrid);
			CSG_Parameter	*pLUT	= DataObject_Get_Parameter(pGrid, "LUT");
			pLUT->asTable()->Assign_Values(&LUTs[0]);
			DataObject_Set_Parameter(pGrid, pLUT);
		}
		break;
	}

	//-----------------------------------------------------
	if( LUT.Get_Count() == 0 )
	{
		Error_Set(_TL("empty look-up table, nothing to replace"));

		return( false );
	}

	//-----------------------------------------------------
	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#ifndef _DEBUG
		#pragma omp parallel for
		#endif
		for(int x=0; x<Get_NX(); x++)
		{
			double	Value	= pGrid->asDouble(x, y);

			for(int i=0; i<LUT.Get_Count(); i++)
			{
				if( Method == 0 )
				{
					if( LUT[i].asDouble(1) == Value )
					{
						pGrid->Set_Value(x, y, LUT[i].asDouble(0));

						break;
					}
				}
				else
				{
					if( LUT[i].asDouble(1) <= Value && Value <= LUT[i].asDouble(2) )
					{
						pGrid->Set_Value(x, y, LUT[i].asDouble(0));

						break;
					}
				}
			}
		}
	}

	//-----------------------------------------------------
	if( pGrid == Parameters("INPUT")->asGrid() )
	{
		DataObject_Update(pGrid);
	}

	return( true );
}
예제 #18
0
//---------------------------------------------------------
bool CSurfer_BLN_Import::On_Execute(void)
{
	bool				bOk;
	int					ID, Flag, iPoint, nPoints;
	double				x, y;
	FILE				*Stream;
	TSG_Shape_Type		Type;
	CSG_String			FileName, sLine, sName, sDesc, sTemp;
	CSG_Table_Record	*pRecord;
	CSG_Table			*pTable;
	CSG_Shape			*pShape;
	CSG_Shapes			*pShapes;

	//-----------------------------------------------------
	pShapes		= Parameters("SHAPES")	->asShapes();
	pTable		= Parameters("TABLE")	->asTable();
	FileName	= Parameters("FILE")	->asString();

	switch( Parameters("TYPE")->asInt() )
	{
	case 0:				Type	= SHAPE_TYPE_Point;		break;
	case 1:	default:	Type	= SHAPE_TYPE_Line;		break;
	case 2:				Type	= SHAPE_TYPE_Polygon;	break;
	}

	//-----------------------------------------------------
	if( (Stream = fopen(FileName.b_str(), "r")) != NULL )
	{
		bOk		= true;
		ID		= 0;

		if(	pShapes->Get_Type() != SHAPE_TYPE_Undefined
		&&	pShapes->Get_Type() != Type )
		{
			pShapes	= SG_Create_Shapes(Type, SG_File_Get_Name(FileName, false));
			Parameters("SHAPES")->Set_Value(pShapes);
			DataObject_Add(pShapes);
		}
		else
		{
			pShapes->Create(Type, SG_File_Get_Name(FileName, false));
		}

		if( Type == SHAPE_TYPE_Point )
		{
			if( pTable == NULL )
			{
				pTable	= SG_Create_Table();
				Parameters("TABLE")->Set_Value(pTable);
			}
			else
			{
				pTable->Destroy();
			}

			pTable ->Add_Field("ID"		, SG_DATATYPE_Int);
			pTable ->Add_Field("FLAG"	, SG_DATATYPE_Int);
			pTable ->Add_Field("NAME"	, SG_DATATYPE_String);
			pTable ->Add_Field("DESC"	, SG_DATATYPE_String);

			pShapes->Add_Field("ID"		, SG_DATATYPE_Int);
			pShapes->Add_Field("ID_LUT"	, SG_DATATYPE_Int);
			pShapes->Add_Field("Z"		, SG_DATATYPE_Double);
		}
		else
		{
			pShapes->Add_Field("ID"		, SG_DATATYPE_Int);
			pShapes->Add_Field("FLAG"	, SG_DATATYPE_Int);
			pShapes->Add_Field("NAME"	, SG_DATATYPE_String);
			pShapes->Add_Field("DESC"	, SG_DATATYPE_String);
		}

		//-------------------------------------------------
		while( bOk && SG_Read_Line(Stream, sLine) && sLine.BeforeFirst(',').asInt(nPoints) && nPoints > 0 && Process_Get_Okay(true) )
		{
			Process_Set_Text(CSG_String::Format(SG_T("%d. %s"), ++ID, _TL("shape in process")));

			sTemp	= sLine.AfterFirst (',');	sLine	= sTemp;
			Flag	= sLine.BeforeFirst(',').asInt();

			sTemp	= sLine.AfterFirst (',');	sLine	= sTemp;
			sTemp	= sLine.BeforeFirst(',');
			sName	= sTemp.AfterFirst('\"').BeforeLast('\"');

			sTemp	= sLine.AfterFirst (',');	sLine	= sTemp;
			sTemp	= sLine.BeforeFirst(',');
			sDesc	= sTemp.AfterFirst('\"').BeforeLast('\"');

			if( Type == SHAPE_TYPE_Point )
			{
				pRecord	= pTable->Add_Record();
				pRecord->Set_Value(0, ID);
				pRecord->Set_Value(1, Flag);
				pRecord->Set_Value(2, sName);
				pRecord->Set_Value(3, sDesc);

				for(iPoint=0; iPoint<nPoints && bOk; iPoint++)
				{
					if( (bOk = SG_Read_Line(Stream, sLine)) == true )
					{
						pShape	= pShapes->Add_Shape();
						pShape->Set_Value(0, iPoint + 1);
						pShape->Set_Value(1, ID);
						pShape->Set_Value(2, sLine.AfterLast (',').asDouble());

						x	= sLine.BeforeFirst(',').asDouble();
						y	= sLine.AfterFirst (',').asDouble();
						pShape->Add_Point(x, y);
					}
				}
			}
			else
			{
				pShape	= pShapes->Add_Shape();
				pShape->Set_Value(0, ID);
				pShape->Set_Value(1, Flag);
				pShape->Set_Value(2, sName);
				pShape->Set_Value(3, sDesc);

				for(iPoint=0; iPoint<nPoints && bOk; iPoint++)
				{
					if( (bOk = SG_Read_Line(Stream, sLine)) == true )
					{
						x	= sLine.BeforeFirst(',').asDouble();
						y	= sLine.AfterFirst (',').asDouble();
						pShape->Add_Point(x, y);
					}
				}
			}
		}

		fclose(Stream);
	}

	//-----------------------------------------------------
	if( pShapes->is_Valid() && pShapes->Get_Count() > 0 )
	{
		return( true );
	}

	return( false );
}