//--------------------------------------------------------- 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 CTable_Fill_Record_Gaps::On_Execute(void) { int Method; CSG_Table *pTable; //----------------------------------------------------- pTable = Parameters("TABLE") ->asTable(); m_pNoGaps = Parameters("NOGAPS") ->asTable(); m_fOrder = Parameters("ORDER") ->asInt(); Method = Parameters("METHOD") ->asInt(); if( pTable->Get_Count() == 0 || !pTable->Set_Index(m_fOrder, TABLE_INDEX_Ascending) ) { return( false ); } //----------------------------------------------------- CSG_Table_Record *pA, *pB; m_pNoGaps->Create(pTable); m_pNoGaps->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pTable->Get_Name(), _TL("no gaps"))); m_pNoGaps->Add_Record(pB = pTable->Get_Record(0)); //----------------------------------------------------- for(int iRecord=1; iRecord<pTable->Get_Count() && Set_Progress(iRecord, pTable->Get_Count()-1); iRecord++) { pA = pB; pB = pTable->Get_Record(iRecord); int iA = pA->asInt(m_fOrder); int iB = pB->asInt(m_fOrder); if( iB - iA > 1 ) { int iStart = m_pNoGaps->Get_Count(); for(int i=iA+1; i<iB; i++) { m_pNoGaps->Add_Record()->Set_Value(m_fOrder, i); } for(int iField=0; iField<pTable->Get_Field_Count(); iField++) { if( iField != m_fOrder && ::SG_Data_Type_is_Numeric(pTable->Get_Field_Type(iField)) ) { switch( Method ) { case 0: Set_Nearest (iStart, iField, pA, pB); break; case 1: Set_Linear (iStart, iField, pA, pB); break; case 2: Set_Spline (iStart, iField, pTable->Get_Record(iRecord - 2), pA, pB, pTable->Get_Record(iRecord + 1)); break; } } } } m_pNoGaps->Add_Record(pB); } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CSG_ODBC_Connection::_Table_Load(CSG_Table &Table, const CSG_String &Select, const CSG_String &Name, bool bLOB) { //----------------------------------------------------- if( !is_Connected() ) { _Error_Message(_TL("no database connection")); return( false ); } //----------------------------------------------------- try { int valInt, iField, nFields; long valLong; float valFloat; double valDouble; std_string valString; otl_long_string valRaw(m_Connection.get_max_long_size()); otl_column_desc *Fields; otl_stream Stream; CSG_Bytes BLOB; Stream.set_all_column_types (otl_all_date2str); Stream.set_lob_stream_mode (bLOB); Stream.open (bLOB ? 1 : m_Size_Buffer, Select, m_Connection); Fields = Stream.describe_select(nFields); if( Fields == NULL || nFields <= 0 ) { _Error_Message(_TL("no fields in selection")); return( false ); } //------------------------------------------------- Table.Destroy(); Table.Set_Name(Name); for(iField=0; iField<nFields; iField++) { if( _Get_Type_From_SQL(Fields[iField].otl_var_dbtype) == SG_DATATYPE_Undefined ) { return( false ); } Table.Add_Field(Fields[iField].name, _Get_Type_From_SQL(Fields[iField].otl_var_dbtype)); } //------------------------------------------------- while( !Stream.eof() && SG_UI_Process_Get_Okay() ) // while not end-of-data { CSG_Table_Record *pRecord = Table.Add_Record(); for(iField=0; iField<nFields; iField++) { switch( Table.Get_Field_Type(iField) ) { case SG_DATATYPE_String: Stream >> valString; if( Stream.is_null() ) pRecord->Set_NoData(iField); else pRecord->Set_Value(iField, CSG_String(valString.c_str())); break; case SG_DATATYPE_Short: case SG_DATATYPE_Int: Stream >> valInt; if( Stream.is_null() ) pRecord->Set_NoData(iField); else pRecord->Set_Value(iField, valInt); break; case SG_DATATYPE_DWord: case SG_DATATYPE_Long: Stream >> valLong; if( Stream.is_null() ) pRecord->Set_NoData(iField); else pRecord->Set_Value(iField, valLong); break; case SG_DATATYPE_Float: Stream >> valFloat; if( Stream.is_null() ) pRecord->Set_NoData(iField); else pRecord->Set_Value(iField, valFloat); break; case SG_DATATYPE_Double: Stream >> valDouble; if( Stream.is_null() ) pRecord->Set_NoData(iField); else pRecord->Set_Value(iField, valDouble); break; case SG_DATATYPE_Binary: Stream >> valRaw; if( Stream.is_null() ) pRecord->Set_NoData(iField); else { BLOB.Clear(); for(int i=0; i<valRaw.len(); i++) { BLOB.Add((BYTE)valRaw[i]); } pRecord->Set_Value(iField, BLOB); } break; } } } } //----------------------------------------------------- catch( otl_exception &e ) { _Error_Message(e); return( false ); } return( true ); }
//--------------------------------------------------------- bool CSG_ODBC_Connection::Table_Insert(const CSG_String &Table_Name, const CSG_Table &Table, bool bCommit) { //----------------------------------------------------- if( !is_Connected() ) { _Error_Message(_TL("no database connection")); return( false ); } if( !Table_Exists(Table_Name) ) { return( false ); } CSG_Table Fields = Get_Field_Desc(Table_Name); if( Fields.Get_Count() != Table.Get_Field_Count() ) { return( false ); } //----------------------------------------------------- try { bool bLOB = false; int iField, iRecord; CSG_String Insert; otl_stream Stream; //------------------------------------------------- Insert.Printf(SG_T("INSERT INTO %s VALUES("), Table_Name.c_str()); for(iField=0; iField<Table.Get_Field_Count(); iField++) { if( iField > 0 ) { Insert += SG_T(","); } Insert += CSG_String::Format(SG_T(":f%d"), 1 + iField); switch( Table.Get_Field_Type(iField) ) { default: case SG_DATATYPE_String: Insert += SG_T("<varchar>"); break; case SG_DATATYPE_Date: Insert += SG_T("<char[12]>"); break; case SG_DATATYPE_Char: Insert += SG_T("<char>"); break; case SG_DATATYPE_Short: Insert += SG_T("<short>"); break; case SG_DATATYPE_Int: Insert += SG_T("<int>"); break; case SG_DATATYPE_Color: Insert += SG_T("<long>"); break; case SG_DATATYPE_Long: Insert += SG_T("<long>"); break; case SG_DATATYPE_Float: Insert += SG_T("<float>"); break; case SG_DATATYPE_Double: Insert += SG_T("<double>"); break; } } Insert += SG_T(")"); Stream.set_all_column_types(otl_all_date2str); Stream.set_lob_stream_mode(bLOB); Stream.open(bLOB ? 1 : m_Size_Buffer, Insert, m_Connection); std_string valString; //------------------------------------------------- for(iRecord=0; iRecord<Table.Get_Count() && SG_UI_Process_Set_Progress(iRecord, Table.Get_Count()); iRecord++) { CSG_Table_Record *pRecord = Table.Get_Record(iRecord); for(iField=0; iField<Table.Get_Field_Count(); iField++) { if( pRecord->is_NoData(iField) ) { Stream << otl_null(); } else switch( Table.Get_Field_Type(iField) ) { default: case SG_DATATYPE_String: case SG_DATATYPE_Date: valString = CSG_String(pRecord->asString(iField)); Stream << valString; break; case SG_DATATYPE_Char: Stream << (char)pRecord->asChar (iField); break; case SG_DATATYPE_Short: Stream << pRecord->asShort (iField); break; case SG_DATATYPE_Int: Stream << pRecord->asInt (iField); break; case SG_DATATYPE_Color: case SG_DATATYPE_Long: Stream << (long)pRecord->asInt (iField); break; case SG_DATATYPE_Float: Stream << pRecord->asFloat (iField); break; case SG_DATATYPE_Double: Stream << pRecord->asDouble(iField); break; } } } } //----------------------------------------------------- catch( otl_exception &e ) { _Error_Message(e); return( false ); } return( true ); }
//--------------------------------------------------------- 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 CTable_Record_Statistics_Base::On_Execute(void) { //----------------------------------------------------- CSG_Table *pTable = Parameters("TABLE")->asTable(); if( !pTable->is_Valid() || pTable->Get_Field_Count() <= 0 || pTable->Get_Record_Count() <= 0 ) { Error_Set(_TL("invalid table")); return( false ); } //----------------------------------------------------- CSG_Array_Int _Fields; int *Fields = (int *)Parameters("FIELDS")->asPointer(); int nFields = Parameters("FIELDS")->asInt (); if( nFields == 0 ) { for(int i=0; i<pTable->Get_Field_Count(); i++) { if( SG_Data_Type_is_Numeric(pTable->Get_Field_Type(i)) ) { _Fields.Inc_Array(); _Fields[nFields++] = i; } } if( nFields == 0 ) { Error_Set(_TL("could not find any numeric attribute field")); return( false ); } Fields = _Fields.Get_Array(); } //----------------------------------------------------- if( Parameters("RESULT")->asTable() && Parameters("RESULT")->asTable() != pTable ) { pTable = Parameters("RESULT")->asTable(); pTable->Create( *Parameters("TABLE")->asTable()); pTable->Set_Name(Parameters("TABLE")->asTable()->Get_Name()); } //----------------------------------------------------- double Quantile = Parameters("PCTL_VAL")->asDouble(); int offResult = pTable->Get_Field_Count(); bool bStats[STATS_COUNT]; { for(int i=0; i<STATS_COUNT; i++) { if( (bStats[i] = Parameters(STATS[i][0])->asBool()) == true ) { pTable->Add_Field(STATS[i][1], SG_DATATYPE_Double); } } if( pTable->Get_Field_Count() == offResult ) { Error_Set(_TL("no statistical measure has been selected")); return( false ); } } //----------------------------------------------------- for(int iRecord=0; iRecord<pTable->Get_Count() && Set_Progress(iRecord, pTable->Get_Count()); iRecord++) { CSG_Table_Record *pRecord = pTable->Get_Record(iRecord); CSG_Simple_Statistics s(bStats[STATS_PCTL]); for(int iField=0; iField<nFields; iField++) { if( !pRecord->is_NoData(Fields[iField]) ) { s += pRecord->asDouble(Fields[iField]); } } //------------------------------------------------- int iField = offResult; if( s.Get_Count() > 0 ) { if( bStats[STATS_MEAN ] ) pRecord->Set_Value(iField++, s.Get_Mean ()); if( bStats[STATS_MIN ] ) pRecord->Set_Value(iField++, s.Get_Minimum ()); if( bStats[STATS_MAX ] ) pRecord->Set_Value(iField++, s.Get_Maximum ()); if( bStats[STATS_RANGE] ) pRecord->Set_Value(iField++, s.Get_Range ()); if( bStats[STATS_SUM ] ) pRecord->Set_Value(iField++, s.Get_Sum ()); if( bStats[STATS_NUM ] ) pRecord->Set_Value(iField++, s.Get_Count ()); if( bStats[STATS_VAR ] ) pRecord->Set_Value(iField++, s.Get_Variance()); if( bStats[STATS_STDV ] ) pRecord->Set_Value(iField++, s.Get_StdDev ()); if( bStats[STATS_PCTL ] ) pRecord->Set_Value(iField++, s.Get_Quantile(Quantile)); } else { for(int i=0; i<STATS_COUNT; i++) { if( bStats[i] ) { pRecord->Set_NoData(iField++); } } } } //----------------------------------------------------- if( pTable == Parameters("TABLE")->asTable() ) { DataObject_Update(pTable); } return( true ); }
//--------------------------------------------------------- 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; }
//--------------------------------------------------------- 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 ); }