//--------------------------------------------------------- void CVIEW_Table_Control::On_Field_Rename(wxCommandEvent &event) { int i; CSG_Parameters P; P.Set_Name(_TL("Rename Fields")); for(i=0; i<m_pTable->Get_Field_Count(); i++) { P.Add_String(NULL, "", m_pTable->Get_Field_Name(i), _TL(""), m_pTable->Get_Field_Name(i)); } //----------------------------------------------------- if( DLG_Parameters(&P) ) { for(i=0; i<m_pTable->Get_Field_Count(); i++) { CSG_String s(P(i)->asString()); if( s.Length() > 0 && s.Cmp(m_pTable->Get_Field_Name(i)) ) { m_pTable->Set_Field_Name(i, s); SetColLabelValue(i, s.c_str()); } } g_pData->Update(m_pTable, NULL); } }
//--------------------------------------------------------- void CVIEW_Table_Control::On_Field_Del(wxCommandEvent &event) { int i; CSG_Parameters P; //----------------------------------------------------- P.Set_Name(_TL("Delete Fields")); for(i=0; i<m_pTable->Get_Field_Count(); i++) { P.Add_Value(NULL, SG_Get_String(i, 0), m_pTable->Get_Field_Name(i), _TL(""), PARAMETER_TYPE_Bool, false); } //----------------------------------------------------- if( DLG_Parameters(&P) ) { for(i=m_pTable->Get_Field_Count()-1; i>=0; i--) { if( P(SG_Get_String(i, 0))->asBool() ) { m_pTable->Del_Field(i); DeleteCols(i); } } g_pData->Update(m_pTable, NULL); } }
bool Cihacres_elev::_CreateDialog3() { CSG_String s; CSG_Parameters P; // used to add Parameters in the second dialog CSG_Parameter *pNode; // Dialog design P.Set_Name(_TL("Choose Time Range")); s.Printf(SG_T("Node1"), 1); pNode = P.Add_Node(NULL,s,SG_T("Time Range"),_TL("")); s.Printf(SG_T("FDAY") , 1-1); P.Add_String(pNode,s,_TL("First Day"),_TL(""), m_p_InputTable->Get_Record(0)->asString(m_dateField)); s.Printf(SG_T("LDAY") , 1-2); P.Add_String(pNode,s,_TL("Last Day"),_TL(""), m_p_InputTable->Get_Record(m_p_InputTable->Get_Record_Count()-1)->asString(m_dateField)); if( SG_UI_Dlg_Parameters(&P, _TL("Choose Time Range")) ) { /////////////////////////////////////////////////////////////// // // ASSIGN DATA FROM SECOND DIALOG // /////////////////////////////////////////////////////////////// m_date1 = P(CSG_String::Format(SG_T("FDAY"),m_dateField).c_str())->asString(); m_date2 = P(CSG_String::Format(SG_T("LDAY"),m_streamflowField).c_str())->asString(); return(true); } return(false); }
//--------------------------------------------------------- int CDecision_Tree::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter) { if( CSG_String(pParameter->Get_Identifier()).Find(SG_T("NODE")) >= 0 ) { CSG_String ID = CSG_String(pParameter->Get_Identifier()).BeforeFirst(SG_T('|')); if( pParameter->asBool() ) { CSG_Parameters *pDecision = pParameters->Get_Parameter(ID)->asParameters(); pDecision->Set_Name(CSG_String::Format(SG_T("%s|%s"), pParameters->Get_Name().c_str(), pParameters->Get_Parameter(ID + SG_T("|NAME"))->asString())); Add_Decision(pDecision); } else { pParameters->Get_Parameter(ID)->asParameters()->Del_Parameters(); } } if( !SG_STR_CMP(pParameter->Get_Identifier(), SG_T("GRID")) ) { if( pParameter->asGrid() ) { pParameters->Get_Parameter("THRESHOLD")->Set_Value(pParameter->asGrid()->Get_ArithMean()); } } return( 0 ); }
//--------------------------------------------------------- void CActive_Attributes_Control::On_Field_Rename(wxCommandEvent &event) { if( !g_pActive->Get_Active_Data_Item() || g_pActive->Get_Active_Data_Item()->Get_Type() != WKSP_ITEM_Grids ) { return; } CSG_Grids *pGrids = ((CWKSP_Grids *)g_pActive->Get_Active_Data_Item())->Get_Grids(); //----------------------------------------------------- int i; CSG_Parameters P; P.Set_Name(_TL("Rename Fields")); for(i=0; i<m_pTable->Get_Field_Count(); i++) { P.Add_String(NULL, SG_Get_String(i), m_pTable->Get_Field_Name(i), _TL(""), m_pTable->Get_Field_Name(i)); } //----------------------------------------------------- if( DLG_Parameters(&P) ) { for(i=0; i<m_pTable->Get_Field_Count(); i++) { CSG_String s(P(i)->asString()); if( s.Length() > 0 && s.Cmp(m_pTable->Get_Field_Name(i)) ) { pGrids->Get_Attributes_Ptr()->Set_Field_Name(i, s); SetColLabelValue(i, s.c_str()); } } g_pData->Update(pGrids, NULL); } }
//--------------------------------------------------------- void CActive_Attributes_Control::On_Field_Del(wxCommandEvent &event) { if( !g_pActive->Get_Active_Data_Item() || g_pActive->Get_Active_Data_Item()->Get_Type() != WKSP_ITEM_Grids ) { return; } CSG_Grids *pGrids = ((CWKSP_Grids *)g_pActive->Get_Active_Data_Item())->Get_Grids(); //----------------------------------------------------- int i; CSG_Parameters P; //----------------------------------------------------- P.Set_Name(_TL("Delete Fields")); for(i=0; i<m_pTable->Get_Field_Count(); i++) { P.Add_Value(NULL, SG_Get_String(i), m_pTable->Get_Field_Name(i), _TL(""), PARAMETER_TYPE_Bool, false); } //----------------------------------------------------- if( DLG_Parameters(&P) ) { for(i=m_pTable->Get_Field_Count()-1; i>=0; i--) { if( P(SG_Get_String(i))->asBool() ) { pGrids->Get_Attributes_Ptr()->Del_Field(i); DeleteCols(i); } } g_pData->Update(pGrids, NULL); } }
//--------------------------------------------------------- int CGW_Multi_Regression::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter) { if( m_Grid_Target.On_User_Changed(pParameters, pParameter) ) { return( true ); } if( !SG_STR_CMP(pParameter->Get_Identifier(), SG_T("POINTS")) ) { CSG_Shapes *pPoints = pParameters->Get_Parameter("POINTS") ->asShapes(); CSG_Parameters *pAttributes = pParameters->Get_Parameter("PREDICTORS") ->asParameters(); pAttributes->Destroy(); pAttributes->Set_Name(_TL("Predictors")); for(int i=0; pPoints && i<pPoints->Get_Field_Count(); i++) { switch( pPoints->Get_Field_Type(i) ) { default: // not numeric break; case SG_DATATYPE_Byte: case SG_DATATYPE_Char: case SG_DATATYPE_Word: case SG_DATATYPE_Short: case SG_DATATYPE_DWord: case SG_DATATYPE_Int: case SG_DATATYPE_ULong: case SG_DATATYPE_Long: case SG_DATATYPE_Float: case SG_DATATYPE_Double: pAttributes->Add_Value( NULL, SG_Get_String(i, 0), pPoints->Get_Field_Name(i), _TL(""), PARAMETER_TYPE_Bool, false ); break; } } return( true ); } return( false ); }
//--------------------------------------------------------- void CVIEW_Table_Control::On_Field_Sort(wxCommandEvent &event) { CSG_String sFields, sOrder; CSG_Parameter *pNode; CSG_Parameters P; //----------------------------------------------------- for(int i=0; i<m_pTable->Get_Field_Count(); i++) { sFields.Append(m_pTable->Get_Field_Name(i)); sFields.Append('|'); } sOrder.Printf(SG_T("%s|%s|%s|"), _TL("unsorted"), _TL("ascending"), _TL("descending") ); //----------------------------------------------------- P.Set_Name(_TL("Sort Table")); pNode = P.Add_Choice(NULL , "FIELD_1" , _TL("Sort first by") , _TL(""), sFields , !m_pTable->is_Indexed() ? 0 : m_pTable->Get_Index_Field(0)); pNode = P.Add_Choice(pNode, "ORDER_1" , _TL("Direction") , _TL(""), sOrder , !m_pTable->is_Indexed() ? 1 : m_pTable->Get_Index_Order(0)); pNode = P.Add_Choice(NULL , "FIELD_2" , _TL("Sort second by") , _TL(""), sFields , !m_pTable->is_Indexed() ? 0 : m_pTable->Get_Index_Field(1)); pNode = P.Add_Choice(pNode, "ORDER_2" , _TL("Direction") , _TL(""), sOrder , !m_pTable->is_Indexed() ? 0 : m_pTable->Get_Index_Order(1)); pNode = P.Add_Choice(NULL , "FIELD_3" , _TL("Sort third by") , _TL(""), sFields , !m_pTable->is_Indexed() ? 0 : m_pTable->Get_Index_Field(2)); pNode = P.Add_Choice(pNode, "ORDER_3" , _TL("Direction") , _TL(""), sOrder , !m_pTable->is_Indexed() ? 0 : m_pTable->Get_Index_Order(2)); //----------------------------------------------------- if( DLG_Parameters(&P) ) { m_pTable->Set_Index( P("FIELD_1")->asInt(), P("ORDER_1")->asInt() == 1 ? TABLE_INDEX_Ascending : P("ORDER_1")->asInt() == 2 ? TABLE_INDEX_Descending : TABLE_INDEX_None, P("FIELD_2")->asInt(), P("ORDER_2")->asInt() == 1 ? TABLE_INDEX_Ascending : P("ORDER_2")->asInt() == 2 ? TABLE_INDEX_Descending : TABLE_INDEX_None, P("FIELD_3")->asInt(), P("ORDER_3")->asInt() == 1 ? TABLE_INDEX_Ascending : P("ORDER_3")->asInt() == 2 ? TABLE_INDEX_Descending : TABLE_INDEX_None ); _Set_Records(); } }
//--------------------------------------------------------- void CVIEW_Table_Control::On_Field_Type(wxCommandEvent &event) { int i, *Types = new int[m_pTable->Get_Field_Count()]; CSG_Parameters P; P.Set_Name(_TL("Change Field Type")); for(i=0; i<m_pTable->Get_Field_Count(); i++) { switch( m_pTable->Get_Field_Type(i) ) { default: case SG_DATATYPE_String: Types[i] = 0; break; case SG_DATATYPE_Date: Types[i] = 1; break; case SG_DATATYPE_Color: Types[i] = 2; break; case SG_DATATYPE_Byte: Types[i] = 3; break; case SG_DATATYPE_Char: Types[i] = 4; break; case SG_DATATYPE_Word: Types[i] = 5; break; case SG_DATATYPE_Short: Types[i] = 6; break; case SG_DATATYPE_DWord: Types[i] = 7; break; case SG_DATATYPE_Int: Types[i] = 8; break; case SG_DATATYPE_ULong: Types[i] = 9; break; case SG_DATATYPE_Long: Types[i] = 10; break; case SG_DATATYPE_Float: Types[i] = 11; break; case SG_DATATYPE_Double: Types[i] = 12; break; case SG_DATATYPE_Binary: Types[i] = 13; break; } P.Add_Choice(NULL, "", m_pTable->Get_Field_Name(i), _TL(""), CSG_String::Format(SG_T("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|"), SG_Data_Type_Get_Name(SG_DATATYPE_String).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Date ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Color ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Byte ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Char ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Word ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Short ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_DWord ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Int ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_ULong ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Long ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Float ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Double).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Binary).c_str() ), Types[i] ); } //----------------------------------------------------- if( DLG_Parameters(&P) ) { bool bChanged = false; for(i=0; i<m_pTable->Get_Field_Count(); i++) { TSG_Data_Type Type; switch( P(i)->asInt() ) { default: case 0: Type = SG_DATATYPE_String; break; case 1: Type = SG_DATATYPE_Date; break; case 2: Type = SG_DATATYPE_Color; break; case 3: Type = SG_DATATYPE_Byte; break; case 4: Type = SG_DATATYPE_Char; break; case 5: Type = SG_DATATYPE_Word; break; case 6: Type = SG_DATATYPE_Short; break; case 7: Type = SG_DATATYPE_DWord; break; case 8: Type = SG_DATATYPE_Int; break; case 9: Type = SG_DATATYPE_ULong; break; case 10: Type = SG_DATATYPE_Long; break; case 11: Type = SG_DATATYPE_Float; break; case 12: Type = SG_DATATYPE_Double; break; case 13: Type = SG_DATATYPE_Binary; break; } if( Type != Types[i] ) { m_pTable->Set_Field_Type(i, Type); bChanged = true; } } if( bChanged ) { Update_Table(); g_pData->Update(m_pTable, NULL); } } }
//--------------------------------------------------------- void CVIEW_Table_Control::On_Field_Add(wxCommandEvent &event) { int i; CSG_String sFields; CSG_Parameters P; //----------------------------------------------------- for(i=0; i<m_pTable->Get_Field_Count(); i++) { sFields.Append(m_pTable->Get_Field_Name(i)); sFields.Append(wxT('|')); } //----------------------------------------------------- P.Set_Name(_TL("Add Field")); P.Add_String( NULL , "NAME" , _TL("Name"), _TL(""), _TL("Field") ); P.Add_Choice( NULL , "TYPE" , _TL("Field Type"), _TL(""), CSG_String::Format(SG_T("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|"), SG_Data_Type_Get_Name(SG_DATATYPE_String).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Date ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Color ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Byte ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Char ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Word ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Short ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_DWord ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Int ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_ULong ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Long ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Float ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Double).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Binary).c_str() ), 0 ); P.Add_Choice( NULL , "FIELD" , _TL("Insert Position"), _TL(""), sFields, m_pTable->Get_Field_Count() - 1 ); P.Add_Choice( NULL , "INSERT" , _TL("Insert Method"), _TL(""), CSG_String::Format(SG_T("%s|%s|"), _TL("before"), _TL("after") ), 1 ); //----------------------------------------------------- if( DLG_Parameters(&P) ) { int Position; TSG_Data_Type Type; switch( P("TYPE")->asInt() ) { default: case 0: Type = SG_DATATYPE_String; break; case 1: Type = SG_DATATYPE_Date; break; case 2: Type = SG_DATATYPE_Color; break; case 3: Type = SG_DATATYPE_Byte; break; case 4: Type = SG_DATATYPE_Char; break; case 5: Type = SG_DATATYPE_Word; break; case 6: Type = SG_DATATYPE_Short; break; case 7: Type = SG_DATATYPE_DWord; break; case 8: Type = SG_DATATYPE_Int; break; case 9: Type = SG_DATATYPE_ULong; break; case 10: Type = SG_DATATYPE_Long; break; case 11: Type = SG_DATATYPE_Float; break; case 12: Type = SG_DATATYPE_Double; break; case 13: Type = SG_DATATYPE_Binary; break; } Position = P("FIELD")->asInt() + P("INSERT")->asInt(); m_pTable->Add_Field(P("NAME")->asString(), Type, Position); g_pData->Update(m_pTable, NULL); } }
bool Cihacres_elev::_CreateDialog2() { int i; //std::ofstream f("_out_elev.txt"); CSG_Parameters P; // used to add Parameters in the second dialog CSG_Parameter *pNode, *pNode1; CSG_String s; CSG_String tmpNode, tmpName; P.Set_Name(_TL("IHACRES Elevation Bands (Dialog 2)")); // Input file ---------------------------------------------- pNode = P.Add_Table( NULL , "TABLE" , _TL("IHACRES Input Table"), _TL(""), PARAMETER_INPUT ); P.Add_Table_Field( pNode , "DATE_Field" , _TL("Date Column"), SG_T("Select the column containing the Date") ); P.Add_Table_Field( pNode , "DISCHARGE_Field" , _TL("Streamflow (obs.) Column"), SG_T("Select the column containing the observed streamflow time series)") ); for (i = 0; i < m_nElevBands; i++) { tmpNode = convert_sl::Int2String(i+1).c_str(); //s.Printf(tmpNode.c_str(), i); //pNode1 = P.Add_Node(NULL,s,SG_T("Elevation Band Input",_TL("")); tmpName = SG_T("PCP Column: Elevation Band: "); tmpName+=tmpNode; P.Add_Table_Field( pNode , tmpName.c_str(), tmpName.c_str(), SG_T("Select Precipitation Column") ); tmpName = SG_T("TMP Column: Elevation Band: "); tmpName+=tmpNode; P.Add_Table_Field( pNode , tmpName.c_str() , tmpName.c_str(), SG_T("Select Temperature Column") ); } // Input file ---------------------------------------------- for (i = 0; i < m_nElevBands; i++) { tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+100).c_str(); tmpName = SG_T("Elevation Band "); tmpName+=convert_sl::Int2String(i+1).c_str(); s.Printf(tmpNode.c_str(), i+100); pNode = P.Add_Node(NULL,s,tmpName.c_str(),_TL("")); tmpName = SG_T("Area [km2] Elev("); tmpName += tmpNode; tmpName += SG_T(")"); P.Add_Value( pNode, tmpName, _TL("Area [km2]"), _TL(""), PARAMETER_TYPE_Double ); tmpName = SG_T("Mean Elevation [m.a.s.l] Elev("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode, tmpName, _TL("Mean Elevation [m.a.s.l]"), _TL(""), PARAMETER_TYPE_Double ); //} // Parameters of non-linear module ------------------------- tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+150).c_str(); s.Printf(tmpNode.c_str(), i+150); pNode1 = P.Add_Node(pNode,s,SG_T("Non-Linear Module"),_TL("")); tmpName = SG_T("TwFAC("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1, tmpName, _TL("Wetness decline time constant (Tw)"), _TW("Tw is approximately the time constant, or inversely, " "the rate at which the catchment wetness declines in the absence of rainfall"), PARAMETER_TYPE_Double, 1.0, 0.01, true, 150.0, true ); tmpName = SG_T("TFAC("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1, tmpName, SG_T("Temperature Modulation Factor (f)"), _TL("Temperature Modulation Factor f"), PARAMETER_TYPE_Double, 1.0, 0.0001, true, 10.0, true ); tmpName = SG_T("CFAC("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1,tmpName, _TL("Parameter (c)"), _TL("Parameter (c) to fit streamflow volume"), PARAMETER_TYPE_Double, 0.001, 0.0, true, 1.0, true ); switch(m_IHAC_version) { case 0: // Jakeman & Hornberger (1993) break; case 1: // Croke et al. (2005) tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+200).c_str(); s.Printf(tmpNode.c_str(), i+200); pNode1 = P.Add_Node(pNode,s,SG_T("Soil Moisture Power Eq."),_TL("")); tmpName = SG_T("LFAC("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1, tmpName, _TL("Parameter (l)"), _TL("Soil moisture index threshold"), PARAMETER_TYPE_Double, 0.0, 0.0, true, 5.0, true ); tmpName = SG_T("PFAC("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1, tmpName, _TL("Parameter (p)"), _TL("non-linear response term"), PARAMETER_TYPE_Double, 0.0, 0.0, true, 5.0, true ); break; } // Parameters of non-linear module ------------------------- // Parameters of linear module ----------------------------- switch(m_StorConf) { case 0: // single storage tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+250).c_str(); s.Printf(tmpNode.c_str(), i+250); pNode1 = P.Add_Node(pNode,s,SG_T("Linear Module"),_TL("")); tmpName = SG_T("AFAC("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1, tmpName, _TL("(a)"), _TL(""), PARAMETER_TYPE_Double, -0.8, -0.99, true, -0.01, true ); tmpName = SG_T("BFAC("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1, tmpName, _TL("(b)"), _TL(""), PARAMETER_TYPE_Double, 0.2, 0.001, true, 1.0, true ); break; case 1: // two parallel storages tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+250).c_str(); s.Printf(tmpNode.c_str(), i+250); pNode1 = P.Add_Node(pNode,s,SG_T("Linear Module"),_TL("")); // Parameter a tmpName = SG_T("AQ("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1,tmpName, _TL("a(q)"), _TL(""), PARAMETER_TYPE_Double, -0.7, -0.99, true, -0.01, true ); tmpName = SG_T("AS("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1, tmpName, _TL("a(s)"), _TL(""), PARAMETER_TYPE_Double, -0.9, -0.99, true, -0.01, true ); // Parameter b tmpName = SG_T("BQ("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1, tmpName, _TL("b(q)"), _TL(""), PARAMETER_TYPE_Double, 0.0, 0.0, true, 1.0, true ); break; case 2: // two storages in series break; } // end switch (storconf) // Parameters of linear module ----------------------------- tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+300).c_str(); s.Printf(tmpNode.c_str(), i+300); pNode1 = P.Add_Node(pNode,s,SG_T("Time Delay after Start of Rainfall (INTEGER)"),_TL("")); tmpName = SG_T("DELAY("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1,tmpName, SG_T("Time Delay (Rain-Runoff)"), SG_T("The delay after the start of rainfall, before the discharge starts to rise."), PARAMETER_TYPE_Int, 0, 1, true, 100, true ); // snow module parameters ---------------------------------- if (m_bSnowModule) { tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+350).c_str(); s.Printf(tmpNode.c_str(), i+350); pNode1 = P.Add_Node(pNode,s,SG_T("Snow Module Parameters"),_TL("")); tmpName = SG_T("T_RAIN("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1,tmpName, SG_T("Temperature Threshold for Rainfall)"), SG_T("Below this threshold precipitation will fall as snow"), PARAMETER_TYPE_Double, -1.0, -10.0, true, 10.0, true ); tmpName = SG_T("T_MELT("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1,tmpName, SG_T("Temperature Threshold for Melting"), SG_T("Above this threshold snow will start to melt"), PARAMETER_TYPE_Double, 1.0, -5.0, true, 10.0, true ); tmpName = SG_T("DD_FAC("); tmpName += tmpNode; tmpName += _TL(")"); P.Add_Value( pNode1,tmpName, SG_T("Day-Degree Factor"), SG_T("Day-Degree Factor depends on catchment characteristics"), PARAMETER_TYPE_Double, 0.7, 0.7, true, 9.2, true ); } // snow module parameters ---------------------------------- } if( SG_UI_Dlg_Parameters(&P, _TL("IHACRES Distributed Input Dialog 2")) ) { // input table m_p_InputTable = P("TABLE") ->asTable(); // field numbers m_dateField = P("DATE_Field") ->asInt(); m_streamflowField = P("DISCHARGE_Field") ->asInt(); for (int i = 0; i < m_nElevBands; i++) { tmpNode = convert_sl::Int2String(i+1).c_str(); // get precipitation column of Elevation Band[i] tmpName = SG_T("PCP Column: Elevation Band: "); tmpName+=tmpNode; m_p_pcpField[i] = P(tmpName) ->asInt(); // get temperature column of Elevation Band[i] tmpName = SG_T("TMP Column: Elevation Band: "); tmpName+=tmpNode; m_p_tmpField[i] = P(tmpName) ->asInt(); tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+100).c_str(); // get area[km2] of Elevation Band[i] tmpName = SG_T("Area [km2] Elev("); tmpName += tmpNode; tmpName += _TL(")"); m_p_elevbands[i].m_area = P(tmpName) ->asDouble(); // get mean elevation of Elevation Band[i] tmpName = SG_T("Mean Elevation [m.a.s.l] Elev("); tmpName += tmpNode; tmpName += _TL(")"); m_p_elevbands[i].m_mean_elev =P(tmpName) ->asDouble(); // non-linear module parameters tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+150).c_str(); // get Tw tmpName = SG_T("TwFAC("); tmpName += tmpNode; tmpName += _TL(")"); m_p_nonlinparms->mp_tw[i] = P(tmpName) ->asDouble(); // get f tmpName = SG_T("TFAC("); tmpName += tmpNode; tmpName += _TL(")"); m_p_nonlinparms->mp_f[i] = P(tmpName) ->asDouble(); // get c tmpName = SG_T("CFAC("); tmpName += tmpNode; tmpName += _TL(")"); m_p_nonlinparms->mp_c[i] = P(tmpName) ->asDouble(); switch(m_IHAC_version) { case 0: // Jakeman & Hornberger (1993) break; case 1: // Croke et al. (2005) // get l tmpName = SG_T("LFAC("); tmpName += tmpNode; tmpName += _TL(")"); m_p_nonlinparms->mp_l[i]= P(tmpName) ->asDouble(); // get p tmpName = SG_T("PFAC("); tmpName += tmpNode; tmpName += _TL(")"); m_p_nonlinparms->mp_p[i]= P(tmpName) ->asDouble(); } // linear module parameters switch(m_nStorages) { case 1: // single storage tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+250).c_str(); // get a tmpName = SG_T("AFAC("); tmpName += tmpNode; tmpName += _TL(")"); m_p_linparms->a[i] = P(tmpName) ->asDouble(); // get b tmpName = SG_T("BFAC("); tmpName += tmpNode; tmpName += _TL(")"); m_p_linparms->b[i] = P(tmpName) ->asDouble(); break; case 2: // two storages tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+250).c_str(); // get aq tmpName = SG_T("AQ("); tmpName += tmpNode; tmpName += _TL(")"); m_p_linparms->aq[i] = P(tmpName) ->asDouble(); // get bq tmpName = SG_T("BQ("); tmpName += tmpNode; tmpName += _TL(")"); m_p_linparms->bq[i] = P(tmpName) ->asDouble(); // get as tmpName = SG_T("AS("); tmpName += tmpNode; tmpName += _TL(")"); m_p_linparms->as[i] = P(tmpName) ->asDouble(); // get bs m_p_linparms->bs[i] = ihacres.Calc_Parm_BS(m_p_linparms->aq[i],m_p_linparms->as[i],m_p_linparms->bq[i]); break; } // get delay tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+300).c_str(); tmpName = SG_T("DELAY("); tmpName += tmpNode; tmpName += _TL(")"); m_delay = P(tmpName) ->asInt(); if (m_bSnowModule) { tmpNode = SG_T("Node"); tmpNode+=convert_sl::Int2String(i+350).c_str(); // get T_RAIN tmpName = SG_T("T_RAIN("); tmpName += tmpNode; tmpName += _TL(")"); m_pSnowparms[i].T_Rain = P(tmpName) ->asDouble(); // get T_MELT tmpName = SG_T("T_MELT("); tmpName += tmpNode; tmpName += _TL(")"); m_pSnowparms[i].T_Melt = P(tmpName) ->asDouble(); // get DD_FAC tmpName = SG_T("DD_FAC("); tmpName += tmpNode; tmpName += _TL(")"); m_pSnowparms[i].DD_FAC = P(tmpName) ->asDouble(); } } return(true); } return(false); }
//--------------------------------------------------------- void CActive_Attributes_Control::On_Field_Type(wxCommandEvent &event) { if( !g_pActive->Get_Active_Data_Item() || g_pActive->Get_Active_Data_Item()->Get_Type() != WKSP_ITEM_Grids ) { return; } CSG_Grids *pGrids = ((CWKSP_Grids *)g_pActive->Get_Active_Data_Item())->Get_Grids(); //----------------------------------------------------- int i, *Types = new int[m_pTable->Get_Field_Count()]; CSG_Parameters P; P.Set_Name(_TL("Change Field Type")); for(i=0; i<m_pTable->Get_Field_Count(); i++) { switch( m_pTable->Get_Field_Type(i) ) { default: case SG_DATATYPE_String: Types[i] = 0; break; case SG_DATATYPE_Date : Types[i] = 1; break; case SG_DATATYPE_Color : Types[i] = 2; break; case SG_DATATYPE_Byte : Types[i] = 3; break; case SG_DATATYPE_Char : Types[i] = 4; break; case SG_DATATYPE_Word : Types[i] = 5; break; case SG_DATATYPE_Short : Types[i] = 6; break; case SG_DATATYPE_DWord : Types[i] = 7; break; case SG_DATATYPE_Int : Types[i] = 8; break; case SG_DATATYPE_ULong : Types[i] = 9; break; case SG_DATATYPE_Long : Types[i] = 10; break; case SG_DATATYPE_Float : Types[i] = 11; break; case SG_DATATYPE_Double: Types[i] = 12; break; case SG_DATATYPE_Binary: Types[i] = 13; break; } P.Add_Choice("", SG_Get_String(i), m_pTable->Get_Field_Name(i), _TL(""), CSG_String::Format("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|", SG_Data_Type_Get_Name(SG_DATATYPE_String).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Date ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Color ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Byte ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Char ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Word ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Short ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_DWord ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Int ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_ULong ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Long ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Float ).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Double).c_str(), SG_Data_Type_Get_Name(SG_DATATYPE_Binary).c_str() ), Types[i] ); } //----------------------------------------------------- if( DLG_Parameters(&P) ) { bool bChanged = false; for(i=0; i<m_pTable->Get_Field_Count(); i++) { TSG_Data_Type Type; switch( P(i)->asInt() ) { default: Type = SG_DATATYPE_String; break; case 1: Type = SG_DATATYPE_Date ; break; case 2: Type = SG_DATATYPE_Color ; break; case 3: Type = SG_DATATYPE_Byte ; break; case 4: Type = SG_DATATYPE_Char ; break; case 5: Type = SG_DATATYPE_Word ; break; case 6: Type = SG_DATATYPE_Short ; break; case 7: Type = SG_DATATYPE_DWord ; break; case 8: Type = SG_DATATYPE_Int ; break; case 9: Type = SG_DATATYPE_ULong ; break; case 10: Type = SG_DATATYPE_Long ; break; case 11: Type = SG_DATATYPE_Float ; break; case 12: Type = SG_DATATYPE_Double; break; case 13: Type = SG_DATATYPE_Binary; break; } if( Type != m_pTable->Get_Field_Type(i) ) { pGrids->Get_Attributes_Ptr()->Set_Field_Type(i, Type); bChanged = true; } } if( bChanged ) { Update_Table(); g_pData->Update(pGrids, NULL); } } }
//--------------------------------------------------------- bool CPointcloud_To_Text_File::On_Execute(void) { CSG_PointCloud *pPoints; CSG_String fileName; CSG_File *pTabStream = NULL; bool bWriteHeader; CSG_String fieldSep; CSG_Parameters P; CSG_Parameter *pNode; CSG_String s; std::vector<int> vCol, vPrecision; //----------------------------------------------------- pPoints = Parameters("POINTS") ->asPointCloud(); fileName = Parameters("FILE") ->asString(); bWriteHeader = Parameters("WRITE_HEADER")->asBool(); switch (Parameters("FIELDSEP")->asInt()) { default: case 0: fieldSep = "\t"; break; case 1: fieldSep = " "; break; case 2: fieldSep = ","; break; } if( fileName.Length() == 0 ) { SG_UI_Msg_Add_Error(_TL("Please provide an output file name!")); return( false ); } if (SG_UI_Get_Window_Main()) { P.Set_Name(_TL("Check the fields to export")); for(int iField=0; iField<pPoints->Get_Field_Count(); iField++) { s.Printf(SG_T("NODE_%03d") , iField + 1); pNode = P.Add_Node(NULL, s, CSG_String::Format(_TL("%d. %s"), iField + 1, _TL("Field")), _TL("")); s.Printf(SG_T("FIELD_%03d"), iField); P.Add_Value(pNode, s, CSG_String::Format(SG_T("%s"), pPoints->Get_Field_Name(iField)), _TL(""), PARAMETER_TYPE_Bool, false); s.Printf(SG_T("PRECISION_%03d"), iField); P.Add_Value(pNode, s, _TL("Decimal Precision"), _TL(""), PARAMETER_TYPE_Int, 2.0, 0.0, true); } //----------------------------------------------------- if( Dlg_Parameters(&P, _TL("Field Properties")) ) { vCol.clear(); vPrecision.clear(); for(int iField=0; iField<pPoints->Get_Field_Count(); iField++) { if( P(CSG_String::Format(SG_T("FIELD_%03d" ), iField).c_str())->asBool() ) { vCol.push_back(iField); vPrecision.push_back(P(CSG_String::Format(SG_T("PRECISION_%03d" ), iField).c_str())->asInt()); } } } else return( false ); } else // CMD { CSG_String sFields, sPrecision; CSG_String token; int iValue; sFields = Parameters("FIELDS")->asString(); sPrecision = Parameters("PRECISIONS")->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 < 0 || iValue > pPoints->Get_Field_Count() - 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_precisons(sPrecision.c_str(), ";", SG_TOKEN_STRTOK); while( tkz_precisons.Has_More_Tokens() ) { token = tkz_precisons.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 ); } vPrecision.push_back(iValue); } } if( vCol.size() == 0 ) { SG_UI_Msg_Add_Error(_TL("Please provide at least one column to export!")); return( false ); } if( vCol.size() != vPrecision.size() ) { SG_UI_Msg_Add_Error(_TL("Number of fields and precisions must be equal!")); return( false ); } //----------------------------------------------------- pTabStream = new CSG_File(); if( !pTabStream->Open(fileName, SG_FILE_W, false) ) { SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unable to open output file %s!"), fileName.c_str())); delete (pTabStream); return (false); } if( bWriteHeader ) { CSG_String sHeader; for(size_t i=0; i<vCol.size(); i++) { sHeader += CSG_String::Format(SG_T("%s"), pPoints->Get_Field_Name(vCol.at(i))); if( i < vCol.size()-1 ) sHeader += fieldSep.c_str(); } sHeader += SG_T("\n"); pTabStream->Write(sHeader); } for(int iPoint=0; iPoint<pPoints->Get_Count() && Set_Progress(iPoint, pPoints->Get_Count()); iPoint++) { CSG_String sLine; for(size_t i=0; i<vCol.size(); i++) { switch (pPoints->Get_Field_Type(vCol.at(i))) { case SG_DATATYPE_Double: case SG_DATATYPE_Float: sLine += SG_Get_String(pPoints->Get_Value(iPoint, vCol.at(i)), vPrecision.at(i), false); break; default: sLine += CSG_String::Format(SG_T("%d"), (int)pPoints->Get_Value(iPoint, vCol.at(i))); break; } if( i < vCol.size()-1 ) sLine += fieldSep.c_str(); } sLine += SG_T("\n"); pTabStream->Write(sLine); } pTabStream->Close(); delete (pTabStream); return( true ); }
//--------------------------------------------------------- bool CPointCloud_From_Text_File::On_Execute(void) { CSG_String fileName; int iField, iType; TSG_Data_Type Type; CSG_String Name, Types, s; CSG_PointCloud *pPoints; CSG_Parameters P; CSG_Parameter *pNode; int xField, yField, zField, nAttribs, max_iField; bool bSkipHeader; char fieldSep; std::vector<int> vCol; std::ifstream tabStream; std::string tabLine; double lines; long cntPt, cntInvalid; double x, y, z, value; //----------------------------------------------------- fileName = Parameters("FILE") ->asString(); xField = Parameters("XFIELD") ->asInt() - 1; yField = Parameters("YFIELD") ->asInt() - 1; zField = Parameters("ZFIELD") ->asInt() - 1; nAttribs = Parameters("ATTRIBS") ->asInt(); bSkipHeader = Parameters("SKIP_HEADER") ->asBool(); switch (Parameters("FIELDSEP")->asInt()) { default: case 0: fieldSep = '\t'; break; case 1: fieldSep = ' '; break; case 2: fieldSep = ','; break; } 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")) ) { pPoints = SG_Create_PointCloud(); pPoints->Set_Name(SG_File_Get_Name(fileName, false)); Parameters("POINTS")->Set_Value(pPoints); 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); switch( iType ) { default: case 0: Type = SG_DATATYPE_Char; break; case 1: Type = SG_DATATYPE_Short; break; case 2: Type = SG_DATATYPE_Int; break; case 3: Type = SG_DATATYPE_Float; break; case 4: Type = SG_DATATYPE_Double; break; } pPoints->Add_Field(Name, Type); } } else return( false ); } else { pPoints = SG_Create_PointCloud(); pPoints->Create(); pPoints->Set_Name(SG_File_Get_Name(fileName, false)); Parameters("POINTS")->Set_Value(pPoints); } max_iField = M_GET_MAX(xField, yField); max_iField = M_GET_MAX(max_iField, zField); for( unsigned int i=0; i<vCol.size(); i++ ) { if( max_iField < vCol.at(i) ) max_iField = vCol.at(i); } // 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() < max_iField - 1 ) { SG_UI_Msg_Add(CSG_String::Format(_TL("WARNING: Skipping misformatted line (%.0f)!"), cntPt), true); cntInvalid++; continue; } x = strtod(tabCols[xField].c_str(), NULL); y = strtod(tabCols[yField].c_str(), NULL); z = strtod(tabCols[zField].c_str(), NULL); pPoints->Add_Point(x, y, z); for( int i=0; i<nAttribs; i++ ) { value = strtod(tabCols.at(vCol.at(i)).c_str(), NULL); pPoints->Set_Attribute(i, value); } } // finalize //--------------------------------------------------------- tabStream.close(); CSG_Parameters sParms; DataObject_Get_Parameters(pPoints, sParms); if (sParms("COLORS_ATTRIB") && sParms("COLORS_TYPE") && sParms("METRIC_COLORS") && sParms("METRIC_ZRANGE") && sParms("COLORS_AGGREGATE")) { sParms("COLORS_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("COLORS_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(_TL("WARNING: Skipped %d invalid points!"), cntInvalid), true); SG_UI_Msg_Add(CSG_String::Format(_TL("%d points sucessfully imported."), (cntPt-cntInvalid)), true); 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 ); }