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); }
//--------------------------------------------------------- bool CShapes_SRID_Update::On_Execute(void) { if( !Get_Connection()->has_PostGIS() ) { Error_Set(_TL("no PostGIS layer")); return( false ); } //----------------------------------------------------- CSG_String Select; CSG_Table Table; Select.Printf(SG_T("f_table_name='%s'"), Parameters("TABLES")->asString()); if( !Get_Connection()->Table_Load(Table, "geometry_columns", "*", Select) || Table.Get_Count() != 1 ) { return( false ); } Select.Printf(SG_T("SELECT UpdateGeometrySRID('%s', '%s', %d)"), Parameters("TABLES")->asString(), Table[0].asString("f_geometry_column"), Get_SRID() ); //----------------------------------------------------- if( !Get_Connection()->Execute(Select) ) { return( false ); } return( true ); }
//--------------------------------------------------------- void CGrid_Cluster_Analysis::Save_Statistics(CSG_Parameter_Grid_List *pGrids, bool bNormalize, const CSG_Cluster_Analysis &Analysis) { int iCluster, iFeature; CSG_String s; CSG_Table *pTable = Parameters("STATISTICS")->asTable(); pTable->Destroy(); pTable->Set_Name(_TL("Cluster Analysis")); pTable->Add_Field(_TL("ClusterID") , SG_DATATYPE_Int); pTable->Add_Field(_TL("Elements") , SG_DATATYPE_Int); pTable->Add_Field(_TL("Std.Dev.") , SG_DATATYPE_Double); s.Printf(SG_T("\n%s:\t%d \n%s:\t%ld \n%s:\t%d \n%s:\t%d \n%s:\t%f\n\n%s\t%s\t%s"), _TL("Number of Iterations") , Analysis.Get_Iteration(), _TL("Number of Elements") , Analysis.Get_nElements(), _TL("Number of Variables") , Analysis.Get_nFeatures(), _TL("Number of Clusters") , Analysis.Get_nClusters(), _TL("Standard Deviation") , sqrt(Analysis.Get_SP()), _TL("Cluster"), _TL("Elements"), _TL("Std.Dev.") ); for(iFeature=0; iFeature<Analysis.Get_nFeatures(); iFeature++) { s += CSG_String::Format(SG_T("\t%s"), pGrids->asGrid(iFeature)->Get_Name()); pTable->Add_Field(pGrids->asGrid(iFeature)->Get_Name(), SG_DATATYPE_Double); } Message_Add(s); for(iCluster=0; iCluster<Analysis.Get_nClusters(); iCluster++) { s.Printf(SG_T("\n%d\t%d\t%f"), iCluster, Analysis.Get_nMembers(iCluster), sqrt(Analysis.Get_Variance(iCluster))); CSG_Table_Record *pRecord = pTable->Add_Record(); pRecord->Set_Value(0, iCluster); pRecord->Set_Value(1, Analysis.Get_nMembers(iCluster)); pRecord->Set_Value(2, sqrt(Analysis.Get_Variance(iCluster))); for(iFeature=0; iFeature<Analysis.Get_nFeatures(); iFeature++) { double Centroid = Analysis.Get_Centroid(iCluster, iFeature); if( bNormalize ) { Centroid = pGrids->asGrid(iFeature)->Get_Mean() + Centroid * pGrids->asGrid(iFeature)->Get_StdDev(); } s += CSG_String::Format(SG_T("\t%f"), Centroid); pRecord->Set_Value(iFeature + 3, Centroid); } Message_Add(s, false); } }
//--------------------------------------------------------- bool CSRTM30_Import::On_Execute(void) { char x_sTile[9][5] = { "W180", "W140", "W100", "W060", "W020", "E020", "E060", "E100", "E140" }, y_sTile[3][4] = { "S10", "N40", "N90" }; double dSize = 30.0 / (60.0 * 60.0); //----------------------------------------------------- int xTile, yTile; double xMin, xMax, yMin, yMax; TSG_Rect rOut, rTile; CSG_String sTile; CSG_Grid *pOut; //----------------------------------------------------- xMin = Parameters("XMIN")->asInt(); xMax = Parameters("XMAX")->asInt(); yMin = Parameters("YMIN")->asInt(); yMax = Parameters("YMAX")->asInt(); rOut.xMin = (180 + xMin) / 40.0 * X_WIDTH; rOut.xMax = rOut.xMin + (int)((xMax - xMin) / dSize); rOut.yMin = ( 60 + yMin) / 50.0 * Y_WIDTH; rOut.yMax = rOut.yMin + (int)((yMax - yMin) / dSize); //----------------------------------------------------- pOut = SG_Create_Grid(SG_DATATYPE_Short, (int)(rOut.xMax - rOut.xMin), (int)(rOut.yMax - rOut.yMin), dSize, xMin + 0.5 * dSize, yMin + 0.5 * dSize ); pOut->Set_NoData_Value(-9999); pOut->Assign_NoData(); pOut->Set_Name(SG_T("SRTM30")); pOut->Get_Projection().Create(SG_T("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]")); //----------------------------------------------------- for(yTile=0, rTile.yMin=0, rTile.yMax=Y_WIDTH; yTile<3; yTile++, rTile.yMin+=Y_WIDTH, rTile.yMax+=Y_WIDTH) { for(xTile=0, rTile.xMin=0, rTile.xMax=X_WIDTH; xTile<9; xTile++, rTile.xMin+=X_WIDTH, rTile.xMax+=X_WIDTH) { sTile.Printf(SG_T("Tile: %s%s"), x_sTile[xTile], y_sTile[yTile]); Process_Set_Text(sTile); sTile.Printf(SG_T("%s%s%s.dem"), Parameters("PATH")->asString(), x_sTile[xTile], y_sTile[yTile]); Tile_Load(sTile, rTile, pOut, rOut); } } //----------------------------------------------------- Parameters("GRID")->Set_Value(pOut); return( true ); }
//--------------------------------------------------------- CSG_String CSG_Module_Library::Get_Menu(int i) { CSG_String sMenu; if( Get_Module(i) != NULL ) { bool bAbsolute = false; const SG_Char *sModule = Get_Module(i)->Get_MenuPath(); if( sModule && *sModule && *(sModule + 1) == ':' ) { bAbsolute = sModule[0] == SG_T('A') || sModule[0] == SG_T('a'); sModule += 2; } if( bAbsolute ) // menu path is relative to top menu... { if( sModule && *sModule ) { sMenu.Printf(SG_T("%s"), sModule); } } else // menu path is relative to library menu... { const SG_Char *sLibrary = Get_Info(MLB_INFO_Menu_Path); if( sModule && *sModule ) { if( sLibrary && *sLibrary ) { sMenu.Printf(SG_T("%s|%s"), sLibrary, sModule); } else { sMenu.Printf(SG_T("%s"), sModule); } } else if( sLibrary && *sLibrary ) { sMenu.Printf(SG_T("%s"), sLibrary); } } if( sMenu.Length() > 0 ) { sMenu.Append(SG_T("|")); } sMenu.Append(Get_Info(MLB_INFO_Name)); } return( sMenu ); }
//--------------------------------------------------------- void CPC_Cluster_Analysis::Write_Result(CSG_Table *pTable, long nElements, int nCluster, double SP) { CSG_String s; CSG_Table_Record *pRecord; pTable->Destroy(); s = CSG_String::Format(_TL("Cluster Analysis PC")); s += CSG_String::Format(SG_T("_%s"), pInput->Get_Name()); pTable->Set_Name(s); pTable->Add_Field(_TL("ClusterID") , SG_DATATYPE_Int); pTable->Add_Field(_TL("Elements") , SG_DATATYPE_Int); pTable->Add_Field(_TL("Variance") , SG_DATATYPE_Double); s.Printf(SG_T("\n%s:\t%ld \n%s:\t%d \n%s:\t%d \n%s:\t%f"), _TL("Number of Elements") , nElements, _TL("\nNumber of Variables") , m_nFeatures, _TL("\nNumber of Clusters") , nCluster, _TL("\nValue of Target Function") , SP ); s.Append(CSG_String::Format(SG_T("%s\t%s\t%s"), _TL("Cluster"), _TL("Elements"), _TL("Variance"))); for( int j=0; j<m_nFeatures; j++ ) { s.Append(CSG_String::Format(SG_T("\t%02d_%s"), j + 1, pInput->Get_Field_Name(m_Features[j]))); pTable->Add_Field(pInput->Get_Field_Name(m_Features[j]), SG_DATATYPE_Double); } Message_Add(s); for( int i=0; i<nCluster; i++ ) { s.Printf(SG_T("%d\t%d\t%f"), i, nMembers[i], Variances[i]); pRecord = pTable->Add_Record(); pRecord->Set_Value(0, i); pRecord->Set_Value(1, nMembers[i]); pRecord->Set_Value(2, Variances[i]); for( int j=0; j<m_nFeatures; j++ ) { s.Append(CSG_String::Format(SG_T("\t%f"), Centroids[i][j])); pRecord->Set_Value(j + 3, Centroids[i][j]); } Message_Add(s); } }
//--------------------------------------------------------- CSG_String CLine_Dissolve::Statistics_Get_Name(const CSG_String &Type, const CSG_String &Name) { CSG_String s; switch( Parameters("STAT_NAMING")->asInt() ) { default: s.Printf("%s_%s", Type.c_str(), Name.c_str()); break; case 1: s.Printf("%s_%s", Name.c_str(), Type.c_str()); break; case 2: s.Printf("%s" , Name.c_str() ); break; case 3: s.Printf("%s" , Type.c_str() ); break; } return( s ); }
//--------------------------------------------------------- CSG_String CPolygonStatisticsFromPoints::Get_Field_Name(const CSG_String &Type, const CSG_String &Name) { CSG_String s; switch( Parameters("FIELD_NAME")->asInt() ) { default: case 0: s.Printf(SG_T("%s_%s"), Type.c_str(), Name.c_str()); break; case 1: s.Printf(SG_T("%s_%s"), Name.c_str(), Type.c_str()); break; case 2: s.Printf(SG_T("%s" ), Name.c_str() ); break; case 3: s.Printf(SG_T("%s" ), Type.c_str() ); break; } return( s ); }
//--------------------------------------------------------------------- /////////////////////////////////////////////////////////////////////// // // DIALOGS // /////////////////////////////////////////////////////////////////////// //--------------------------------------------------------------------- // DIALOG 1 //--------------------------------------------------------------------- void Cihacres_elev::_CreateDialog1() { CSG_Parameter *pNode; CSG_String s; pNode = Parameters.Add_Choice( NULL , "NELEVBANDS" , SG_T("Number of elevation bands"), _TL(""), SG_T("2|3|4|5|6|7|8|9|10") ); pNode = Parameters.Add_Value( pNode, "AREA_tot", _TL("Total Catchment Area [km2]"), _TL(""), PARAMETER_TYPE_Double ); s.Printf(SG_T("Node1"), 1); pNode = Parameters.Add_Node(NULL,s,SG_T("IHACRES Version"),_TL("")); Parameters.Add_Choice( pNode , "IHACVERS" , SG_T("IHACRES Version"), _TL(""), CSG_String::Format(SG_T("%s|%s|"), _TL("Jakeman & Hornberger (1993)"), // 0 _TL("Croke et al. (2005) !!! not yet implemented !!!") // 1 ) ); s.Printf(SG_T("Node2"), 2); pNode = Parameters.Add_Node(NULL,s,SG_T("Storage Configuration"),_TL("")); Parameters.Add_Choice( pNode , "STORAGE" , SG_T("Storage"), _TL(""), CSG_String::Format(SG_T("%s|%s|%s|"), _TL("Single Storage"), // 0 _TL("Two Parallel Storages"), // 1 _TL("Two Storages in Series !!! not yet implemented !!!") // 2 ) ); Parameters.Add_Value( pNode, "SNOW_MODULE", _TL("Using the snow-melt module?"), _TL("If checked, snow-melt module is used."), PARAMETER_TYPE_Bool, false ); }
//--------------------------------------------------------- bool CSG_PointCloud::_Get_Field_Value(char *pPoint, int iField, CSG_String &Value) const { if( pPoint && iField >= 0 && iField < m_nFields ) { switch( m_Field_Type[iField] ) { default: Value.Printf("%f", _Get_Field_Value(pPoint, iField)); break; case SG_DATATYPE_Date: case SG_DATATYPE_String: { char s[PC_STR_NBYTES + 1]; memcpy(s, pPoint + m_Field_Offset[iField], PC_STR_NBYTES); s[PC_STR_NBYTES] = '\0'; Value = s; } break; } return( true ); } return( false ); }
//--------------------------------------------------------- bool CGrid_Volume::On_Execute(void) { int x, y, Method; double Level, Volume, z; CSG_Grid *pGrid; CSG_String s; //----------------------------------------------------- pGrid = Parameters("GRID") ->asGrid(); Level = Parameters("LEVEL") ->asDouble(); Method = Parameters("METHOD") ->asInt(); //----------------------------------------------------- for(y=0, Volume=0.0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( !pGrid->is_NoData(x, y) ) { z = pGrid->asDouble(x, y) - Level; switch( Method ) { case 0: // Count Only Above Base Level if( z > 0.0 ) { Volume += z; } break; case 1: // Count Only Below Base Level if( z < 0.0 ) { Volume -= z; } break; case 2: // Subtract Volumes Below Base Level Volume += z; break; case 3: // Add Volumes Below Base Level Volume += fabs(z); break; } } } } //----------------------------------------------------- Volume *= pGrid->Get_Cellarea(); s.Printf(_TL("Volume: %f"), Volume); Message_Add(s); Message_Dlg(s, _TL("Grid Volume")); //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- CTL_Merge::CTL_Merge(void) { //----------------------------------------------------- Set_Name (SG_T("Merge Translation Files")); Set_Author (SG_T("O. Conrad (c) 2010")); Set_Description (SG_T("")); //----------------------------------------------------- CSG_String Filter; Filter.Printf(SG_T("%s|*.lng;*.txt|%s|*.lng|%s|*.txt|%s|*.*"), SG_T("All recognized Files"), SG_T("SAGA Translation File (*.lng)"), SG_T("Text Files (*.txt)"), SG_T("All Files") ); //----------------------------------------------------- Parameters.Add_FilePath( NULL , "TARGET" , SG_T("Target"), SG_T(""), Filter, NULL, true ); Parameters.Add_FilePath( NULL , "FILES" , SG_T("Files"), SG_T(""), Filter, NULL, false, false, true ); }
//--------------------------------------------------------- CSG_String SG_Double_To_Degree(double Value) { SG_Char c; int d, h; double s; CSG_String String; if( Value < 0.0 ) { Value = -Value; c = SG_T('-'); } else { c = SG_T('+'); } Value = fmod(Value, 360.0); d = (int)Value; Value = 60.0 * (Value - d); h = (int)Value; Value = 60.0 * (Value - h); s = Value; String.Printf(SG_T("%c%03d\xb0%02d'%02.*f''"), c, d, h, SG_Get_Significant_Decimals(s), s); return( String ); }
//--------------------------------------------------------- bool CSG_ODBC_Connection::Table_Load(CSG_Table &Table, const CSG_String &Tables, const CSG_String &Fields, const CSG_String &Where, const CSG_String &Group, const CSG_String &Having, const CSG_String &Order, bool bDistinct, bool bLOB) { CSG_String Select; Select.Printf(SG_T("SELECT %s %s FROM %s"), bDistinct ? SG_T("DISTINCT") : SG_T("ALL"), Fields.c_str(), Tables.c_str()); if( Where.Length() ) { Select += SG_T(" WHERE ") + Where; } if( Group.Length() ) { Select += SG_T(" GROUP BY ") + Group; if( Having.Length() ) { Select += SG_T(" HAVING ") + Having; } } if( Order.Length() ) { Select += SG_T(" ORDER BY ") + Order; } return( _Table_Load(Table, Select, Table.Get_Name(), bLOB) ); }
//--------------------------------------------------------- CSG_String CSG_Module_Library::Get_Summary(bool bHTML) { CSG_String s; if( bHTML ) { s.Printf( SG_T("%s: <b>%s</b><br>%s: <i>%s</i><br>%s: <i>%s</i><br>%s: <i>%s</i><hr>%s"), LNG("[CAP] Module Library") , Get_Info(MLB_INFO_Name), LNG("[CAP] Author") , Get_Info(MLB_INFO_Author), LNG("[CAP] Version") , Get_Info(MLB_INFO_Version), LNG("[CAP] File") , Get_File_Name().c_str(), Get_Info(MLB_INFO_Description) ); s.Append(CSG_String::Format(SG_T("<hr><b>%s:<ul>"), LNG("[CAP] Modules"))); for(int i=0; i<Get_Count(); i++) { s.Append(CSG_String::Format(SG_T("<li>%s</li>"), Get_Module(i)->Get_Name())); } s.Append(SG_T("</ul>")); s.Replace(SG_T("\n"), SG_T("<br>")); } else { s.Printf( SG_T("%s: %s\n%s: %s\n%s: %s\n%s: %s\n\n%s"), LNG("[CAP] Module Library") , Get_Info(MLB_INFO_Name), LNG("[CAP] Author") , Get_Info(MLB_INFO_Author), LNG("[CAP] Version") , Get_Info(MLB_INFO_Version), LNG("[CAP] File") , Get_File_Name().c_str(), Get_Info(MLB_INFO_Description) ); s.Append(CSG_String::Format(SG_T("\n\n%s:\n"), LNG("[CAP] Modules"))); for(int i=0; i<Get_Count(); i++) { s.Append(CSG_String::Format(SG_T("- %s\n"), Get_Module(i)->Get_Name())); } } return( s ); }
//--------------------------------------------------------- bool CGridding_Spline_MBA_Grid::_Get_Difference(CSG_Grid &Phi) { int xPoint, yPoint, nErrors; double x, y, z, zMax, zMean; TSG_Point_Z p; CSG_String s; //----------------------------------------------------- for(yPoint=0, p.y=m_Points.Get_YMin(), zMax=0.0, nErrors=0, zMean=0.0; yPoint<m_Points.Get_NY() && Set_Progress(yPoint, m_Points.Get_NY()); yPoint++, p.y+=m_Points.Get_Cellsize()) { for(xPoint=0, p.x=m_Points.Get_XMin(); xPoint<m_Points.Get_NX(); xPoint++, p.x+=m_Points.Get_Cellsize()) { if( !m_Points.is_NoData(xPoint, yPoint) ) { x = (p.x - Phi.Get_XMin()) / Phi.Get_Cellsize(); y = (p.y - Phi.Get_YMin()) / Phi.Get_Cellsize(); z = m_Points.asDouble(xPoint, yPoint) - BA_Get_Value(x, y, Phi); m_Points.Set_Value(xPoint, yPoint, z); if( (z = fabs(z)) > m_Epsilon ) { nErrors ++; zMean += fabs(z); if( fabs(z) > zMax ) { zMax = fabs(z); } } else { // m_Points.Set_Value(xPoint, yPoint, 0.0); m_Points.Set_NoData(xPoint, yPoint); } } } } if( nErrors > 0 ) { zMean /= nErrors; } //----------------------------------------------------- int i = 1 + (int)(0.5 + log(Phi.Get_NX() - 4.0) / log(2.0)); s.Printf(SG_T("%s:%d, %s:%d, %s:%f, %s:%f"), _TL("level"), i, _TL("error"), nErrors, _TL("max") , zMax, _TL("mean") , zMean ); Process_Set_Text(s); Message_Add (s); return( zMax >= m_Epsilon && i < m_Level_Max && Process_Get_Okay(false) ); }
//--------------------------------------------------------- CSG_String CGCS_Graticule::Get_Degree(double Value, int Precision) { if( Precision == DEG_PREC_DEG ) { return( SG_Get_String(Value, -12) + "\xb0" ); } SG_Char c; int d, h; double s; CSG_String String; if( Value < 0.0 ) { Value = -Value; c = SG_T('-'); } else { c = SG_T('+'); } Value = fmod(Value, 360.0); d = (int)Value; Value = 60.0 * (Value - d); h = (int)Value; Value = 60.0 * (Value - h); s = Value; if( s > 0.0 || Precision == DEG_PREC_FULL ) { String.Printf(SG_T("%c%d\xb0%02d'%02.*f''"), c, d, h, SG_Get_Significant_Decimals(s), s); } else if( h > 0 || Precision == DEG_PREC_MIN ) { String.Printf(SG_T("%c%d\xb0%02d'" ), c, d, h); } else { String.Printf(SG_T("%c%d\xb0" ), c, d); } return( String ); }
int SG_Grid_Cache_Check(CSG_Grid_System &m_System, int nValueBytes) { if( SG_Grid_Cache_Get_Automatic() && m_System.Get_NCells() * nValueBytes > SG_Grid_Cache_Get_Threshold() ) { switch( SG_Grid_Cache_Get_Confirm() ) { default: break; case 1: { CSG_String s; s.Printf(SG_T("%s\n%s\n%s: %.2fMB"), LNG("Shall I activate file caching for new grid."), m_System.Get_Name(), LNG("Total memory size"), (m_System.Get_NCells() * nValueBytes) / (double)N_MEGABYTE_BYTES ); if( SG_UI_Dlg_Continue(s, LNG("Activate Grid File Cache?")) ) { // Memory_Type = GRID_MEMORY_Cache; return( SG_Grid_Cache_Get_Threshold() ); } } break; case 2: { CSG_Parameters p(NULL, LNG("Activate Grid File Cache?"), SG_T("")); p.Add_Value( NULL , SG_T("BUFFERSIZE") , LNG("Buffer Size [MB]"), SG_T(""), PARAMETER_TYPE_Double, SG_Grid_Cache_Get_Threshold_MB(), 0.0, true ); if( SG_UI_Dlg_Parameters(&p, LNG("Activate Grid File Cache?")) ) { // Memory_Type = GRID_MEMORY_Cache; // Set_Buffer_Size((int)(p(SG_T("BUFFERSIZE"))->asDouble() * N_MEGABYTE_BYTES)); return( (int)(p(SG_T("BUFFERSIZE"))->asDouble() * N_MEGABYTE_BYTES) ); } } break; } } return( 0 ); }
//--------------------------------------------------------- bool CGridding_Spline_MBA::_Get_Difference(CSG_Grid &Phi) { int i, nErrors; double x, y, z, zMax, zMean; CSG_String s; //----------------------------------------------------- for(i=0, zMax=0.0, nErrors=0, zMean=0.0; i<m_Points.Get_Count(); i++) { x = (m_Points[i].x - Phi.Get_XMin()) / Phi.Get_Cellsize(); y = (m_Points[i].y - Phi.Get_YMin()) / Phi.Get_Cellsize(); z = (m_Points[i].z = m_Points[i].z - BA_Get_Value(x, y, Phi)); if( (z = fabs(z)) > m_Epsilon ) { nErrors ++; zMean += fabs(z); if( fabs(z) > zMax ) { zMax = fabs(z); } } else { m_Points[i].z = 0.0; } } if( nErrors > 0 ) { zMean /= nErrors; } //----------------------------------------------------- i = 1 + (int)(0.5 + log(Phi.Get_NX() - 4.0) / log(2.0)); s.Printf(SG_T("%s:%d, %s:%d, %s:%f, %s:%f"), _TL("level"), i, _TL("error"), nErrors, _TL("max") , zMax, _TL("mean") , zMean ); Process_Set_Text(s); Message_Add (s); return( zMax >= m_Epsilon && i < m_Level_Max && Process_Get_Okay(false) ); }
//--------------------------------------------------------- CSG_String SG_Get_String(double Value, int Precision, bool bScientific) { CSG_String s; if( Precision >= 0 ) { s.Printf(SG_T("%.*f"), Precision, Value); } else if( Precision == -1 ) { s.Printf(SG_T("%f"), Value); } else // if( Precision == -2 ) { Precision = SG_Get_Significant_Decimals(Value, abs(Precision)); s.Printf(SG_T("%.*f"), SG_Get_Significant_Decimals(Value, abs(Precision)), Value); if( Precision > 0 ) { while( s.Length() > 1 && s[s.Length() - 1] == '0' ) { s = s.Left(s.Length() - 1); } if( s.Length() > 1 && (s[s.Length() - 1] == '.' || s[s.Length() - 1] == ',') ) { s = s.Left(s.Length() - 1); } } } s.Replace(",", "."); return( s ); }
//--------------------------------------------------------- const SG_Char * CSG_Regression::asString(void) { static CSG_String s; s.Printf( SG_T("N = %d\n") SG_T(" Min. = %.6f Max. = %.6f\n Arithmetic Mean = %.6f\n Variance = %.6f\n Standard Deviation = %.6f\n") SG_T(" Min. = %.6f Max. = %.6f\n Arithmetic Mean = %.6f\n Variance = %.6f\n Standard Deviation = %.6f\n") SG_T("Linear Regression:\n Y = %.6f * X %+.6f\n (r=%.4f, r\xc2\xb2=%.4f)"), m_nValues, m_xMin, m_xMax, m_xMean, m_xVar, sqrt(m_xVar), m_yMin, m_yMax, m_yMean, m_yVar, sqrt(m_yVar), m_RCoeff, m_RConst, m_R, m_R*m_R ); return( s ); }
bool CFit::On_Execute(void) { int i, j, NrVars; vector < double> x, y, StartValue, Result; CSG_String msg; CSG_Parameters StartParameters; const SG_Char *formel = Parameters("FORMEL")->asString(); Formel.Add_Function(SG_T("NUG"), (TSG_PFNC_Formula_1) NUG, 1, 0); Formel.Add_Function(SG_T("SPH"), (TSG_PFNC_Formula_1) SPH, 2, 0); Formel.Add_Function(SG_T("EXP"), (TSG_PFNC_Formula_1) EXP, 2, 0); Formel.Add_Function(SG_T("LIN"), (TSG_PFNC_Formula_1) LIN, 2, 0); Formel.Set_Formula(formel); if (Formel.Get_Error(msg)) { Message_Add(msg); return false; } const SG_Char *uservars = NULL; uservars = Formel.Get_Used_Variables(); NrVars = 0; for (i = 0; i < SG_STR_LEN(uservars); i++) { if (uservars[i] >='a' && uservars[i] <= 'z') { if (uservars[i] != 'x') vars[NrVars++] = uservars[i]; } } vars[NrVars] =(char) 0; StartParameters.Add_Info_String(NULL, _TL(""), _TL("Formula"), _TL("Formula"), formel); for (i = 0; i < strlen(vars); i++) { CSG_String c(vars[i]); StartParameters.Add_Value(NULL, c, c, _TL("Start Value"), PARAMETER_TYPE_Double, 1.0); } Dlg_Parameters(&StartParameters, _TL("Start Values")); for (i = 0; i < strlen(vars); i++) { char c[3]; sprintf(c, "%c", vars[i]); StartValue.push_back(StartParameters(c)->asDouble()); } CSG_Table *pTable = Parameters("SOURCE")->asTable(); int Record_Count = pTable->Get_Record_Count(); int yField = Parameters("YFIELD")->asInt(); int xField = Parameters("XFIELD")->asInt(); bool Use_X = Parameters("USE_X")->asBool(); pTable->Add_Field(_TL("Fit") , SG_DATATYPE_Double); for (i = 0; i < Record_Count; i++) { CSG_Table_Record * Record = pTable->Get_Record(i); if (Use_X) { x.push_back(Record->asDouble(xField)); } else { x.push_back(i); } y.push_back(Record->asDouble(yField)); } TLMFit *Fit; Fit = new TLMFit(x, y, StartValue, FitFunc); int max_iter = Parameters("ITER")->asInt(); double Max_lamda = Parameters("LAMDA")->asInt(); int iter = 0; try { Fit->Fit(); while ((Fit->Alamda() < Max_lamda) &&(iter < max_iter) &&Process_Get_Okay(true)) { Fit->Fit(); iter++; } } catch (ESingularMatrix &E) { if (E.Type == 1 || E.Type == 2) { msg.Printf(_TL("Matrix signular\n")); Message_Add(msg); return false; } } Result = Fit->Param(); for (i = 0; i < NrVars; i++) { Formel.Set_Variable(vars[i], (double) Result[i]); } msg.Printf(_TL("Model Parameters:")); Message_Add(msg); for (i = 0; i < NrVars; i++) { msg.Printf(SG_T("%c = %f\n"), vars[i], Result[i]); Message_Add(msg); } msg.Printf(_TL("\nRMS of Residuals (stdfit):\t%f\n"), sqrt(Fit->Chisq()/x.size())); Message_Add(msg); msg.Printf(_TL("Correlation Matrix of the Fit Parameters:\n")); Message_Add(msg); vector< vector < double> > covar = Fit->Covar(); msg.Printf(_TL("")); for (j = 0; j < NrVars; j++) msg.Printf(SG_T("%s\t%c"), msg.c_str(), vars[j]); msg.Printf(SG_T("%s\n"), msg.c_str()); Message_Add(msg); for (i = 0; i < NrVars; i++) { msg.Printf(SG_T("%c"), vars[i]); for (j = 0; j <= i; j++) { msg.Printf(SG_T("%s\t%f"), msg.c_str(), covar[i][j]/covar[i][i]); } msg.Printf(SG_T("%s\n"), msg.c_str()); Message_Add(msg); } int Field_Count = pTable->Get_Field_Count(); for (i = 0; i < Record_Count; i++) { CSG_Table_Record * Record = pTable->Get_Record(i); Record->Set_Value(Field_Count - 1, Formel.Get_Value(x[i])); } // API_FREE (uservars); return (true); }
//--------------------------------------------------------- bool CSG_ODBC_Connection::Table_Load_BLOBs(CSG_Bytes_Array &BLOBs, const CSG_String &Table_Name, const CSG_String &Field, const CSG_String &Where, const CSG_String &Order) { //----------------------------------------------------- if( !is_Connected() ) { _Error_Message(_TL("no database connection")); return( false ); } //----------------------------------------------------- try { bool bLOB = true; int nFields; otl_column_desc *Fields; otl_long_string valRaw(m_Connection.get_max_long_size()); otl_stream Stream; CSG_String Select; //------------------------------------------------- Select.Printf(SG_T("SELECT %s FROM %s"), Field.c_str(), Table_Name.c_str()); if( Where.Length() ) { Select += SG_T(" WHERE ") + Where; } if( Order.Length() ) { Select += SG_T(" ORDER BY ") + Order; } //------------------------------------------------- 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 ); } if( nFields != 1 ) { _Error_Message(_TL("more than one field in selection")); return( false ); } if( _Get_Type_From_SQL(Fields[0].otl_var_dbtype) != SG_DATATYPE_Binary )//|| _Get_Type_From_SQL(Fields[0].otl_var_dbtype) != SG_DATATYPE_String ) { _Error_Message(_TL("field cannot be mapped to binary object")); return( false ); } //------------------------------------------------- BLOBs.Destroy(); while( !Stream.eof() && SG_UI_Process_Get_Okay() ) // while not end-of-data { CSG_Bytes *pBLOB = BLOBs.Add(); Stream >> valRaw; if( !Stream.is_null() ) { for(int i=0; i<valRaw.len(); i++) { pBLOB->Add((BYTE)valRaw[i]); } } } } //----------------------------------------------------- catch( otl_exception &e ) { _Error_Message(e); return( false ); } return( true ); }
//--------------------------------------------------------- bool CPointCloud_From_Text_File::On_Execute(void) { //----------------------------------------------------- CSG_File Stream; if( !Stream.Open(Parameters("FILE")->asString(), SG_FILE_R, false) ) { Error_Set(_TL("Unable to open input file!")); return( false ); } //----------------------------------------------------- int xField = Parameters("XFIELD")->asInt() - 1; int yField = Parameters("YFIELD")->asInt() - 1; int zField = Parameters("ZFIELD")->asInt() - 1; char Separator; switch( Parameters("SEPARATOR")->asInt() ) { default: Separator = '\t'; break; case 1: Separator = ' '; break; case 2: Separator = ','; break; } //----------------------------------------------------- CSG_String sLine; CSG_Strings Values; if( !Stream.Read_Line(sLine) ) { Error_Set(_TL("Empty file!")); return( false ); } if( Parameters("SKIP_HEADER")->asBool() ) // header contains field names { CSG_String_Tokenizer tokValues(sLine, Separator); // read each field name for later use while( tokValues.Has_More_Tokens() ) { Values += tokValues.Get_Next_Token(); } } else { Stream.Seek_Start(); } //----------------------------------------------------- CSG_PointCloud *pPoints = SG_Create_PointCloud(); pPoints->Set_Name(SG_File_Get_Name(Parameters("FILE")->asString(), false)); Parameters("POINTS")->Set_Value(pPoints); CSG_Array_Int Fields; //----------------------------------------------------- if( SG_UI_Get_Window_Main() ) { CSG_Parameters &Fields = *Parameters("FIELDSPECS")->asParameters(); int nFields = Fields.Get_Count() / 2; CSG_String Names, Types; for(int iField=0; iField<nFields; iField++) { Names += CSG_String::Format("%s;", Fields(CSG_String::Format("NAME%03d", iField))->asString()); Types += CSG_String::Format("%d;", Fields(CSG_String::Format("TYPE%03d", iField))->asInt ()); } Parameters("FIELDNAMES")->Set_Value(Names); Parameters("FIELDTYPES")->Set_Value(Types); } { TSG_Data_Type Type = SG_DATATYPE_Float; // default CSG_String_Tokenizer tokFields(Parameters("FIELDS" )->asString(), ";"); CSG_String_Tokenizer tokTypes (Parameters("FIELDTYPES")->asString(), ";"); CSG_String_Tokenizer tokNames (Parameters("FIELDNAMES")->asString(), ";"); while( tokFields.Has_More_Tokens() ) { int iField; if( !tokFields.Get_Next_Token().asInt(iField) || iField < 1 ) { Error_Set(_TL("Error parsing attribute field index")); return( false ); } Fields += iField - 1; CSG_String Name; if( tokNames.Has_More_Tokens() ) { Name = tokNames.Get_Next_Token(); Name.Trim(true); Name.Trim(false); } if( Name.is_Empty() ) { if( iField - 1 < Values.Get_Count() ) { Name = Values[iField - 1]; } else { Name.Printf("FIELD%02d", iField); } } if( tokTypes.Has_More_Tokens() ) { Get_Data_Type(Type, tokTypes.Get_Next_Token()); } pPoints->Add_Field(Name, Type); } } //----------------------------------------------------- Process_Set_Text(_TL("Importing data ...")); int nLines = 0; sLong Length = Stream.Length(); while( Stream.Read_Line(sLine) ) { nLines++; if( pPoints->Get_Count() % 10000 == 0 && !Set_Progress((double)Stream.Tell(), (double)Length) ) { return( true ); // user break } //------------------------------------------------- CSG_String_Tokenizer tokValues(sLine, Separator); Values.Clear(); while( tokValues.Has_More_Tokens() ) // read every column in this line and fill vector { Values += tokValues.Get_Next_Token(); } //------------------------------------------------- double x, y, z; if( xField >= Values.Get_Count() || !Values[xField].asDouble(x) || yField >= Values.Get_Count() || !Values[yField].asDouble(y) || zField >= Values.Get_Count() || !Values[zField].asDouble(z) ) { Message_Fmt("\n%s: %s [%d]", _TL("Warning"), _TL("Skipping misformatted line"), nLines); continue; } pPoints->Add_Point(x, y, z); //------------------------------------------------- for(int iAttribute=0; iAttribute<pPoints->Get_Attribute_Count(); iAttribute++) { if( Fields[iAttribute] >= Values.Get_Count() ) { pPoints->Set_NoData(3 + iAttribute); } else switch( pPoints->Get_Attribute_Type(iAttribute) ) { case SG_DATATYPE_String: pPoints->Set_Attribute(iAttribute, Values[Fields[iAttribute]]); break; default: { double Value; if( Values[Fields[iAttribute]].asDouble(Value) ) { pPoints->Set_Attribute(iAttribute, Value); } else { pPoints->Set_NoData(3 + iAttribute); } } break; } } } //----------------------------------------------------- DataObject_Set_Parameter(pPoints, "DISPLAY_VALUE_AGGREGATE", 3); // highest z DataObject_Set_Parameter(pPoints, "COLORS_TYPE" , 3); // graduated colors DataObject_Set_Parameter(pPoints, "METRIC_ATTRIB" , 2); // z attrib DataObject_Set_Parameter(pPoints, "METRIC_ZRANGE", pPoints->Get_Minimum(2), pPoints->Get_Maximum(2)); DataObject_Update(pPoints); //----------------------------------------------------- if( nLines > pPoints->Get_Count() ) { Message_Add(" ", true); Message_Fmt("%s: %d %s", _TL("Warning"), nLines - pPoints->Get_Count(), _TL("invalid points have been skipped")); } Message_Add(" ", true); Message_Fmt("%d %s", pPoints->Get_Count(), _TL("points have been imported with success")); return( true ); }
//--------------------------------------------------------- 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 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); }
//--------------------------------------------------------- bool CGrid_Cluster_Analysis::_On_Execute(void) { int i, j, *nMembers, nCluster, nElements; double *Variances, **Centroids, SP; CSG_Grid **Grids, *pCluster; CSG_Parameter_Grid_List *pGrids; //----------------------------------------------------- pGrids = Parameters("GRIDS") ->asGridList(); pCluster = Parameters("CLUSTER") ->asGrid(); nCluster = Parameters("NCLUSTER")->asInt(); if( pGrids->Get_Count() < 1 ) { return( false ); } //----------------------------------------------------- Grids = (CSG_Grid **)SG_Malloc(pGrids->Get_Count() * sizeof(CSG_Grid *)); if( Parameters("NORMALISE")->asBool() ) { for(i=0; i<pGrids->Get_Count(); i++) { Grids[i] = SG_Create_Grid(pGrids->asGrid(i), SG_DATATYPE_Float); Grids[i] ->Assign(pGrids->asGrid(i)); Grids[i] ->Standardise(); } } else { for(i=0; i<pGrids->Get_Count(); i++) { Grids[i] = pGrids->asGrid(i); } } pCluster->Set_NoData_Value(-1.0); pCluster->Assign_NoData(); nMembers = (int *)SG_Malloc(nCluster * sizeof(int)); Variances = (double *)SG_Malloc(nCluster * sizeof(double)); Centroids = (double **)SG_Malloc(nCluster * sizeof(double *)); for(i=0; i<nCluster; i++) { Centroids[i] = (double *)SG_Malloc(pGrids->Get_Count() * sizeof(double)); } //------------------------------------------------- switch( Parameters("METHOD")->asInt() ) { case 0: SP = _MinimumDistance (Grids, pGrids->Get_Count(), pCluster, nCluster, nMembers, Variances, Centroids, nElements = Get_NCells()); break; case 1: SP = _HillClimbing (Grids, pGrids->Get_Count(), pCluster, nCluster, nMembers, Variances, Centroids, nElements = Get_NCells()); break; case 2: SP = _MinimumDistance (Grids, pGrids->Get_Count(), pCluster, nCluster, nMembers, Variances, Centroids, nElements = Get_NCells()); SP = _HillClimbing (Grids, pGrids->Get_Count(), pCluster, nCluster, nMembers, Variances, Centroids, nElements = Get_NCells()); break; } //------------------------------------------------- if( Parameters("NORMALISE")->asBool() ) { for(i=0; i<pGrids->Get_Count(); i++) { delete(Grids[i]); for(j=0; j<nCluster; j++) { Centroids[j][i] = pGrids->asGrid(i)->Get_StdDev() * Centroids[j][i] + pGrids->asGrid(i)->Get_Mean(); } } } //------------------------------------------------- Save_LUT(pCluster); //------------------------------------------------- int iCluster, iFeature; CSG_String s; CSG_Table_Record *pRecord; CSG_Table *pTable; pTable = Parameters("STATISTICS")->asTable(); pTable->Destroy(); pTable->Set_Name(_TL("Cluster Analysis")); pTable->Add_Field(_TL("ClusterID") , SG_DATATYPE_Int); pTable->Add_Field(_TL("Elements") , SG_DATATYPE_Int); pTable->Add_Field(_TL("Std.Dev.") , SG_DATATYPE_Double); s.Printf(SG_T("\n%s:\t%ld \n%s:\t%d \n%s:\t%d \n%s:\t%f\n\n%s\t%s\t%s"), _TL("Number of Elements") , nElements, _TL("Number of Variables") , pGrids->Get_Count(), _TL("Number of Clusters") , nCluster, _TL("Standard Deviation") , sqrt(SP), _TL("Cluster"), _TL("Elements"), _TL("Std.Dev.") ); for(iFeature=0; iFeature<pGrids->Get_Count(); iFeature++) { s += CSG_String::Format(SG_T("\t%s"), pGrids->asGrid(iFeature)->Get_Name()); pTable->Add_Field(pGrids->asGrid(iFeature)->Get_Name(), SG_DATATYPE_Double); } Message_Add(s); for(iCluster=0; iCluster<nCluster; iCluster++) { Variances[iCluster] = nMembers[iCluster] ? Variances[iCluster] / nMembers[iCluster] : 0.0; s.Printf(SG_T("\n%d\t%d\t%f"), iCluster, nMembers[iCluster], sqrt(Variances[iCluster])); pRecord = pTable->Add_Record(); pRecord->Set_Value(0, iCluster); pRecord->Set_Value(1, nMembers[iCluster]); pRecord->Set_Value(2, sqrt(Variances[iCluster])); for(iFeature=0; iFeature<pGrids->Get_Count(); iFeature++) { double Centroid = Centroids[iCluster][iFeature]; if( Parameters("NORMALISE")->asBool() ) { Centroid = pGrids->asGrid(iFeature)->Get_Mean() + Centroid * pGrids->asGrid(iFeature)->Get_StdDev(); } s += CSG_String::Format(SG_T("\t%f"), Centroid); pRecord->Set_Value(iFeature + 3, Centroid); } Message_Add(s, false); } //------------------------------------------------- for(i=0; i<nCluster; i++) { SG_Free(Centroids[i]); } SG_Free(Centroids); SG_Free(Variances); SG_Free(nMembers); SG_Free(Grids); 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 CPolygon_Intersection::On_Execute(void) { CSG_String sName; m_pShapes_A = Parameters("SHAPES_A") ->asShapes(); m_pShapes_B = Parameters("SHAPES_B") ->asShapes(); m_pShapes_AB = Parameters("SHAPES_AB") ->asShapes(); m_bSplitParts = Parameters("SPLITPARTS") ->asBool(); m_iField_A = Parameters("FIELD_A") ->asInt(); if( m_iField_A >= m_pShapes_A->Get_Field_Count() ) { m_iField_A = -1; } m_iField_B = Parameters("FIELD_B") ->asInt(); if( m_iField_B >= m_pShapes_B->Get_Field_Count() ) { m_iField_B = -1; } if( m_pShapes_A->Get_Type() == SHAPE_TYPE_Polygon && m_pShapes_A->is_Valid() && m_pShapes_B->Get_Type() == SHAPE_TYPE_Polygon && m_pShapes_B->is_Valid() ) { m_pShapes_AB->Create(SHAPE_TYPE_Polygon); m_pShapes_AB->Add_Field("ID" , SG_DATATYPE_Int); m_pShapes_AB->Add_Field("ID_A" , SG_DATATYPE_Int); m_pShapes_AB->Add_Field("ID_B" , SG_DATATYPE_Int); m_pShapes_AB->Add_Field("ID_AB" , SG_DATATYPE_Int); if( m_iField_A >= 0 ) { m_pShapes_AB->Add_Field(m_pShapes_A->Get_Field_Name(m_iField_A), m_pShapes_A->Get_Field_Type(m_iField_A)); } if( m_iField_B >= 0 ) { m_pShapes_AB->Add_Field(m_pShapes_B->Get_Field_Name(m_iField_B), m_pShapes_B->Get_Field_Type(m_iField_B)); } //------------------------------------------------- switch( Parameters("METHOD")->asInt() ) { //------------------------------------------------- case 0: // Complete Intersection... sName.Printf(SG_T("%s [%s]-[%s]"), _TL("Intersection"), m_pShapes_A->Get_Name(), m_pShapes_B->Get_Name()); Get_Intersection(m_pShapes_A, m_pShapes_B, MODE_FIRST); Get_Difference (m_pShapes_A, m_pShapes_B, MODE_FIRST); Get_Difference (m_pShapes_B, m_pShapes_A, MODE_SECOND); break; //------------------------------------------------- case 1: // Intersection... sName.Printf(SG_T("%s [%s]-[%s]"), _TL("Intersection"), m_pShapes_A->Get_Name(), m_pShapes_B->Get_Name()); Get_Intersection(m_pShapes_A, m_pShapes_B, MODE_FIRST); break; //------------------------------------------------- case 2: // Difference A - B... sName.Printf(SG_T("%s [%s]-[%s]"), _TL("Difference"), m_pShapes_A->Get_Name(), m_pShapes_B->Get_Name()); Get_Difference (m_pShapes_A, m_pShapes_B, MODE_FIRST); break; //------------------------------------------------- case 3: // Difference B - A... sName.Printf(SG_T("%s [%s]-[%s]"), _TL("Difference"), m_pShapes_B->Get_Name(), m_pShapes_A->Get_Name()); Get_Difference (m_pShapes_B, m_pShapes_A, MODE_SECOND); break; } //------------------------------------------------- m_pShapes_AB->Set_Name(sName); return( m_pShapes_AB->Get_Count() > 0 ); } return( false ); }/**/