//---------------------------------------------------------
bool CSG_mRMR::Set_Data(CSG_Table &Data, int ClassField, double Threshold)
{
    if( !Get_Memory(Data.Get_Field_Count(), Data.Get_Count()) )
    {
        return( false );
    }

    //-----------------------------------------------------
    if( ClassField < 0 || ClassField >= m_nVars )
    {
        ClassField	= 0;
    }

    Data.Set_Index(ClassField, TABLE_INDEX_Ascending);

    CSG_String	s;

    for(int iSample=0, Class=0; iSample<m_nSamples; iSample++)
    {
        double	*pData	= m_Samples[iSample] = m_Samples[0] + iSample * m_nVars;

        if( s.Cmp(Data[iSample].asString(ClassField)) )
        {
            s	= Data[iSample].asString(ClassField);

            Class++;
        }

        *pData++	= Class;

        for(int iVar=0; iVar<m_nVars; iVar++)
        {
            if( iVar != ClassField )
            {
                *pData++	= Data[iSample].asDouble(iVar);
            }
        }
    }

    Data.Del_Index();

    m_VarNames	+= Data.Get_Field_Name(ClassField);

    for(int iVar=0; iVar<m_nVars; iVar++)
    {
        if( iVar != ClassField )
        {
            m_VarNames	+= Data.Get_Field_Name(iVar);
        }
    }

    //-----------------------------------------------------
    if( Threshold >= 0.0 )	// discretization
    {
        Discretize(Threshold);
    }

    return( true );
}
//---------------------------------------------------------
bool CTable_Running_Average::On_Execute(void)
{
	int			iValue, nValues;
	CSG_Table	*pTable;

	//-----------------------------------------------------
	pTable	= Parameters("INPUT")	->asTable();
	iValue	= Parameters("FIELD")	->asInt();
	nValues	= Parameters("COUNT")	->asInt();

	if( Parameters("OUTPUT")->asTable() && Parameters("OUTPUT")->asTable() != pTable )
	{
		pTable	= Parameters("OUTPUT")	->asTable();

		pTable->Create(*Parameters("INPUT")->asTable());
	}

	//-----------------------------------------------------
	if( pTable->is_Valid() )
	{
		int		i, iLo, iHi, nRange, iAverage;
		double	sValues;

		iAverage	= pTable->Get_Field_Count();
		pTable->Add_Field(CSG_String::Format(SG_T("%s [%s]"), pTable->Get_Field_Name(iValue), _TL("Average")), SG_DATATYPE_Double);

		nRange	= nValues / 2;
		sValues	= 0.0;

		for(iLo=-nValues, i=-nRange, iHi=0; i<pTable->Get_Count() && Set_Progress(i, pTable->Get_Count() + nRange); iLo++, i++, iHi++)
		{
			sValues	+= pTable->Get_Record(iHi < pTable->Get_Count() ? iHi : pTable->Get_Count() - 1)->asDouble(iValue);

			if( i < 0 )
			{
				sValues	+= pTable->Get_Record( 0 )->asDouble(iValue);
			}
			else
			{
				if( iLo < 0 )
				{
					sValues	-= pTable->Get_Record( 0 )->asDouble(iValue);
				}
				else if( iLo >= 0 )
				{
					sValues	-= pTable->Get_Record(iLo)->asDouble(iValue);
				}

				pTable->Get_Record(i)->Set_Value(iAverage, sValues / (double)nValues);
			}
		}

		return( true );
	}

	//-----------------------------------------------------
	return( false );
}
//---------------------------------------------------------
bool CShapes_Split_by_Attribute::On_Execute(void)
{
	int			iField;
	CSG_Table	*pTable;

	//-----------------------------------------------------
	pTable	= Parameters("TABLE")	->asTable();
	iField	= Parameters("FIELD")	->asInt();

	Parameters("CUTS")->asTableList()->Del_Items();

	//-----------------------------------------------------
	if( pTable->is_Valid() && pTable->Set_Index(iField, TABLE_INDEX_Ascending) )
	{
		CSG_String	sValue;
		CSG_Table	*pCut	= NULL;

		for(int iRecord=0; iRecord<pTable->Get_Count() && Set_Progress(iRecord, pTable->Get_Count()); iRecord++)
		{
			CSG_Table_Record	*pRecord	= pTable->Get_Record_byIndex(iRecord);

			if( !pCut || sValue.Cmp(pRecord->asString(iField)) )
			{
				pCut	= pTable->Get_ObjectType() == DATAOBJECT_TYPE_Shapes
						? SG_Create_Shapes(((CSG_Shapes *)pTable)->Get_Type(), SG_T(""), pTable)
						: SG_Create_Table(pTable);

				pCut->Set_Name(CSG_String::Format(SG_T("%s [%s = %s]"),
					pTable->Get_Name(),
					pTable->Get_Field_Name(iField),
					pRecord->asString(iField)
				));

				Parameters("CUTS")->asTableList()->Add_Item(pCut);

				sValue	= pRecord->asString(iField);
			}

			pCut->Add_Record(pRecord);
		}

		return( pCut != NULL );
	}

	return( false );
}
Example #4
0
//---------------------------------------------------------
bool		DLG_Table_Fields(const wxString &Caption, CSG_Parameter_Table_Fields *pFields)
{
	CSG_Table	*pTable	= pFields->Get_Table();

	if( pTable )
	{
		int				i;
		CSG_Parameters	P;

		for(i=0; i<pTable->Get_Field_Count(); i++)
		{
			P.Add_Value(NULL, SG_Get_String(i), pTable->Get_Field_Name(i), _TL(""), PARAMETER_TYPE_Bool, false);
		}

		for(i=0; i<pFields->Get_Count(); i++)
		{
			CSG_Parameter	*pParameter	= P(pFields->Get_Index(i));

			if( pParameter )
			{
				pParameter->Set_Value(true);
			}
		}

		if( DLG_Parameters(&P) )
		{
			CSG_String	s;

			for(i=0; i<pTable->Get_Field_Count(); i++)
			{
				if( P(i)->asBool() )
				{
					s	+= CSG_String::Format(s.Length() ? SG_T(",%d") : SG_T("%d"), i);
				}
			}

			pFields->Set_Value(s);

			return( true );
		}
	}

	return( false );
}
//---------------------------------------------------------
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 );
}
Example #6
0
//---------------------------------------------------------
bool CSG_ODBC_Connection::Table_Create(const CSG_String &Table_Name, const CSG_Table &Table, const CSG_Buffer &Flags, bool bCommit)
{
	if( Table.Get_Field_Count() <= 0 )
	{
		_Error_Message(_TL("no attributes in table"));

		return( false );
	}

	//-----------------------------------------------------
	int			iField;
	CSG_String	SQL;

	SQL.Printf(SG_T("CREATE TABLE \"%s\"("), Table_Name.c_str());

	for(iField=0; iField<Table.Get_Field_Count(); iField++)
	{
		CSG_String	s;

		switch( Table.Get_Field_Type(iField) )
		{
		default:
		case SG_DATATYPE_String:
			s	= CSG_String::Format(SG_T("VARCHAR(%d)"), Table.Get_Field_Length(iField));
			break;

		case SG_DATATYPE_Char:
			s	= SG_T("SMALLINT");
			break;

		case SG_DATATYPE_Short:
			s	= SG_T("SMALLINT");
			break;

		case SG_DATATYPE_Int:
			s	= SG_T("INT");
			break;

		case SG_DATATYPE_Color:
			s	= SG_T("INT");
			break;

		case SG_DATATYPE_Long:
			s	= SG_T("INT");
			break;

		case SG_DATATYPE_Float:
			s	= SG_T("FLOAT");
			break;

		case SG_DATATYPE_Double:
			s	= is_PostgreSQL()	? SG_T("DOUBLE PRECISION")
				: SG_T("DOUBLE");
			break;

		case SG_DATATYPE_Binary:
			s	= is_PostgreSQL()	? SG_T("BYTEA")
				: is_Access()		? SG_T("IMAGE")
				: SG_T("VARBINARY");
			break;
		}

		//-------------------------------------------------
		char	Flag	= (int)Flags.Get_Size() == Table.Get_Field_Count() ? Flags[iField] : 0;

		if( (Flag & SG_ODBC_PRIMARY_KEY) == 0 )
		{
			if( (Flag & SG_ODBC_UNIQUE) != 0 )
			{
				s	+= SG_T(" UNIQUE");
			}

			if( (Flag & SG_ODBC_NOT_NULL) != 0 )
			{
				s	+= SG_T(" NOT NULL");
			}
		}

		//-------------------------------------------------
		if( iField > 0 )
		{
			SQL	+= SG_T(", ");
		}

		SQL	+= CSG_String::Format(SG_T("%s %s"), Table.Get_Field_Name(iField), s.c_str());
	}

	//-----------------------------------------------------
	if( (int)Flags.Get_Size() == Table.Get_Field_Count() )
	{
		CSG_String	s;

		for(iField=0; iField<Table.Get_Field_Count(); iField++)
		{
			if( (Flags[iField] & SG_ODBC_PRIMARY_KEY) != 0 )
			{
				s	+= s.Length() == 0 ? SG_T(", PRIMARY KEY(") : SG_T(", ");
				s	+= Table.Get_Field_Name(iField);
			}
		}

		if( s.Length() > 0 )
		{
			SQL	+= s + SG_T(")");
		}
	}

	//-----------------------------------------------------
	SQL	+= SG_T(")");

	//-----------------------------------------------------
	return( Execute(SQL, bCommit) );
}
//---------------------------------------------------------
bool CGridsFromTableAndGrid::On_Execute(void)
{
	int						iField, iRecord, iAttribute, nAttributes, *Attribute;
	sLong					iCell, jCell;
	CSG_Parameter_Grid_List	*pGrids;
	CSG_Grid				*pClasses;
	CSG_Table				*pTable;

	//-----------------------------------------------------
	pClasses	= Parameters("CLASSES" )->asGrid();
	pGrids		= Parameters("GRIDS"   )->asGridList();
	pTable		= Parameters("TABLE"   )->asTable();
	iField		= Parameters("ID_FIELD")->asInt();

	pGrids->Del_Items();

	if( !pClasses->Set_Index() )
	{
		Error_Set(_TL("index creation failed"));

		return( false );
	}

	//-----------------------------------------------------
	if( pTable->Get_Field_Count() == 0 || pTable->Get_Count() == 0 )
	{
		Message_Add(_TL("selected table contains no valid records"));

		return( false );
	}

	//-----------------------------------------------------
	if( !pTable->Set_Index(iField, TABLE_INDEX_Ascending) )
	{
		Message_Add(_TL("failed to create index for table"));

		return( false );
	}

	//-----------------------------------------------------
	Attribute	= new int[pTable->Get_Field_Count()];

	for(iAttribute=0, nAttributes=0; iAttribute<pTable->Get_Field_Count(); iAttribute++)
	{
		if( iAttribute != iField && pTable->Get_Field_Type(iAttribute) != SG_DATATYPE_String )
		{
			Attribute[nAttributes++]	= iAttribute;

			CSG_Grid	*pGrid	= SG_Create_Grid(*Get_System());

			pGrid->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pClasses->Get_Name(), pTable->Get_Field_Name(iAttribute)));

			pGrids->Add_Item(pGrid);
		}
	}

	if( nAttributes == 0 )
	{
		delete[](Attribute);

		Message_Add(_TL("selected table does not have numeric attributes"));

		return( false );
	}

	//-----------------------------------------------------
	CSG_Table_Record	*pRecord	= pTable->Get_Record_byIndex(0);

	for(iCell=0, iRecord=0; iCell<Get_NCells() && pRecord && Set_Progress_NCells(iCell); iCell++)
	{
		if( pClasses->Get_Sorted(iCell, jCell, false, true) )
		{
			double	valClass	= pClasses->asDouble(jCell);

			while( pRecord && pRecord->asDouble(iField) < valClass )
			{
				pRecord	= pTable->Get_Record_byIndex(++iRecord);
			}

			if( !pRecord || pRecord->asDouble(iField) > valClass )
			{
				for(iAttribute=0; iAttribute<nAttributes; iAttribute++)
				{
					pGrids->asGrid(iAttribute)->Set_NoData(jCell);
				}
			}
			else
			{
				for(iAttribute=0; iAttribute<nAttributes; iAttribute++)
				{
					pGrids->asGrid(iAttribute)->Set_Value(jCell, pRecord->asDouble(Attribute[iAttribute]));
				}
			}
		}
	}

	//-----------------------------------------------------
	delete[](Attribute);

	return true;
}
Example #8
0
//---------------------------------------------------------
bool CSG_Table::_Load_Text(const CSG_String &File_Name, bool bHeadline, const SG_Char *Separator)
{
	int			i, iField, fLength;
	CSG_String	sLine, sField;
	CSG_File	Stream;
	CSG_Table	Table;

	//-----------------------------------------------------
	if( Stream.Open(File_Name, SG_FILE_R, false) == false )
	{
		return( false );
	}

	if( !Stream.Read_Line(sLine) )
	{
		return( false );
	}

	//-----------------------------------------------------
	sLine	+= Separator;

	while( (i = sLine.Find(Separator)) >= 0 )
	{
		sField	= bHeadline ? sLine.Left(i) : CSG_String::Format(SG_T("FIELD_%02d"), Table.Get_Field_Count() + 1);

		if( sField[0] == SG_T('\"') && sField[(int)(sField.Length() - 1)] == SG_T('\"') )	// remove quota
		{
			sField	= sField.AfterFirst('\"').BeforeLast('\"');
		}

		Table.Add_Field(sField, SG_DATATYPE_String);

		sLine.Remove(0, i + 1);
	}

	//-----------------------------------------------------
	TSG_Data_Type	*Type	= new TSG_Data_Type[Table.Get_Field_Count()];

	for(iField=0; iField<Table.Get_Field_Count(); iField++)
	{
		Type[iField]	= SG_DATATYPE_Int;
	}

	if( !bHeadline )
	{
		Stream.Seek_Start();
	}

	fLength	= Stream.Length();

	while( Stream.Read_Line(sLine) && sLine.Length() > 0 && SG_UI_Process_Set_Progress(Stream.Tell(), fLength) )
	{
		CSG_Table_Record	*pRecord	= Table._Add_Record();

		sLine	+= Separator;

		for(iField=0; iField<Table.Get_Field_Count(); iField++)
		{
			if( (i = sLine.Find(Separator)) >= 0 )
			{
				sField	= sLine.Left(i);

				if( sField[0] == SG_T('\"') && sField[(int)(sField.Length() - 1)] == SG_T('\"') )	// remove quota
				{
					sField	= sField.AfterFirst('\"').BeforeLast('\"');
				}

				if( Type[iField] != SG_DATATYPE_String )
				{
					double	Value;

					if( SG_SSCANF(sField, SG_T("%lf"), &Value) != 1 )
					{
						Type[iField]	= SG_DATATYPE_String;
					}
					else if( Type[iField] != SG_DATATYPE_Double && Value - (int)Value != 0.0 )
					{
						Type[iField]	= SG_DATATYPE_Double;
					}
				}

				pRecord->Set_Value(iField, sField);

				sLine.Remove(0, i + 1);
			}
			else
			{
				break;
			}
		}
	}

	//-----------------------------------------------------
	if( Table.Get_Count() > 0 )
	{
		for(iField=0; iField<Table.Get_Field_Count(); iField++)
		{
			Add_Field(Table.Get_Field_Name(iField), Type[iField]);
		}

		for(int iRecord=0; iRecord<Table.Get_Count() && SG_UI_Process_Set_Progress(iRecord, Table.Get_Count()); iRecord++)
		{
			CSG_Table_Record	*pRecord	= _Add_Record();

			for(iField=0; iField<Get_Field_Count(); iField++)
			{
				switch( Get_Field_Type(iField) )
				{
				default:						pRecord->Set_Value(iField, Table[iRecord].asString(iField));	break;
				case SG_DATATYPE_Int:		pRecord->Set_Value(iField, Table[iRecord].asInt   (iField));	break;
				case SG_DATATYPE_Double:	pRecord->Set_Value(iField, Table[iRecord].asDouble(iField));	break;
				}
			}
		}
	}

	delete[](Type);

	SG_UI_Process_Set_Ready();

	return( Get_Field_Count() > 0 );
}
//---------------------------------------------------------
bool CTable_Text_Export::On_Execute(void)
{
    CSG_String	StrFormat, Separator;
    CSG_File	Stream;
    CSG_Table	*pTable;

    //-----------------------------------------------------
    pTable		= Parameters("TABLE"   )->asTable();
    StrFormat	= Parameters("STRQUOTA")->asBool() ? SG_T("\"%s\"") : SG_T("%s");

    switch( Parameters("SEPARATOR")->asInt() )
    {
    case 0:
        Separator	= "\t";
        break;
    case 1:
        Separator	=  ";";
        break;
    case 2:
        Separator	=  ",";
        break;
    case 3:
        Separator	=  " ";
        break;
    default:
        Separator	= Parameters("SEP_OTHER")->asString();
        break;
    }

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

    //-----------------------------------------------------
    else
    {
        if( Parameters("HEADLINE")->asBool() )
        {
            for(int iField=0; iField<pTable->Get_Field_Count(); iField++)
            {
                Stream.Printf(StrFormat.c_str(), pTable->Get_Field_Name(iField));
                Stream.Printf(iField < pTable->Get_Field_Count() - 1 ? Separator.c_str() : SG_T("\n"));
            }
        }

        //-------------------------------------------------
        for(int iRecord=0; iRecord<pTable->Get_Record_Count() && Set_Progress(iRecord, pTable->Get_Record_Count()); iRecord++)
        {
            CSG_Table_Record	*pRecord	= pTable->Get_Record(iRecord);

            for(int iField=0; iField<pTable->Get_Field_Count(); iField++)
            {
                switch( pTable->Get_Field_Type(iField) )
                {
                default:
                case SG_DATATYPE_Char:
                case SG_DATATYPE_String:
                case SG_DATATYPE_Date:
                    Stream.Printf(StrFormat.c_str(), pRecord->asString(iField));
                    break;

                case SG_DATATYPE_Short:
                case SG_DATATYPE_Int:
                case SG_DATATYPE_Color:
                    Stream.Printf(SG_T("%d")	, pRecord->asInt(iField));
                    break;

                case SG_DATATYPE_Long:
                    Stream.Printf(SG_T("%ld")	, (long)pRecord->asDouble(iField));
                    break;

                case SG_DATATYPE_ULong:
                    Stream.Printf(SG_T("%lu")	, (unsigned long)pRecord->asDouble(iField));
                    break;

                case SG_DATATYPE_Float:
                case SG_DATATYPE_Double:
                    Stream.Printf(SG_T("%f")	, pRecord->asDouble(iField));
                    break;
                }

                Stream.Printf(iField < pTable->Get_Field_Count() - 1 ? Separator.c_str() : SG_T("\n"));
            }
        }

        //-------------------------------------------------
        Stream.Close();

        return( true );
    }

    return( false );
}
//---------------------------------------------------------
bool CTable_Field_Deletion::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Parameter_Table_Fields	*pFields	= Parameters("FIELDS")->asTableFields();

	if( pFields->Get_Count() <= 0 )
	{
		Error_Set(_TL("no fields in selection"));

		return( false );
	}

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

	CSG_Table	*pOutput = NULL;

	if( pTable->Get_ObjectType() == DATAOBJECT_TYPE_Shapes )
	{
		if( (pOutput = Parameters("OUT_SHAPES")->asShapes()) != NULL && pOutput != pTable )
		{
			((CSG_Shapes *)pOutput)->Create(((CSG_Shapes *)pTable)->Get_Type(), (const wchar_t*)0, (CSG_Table *)0, ((CSG_Shapes *)pTable)->Get_Vertex_Type());
		}
	}
	else // if( pTable->Get_ObjectType() == DATAOBJECT_TYPE_Table )
	{
		if( (pOutput = Parameters("OUT_TABLE"  )->asTable()) != NULL && pOutput != pTable )
		{
			pOutput->Destroy();
		}
	}

	//-----------------------------------------------------
	if( pOutput )
	{
		int		nFields		= pTable->Get_Field_Count() - pFields->Get_Count();
		int		*pFieldsOut	= new int[nFields];

		int		iField = 0;

		for(int i=0; i<pTable->Get_Field_Count(); i++)
		{
			bool bDelete = false;

			for(int j=0; j<pFields->Get_Count(); j++)
			{
				if( i == pFields->Get_Index(j) )
				{
					bDelete = true;
					break;
				}
			}

			if( !bDelete )
			{
				pFieldsOut[iField] = i;
				iField++;
			}
		}


		pOutput->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pTable->Get_Name(), _TL("Changed")));

		for(iField=0; iField<nFields; iField++)
		{
			pOutput->Add_Field(pTable->Get_Field_Name(pFieldsOut[iField]), pTable->Get_Field_Type(pFieldsOut[iField]));
		}

		for(int iRecord=0; iRecord<pTable->Get_Count(); iRecord++)
		{
			CSG_Table_Record	*pOut, *pIn	= pTable->Get_Record(iRecord);

			if( pOutput->Get_ObjectType() == DATAOBJECT_TYPE_Shapes )
			{
				pOut	= ((CSG_Shapes *)pOutput)->Add_Shape(pIn, SHAPE_COPY_GEOM);

				if( ((CSG_Shapes *)pOutput)->Get_Vertex_Type() > SG_VERTEX_TYPE_XY )
				{
					for(int iPart=0; iPart<((CSG_Shape *)pIn)->Get_Part_Count(); iPart++)
					{
						for(int iPoint=0; iPoint<((CSG_Shape *)pIn)->Get_Point_Count(iPart); iPoint++)
						{
							((CSG_Shape *)pOut)->Set_Z(((CSG_Shape *)pIn)->Get_Z(iPoint, iPart), iPoint, iPart);

							if( ((CSG_Shapes *)pOutput)->Get_Vertex_Type() == SG_VERTEX_TYPE_XYZM )
							{
								((CSG_Shape *)pOut)->Set_M(((CSG_Shape *)pIn)->Get_M(iPoint, iPart), iPoint, iPart);
							}
						}
					}
				}
			}
			else
			{
				pOut	= pOutput->Add_Record();
			}

			for(iField=0; iField<nFields; iField++)
			{
				*pOut->Get_Value(iField)	= *pIn->Get_Value(pFieldsOut[iField]);
			}
		}

		delete[] pFieldsOut;
	}
	else
	{
		for(int iField=pFields->Get_Count()-1; iField>=0; iField--)
		{
			pTable->Del_Field(pFields->Get_Index(pFields->Get_Index(iField)));
		}

		DataObject_Update(pTable);
	}

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