//--------------------------------------------------------- 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_Table::_Load_Text(const CSG_String &File_Name, bool bHeadline, const SG_Char *Separator) { int i, iField; long fLength; CSG_String sLine, sField; CSG_File Stream; CSG_Table Table; //----------------------------------------------------- if( Stream.Open(File_Name, SG_FILE_R, false) == false ) { return( false ); } if( (fLength = Stream.Length()) <= 0 ) { return( false ); } if( !Stream.Read_Line(sLine) ) { return( false ); } //----------------------------------------------------- sLine += Separator; while( (i = sLine.Find(Separator)) >= 0 ) { sField.Clear(); if( bHeadline ) { sField = sLine.Left(i); if( sField[0] == SG_T('\"') && sField[(int)(sField.Length() - 1)] == SG_T('\"') ) // remove quota { sField = sField.AfterFirst('\"').BeforeLast('\"'); } } if( sField.Length() == 0 ) { sField.Printf(SG_T("F%02d"), Table.Get_Field_Count() + 1); } 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(); } 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('\"'); Type[iField] = SG_DATATYPE_String; } if( Type[iField] != SG_DATATYPE_String && sField.Length() > 0 ) { 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++) { if( *Table[iRecord].asString(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; } } else { pRecord->Set_NoData(iField); } } } } delete[](Type); SG_UI_Process_Set_Ready(); return( Get_Field_Count() > 0 ); }
//--------------------------------------------------------- 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 CSoil_Texture_Table::On_Execute(void) { //----------------------------------------------------- CSG_Table *pTable = Parameters("TABLE")->asTable(); int iSand = Parameters("SAND" )->asInt(); int iSilt = Parameters("SILT" )->asInt(); int iClay = Parameters("CLAY" )->asInt(); int iTexture = Parameters("TEXTURE")->asInt(); //----------------------------------------------------- if( (iSand >= 0 ? 1 : 0) + (iSilt >= 0 ? 1 : 0) + (iClay >= 0 ? 1 : 0) < 2 ) { Error_Set(_TL("at least two contents (sand, silt, clay) have to be given")); return( false ); } //----------------------------------------------------- if( iTexture < 0 ) { iTexture = pTable->Get_Field_Count(); pTable->Add_Field("TEXTURE", SG_DATATYPE_String); } //----------------------------------------------------- for(int i=0; i<pTable->Get_Count() && Set_Progress(i, pTable->Get_Count()); i++) { CSG_Table_Record *pRecord = pTable->Get_Record(i); if( (iSand >= 0 && pRecord->is_NoData(iSand)) || (iSilt >= 0 && pRecord->is_NoData(iSilt)) || (iClay >= 0 && pRecord->is_NoData(iClay)) ) { pRecord->Set_NoData(iTexture); } else { int Class = -1; if( iSand >= 0 && iSilt >= 0 && iClay >= 0 ) { double Sum; Class = Get_Texture(pRecord->asDouble(iSand), pRecord->asDouble(iSilt), pRecord->asDouble(iClay), Sum); } else if( iSilt < 0 ) { Class = Get_Texture_SandClay(pRecord->asDouble(iSand), pRecord->asDouble(iClay)); } else if( iClay < 0 ) { Class = Get_Texture_SandSilt(pRecord->asDouble(iSand), pRecord->asDouble(iSilt)); } else if( iSand < 0 ) { Class = Get_Texture_SiltClay(pRecord->asDouble(iSilt), pRecord->asDouble(iClay)); } pRecord->Set_Value (iTexture, Classes[Class].Key); } } DataObject_Update(pTable); //----------------------------------------------------- CSG_Parameter *pLUT = DataObject_Get_Parameter(pTable, "LUT"); if( pLUT && pLUT->asTable() ) { CSG_Table *pClasses = pLUT->asTable(); for(int iClass=0; iClass<12; iClass++) { CSG_Table_Record *pClass = pClasses->Get_Record(iClass); if( pClass == NULL ) { pClass = pClasses->Add_Record(); } pClass->Set_Value(0, Classes[iClass].Color); pClass->Set_Value(1, Classes[iClass].Name); pClass->Set_Value(2, Classes[iClass].Name); pClass->Set_Value(3, Classes[iClass].Key); pClass->Set_Value(4, Classes[iClass].Key); } while( pClasses->Get_Count() > 12 ) { pClasses->Del_Record(pClasses->Get_Count() - 1); } DataObject_Set_Parameter(pTable, pLUT); // Lookup Table DataObject_Set_Parameter(pTable, "LUT_ATTRIB" , iTexture); // Lookup Table Attribute DataObject_Set_Parameter(pTable, "COLORS_TYPE", 1 ); // Color Classification Type: Lookup Table } //----------------------------------------------------- return( true ); }