//---------------------------------------------------------
int CTable_PCA::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
{
    if(	!SG_STR_CMP(pParameter->Get_Identifier(), SG_T("TABLE")) )
    {
        CSG_Table		*pTable		= pParameter->asTable();
        CSG_Parameters	*pFields	= pParameters->Get_Parameter("FIELDS")->asParameters();

        pFields->Del_Parameters();

        if( pTable && pTable->Get_Field_Count() > 0 )
        {
            for(int i=0; i<pTable->Get_Field_Count(); i++)
            {
                if( SG_Data_Type_is_Numeric(pTable->Get_Field_Type(i)) )
                {
                    pFields->Add_Value(NULL, CSG_String::Format(SG_T("%d"), i), pTable->Get_Field_Name(i), _TL(""), PARAMETER_TYPE_Bool, false);
                }
            }
        }
    }

    return( 0 );
}
//---------------------------------------------------------
bool CPolygonStatisticsFromPoints::On_Execute(void)
{
	int						i, j, n, Offset, *bAttribute;
	CSG_Simple_Statistics	*Statistics;
	CSG_Parameters			*pParameters;
	CSG_Shapes				*pPolygons, *pPoints;

	//-----------------------------------------------------
	pPoints		= Parameters("POINTS")		->asShapes();
	pPolygons	= Parameters("POLYGONS")	->asShapes();

	if( pPolygons->Get_Count() <= 0 || pPoints->Get_Count() <= 0 )
	{
		return( false );
	}

	//-----------------------------------------------------
	pParameters	= Get_Parameters("ATTRIBUTES");

	pParameters->Del_Parameters();

	for(i=0; i<pPoints->Get_Field_Count(); i++)
	{
		if( SG_Data_Type_is_Numeric(pPoints->Get_Field_Type(i)) )
		{
			CSG_Parameter	*pNode	= pParameters->Add_Node(NULL, CSG_String::Format(SG_T("%d"), i), pPoints->Get_Field_Name(i), _TL(""));

			for(j=0; j<STAT_Count; j++)
			{
				pParameters->Add_Value(pNode,
					CSG_String::Format(SG_T("%d|%d"), i, j),
					CSG_String::Format(SG_T("[%s]"), STAT_Name[j].c_str()),
					_TL(""), PARAMETER_TYPE_Bool, false
				);
			}
		}
	}

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

	//-----------------------------------------------------
	if( Parameters("STATISTICS")->asShapes() == NULL )
	{
		Parameters("STATISTICS")->Set_Value(pPolygons);
	}
	else if( pPolygons != Parameters("STATISTICS")->asShapes() )
	{
		Parameters("STATISTICS")->asShapes()->Assign(pPolygons);

		pPolygons	= Parameters("STATISTICS")->asShapes();
	}

	//-----------------------------------------------------
	bAttribute	= new int[pPoints->Get_Field_Count()];
	Offset		= pPolygons->Get_Field_Count();

	for(i=0, n=0; i<pPoints->Get_Field_Count(); i++)
	{
		bAttribute[i]	= 0;

		if( SG_Data_Type_is_Numeric(pPoints->Get_Field_Type(i)) )
		{
			for(j=0; j<STAT_Count; j++)
			{
				CSG_Parameter	*pParameter	= pParameters->Get_Parameter(CSG_String::Format(SG_T("%d|%d"), i, j));

				if( pParameter && pParameter->asBool() )
				{
					bAttribute[i]	|= STAT_Flag[j];

					pPolygons->Add_Field(CSG_String::Format(SG_T("%s_%s"), pPoints->Get_Field_Name(i), STAT_Name[j].c_str()), SG_DATATYPE_Double);

					n++;
				}
			}
		}
	}

	if( n == 0 )
	{
		delete[](bAttribute);

		return( false );
	}

	//-----------------------------------------------------
	Statistics	= new CSG_Simple_Statistics[pPoints->Get_Field_Count()];

	for(int iPolygon=0; iPolygon<pPolygons->Get_Count() && Set_Progress(iPolygon, pPolygons->Get_Count()); iPolygon++)
	{
		CSG_Shape_Polygon	*pPolygon	= (CSG_Shape_Polygon *)pPolygons->Get_Shape(iPolygon);

		//-------------------------------------------------
		for(i=0; i<pPoints->Get_Field_Count(); i++)
		{
			Statistics[i].Invalidate();
		}

		//-------------------------------------------------
		for(int iPoint=0; iPoint<pPoints->Get_Count() && Process_Get_Okay(); iPoint++)
		{
			CSG_Shape	*pPoint	= pPoints->Get_Shape(iPoint);

			if( pPolygon->is_Containing(pPoint->Get_Point(0)) )
			{
				for(i=0; i<pPoints->Get_Field_Count(); i++)
				{
					if( bAttribute[i] )
					{
						Statistics[i].Add_Value(pPoint->asDouble(i));
					}
				}
			}
		}

		//-------------------------------------------------
		for(i=0, n=Offset; i<pPoints->Get_Field_Count(); i++)
		{
			if( bAttribute[i] )
			{
				if( Statistics[i].Get_Count() > 0 )
				{
					if( bAttribute[i] & STAT_Flag[STAT_Sum] )	{	pPolygon->Set_Value(n++, Statistics[i].Get_Sum());		}
					if( bAttribute[i] & STAT_Flag[STAT_Avg] )	{	pPolygon->Set_Value(n++, Statistics[i].Get_Mean());		}
					if( bAttribute[i] & STAT_Flag[STAT_Var] )	{	pPolygon->Set_Value(n++, Statistics[i].Get_Variance());	}
					if( bAttribute[i] & STAT_Flag[STAT_Dev] )	{	pPolygon->Set_Value(n++, Statistics[i].Get_StdDev());	}
					if( bAttribute[i] & STAT_Flag[STAT_Min] )	{	pPolygon->Set_Value(n++, Statistics[i].Get_Minimum());	}
					if( bAttribute[i] & STAT_Flag[STAT_Max] )	{	pPolygon->Set_Value(n++, Statistics[i].Get_Maximum());	}
					if( bAttribute[i] & STAT_Flag[STAT_Num] )	{	pPolygon->Set_Value(n++, Statistics[i].Get_Count());	}
				}
				else
				{
					if( bAttribute[i] & STAT_Flag[STAT_Sum] )	{	pPolygon->Set_NoData(n++);	}
					if( bAttribute[i] & STAT_Flag[STAT_Avg] )	{	pPolygon->Set_NoData(n++);	}
					if( bAttribute[i] & STAT_Flag[STAT_Var] )	{	pPolygon->Set_NoData(n++);	}
					if( bAttribute[i] & STAT_Flag[STAT_Dev] )	{	pPolygon->Set_NoData(n++);	}
					if( bAttribute[i] & STAT_Flag[STAT_Min] )	{	pPolygon->Set_NoData(n++);	}
					if( bAttribute[i] & STAT_Flag[STAT_Max] )	{	pPolygon->Set_NoData(n++);	}
					if( bAttribute[i] & STAT_Flag[STAT_Num] )	{	pPolygon->Set_NoData(n++);	}
				}
			}
		}
	}

	//-----------------------------------------------------
	delete[](Statistics);
	delete[](bAttribute);

	DataObject_Update(pPolygons);

	return( true );
}
//---------------------------------------------------------
bool CTable_Text_Import_Fixed_Cols::On_Execute(void)
{
    bool			bHeader;
    int				i, nChars, iField, nFields, *iFirst, *iLength;
    CSG_String		sLine;
    CSG_File		Stream;
    CSG_Table		*pTable;

    //-----------------------------------------------------
    pTable	= Parameters("TABLE")		->asTable();
    bHeader	= Parameters("HEADLINE")	->asBool();

    //-----------------------------------------------------
    if( !Stream.Open(Parameters("FILENAME")->asString(), SG_FILE_R, true) )
    {
        Message_Add(_TL("file could not be opened"));

        return( false );
    }

    if( !Stream.Read_Line(sLine) || (nChars = (int)sLine.Length()) <= 0 )
    {
        Message_Add(_TL("empty or corrupted file"));

        return( false );
    }

    //-----------------------------------------------------
    pTable->Destroy();
    pTable->Set_Name(SG_File_Get_Name(Parameters("FILENAME")->asString(), false));

    switch( Parameters("FIELDDEF")->asInt() )
    {
    //-----------------------------------------------------
    case 0:
    {
        CSG_Parameters	*pBreaks	= Get_Parameters("BREAKS");

        pBreaks->Del_Parameters();

        for(i=0; i<nChars; i++)
        {
            pBreaks->Add_Value(NULL,
                               CSG_String::Format(SG_T("%03d"), i),
                               CSG_String::Format(SG_T("%03d %c"), i + 1, sLine[i]),
                               _TL(""), PARAMETER_TYPE_Bool, false
                              );
        }

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

        //-------------------------------------------------
        for(i=0, nFields=1; i<pBreaks->Get_Count(); i++)
        {
            if( pBreaks->Get_Parameter(i)->asBool() )
            {
                nFields++;
            }
        }

        //-------------------------------------------------
        iFirst		= new int[nFields];
        iLength		= new int[nFields];

        iFirst[0]	= 0;

        for(i=0, iField=1; i<pBreaks->Get_Count() && iField<nFields; i++)
        {
            if( pBreaks->Get_Parameter(i)->asBool() )
            {
                iFirst[iField++]	= i + 1;
            }
        }

        //-------------------------------------------------
        for(iField=0; iField<nFields; iField++)
        {
            iLength[iField]	= (iField < nFields - 1 ? iFirst[iField + 1] : (int)sLine.Length()) - iFirst[iField];

            pTable->Add_Field(bHeader ? sLine.Mid(iFirst[iField], iLength[iField]) : CSG_String::Format(SG_T("FIELD%03d"), iField + 1), SG_DATATYPE_String);
        }
    }
    break;

    //-----------------------------------------------------
    case 1:
    {
        CSG_Parameters	*pFields	= Get_Parameters("FIELDS");

        pFields->Del_Parameters();

        nFields	= Parameters("NFIELDS")->asInt();

        for(iField=0; iField<nFields; iField++)
        {
            CSG_String		s		= CSG_String::Format(SG_T("%03d"), iField);
            CSG_Parameter	*pNode	= pFields->Add_Node(NULL, SG_T("NODE") + s, _TL("Field") + s, _TL(""));
            pFields->Add_Value	(pNode, SG_T("LENGTH") + s, _TL("Length"), _TL(""), PARAMETER_TYPE_Int, 1, 1, true);
            //	pFields->Add_Value	(pNode, SG_T("IMPORT") + s, _TL("Import"), _TL(""), PARAMETER_TYPE_Bool, true);
            pFields->Add_Choice	(pNode, SG_T("TYPE")   + s, _TL("Type")  , _TL(""), CSG_String::Format(SG_T("%s|%s|%s|%s|%s|"),
                                 _TL("text"),
                                 _TL("2 byte integer"),
                                 _TL("4 byte integer"),
                                 _TL("4 byte float"),
                                 _TL("8 byte float"))
                                );
        }

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

        //-------------------------------------------------
        iFirst		= new int[nFields];
        iLength		= new int[nFields];

        iFirst[0]	= 0;

        for(iField=0, i=0; iField<nFields && i<nChars; iField++)
        {
            CSG_String		s		= CSG_String::Format(SG_T("%03d"), iField);

            iFirst [iField]	= i;
            iLength[iField]	= pFields->Get_Parameter(SG_T("LENGTH") + s)->asInt();

            i	+= iLength[iField];

            CSG_String		Name	= bHeader ? sLine.Mid(iFirst[iField], iLength[iField]) : CSG_String::Format(SG_T("FIELD%03d"), iField + 1);

            switch( pFields->Get_Parameter(SG_T("TYPE") + s)->asInt() )
            {
            default:
            case 0:
                pTable->Add_Field(Name, SG_DATATYPE_String);
                break;
            case 1:
                pTable->Add_Field(Name, SG_DATATYPE_Short);
                break;
            case 2:
                pTable->Add_Field(Name, SG_DATATYPE_Int);
                break;
            case 3:
                pTable->Add_Field(Name, SG_DATATYPE_Float);
                break;
            case 4:
                pTable->Add_Field(Name, SG_DATATYPE_Double);
                break;
            }
        }
    }
    break;

    //-----------------------------------------------------
    case 2:
    {
        CSG_Table	*pList	= Parameters("LIST")->asTable();

        nFields	= pList->Get_Count();

        //-------------------------------------------------
        iFirst		= new int[nFields];
        iLength		= new int[nFields];

        iFirst[0]	= 0;

        for(iField=0, i=0; iField<nFields && i<nChars; iField++)
        {
            iFirst [iField]	= i;
            iLength[iField]	= pList->Get_Record(iField)->asInt(1);

            i	+= iLength[iField];

            CSG_String		Name	= bHeader ? sLine.Mid(iFirst[iField], iLength[iField]) : CSG_String(pList->Get_Record(iField)->asString(0));

            switch( pList->Get_Record(iField)->asInt(2) )
            {
            case 0:
                pTable->Add_Field(Name, SG_DATATYPE_String);
                break;
            case 1:
                pTable->Add_Field(Name, SG_DATATYPE_Short);
                break;
            case 2:
                pTable->Add_Field(Name, SG_DATATYPE_Int);
                break;
            case 3:
                pTable->Add_Field(Name, SG_DATATYPE_Float);
                break;
            default:
            case 4:
                pTable->Add_Field(Name, SG_DATATYPE_Double);
                break;
            }
        }
    }
    break;
    }

    //-----------------------------------------------------
    if( bHeader )
    {
        Stream.Read_Line(sLine);
    }

    //-----------------------------------------------------
    int		fLength	= Stream.Length();

    do
    {
        if( sLine.Length() == nChars )
        {
            CSG_Table_Record	*pRecord	= pTable->Add_Record();

            for(iField=0; iField<nFields; iField++)
            {
                pRecord->Set_Value(iField, sLine.Mid(iFirst[iField], iLength[iField]));
            }
        }
    }
    while( Stream.Read_Line(sLine) && Set_Progress(Stream.Tell(), fLength) );

    //-----------------------------------------------------
    delete[](iFirst);
    delete[](iLength);

    //-----------------------------------------------------
    return( true );
}