//--------------------------------------------------------- bool CTable_List::On_Execute(void) { CSG_Table *pTables = Parameters("TABLES")->asTable(); pTables->Destroy(); pTables->Set_Name(_TL("Tables")); pTables->Add_Field(_TL("Table"), SG_DATATYPE_String); if( Get_Connection() ) { CSG_Strings Tables; Get_Connection()->Get_Tables(Tables); for(int i=0; i<Tables.Get_Count(); i++) { CSG_Table_Record *pTable = pTables->Add_Record(); pTable->Set_Value(0, Tables[i]); } pTables->Set_Name(Get_Connection()->Get_Server() + " [" + _TL("Tables") + "]"); return( true ); } return( false ); }
//--------------------------------------------------------- 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 CGSPoints_Distances::On_Execute(void) { //----------------------------------------------------- CSG_Shapes *pPoints = Parameters("POINTS") ->asShapes(); CSG_Table *pTable = Parameters("TABLE") ->asTable(); //----------------------------------------------------- CSG_PRQuadTree QT(pPoints, 0); CSG_Simple_Statistics s; double x, y, z; for(int iPoint=0; iPoint<pPoints->Get_Count() && Set_Progress(iPoint, pPoints->Get_Count()); iPoint++) { TSG_Point p = pPoints->Get_Shape(iPoint)->Get_Point(0); if( QT.Select_Nearest_Points(p.x, p.y, 2) && QT.Get_Selected_Point(1, x, y, z) && (x != p.x || y != p.y) ) { s.Add_Value(SG_Get_Distance(x, y, p.x, p.y)); } } //----------------------------------------------------- if( s.Get_Count() > 0 ) { CSG_Table_Record *pRecord; pTable->Destroy(); pTable->Set_Name(CSG_String::Format(SG_T("%s [%s]"), _TL("Minimum Distance Analysis"), pPoints->Get_Name())); pTable->Add_Field(SG_T("NAME") , SG_DATATYPE_String); pTable->Add_Field(SG_T("VALUE") , SG_DATATYPE_Double); SET_VALUE(_TL("Mean Average") , s.Get_Mean()); SET_VALUE(_TL("Minimum") , s.Get_Minimum()); SET_VALUE(_TL("Maximum") , s.Get_Maximum()); SET_VALUE(_TL("Standard Deviation") , s.Get_StdDev()); SET_VALUE(_TL("Duplicates") , pPoints->Get_Count() - s.Get_Count()); DataObject_Update(pTable, SG_UI_DATAOBJECT_SHOW); return( true ); } Message_Dlg(_TL("not enough observations")); return( false ); }
//--------------------------------------------------------- bool CBifurcation::On_Execute(void) { int i; double p, r, dr, max, min, seed, nValues, nPreIterations; CSG_Table_Record *pRecord; CSG_Table *pTable; nPreIterations = Parameters("ITERATIONS")->asInt(); nValues = Parameters("NVALUES")->asInt(); seed = Parameters("SEED")->asDouble(); min = Parameters("RANGE")->asRange()->Get_Min(); max = Parameters("RANGE")->asRange()->Get_Max(); dr = (max - min) / 1000.0; pTable = Parameters("TABLE")->asTable(); pTable->Destroy(); pTable->Set_Name(_TL("Feigenbaum's Bifurcation")); pTable->Add_Field("Growth" , SG_DATATYPE_Double); for(i=0; i<nValues; i++) { pTable->Add_Field(CSG_String::Format(SG_T("VALUE_%d"), i + 1), SG_DATATYPE_Double); } for(r=min; r<=max; r+=dr) { pRecord = pTable->Add_Record(); pRecord->Set_Value(0, r); p = seed; for(i=0; i<nPreIterations; i++) { p = r * p * (1.0 - p); } for(i=0; i<nValues; i++) { p = r * p * (1.0 - p); pRecord->Set_Value(i + 1, p); } } return( true ); }
//--------------------------------------------------------- bool CTable_List::On_Execute(void) { CSG_Table *pTables = Parameters("TABLES")->asTable(); pTables->Destroy(); pTables->Set_Name(Get_Connection()->Get_Connection() + " [" + _TL("Tables") + "]"); pTables->Add_Field(_TL("Table"), SG_DATATYPE_String); pTables->Add_Field(_TL("Type" ), SG_DATATYPE_String); CSG_Strings Tables; if( Get_Connection()->Get_Tables(Tables) ) { CSG_Table t; for(int i=0; i<Tables.Get_Count(); i++) { CSG_Table_Record *pTable = pTables->Add_Record(); pTable->Set_Value(0, Tables[i]); if( Get_Connection()->Table_Load(t, "geometry_columns", "type", CSG_String::Format("f_table_name='%s'", Tables[i].c_str())) && t.Get_Count() == 1 ) { pTable->Set_Value(1, t[0][0].asString()); } else if( Get_Connection()->Table_Load(t, "raster_columns", "*", CSG_String::Format("r_table_name='%s'", Tables[i].c_str())) && t.Get_Count() == 1 ) { pTable->Set_Value(1, "RASTER"); } else { pTable->Set_Value(1, "TABLE"); } } } return( pTables->Get_Count() > 0 ); }
//--------------------------------------------------------- bool CChange_Detection::Get_Classes(CSG_Table &Classes, CSG_Grid *pGrid, bool bInitial) { CSG_Table *pClasses; Classes.Destroy(); Classes.Add_Field(_TL("NAME") , SG_DATATYPE_String); Classes.Add_Field(_TL("MIN") , SG_DATATYPE_Double); Classes.Add_Field(_TL("MAX") , SG_DATATYPE_Double); //----------------------------------------------------- if( (pClasses = Parameters(bInitial ? "INI_LUT" : "FIN_LUT")->asTable()) != NULL ) { int fNam = Parameters(bInitial ? "INI_LUT_NAM" : "FIN_LUT_NAM")->asInt(); int fMin = Parameters(bInitial ? "INI_LUT_MIN" : "FIN_LUT_MIN")->asInt(); int fMax = Parameters(bInitial ? "INI_LUT_MAX" : "FIN_LUT_MAX")->asInt(); if( fNam < 0 || fNam >= pClasses->Get_Field_Count() ) { fNam = fMin; } if( fMax < 0 || fMax >= pClasses->Get_Field_Count() ) { fMax = fMin; } for(int iClass=0; iClass<pClasses->Get_Count(); iClass++) { CSG_Table_Record *pClass = Classes.Add_Record(); pClass->Set_Value(CLASS_NAM, pClasses->Get_Record(iClass)->asString(fNam)); pClass->Set_Value(CLASS_MIN, pClasses->Get_Record(iClass)->asDouble(fMin)); pClass->Set_Value(CLASS_MAX, pClasses->Get_Record(iClass)->asDouble(fMax)); } } //----------------------------------------------------- else if( DataObject_Get_Parameter(pGrid, "LUT") ) { pClasses = DataObject_Get_Parameter(pGrid, "LUT")->asTable(); for(int iClass=0; iClass<pClasses->Get_Count(); iClass++) { CSG_Table_Record *pClass = Classes.Add_Record(); pClass->Set_Value(CLASS_NAM, pClasses->Get_Record(iClass)->asString(1)); pClass->Set_Value(CLASS_MIN, pClasses->Get_Record(iClass)->asDouble(3)); pClass->Set_Value(CLASS_MAX, pClasses->Get_Record(iClass)->asDouble(4)); } } //----------------------------------------------------- else { double z; for(sLong i=0; i<Get_NCells() && Set_Progress_NCells(i); i++) { double iz = pGrid->asDouble(pGrid->Get_Sorted(i, false, false)); if( i == 0 || iz != z ) { CSG_Table_Record *pClass = Classes.Add_Record(); pClass->Set_Value(CLASS_NAM, z = iz); pClass->Set_Value(CLASS_MIN, z); pClass->Set_Value(CLASS_MAX, z); } } } //----------------------------------------------------- return( Classes.Get_Count() > 0 ); }
//--------------------------------------------------------- bool CTable_Field_Deletion::On_Execute(void) { //----------------------------------------------------- CSG_Parameter_Table_Fields *pFields = Parameters("FIELDS")->asTableFields(); if( pFields->Get_Count() <= 0 ) { Error_Set(_TL("no fields in selection")); return( false ); } //----------------------------------------------------- CSG_Table *pTable = Parameters("TABLE")->asTable(); CSG_Table *pOutput = NULL; if( pTable->Get_ObjectType() == DATAOBJECT_TYPE_Shapes ) { if( (pOutput = Parameters("OUT_SHAPES")->asShapes()) != NULL && pOutput != pTable ) { ((CSG_Shapes *)pOutput)->Create(((CSG_Shapes *)pTable)->Get_Type(), (const wchar_t*)0, (CSG_Table *)0, ((CSG_Shapes *)pTable)->Get_Vertex_Type()); } } else // if( pTable->Get_ObjectType() == DATAOBJECT_TYPE_Table ) { if( (pOutput = Parameters("OUT_TABLE" )->asTable()) != NULL && pOutput != pTable ) { pOutput->Destroy(); } } //----------------------------------------------------- if( pOutput ) { int nFields = pTable->Get_Field_Count() - pFields->Get_Count(); int *pFieldsOut = new int[nFields]; int iField = 0; for(int i=0; i<pTable->Get_Field_Count(); i++) { bool bDelete = false; for(int j=0; j<pFields->Get_Count(); j++) { if( i == pFields->Get_Index(j) ) { bDelete = true; break; } } if( !bDelete ) { pFieldsOut[iField] = i; iField++; } } pOutput->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pTable->Get_Name(), _TL("Changed"))); for(iField=0; iField<nFields; iField++) { pOutput->Add_Field(pTable->Get_Field_Name(pFieldsOut[iField]), pTable->Get_Field_Type(pFieldsOut[iField])); } for(int iRecord=0; iRecord<pTable->Get_Count(); iRecord++) { CSG_Table_Record *pOut, *pIn = pTable->Get_Record(iRecord); if( pOutput->Get_ObjectType() == DATAOBJECT_TYPE_Shapes ) { pOut = ((CSG_Shapes *)pOutput)->Add_Shape(pIn, SHAPE_COPY_GEOM); if( ((CSG_Shapes *)pOutput)->Get_Vertex_Type() > SG_VERTEX_TYPE_XY ) { for(int iPart=0; iPart<((CSG_Shape *)pIn)->Get_Part_Count(); iPart++) { for(int iPoint=0; iPoint<((CSG_Shape *)pIn)->Get_Point_Count(iPart); iPoint++) { ((CSG_Shape *)pOut)->Set_Z(((CSG_Shape *)pIn)->Get_Z(iPoint, iPart), iPoint, iPart); if( ((CSG_Shapes *)pOutput)->Get_Vertex_Type() == SG_VERTEX_TYPE_XYZM ) { ((CSG_Shape *)pOut)->Set_M(((CSG_Shape *)pIn)->Get_M(iPoint, iPart), iPoint, iPart); } } } } } else { pOut = pOutput->Add_Record(); } for(iField=0; iField<nFields; iField++) { *pOut->Get_Value(iField) = *pIn->Get_Value(pFieldsOut[iField]); } } delete[] pFieldsOut; } else { for(int iField=pFields->Get_Count()-1; iField>=0; iField--) { pTable->Del_Field(pFields->Get_Index(pFields->Get_Index(iField))); } DataObject_Update(pTable); } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CTable_PCA::Get_Components(CSG_Matrix &Eigen_Vectors, CSG_Vector &Eigen_Values) { int i, j, n, field0; double Sum, Cum; /////////////////////////////////////////////////////// //----------------------------------------------------- for(i=0, Sum=0.0; i<m_nFeatures; i++) { Sum += Eigen_Values[i]; } Sum = Sum > 0.0 ? 100.0 / Sum : 0.0; Message_Add(CSG_String::Format(SG_T("\n%s, %s, %s\n"), _TL("explained variance"), _TL("explained cumulative variance"), _TL("Eigenvalue")), false); for(j=m_nFeatures-1, Cum=0.0; j>=0; j--) { Cum += Eigen_Values[j] * Sum; Message_Add(CSG_String::Format(SG_T("%6.2f\t%6.2f\t%18.5f\n"), Eigen_Values[j] * Sum, Cum, Eigen_Values[j]), false); } Message_Add(CSG_String::Format(SG_T("\n%s:\n"), _TL("Eigenvectors")), false); for(j=0; j<m_nFeatures; j++) { for(i=0; i<m_nFeatures; i++) { Message_Add(CSG_String::Format(SG_T("%12.4f"), Eigen_Vectors[j][m_nFeatures - 1 - i]), false); } Message_Add(SG_T("\n"), false); } /////////////////////////////////////////////////////// //----------------------------------------------------- n = Parameters("NFIRST")->asInt(); if( n <= 0 || n > m_nFeatures ) { n = m_nFeatures; } //----------------------------------------------------- CSG_Table *pPCA = Parameters("PCA")->asTable(); if( pPCA == NULL ) { pPCA = m_pTable; } if( pPCA != m_pTable ) { pPCA->Destroy(); pPCA->Set_Name(CSG_String::Format(SG_T("%s [%s]"), m_pTable->Get_Name(), _TL("Principal Components"))); } //----------------------------------------------------- field0 = pPCA->Get_Field_Count(); for(i=0; i<n; i++) { pPCA->Add_Field(CSG_String::Format(SG_T("%s %d"), _TL("Component"), i + 1), SG_DATATYPE_Double); } //----------------------------------------------------- for(int iElement=0; iElement<m_pTable->Get_Count() && Set_Progress(iElement, m_pTable->Get_Count()); iElement++) { if( !is_NoData(iElement) ) { CSG_Table_Record *pElement = pPCA == m_pTable ? pPCA->Get_Record(iElement) : pPCA->Add_Record(); for(i=0, j=m_nFeatures-1; i<n; i++, j--) { double d = 0.0; for(int k=0; k<m_nFeatures; k++) { d += Get_Value(k, iElement) * Eigen_Vectors[k][j]; } pElement->Set_Value(field0 + i, d); } } } //----------------------------------------------------- if( pPCA == m_pTable ) { DataObject_Update(m_pTable); } return( true ); }
//--------------------------------------------------------- bool CWator::On_Execute(void) { //----------------------------------------------------- m_pWator = m_Grid_Target.Get_Grid("GRID", SG_DATATYPE_Byte); if( !m_pWator ) { Error_Set(_TL("could not create target grid")); return( false ); } //----------------------------------------------------- m_pWator->Set_Name(_TL("Wa-Tor")); m_pWator->Set_NoData_Value(-1); CSG_Colors Colors(3); Colors.Set_Color(0, SG_COLOR_BLACK); Colors.Set_Color(1, SG_COLOR_GREEN); Colors.Set_Color(2, SG_COLOR_RED ); DataObject_Add (m_pWator); DataObject_Set_Colors(m_pWator, Colors); DataObject_Update (m_pWator, 0, 2, SG_UI_DATAOBJECT_SHOW); //----------------------------------------------------- if( Parameters("REFRESH")->asBool() ) { double Fish_perc = Parameters("INIT_FISH" )->asDouble(); double Shark_perc = Parameters("INIT_SHARK")->asDouble() + Fish_perc; #pragma omp parallel for for(int y=0; y<m_pWator->Get_NY(); y++) { for(int x=0; x<m_pWator->Get_NX(); x++) { double perc = CSG_Random::Get_Uniform(0, 100); if( perc <= Fish_perc ) { m_pWator->Set_Value(x, y, FISH); } else if( perc <= Shark_perc ) { m_pWator->Set_Value(x, y, SHARK); } else { m_pWator->Set_Value(x, y, 0); } } } } //----------------------------------------------------- CSG_Table *pTable = Parameters("TABLE")->asTable(); pTable->Destroy(); pTable->Set_Name(_TL("Wa-Tor")); pTable->Add_Field("Cycle" , SG_DATATYPE_Int); pTable->Add_Field("Fishes", SG_DATATYPE_Int); pTable->Add_Field("Sharks", SG_DATATYPE_Int); //----------------------------------------------------- m_Fish_Birth = Parameters("FISH_BIRTH" )->asInt(); m_Shark_Birth = Parameters("SHARK_BIRTH" )->asInt(); m_Shark_Starve = Parameters("SHARK_STARVE")->asInt(); m_Next .Create(m_pWator, SG_DATATYPE_Byte); m_Age .Create(m_pWator, SG_DATATYPE_Byte); m_Starve.Create(m_pWator, SG_DATATYPE_Byte); #pragma omp parallel for for(int y=0; y<m_pWator->Get_NY(); y++) { for(int x=0; x<m_pWator->Get_NX(); x++) { switch( m_pWator->asByte(x, y) ) { case FISH: m_Age .Set_Value(x, y, CSG_Random::Get_Uniform(0.0, m_Fish_Birth )); break; case SHARK: m_Age .Set_Value(x, y, CSG_Random::Get_Uniform(0.0, m_Shark_Birth )); m_Starve.Set_Value(x, y, CSG_Random::Get_Uniform(0.0, m_Shark_Starve)); break; } } } //----------------------------------------------------- int i; SG_UI_Progress_Lock(true); for(i=1; Process_Get_Okay(true) && Next_Cycle(); i++) { Process_Set_Text("%s: %d", _TL("Life Cycle"), i); CSG_Table_Record *pRecord = pTable->Add_Record(); pRecord->Set_Value(0, i); pRecord->Set_Value(1, m_nFishes); pRecord->Set_Value(2, m_nSharks); DataObject_Update(m_pWator, 0, 2); DataObject_Update(pTable); } SG_UI_Progress_Lock(false); //----------------------------------------------------- m_Next .Destroy(); m_Age .Destroy(); m_Starve.Destroy(); if( is_Progress() ) { Message_Fmt("\n%s %d %s", _TL("Dead after"), i, _TL("Life Cycles")); } return( true ); }
//--------------------------------------------------------- bool CGSGrid_Zonal_Statistics::On_Execute(void) { bool bShortNames; int x, y, nCatGrids, nStatGrids, iGrid, zoneID, catID, NDcount, catLevel, NDcountStat; double statID; CSG_Grid *pZones, *pGrid, *pAspect; CSG_Parameter_Grid_List *pCatList; CSG_Parameter_Grid_List *pStatList; CList_Conti *newZone, *startList, *runList, *newSub, *parent, *runSub, *subList; CList_Stat *runStats; CSG_Table *pOutTab; CSG_Table_Record *pRecord; CSG_String fieldName, tmpName; pZones = Parameters("ZONES") ->asGrid(); pCatList = Parameters("CATLIST") ->asGridList(); pStatList = Parameters("STATLIST") ->asGridList(); pAspect = Parameters("ASPECT") ->asGrid(); pOutTab = Parameters("OUTTAB") ->asTable(); bShortNames = Parameters("SHORTNAMES") ->asBool(); nCatGrids = pCatList ->Get_Count(); nStatGrids = pStatList ->Get_Count(); NDcount = 0; // NoData Counter (ZoneGrid) NDcountStat = 0; // NoData Counter (StatGrids) if (pOutTab != NULL) pOutTab->Destroy(); newZone = new CList_Conti(); // create first list entry (dummy) startList = newZone; for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { runList = startList; zoneID = pZones->asInt(x, y); // get zone ID while( runList->next != NULL && runList->cat < zoneID ) // search for last entry in list or insert point { runList = runList->next; } if( runList->dummy == true ) { runList->cat = zoneID; // first list entry, write and runList->dummy = false; // setup } else if( runList->cat == zoneID ) runList = runList; // zoneID found else if( runList->next == NULL && runList->cat < zoneID ) // append zoneID { newZone = new CList_Conti(); newZone->previous = runList; runList->next = newZone; newZone->cat = zoneID; // ... and write info newZone->dummy = false; runList = newZone; } else // insert new entry { newZone = new CList_Conti(); newZone->next = runList; if( runList->previous != NULL ) { newZone->previous = runList->previous; runList->previous->next = newZone; } runList->previous = newZone; if( runList == startList ) startList = newZone; // if new entry is first element, update startList pointer newZone->cat = zoneID; // ... and write info newZone->dummy = false; runList = newZone; } for(iGrid=0; iGrid<nCatGrids; iGrid++) // collect categories { parent = runList; if( runList->sub == NULL ) // no sub class found { newSub = new CList_Conti(); runList->sub = newSub; } runList = runList->sub; pGrid = pCatList->asGrid(iGrid); if( !pGrid->is_NoData(x, y) ) catID = pGrid->asInt(x, y); else catID = (int)pGrid->Get_NoData_Value(); while( runList->next != NULL && runList->cat < catID ) // search for last entry in list or insert point { runList = runList->next; } if( runList->dummy == true ) { runList->cat = catID; // first list entry, write and runList->dummy = false; // setup runList->parent = parent; } else if( runList->cat == catID ) runList = runList; // zoneID found, all infos already written else if( runList->next == NULL && runList->cat < catID) // append zoneID { newSub = new CList_Conti(); newSub->cat = catID; // ... and write info newSub->previous = runList; newSub->parent = parent; newSub->dummy = false; runList->next = newSub; runList = newSub; } else // insert new entry { newSub = new CList_Conti(); newSub->cat = catID; // ... and write info newSub->next = runList; newSub->parent = parent; newSub->dummy = false; if( runList->previous != NULL ) { newSub->previous = runList->previous; runList->previous->next = newSub; } else parent->sub = newSub; runList->previous = newSub; runList = newSub; } } for(iGrid=0; iGrid<nStatGrids; iGrid++) // collect statistics for StatGrids { if( iGrid == 0 ) { if( runList->stats == NULL ) runList->stats = new CList_Stat(); runStats = runList->stats; } else { if( runStats->next == NULL ) runStats->next = new CList_Stat(); runStats = runStats->next; } if( !pStatList->asGrid(iGrid)->is_NoData(x, y) ) { statID = pStatList->asGrid(iGrid)->asDouble(x, y); if( runStats->dummy == true ) { runStats->min = statID; runStats->max = statID; runStats->dummy = false; } if( runStats->min > statID ) runStats->min = statID; if( runStats->max < statID ) runStats->max = statID; runStats->sum += statID; runStats->dev += pow(statID, 2); } else NDcountStat += 1; } if( pAspect != NULL ) { for( int i=0; i<2; i++ ) { if( nStatGrids == 0 && i == 0 ) { if( runList->stats == NULL ) runList->stats = new CList_Stat(); runStats = runList->stats; } else { if( runStats->next == NULL ) runStats->next = new CList_Stat(); runStats = runStats->next; } if( !pAspect->is_NoData(x, y) ) { statID = pAspect->asDouble(x, y); if( i == 0 ) { if( runStats->dummy == true ) { runStats->min = statID; runStats->max = statID; runStats->dummy = false; } if( runStats->min > statID ) runStats->min = statID; if( runStats->max < statID ) runStats->max = statID; statID = sin(statID); } else statID = cos(statID); runStats->sum += statID; } else NDcountStat += 1; } } runList->count += 1; // sum up unique condition area } } // Create fields in output table (1st = Zone, 2nd = Catgrid1, 3rd = Catgrid 2, ...) fieldName = CSG_String::Format(SG_T("%s"),pZones->Get_Name()).BeforeFirst(SG_Char('.')); if (bShortNames && fieldName.Length() > 10) fieldName.Remove(10, fieldName.Length()-10); pOutTab->Add_Field(fieldName, SG_DATATYPE_Int); for(iGrid=0; iGrid<nCatGrids; iGrid++) { fieldName = CSG_String::Format(SG_T("%s"),pCatList->asGrid(iGrid)->Get_Name()).BeforeFirst(SG_Char('.')); if (bShortNames && fieldName.Length() > 10) fieldName.Remove(10, fieldName.Length()-10); pOutTab->Add_Field(fieldName, SG_DATATYPE_Int); } pOutTab->Add_Field("Count", SG_DATATYPE_Int); for( iGrid=0; iGrid<nStatGrids; iGrid++ ) { tmpName = CSG_String::Format(SG_T("%s"),pStatList->asGrid(iGrid)->Get_Name()).BeforeFirst(SG_Char('.')); fieldName = tmpName; if (bShortNames && fieldName.Length()+3 > 10) fieldName.Remove(7, fieldName.Length()-7); pOutTab->Add_Field(CSG_String::Format(SG_T("%sMIN") , fieldName.c_str()), SG_DATATYPE_Double); pOutTab->Add_Field(CSG_String::Format(SG_T("%sMAX") , fieldName.c_str()), SG_DATATYPE_Double); fieldName = tmpName; if (bShortNames && fieldName.Length()+4 > 10) fieldName.Remove(6, fieldName.Length()-6); pOutTab->Add_Field(CSG_String::Format(SG_T("%sMEAN") , fieldName.c_str()), SG_DATATYPE_Double); fieldName = tmpName; if (bShortNames && fieldName.Length()+6 > 10) fieldName.Remove(4, fieldName.Length()-4); pOutTab->Add_Field(CSG_String::Format(SG_T("%sSTDDEV"), fieldName.c_str()), SG_DATATYPE_Double); fieldName = tmpName; if (bShortNames && fieldName.Length()+3 > 10) fieldName.Remove(7, fieldName.Length()-7); pOutTab->Add_Field(CSG_String::Format(SG_T("%sSUM") , fieldName.c_str()), SG_DATATYPE_Double); } if( pAspect != NULL ) { tmpName = CSG_String::Format(SG_T("%s"),pAspect->Get_Name()).BeforeFirst(SG_Char('.')); fieldName = tmpName; if (bShortNames && fieldName.Length()+3 > 10) fieldName.Remove(7, fieldName.Length()-7); pOutTab->Add_Field(CSG_String::Format(SG_T("%sMIN") , fieldName.c_str()), SG_DATATYPE_Double); pOutTab->Add_Field(CSG_String::Format(SG_T("%sMAX") , fieldName.c_str()), SG_DATATYPE_Double); fieldName = tmpName; if (bShortNames && fieldName.Length()+4 > 10) fieldName.Remove(6, fieldName.Length()-6); pOutTab->Add_Field(CSG_String::Format(SG_T("%sMEAN") , fieldName.c_str()), SG_DATATYPE_Double); } while( startList != NULL ) // scan zone layer list and write cat values in table { runList = startList; while( runList->sub != NULL ) // fall down to lowest layer runList = runList->sub; subList = runList; // use pointer to scan horizontal while( subList != NULL ) // move forward and read all categories of this layer (including the parent layers) { runSub = subList; catLevel = nCatGrids; pRecord = pOutTab->Add_Record(); // create new record in table pRecord->Set_Value((catLevel+1), runSub->count); // read/write field count for(iGrid=0; iGrid<nStatGrids; iGrid++) // read/write statistics { if( iGrid == 0 ) runStats = runSub->stats; else runStats = runStats->next; pRecord->Set_Value(catLevel+2+iGrid*5, runStats->min); pRecord->Set_Value(catLevel+3+iGrid*5, runStats->max); pRecord->Set_Value(catLevel+4+iGrid*5, runStats->sum/runSub->count); pRecord->Set_Value(catLevel+5+iGrid*5, sqrt((runStats->dev - runSub->count*pow(runStats->sum/runSub->count, 2)) / (runSub->count - 1))); // sample //pRecord->Set_Value(catLevel+5+iGrid*5, sqrt((runStats->dev - pow(runStats->sum/runSub->count, 2)) / runSub->count)); // population pRecord->Set_Value(catLevel+6+iGrid*5, runStats->sum); } if( pAspect != NULL ) { iGrid = nStatGrids * 5; if( runSub->cat == pAspect->Get_NoData_Value() ) { for( int i=2; i<5; i++ ) pRecord->Set_Value(catLevel+i+iGrid, 0.0); } else { double min, max, sumYcomp, sumXcomp, val, valYcomp, valXcomp; if( nStatGrids == 0 ) runStats = runSub->stats; else runStats = runStats->next; min = runStats->min; max = runStats->max; sumXcomp = runStats->sum; runStats = runStats->next; sumYcomp = runStats->sum; pRecord ->Set_Value(catLevel+2+iGrid, min*M_RAD_TO_DEG); pRecord ->Set_Value(catLevel+3+iGrid, max*M_RAD_TO_DEG); valXcomp = sumXcomp / runSub->count; valYcomp = sumYcomp / runSub->count; val = valXcomp ? fmod(M_PI_270 + atan2(valYcomp, valXcomp), M_PI_360) : (valYcomp > 0 ? M_PI_270 : (valYcomp < 0 ? M_PI_090 : -1)); val = fmod(M_PI_360 - val, M_PI_360); pRecord ->Set_Value(catLevel+4+iGrid, val*M_RAD_TO_DEG); } } while( runSub != NULL ) // read/write categories { pRecord->Set_Value(catLevel, runSub->cat); runSub = runSub->parent; catLevel -= 1; } subList = subList->next; } while( runList->parent != NULL && runList->parent->next == NULL ) // move up to next 'Caterory with -> next' runList = runList->parent; if( runList->parent != NULL ) // if not upper layer (zones) { runList = runList->parent; // move to parent of next 'Caterory with -> next' if( runList->next != NULL && runList->parent != NULL ) runList->parent->sub = runList->next; // redirect pointer to category which is next 'Categora with -> next' next else if (runList->parent == NULL && runList->next != NULL ) startList = runList->next; // when upper layer (zones) is reached, move to next zone else startList = NULL; // reading finished if( runList->parent == NULL ) startList = runList->next; // ?? when upper layer is reached, move to next zone else runList->sub = runList->sub->next; // on sub layers redirect pointer to ->next } else { if( nCatGrids == 0 ) startList = NULL; else startList = runList->next; // ?? upper layer is reached, move to next zone } runList->next = NULL; delete (runList); // delete disconneted part of the list } if( NDcountStat > 0 ) { Message_Add(CSG_String::Format(SG_T("\n\n\n%s: %d %s\n\n\n"), _TL("WARNING"), NDcountStat, _TL("NoData value(s) in statistic grid(s)!"))); } return (true); }
//--------------------------------------------------------- bool CSG_ODBC_Connection::_Table_Load(CSG_Table &Table, const CSG_String &Select, const CSG_String &Name, bool bLOB) { //----------------------------------------------------- if( !is_Connected() ) { _Error_Message(_TL("no database connection")); return( false ); } //----------------------------------------------------- try { int valInt, iField, nFields; long valLong; float valFloat; double valDouble; std_string valString; otl_long_string valRaw(m_Connection.get_max_long_size()); otl_column_desc *Fields; otl_stream Stream; CSG_Bytes BLOB; Stream.set_all_column_types (otl_all_date2str); Stream.set_lob_stream_mode (bLOB); Stream.open (bLOB ? 1 : m_Size_Buffer, Select, m_Connection); Fields = Stream.describe_select(nFields); if( Fields == NULL || nFields <= 0 ) { _Error_Message(_TL("no fields in selection")); return( false ); } //------------------------------------------------- Table.Destroy(); Table.Set_Name(Name); for(iField=0; iField<nFields; iField++) { if( _Get_Type_From_SQL(Fields[iField].otl_var_dbtype) == SG_DATATYPE_Undefined ) { return( false ); } Table.Add_Field(Fields[iField].name, _Get_Type_From_SQL(Fields[iField].otl_var_dbtype)); } //------------------------------------------------- while( !Stream.eof() && SG_UI_Process_Get_Okay() ) // while not end-of-data { CSG_Table_Record *pRecord = Table.Add_Record(); for(iField=0; iField<nFields; iField++) { switch( Table.Get_Field_Type(iField) ) { case SG_DATATYPE_String: Stream >> valString; if( Stream.is_null() ) pRecord->Set_NoData(iField); else pRecord->Set_Value(iField, CSG_String(valString.c_str())); break; case SG_DATATYPE_Short: case SG_DATATYPE_Int: Stream >> valInt; if( Stream.is_null() ) pRecord->Set_NoData(iField); else pRecord->Set_Value(iField, valInt); break; case SG_DATATYPE_DWord: case SG_DATATYPE_Long: Stream >> valLong; if( Stream.is_null() ) pRecord->Set_NoData(iField); else pRecord->Set_Value(iField, valLong); break; case SG_DATATYPE_Float: Stream >> valFloat; if( Stream.is_null() ) pRecord->Set_NoData(iField); else pRecord->Set_Value(iField, valFloat); break; case SG_DATATYPE_Double: Stream >> valDouble; if( Stream.is_null() ) pRecord->Set_NoData(iField); else pRecord->Set_Value(iField, valDouble); break; case SG_DATATYPE_Binary: Stream >> valRaw; if( Stream.is_null() ) pRecord->Set_NoData(iField); else { BLOB.Clear(); for(int i=0; i<valRaw.len(); i++) { BLOB.Add((BYTE)valRaw[i]); } pRecord->Set_Value(iField, BLOB); } break; } } } } //----------------------------------------------------- catch( otl_exception &e ) { _Error_Message(e); return( false ); } return( true ); }
//--------------------------------------------------------- bool CTable_Trend_Base::On_Execute(void) { int i, j, xField, yField; CSG_String Name; CSG_Table_Record *pRecord; CSG_Table *pTable; pTable = Parameters("TABLE") ->asTable(); xField = Parameters("FIELD_X") ->asInt(); yField = Parameters("FIELD_Y") ->asInt(); //----------------------------------------------------- if( m_Trend.Set_Formula(Parameters("FORMULA")->asString()) ) { m_Trend.Clr_Data(); for(i=0; i<pTable->Get_Record_Count(); i++) { pRecord = pTable->Get_Record(i); m_Trend.Add_Data(pRecord->asDouble(xField), pRecord->asDouble(yField)); } //------------------------------------------------- if( m_Trend.Get_Trend() ) { Message_Fmt("\n%s\n%s: %f", m_Trend.Get_Formula().c_str(), SG_T("r\xb2"), 100.0 * m_Trend.Get_R2()); if( Parameters("TREND")->asTable() == NULL ) { pTable->Add_Field("TREND" , SG_DATATYPE_Double); for(i=0, j=pTable->Get_Field_Count()-1; i<m_Trend.Get_Data_Count(); i++) { pRecord = pTable->Get_Record(i); pRecord->Set_Value(j, m_Trend.Get_Value(m_Trend.Get_Data_X(i))); } } else { Name.Printf("%s [%s]", pTable->Get_Name(), _TL("Trend")); pTable = Parameters("TREND")->asTable(); pTable->Destroy(); pTable->Set_Name(Name); pTable->Add_Field("X" , SG_DATATYPE_Double); pTable->Add_Field("Y" , SG_DATATYPE_Double); pTable->Add_Field("Y_TREND", SG_DATATYPE_Double); for(i=0; i<m_Trend.Get_Data_Count(); i++) { pRecord = pTable->Add_Record(); pRecord->Set_Value(0, m_Trend.Get_Data_X(i)); pRecord->Set_Value(1, m_Trend.Get_Data_Y(i)); pRecord->Set_Value(2, m_Trend.Get_Value(m_Trend.Get_Data_X(i))); } } return( true ); } } return( false ); }
//--------------------------------------------------------- bool CSurfer_BLN_Import::On_Execute(void) { bool bOk; int ID, Flag, iPoint, nPoints; double x, y; FILE *Stream; TSG_Shape_Type Type; CSG_String FileName, sLine, sName, sDesc, sTemp; CSG_Table_Record *pRecord; CSG_Table *pTable; CSG_Shape *pShape; CSG_Shapes *pShapes; //----------------------------------------------------- pShapes = Parameters("SHAPES") ->asShapes(); pTable = Parameters("TABLE") ->asTable(); FileName = Parameters("FILE") ->asString(); switch( Parameters("TYPE")->asInt() ) { case 0: Type = SHAPE_TYPE_Point; break; case 1: default: Type = SHAPE_TYPE_Line; break; case 2: Type = SHAPE_TYPE_Polygon; break; } //----------------------------------------------------- if( (Stream = fopen(FileName.b_str(), "r")) != NULL ) { bOk = true; ID = 0; if( pShapes->Get_Type() != SHAPE_TYPE_Undefined && pShapes->Get_Type() != Type ) { pShapes = SG_Create_Shapes(Type, SG_File_Get_Name(FileName, false)); Parameters("SHAPES")->Set_Value(pShapes); DataObject_Add(pShapes); } else { pShapes->Create(Type, SG_File_Get_Name(FileName, false)); } if( Type == SHAPE_TYPE_Point ) { if( pTable == NULL ) { pTable = SG_Create_Table(); Parameters("TABLE")->Set_Value(pTable); } else { pTable->Destroy(); } pTable ->Add_Field("ID" , SG_DATATYPE_Int); pTable ->Add_Field("FLAG" , SG_DATATYPE_Int); pTable ->Add_Field("NAME" , SG_DATATYPE_String); pTable ->Add_Field("DESC" , SG_DATATYPE_String); pShapes->Add_Field("ID" , SG_DATATYPE_Int); pShapes->Add_Field("ID_LUT" , SG_DATATYPE_Int); pShapes->Add_Field("Z" , SG_DATATYPE_Double); } else { pShapes->Add_Field("ID" , SG_DATATYPE_Int); pShapes->Add_Field("FLAG" , SG_DATATYPE_Int); pShapes->Add_Field("NAME" , SG_DATATYPE_String); pShapes->Add_Field("DESC" , SG_DATATYPE_String); } //------------------------------------------------- while( bOk && SG_Read_Line(Stream, sLine) && sLine.BeforeFirst(',').asInt(nPoints) && nPoints > 0 && Process_Get_Okay(true) ) { Process_Set_Text(CSG_String::Format(SG_T("%d. %s"), ++ID, _TL("shape in process"))); sTemp = sLine.AfterFirst (','); sLine = sTemp; Flag = sLine.BeforeFirst(',').asInt(); sTemp = sLine.AfterFirst (','); sLine = sTemp; sTemp = sLine.BeforeFirst(','); sName = sTemp.AfterFirst('\"').BeforeLast('\"'); sTemp = sLine.AfterFirst (','); sLine = sTemp; sTemp = sLine.BeforeFirst(','); sDesc = sTemp.AfterFirst('\"').BeforeLast('\"'); if( Type == SHAPE_TYPE_Point ) { pRecord = pTable->Add_Record(); pRecord->Set_Value(0, ID); pRecord->Set_Value(1, Flag); pRecord->Set_Value(2, sName); pRecord->Set_Value(3, sDesc); for(iPoint=0; iPoint<nPoints && bOk; iPoint++) { if( (bOk = SG_Read_Line(Stream, sLine)) == true ) { pShape = pShapes->Add_Shape(); pShape->Set_Value(0, iPoint + 1); pShape->Set_Value(1, ID); pShape->Set_Value(2, sLine.AfterLast (',').asDouble()); x = sLine.BeforeFirst(',').asDouble(); y = sLine.AfterFirst (',').asDouble(); pShape->Add_Point(x, y); } } } else { pShape = pShapes->Add_Shape(); pShape->Set_Value(0, ID); pShape->Set_Value(1, Flag); pShape->Set_Value(2, sName); pShape->Set_Value(3, sDesc); for(iPoint=0; iPoint<nPoints && bOk; iPoint++) { if( (bOk = SG_Read_Line(Stream, sLine)) == true ) { x = sLine.BeforeFirst(',').asDouble(); y = sLine.AfterFirst (',').asDouble(); pShape->Add_Point(x, y); } } } } fclose(Stream); } //----------------------------------------------------- if( pShapes->is_Valid() && pShapes->Get_Count() > 0 ) { return( true ); } return( false ); }
//--------------------------------------------------------- bool CGSGrid_Statistics_To_Table::On_Execute(void) { //----------------------------------------------------- CSG_Parameter_Grid_List *pGrids = Parameters("GRIDS")->asGridList(); if( pGrids->Get_Count() < 1 ) { Error_Set(_TL("no grids in selection")); return( false ); } //----------------------------------------------------- CSG_Table *pTable = Parameters("STATS")->asTable(); pTable->Destroy(); pTable->Set_Name(_TL("Statistics for Grids")); pTable->Add_Field(_TL("NAME"), SG_DATATYPE_String); if( Parameters("DATA_CELLS" )->asBool() ) pTable->Add_Field(_TL("DATA_CELLS" ), SG_DATATYPE_Int); if( Parameters("NODATA_CELLS")->asBool() ) pTable->Add_Field(_TL("NODATA_CELLS"), SG_DATATYPE_Int); if( Parameters("CELLSIZE" )->asBool() ) pTable->Add_Field(_TL("CELLSIZE" ), SG_DATATYPE_Double); if( Parameters("MEAN" )->asBool() ) pTable->Add_Field(_TL("MEAN" ), SG_DATATYPE_Double); if( Parameters("MIN" )->asBool() ) pTable->Add_Field(_TL("MIN" ), SG_DATATYPE_Double); if( Parameters("MAX" )->asBool() ) pTable->Add_Field(_TL("MAX" ), SG_DATATYPE_Double); if( Parameters("RANGE" )->asBool() ) pTable->Add_Field(_TL("RANGE" ), SG_DATATYPE_Double); if( Parameters("VAR" )->asBool() ) pTable->Add_Field(_TL("VAR" ), SG_DATATYPE_Double); if( Parameters("STDDEV" )->asBool() ) pTable->Add_Field(_TL("STDDEV" ), SG_DATATYPE_Double); if( Parameters("STDDEVLO" )->asBool() ) pTable->Add_Field(_TL("STDDEVLO" ), SG_DATATYPE_Double); if( Parameters("STDDEVHI" )->asBool() ) pTable->Add_Field(_TL("STDDEVHI" ), SG_DATATYPE_Double); if( Parameters("PCTL" )->asBool() ) pTable->Add_Field(_TL("PCTL" ), SG_DATATYPE_Double); if( pTable->Get_Field_Count() <= 1 ) { Error_Set(_TL("no parameter output specified")); return( false ); } double dRank = Parameters("PCTL")->asBool() ? Parameters("PCTL_VAL")->asDouble() : -1.0; //----------------------------------------------------- for(int i=0; i<pGrids->Get_Count() && Process_Get_Okay(); i++) { CSG_Grid *pGrid = pGrids->asGrid(i); CSG_Table_Record *pRecord = pTable->Add_Record(); pRecord->Set_Value("NAME" , pGrid->Get_Name()); pRecord->Set_Value("DATA_CELLS" , pGrid->Get_NCells() - pGrid->Get_NoData_Count()); pRecord->Set_Value("NODATA_CELLS", pGrid->Get_NoData_Count()); pRecord->Set_Value("CELLSIZE" , pGrid->Get_Cellsize()); pRecord->Set_Value("MEAN" , pGrid->Get_ArithMean()); pRecord->Set_Value("MIN" , pGrid->Get_ZMin()); pRecord->Set_Value("MAX" , pGrid->Get_ZMax()); pRecord->Set_Value("RANGE" , pGrid->Get_ZRange()); pRecord->Set_Value("VAR" , pGrid->Get_Variance()); pRecord->Set_Value("STDDEV" , pGrid->Get_StdDev()); pRecord->Set_Value("STDDEVLO" , pGrid->Get_ArithMean() - pGrid->Get_StdDev()); pRecord->Set_Value("STDDEVHI" , pGrid->Get_ArithMean() + pGrid->Get_StdDev()); if( dRank > 0.0 && dRank < 100.0 ) { pRecord->Set_Value("PCTL", pGrid->Get_Percentile(dRank)); // this is a time consuming operation } } if( dRank > 0.0 && dRank < 100.0 ) { pTable->Set_Field_Name(pTable->Get_Field_Count() - 1, CSG_String::Format(SG_T("%s%02d"), _TL("PCTL"), (int)dRank)); } return( true ); }
//--------------------------------------------------------- bool CTOPMODEL::On_Execute(void) { bool bInfiltration; int iClass, nClasses, iTime, nTimeSteps, n, k; double Precipitation, Evaporation, Infiltration, Infiltration_Excess; CSG_Grid *pAtanB, *pMoist, gClass; CSG_Table_Record *pRecord; CSG_Table *pTable; //----------------------------------------------------- // Get user inputs from the 'Parameters' object... pAtanB = Parameters("ATANB") ->asGrid(); pClimate = Parameters("CLIMATE") ->asTable(); dTime = Parameters("DTIME") ->asDouble(); nClasses = Parameters("NCLASSES") ->asInt(); bInfiltration = Parameters("BINF") ->asBool(); nTimeSteps = pClimate->Get_Record_Count(); if( (pMoist = Parameters("MOIST")->asGrid()) != NULL ) { pMoist->Set_Name(_TL("Soil Moisture Deficit")); DataObject_Set_Colors(pMoist, 100, SG_COLORS_RED_GREY_BLUE, true); } //----------------------------------------------------- pTable = Parameters("TABLE")->asTable(); pTable->Destroy(); pTable->Set_Name(_TL("TOPMODEL - Simulation Output")); pTable->Add_Field(_TL("Total flow (in watershed) [m\xc2\xb3/dt]") , SG_DATATYPE_Double); pTable->Add_Field(_TL("Total flow [m/dt]") , SG_DATATYPE_Double); pTable->Add_Field(_TL("Saturation overland flow [m/dt]") , SG_DATATYPE_Double); pTable->Add_Field(_TL("Subsurface flow [m/dt]") , SG_DATATYPE_Double); pTable->Add_Field(_TL("Vertical (drainage) flux [m/dt]") , SG_DATATYPE_Double); pTable->Add_Field(_TL("Mean saturation deficit (in watershed) [m]") , SG_DATATYPE_Double); pTable->Add_Field(_TL("Infiltration rate [m/dt]") , SG_DATATYPE_Double); pTable->Add_Field(_TL("Infiltration excess runoff [m/dt]") , SG_DATATYPE_Double); //----------------------------------------------------- Vals.Create(dTime, nTimeSteps, &Parameters, pAtanB, nClasses, &gClass); //----------------------------------------------------- inf_bPonding = false; inf_cumf = 0.0; for(iTime=0; iTime<nTimeSteps && Set_Progress(iTime, nTimeSteps); iTime++) { Get_Climate(iTime, Precipitation, Evaporation); if( bInfiltration && Precipitation > 0.0 ) { Infiltration = dTime * Get_Infiltration((iTime + 1) * dTime, Precipitation / dTime); Infiltration_Excess = Precipitation - Infiltration; Precipitation = Infiltration; } else { Infiltration = 0.0; Infiltration_Excess = 0.0; } Run(Evaporation, Precipitation, Infiltration_Excess); for(iClass=0; iClass<Vals.nreach_; iClass++) { k = iTime + iClass + Vals.ndelay_; if( k > nTimeSteps - 1 ) break; Vals.Qt_[k] += Vals.qt_Total * Vals.Add[iClass]; } if( pMoist ) { for(n=0; n<gClass.Get_NCells(); n++) { iClass = gClass.asInt(n); if( iClass >= 0 && iClass < nClasses ) { pMoist->Set_Value(n, Vals.Get_Class(iClass)->S_); } else { pMoist->Set_NoData(n); } } // DataObject_Update(pMoist); DataObject_Update(pMoist, 0, 0.35, true); } pRecord = pTable->Add_Record(); pRecord->Set_Value(0, Vals.Qt_[iTime]); // QT pRecord->Set_Value(1, Vals.qt_Total); // qt pRecord->Set_Value(2, Vals.qo_Total); // q0 pRecord->Set_Value(3, Vals.qs_Total); // qs pRecord->Set_Value(4, Vals.qv_Total); // qv pRecord->Set_Value(5, Vals.Sbar_); // SBar pRecord->Set_Value(6, Infiltration); // Infiltration pRecord->Set_Value(7, Infiltration_Excess); // Infiltration Excess DataObject_Update(pTable); } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CGDAL_Formats::On_Execute(void) { CSG_Table *pFormats = Parameters("FORMATS")->asTable(); pFormats->Destroy(); pFormats->Set_Name(_TL("GDAL Formats")); pFormats->Add_Field("ID" , SG_DATATYPE_String); pFormats->Add_Field("NAME" , SG_DATATYPE_String); pFormats->Add_Field("FILTER", SG_DATATYPE_String); pFormats->Add_Field("TYPE" , SG_DATATYPE_String); pFormats->Add_Field("ACCESS", SG_DATATYPE_String); //----------------------------------------------------- int Type = Parameters("TYPE" )->asInt(); int Access = Parameters("ACCESS")->asInt(); //----------------------------------------------------- if( Type != 1 ) // not vectors only { for(int i=0; i<SG_Get_GDAL_Drivers().Get_Count(); i++) { if( SG_Get_GDAL_Drivers().is_Raster(i) ) { CSG_String R(SG_Get_GDAL_Drivers().Can_Read (i) ? "R" : ""); CSG_String W(SG_Get_GDAL_Drivers().Can_Write(i) ? "W" : ""); if( (Access != 0 || !R.is_Empty()) && (Access != 1 || !W.is_Empty()) ) { CSG_Table_Record *pFormat = pFormats->Add_Record(); pFormat->Set_Value(GDAL_LIST_FMT_ID , SG_Get_GDAL_Drivers().Get_Description(i)); pFormat->Set_Value(GDAL_LIST_FMT_NAME , SG_Get_GDAL_Drivers().Get_Name (i)); pFormat->Set_Value(GDAL_LIST_FMT_FILTER, SG_Get_GDAL_Drivers().Get_Extension (i)); pFormat->Set_Value(GDAL_LIST_FMT_TYPE , "RASTER"); pFormat->Set_Value(GDAL_LIST_FMT_ACCESS, R + W); } } } } //----------------------------------------------------- if( Type != 0 ) // not rasters only { for(int i=0; i<SG_Get_OGR_Drivers().Get_Count(); i++) { if( SG_Get_OGR_Drivers().is_Vector(i) ) { CSG_String R(SG_Get_OGR_Drivers().Can_Read (i) ? "R" : ""); CSG_String W(SG_Get_OGR_Drivers().Can_Write(i) ? "W" : ""); if( (Access != 0 || !R.is_Empty()) && (Access != 1 || !W.is_Empty()) ) { CSG_Table_Record *pFormat = pFormats->Add_Record(); pFormat->Set_Value(GDAL_LIST_FMT_ID , SG_Get_OGR_Drivers().Get_Description(i)); pFormat->Set_Value(GDAL_LIST_FMT_NAME , SG_Get_OGR_Drivers().Get_Name (i)); pFormat->Set_Value(GDAL_LIST_FMT_FILTER, SG_Get_OGR_Drivers().Get_Extension (i)); pFormat->Set_Value(GDAL_LIST_FMT_TYPE , "VECTOR"); pFormat->Set_Value(GDAL_LIST_FMT_ACCESS, R + W); } } } } //----------------------------------------------------- if( Parameters("RECOGNIZED")->asBool() ) { CSG_String Filter_All; for(int i=0; i<pFormats->Get_Count(); i++) { CSG_String Filter = pFormats->Get_Record(i)->asString(GDAL_LIST_FMT_FILTER); if( !Filter.is_Empty() ) { Filter.Replace("/", ";"); Filter_All += (Filter_All.is_Empty() ? "*." : ";*.") + Filter; } } if( !Filter_All.is_Empty() ) { CSG_Table_Record *pFormat = pFormats->Add_Record(); pFormat->Set_Value(GDAL_LIST_FMT_NAME , _TL("All Recognized Files")); pFormat->Set_Value(GDAL_LIST_FMT_FILTER, Filter_All); pFormat->Set_Value(GDAL_LIST_FMT_TYPE , Type == 0 ? "RASTER" : Type == 1 ? "VECTOR" : "RASTER/VECTOR"); pFormat->Set_Value(GDAL_LIST_FMT_ACCESS, Access == 0 ? "R" : Access == 1 ? "W" : "RW" ); } } //----------------------------------------------------- return( pFormats->Get_Count() > 0 ); }
//--------------------------------------------------------- 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 CTable_Text_Import_Numbers::On_Execute(void) { CSG_String sHead, sLine, Separator; CSG_File Stream; //----------------------------------------------------- if( !Stream.Open(Parameters("FILENAME")->asString(), SG_FILE_R, false) ) { Error_Set(_TL("file could not be opened")); return( false ); } if( Parameters("SKIP")->asInt() > 0 ) { int i = Parameters("SKIP")->asInt(); while( i > 0 && Stream.Read_Line(sLine) ) { i--; } } if( !Stream.Read_Line(sHead) || sHead.Length() == 0 ) { Error_Set(_TL("empty or corrupted file")); return( false ); } if( !Parameters("HEADLINE")->asBool() ) { sLine = sHead; } else if( !Stream.Read_Line(sLine) || sLine.Length() == 0 ) { Error_Set(_TL("empty or corrupted file")); return( false ); } //----------------------------------------------------- switch( Parameters("SEPARATOR")->asInt() ) { case 0: Separator = "\t"; break; case 1: Separator = ";"; break; case 2: Separator = ","; break; case 3: Separator = " "; break; default: Separator = Parameters("SEP_OTHER")->asString(); break; } //----------------------------------------------------- CSG_Table *pTable = Parameters("TABLE")->asTable(); pTable->Destroy(); pTable->Set_Name(SG_File_Get_Name(Parameters("FILENAME")->asString(), false)); sHead.Trim(true); sHead.Replace(Separator, "\t"); while( sHead.Length() > 0 ) { sHead.Trim(); if( Parameters("HEADLINE")->asBool() ) { pTable->Add_Field(sHead.BeforeFirst('\t'), SG_DATATYPE_Double); } else { pTable->Add_Field(CSG_String::Format("FIELD%02d", 1 + pTable->Get_Field_Count()), SG_DATATYPE_Double); } sHead = sHead.AfterFirst('\t'); } if( pTable->Get_Field_Count() <= 0 ) { Error_Set(_TL("empty or corrupted file")); return( false ); } //----------------------------------------------------- int fLength = Stream.Length(); bool bOkay = true; do { sLine.Replace(Separator, "\t"); CSG_Table_Record *pRecord = pTable->Add_Record(); for(int i=0; bOkay && i<pTable->Get_Field_Count(); i++) { double Value; sLine.Trim(); if( (bOkay = sLine.asDouble(Value)) == true ) { pRecord->Set_Value(i, Value); sLine = sLine.AfterFirst('\t'); } else { pTable->Del_Record(pTable->Get_Count() - 1); } } } while( bOkay && Stream.Read_Line(sLine) && Set_Progress(Stream.Tell(), fLength) ); return( pTable->Get_Count() > 0 ); }
//--------------------------------------------------------- bool CTable_Text_Import_Fixed_Cols::On_Execute(void) { bool bHeader; int i, nChars, iField, nFields, *iFirst, *iLength; CSG_String sLine; CSG_File Stream; CSG_Table *pTable; //----------------------------------------------------- pTable = Parameters("TABLE") ->asTable(); bHeader = Parameters("HEADLINE") ->asBool(); //----------------------------------------------------- if( !Stream.Open(Parameters("FILENAME")->asString(), SG_FILE_R, true) ) { Message_Add(_TL("file could not be opened")); return( false ); } if( !Stream.Read_Line(sLine) || (nChars = (int)sLine.Length()) <= 0 ) { Message_Add(_TL("empty or corrupted file")); return( false ); } //----------------------------------------------------- pTable->Destroy(); pTable->Set_Name(SG_File_Get_Name(Parameters("FILENAME")->asString(), false)); switch( Parameters("FIELDDEF")->asInt() ) { //----------------------------------------------------- case 0: { CSG_Parameters *pBreaks = Get_Parameters("BREAKS"); pBreaks->Del_Parameters(); for(i=0; i<nChars; i++) { pBreaks->Add_Value(NULL, CSG_String::Format(SG_T("%03d"), i), CSG_String::Format(SG_T("%03d %c"), i + 1, sLine[i]), _TL(""), PARAMETER_TYPE_Bool, false ); } if( !Dlg_Parameters("BREAKS") ) { return( false ); } //------------------------------------------------- for(i=0, nFields=1; i<pBreaks->Get_Count(); i++) { if( pBreaks->Get_Parameter(i)->asBool() ) { nFields++; } } //------------------------------------------------- iFirst = new int[nFields]; iLength = new int[nFields]; iFirst[0] = 0; for(i=0, iField=1; i<pBreaks->Get_Count() && iField<nFields; i++) { if( pBreaks->Get_Parameter(i)->asBool() ) { iFirst[iField++] = i + 1; } } //------------------------------------------------- for(iField=0; iField<nFields; iField++) { iLength[iField] = (iField < nFields - 1 ? iFirst[iField + 1] : (int)sLine.Length()) - iFirst[iField]; pTable->Add_Field(bHeader ? sLine.Mid(iFirst[iField], iLength[iField]) : CSG_String::Format(SG_T("FIELD%03d"), iField + 1), SG_DATATYPE_String); } } break; //----------------------------------------------------- case 1: { CSG_Parameters *pFields = Get_Parameters("FIELDS"); pFields->Del_Parameters(); nFields = Parameters("NFIELDS")->asInt(); for(iField=0; iField<nFields; iField++) { CSG_String s = CSG_String::Format(SG_T("%03d"), iField); CSG_Parameter *pNode = pFields->Add_Node(NULL, SG_T("NODE") + s, _TL("Field") + s, _TL("")); pFields->Add_Value (pNode, SG_T("LENGTH") + s, _TL("Length"), _TL(""), PARAMETER_TYPE_Int, 1, 1, true); // pFields->Add_Value (pNode, SG_T("IMPORT") + s, _TL("Import"), _TL(""), PARAMETER_TYPE_Bool, true); pFields->Add_Choice (pNode, SG_T("TYPE") + s, _TL("Type") , _TL(""), CSG_String::Format(SG_T("%s|%s|%s|%s|%s|"), _TL("text"), _TL("2 byte integer"), _TL("4 byte integer"), _TL("4 byte float"), _TL("8 byte float")) ); } if( !Dlg_Parameters("FIELDS") ) { return( false ); } //------------------------------------------------- iFirst = new int[nFields]; iLength = new int[nFields]; iFirst[0] = 0; for(iField=0, i=0; iField<nFields && i<nChars; iField++) { CSG_String s = CSG_String::Format(SG_T("%03d"), iField); iFirst [iField] = i; iLength[iField] = pFields->Get_Parameter(SG_T("LENGTH") + s)->asInt(); i += iLength[iField]; CSG_String Name = bHeader ? sLine.Mid(iFirst[iField], iLength[iField]) : CSG_String::Format(SG_T("FIELD%03d"), iField + 1); switch( pFields->Get_Parameter(SG_T("TYPE") + s)->asInt() ) { default: case 0: pTable->Add_Field(Name, SG_DATATYPE_String); break; case 1: pTable->Add_Field(Name, SG_DATATYPE_Short); break; case 2: pTable->Add_Field(Name, SG_DATATYPE_Int); break; case 3: pTable->Add_Field(Name, SG_DATATYPE_Float); break; case 4: pTable->Add_Field(Name, SG_DATATYPE_Double); break; } } } break; //----------------------------------------------------- case 2: { CSG_Table *pList = Parameters("LIST")->asTable(); nFields = pList->Get_Count(); //------------------------------------------------- iFirst = new int[nFields]; iLength = new int[nFields]; iFirst[0] = 0; for(iField=0, i=0; iField<nFields && i<nChars; iField++) { iFirst [iField] = i; iLength[iField] = pList->Get_Record(iField)->asInt(1); i += iLength[iField]; CSG_String Name = bHeader ? sLine.Mid(iFirst[iField], iLength[iField]) : CSG_String(pList->Get_Record(iField)->asString(0)); switch( pList->Get_Record(iField)->asInt(2) ) { case 0: pTable->Add_Field(Name, SG_DATATYPE_String); break; case 1: pTable->Add_Field(Name, SG_DATATYPE_Short); break; case 2: pTable->Add_Field(Name, SG_DATATYPE_Int); break; case 3: pTable->Add_Field(Name, SG_DATATYPE_Float); break; default: case 4: pTable->Add_Field(Name, SG_DATATYPE_Double); break; } } } break; } //----------------------------------------------------- if( bHeader ) { Stream.Read_Line(sLine); } //----------------------------------------------------- int fLength = Stream.Length(); do { if( sLine.Length() == nChars ) { CSG_Table_Record *pRecord = pTable->Add_Record(); for(iField=0; iField<nFields; iField++) { pRecord->Set_Value(iField, sLine.Mid(iFirst[iField], iLength[iField])); } } } while( Stream.Read_Line(sLine) && Set_Progress(Stream.Tell(), fLength) ); //----------------------------------------------------- delete[](iFirst); delete[](iLength); //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CGrid_Aspect_Slope_Map::On_Execute(void) { CSG_Grid *pAspect, *pSlope, *pAspectSlope; CSG_Table *pLUT; int iAspectCount = 9; static const double AspectBreaks[] = {0.000, 0.393, 1.178, 1.963, 2.749, 3.534, 4.320, 5.105, 5.890, 6.283}; static const int AspectClass[] = {1, 2, 3, 4, 5, 6, 7, 8, 1}; int iSlopeCount = 4; static const double SlopeBreaks[] = {0.000, 0.087, 0.349, 0.698, 1.571}; static const int SlopeClass[] = {10, 20, 30, 40}; pAspect = Parameters("ASPECT")->asGrid(); pSlope = Parameters("SLOPE")->asGrid(); pAspectSlope = Parameters("ASPECT_SLOPE")->asGrid(); pLUT = Parameters("LUT")->asTable(); //----------------------------------------------------- if( pLUT == NULL ) pLUT = new CSG_Table(); else pLUT->Destroy(); pLUT->Set_Name(SG_T("LUT_Aspect-Slope")); pLUT->Add_Field(SG_T("COLOR"), SG_DATATYPE_Int); pLUT->Add_Field(SG_T("NAME"), SG_DATATYPE_String); pLUT->Add_Field(SG_T("DESCRIPTION"), SG_DATATYPE_String); pLUT->Add_Field(SG_T("MINIMUM"), SG_DATATYPE_Int); pLUT->Add_Field(SG_T("MAXIMUM"), SG_DATATYPE_Int); for(int i=0; i<25; i++) { CSG_Table_Record *pRecord = pLUT->Add_Record(); pRecord->Set_Value(0, LUT_COLOR[i]); pRecord->Set_Value(1, LUT_NAME[i]); pRecord->Set_Value(2, SG_T("")); pRecord->Set_Value(3, LUT_BREAK[i]); pRecord->Set_Value(4, LUT_BREAK[i+1]); } //----------------------------------------------------- #pragma omp parallel for for(sLong n=0; n<Get_NCells(); n++) { int iAspectClass, iSlopeClass; if( pAspect->is_NoData(n) || pSlope->is_NoData(n) ) { pAspectSlope->Set_NoData(n); } else { iAspectClass = Get_Class(pAspect->asDouble(n), iAspectCount, AspectBreaks, AspectClass); iSlopeClass = Get_Class(pSlope->asDouble(n), iSlopeCount, SlopeBreaks, SlopeClass); pAspectSlope->Set_Value(n, iAspectClass + iSlopeClass); } } //----------------------------------------------------- CSG_Parameters Parms; if( DataObject_Get_Parameters(pAspectSlope, Parms) && Parms("COLORS_TYPE") && Parms("LUT") ) { Parms("LUT")->asTable()->Assign(pLUT); Parms("COLORS_TYPE")->Set_Value(1); DataObject_Set_Parameters(pAspectSlope, Parms); } if( Parameters("LUT")->asTable() == NULL ) { delete pLUT; } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CHugget_02::On_Execute(void) { int iStep, nSteps; double sTime, dTime, PrimProd, cHumify, cCarbon, C_Leav, C_Bran, C_Stem, C_Root, C_Litt, C_Humu, C_Coal, P_Leav, P_Bran, P_Stem, P_Root, K_Leav_Litt, K_Bran_Litt, K_Stem_Litt, K_Root_Humu, K_Litt_Humu, K_Humu_Coal, K_Coal_Envi, d_Leav_Litt, d_Bran_Litt, d_Stem_Litt, d_Root_Humu, d_Litt_Humu, d_Humu_Coal, d_Coal_Envi; CSG_Table_Record *pRecord; CSG_Table *pTable; //----------------------------------------------------- sTime = Parameters("TIME_SPAN") ->asDouble(); dTime = Parameters("TIME_STEP") ->asDouble(); nSteps = (int)(sTime / dTime); PrimProd = Parameters("PRIMPROD") ->asDouble(); cHumify = Parameters("CHUMIFY") ->asDouble(); cCarbon = Parameters("CCARBON") ->asDouble(); P_Leav = Parameters("P_LEAV") ->asDouble(); P_Bran = Parameters("P_BRAN") ->asDouble(); P_Stem = Parameters("P_STEM") ->asDouble(); P_Root = Parameters("P_ROOT") ->asDouble(); K_Leav_Litt = Parameters("K_LEAV_LITT") ->asDouble(); K_Bran_Litt = Parameters("K_BRAN_LITT") ->asDouble(); K_Stem_Litt = Parameters("K_STEM_LITT") ->asDouble(); K_Root_Humu = Parameters("K_ROOT_HUMU") ->asDouble(); K_Litt_Humu = Parameters("K_LITT_HUMU") ->asDouble(); K_Humu_Coal = Parameters("K_HUMU_COAL") ->asDouble(); K_Coal_Envi = Parameters("K_COAL_ENVI") ->asDouble(); pTable = Parameters("TABLE") ->asTable(); pTable->Destroy(); pTable->Set_Name(_TL("Carbon Cycle Simulation")); pTable->Add_Field("STEP" , SG_DATATYPE_Int); pTable->Add_Field("TIME" , SG_DATATYPE_Double); pTable->Add_Field("LEAVES" , SG_DATATYPE_Double); pTable->Add_Field("BRANCHES", SG_DATATYPE_Double); pTable->Add_Field("STEMS" , SG_DATATYPE_Double); pTable->Add_Field("ROOTS" , SG_DATATYPE_Double); pTable->Add_Field("LITTER" , SG_DATATYPE_Double); pTable->Add_Field("HUMUS" , SG_DATATYPE_Double); pTable->Add_Field("CHARCOAL", SG_DATATYPE_Double); //----------------------------------------------------- C_Leav = 0.0; C_Bran = 0.0; C_Stem = 0.0; C_Root = 0.0; C_Litt = 0.0; C_Humu = 0.0; C_Coal = 0.0; //----------------------------------------------------- for(iStep=0; iStep<nSteps && Set_Progress(iStep, nSteps); iStep++) { WRITE_RECORD; d_Leav_Litt = K_Leav_Litt * C_Leav; d_Bran_Litt = K_Bran_Litt * C_Bran; d_Stem_Litt = K_Stem_Litt * C_Stem; d_Root_Humu = K_Root_Humu * C_Root; d_Litt_Humu = K_Litt_Humu * C_Litt; d_Humu_Coal = K_Humu_Coal * C_Humu; d_Coal_Envi = K_Coal_Envi * C_Coal; C_Leav = C_Leav + dTime * (-d_Leav_Litt + P_Leav * PrimProd); C_Bran = C_Bran + dTime * (-d_Bran_Litt + P_Bran * PrimProd); C_Stem = C_Stem + dTime * (-d_Stem_Litt + P_Stem * PrimProd); C_Root = C_Root + dTime * (-d_Root_Humu + P_Root * PrimProd); C_Litt = C_Litt + dTime * (-d_Litt_Humu + d_Leav_Litt + d_Bran_Litt + d_Stem_Litt); C_Humu = C_Humu + dTime * (-d_Humu_Coal + cHumify * (d_Root_Humu + d_Litt_Humu)); C_Coal = C_Coal + dTime * (-d_Coal_Envi + cCarbon * (d_Humu_Coal)); } WRITE_RECORD; //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CGSPoints_Semi_Variances::On_Execute(void) { int i, j, k, n, nDistances, nSkip, Attribute; double zi, zj, zMean, v, c, maxDistance, lagDistance; TSG_Point Pt_i, Pt_j; CSG_Vector Count, Variance, Covariance; CSG_Table_Record *pRecord; CSG_Table *pTable; CSG_Shape *pPoint; CSG_Shapes *pPoints; //----------------------------------------------------- pPoints = Parameters("POINTS") ->asShapes(); pTable = Parameters("RESULT") ->asTable(); Attribute = Parameters("FIELD") ->asInt(); nSkip = Parameters("NSKIP") ->asInt(); maxDistance = Parameters("DISTMAX") ->asDouble(); nDistances = Parameters("DISTCOUNT") ->asInt(); if( maxDistance <= 0.0 ) { maxDistance = SG_Get_Length(pPoints->Get_Extent().Get_XRange(), pPoints->Get_Extent().Get_YRange()); } lagDistance = maxDistance / nDistances; zMean = pPoints->Get_Mean(Attribute); Count .Create(nDistances); Variance .Create(nDistances); Covariance .Create(nDistances); //----------------------------------------------------- for(i=0, n=0; i<pPoints->Get_Count() && Set_Progress(n, SG_Get_Square(pPoints->Get_Count()/nSkip)/2); i+=nSkip) { pPoint = pPoints->Get_Shape(i); if( !pPoint->is_NoData(Attribute) ) { Pt_i = pPoint->Get_Point(0); zi = pPoint->asDouble(Attribute); for(j=i+nSkip; j<pPoints->Get_Count(); j+=nSkip, n++) { pPoint = pPoints->Get_Shape(j); if( !pPoint->is_NoData(Attribute) ) { Pt_j = pPoint->Get_Point(0); k = (int)(SG_Get_Distance(Pt_i, Pt_j) / lagDistance); if( k < nDistances ) { zj = pPoint->asDouble(Attribute); v = SG_Get_Square(zi - zj); c = (zi - zMean) * (zj - zMean); Count [k] ++; Variance [k] += v; Covariance[k] += c; } } } } } //----------------------------------------------------- pTable->Destroy(); pTable->Set_Name(CSG_String::Format(SG_T("%s [%s: %s]"), pPoints->Get_Name(), _TL("Variogram"), pPoints->Get_Field_Name(Attribute))); pTable->Add_Field(_TL("Class") , SG_DATATYPE_Int); // FIELD_CLASSNR pTable->Add_Field(_TL("Distance") , SG_DATATYPE_Double); // FIELD_DISTANCE pTable->Add_Field(_TL("Count") , SG_DATATYPE_Int); // FIELD_COUNT pTable->Add_Field(_TL("Variance") , SG_DATATYPE_Double); // FIELD_VARIANCE pTable->Add_Field(_TL("Cum.Var.") , SG_DATATYPE_Double); // FIELD_VARCUMUL pTable->Add_Field(_TL("Covariance") , SG_DATATYPE_Double); // FIELD_COVARIANCE pTable->Add_Field(_TL("Cum.Covar.") , SG_DATATYPE_Double); // FIELD_COVARCUMUL for(i=0, v=0.0, c=0.0, n=0; i<nDistances; i++) { if( Count[i] > 0 ) { n += (int)Count[i]; v += Variance [i]; c += Covariance[i]; pRecord = pTable->Add_Record(); pRecord->Set_Value(FIELD_CLASSNR , (i + 1)); pRecord->Set_Value(FIELD_DISTANCE , (i + 1) * lagDistance); pRecord->Set_Value(FIELD_COUNT , Count[i]); pRecord->Set_Value(FIELD_VARIANCE , 0.5 * Variance [i] / Count[i]); pRecord->Set_Value(FIELD_VARCUMUL , 0.5 * v / n); pRecord->Set_Value(FIELD_COVARIANCE , 1.0 * Covariance[i] / Count[i]); pRecord->Set_Value(FIELD_COVARCUMUL , 1.0 * c / n); } } return( true ); }
//--------------------------------------------------------- bool CDistanceMatrix::On_Execute(void) { //----------------------------------------------------- CSG_Shapes *pPoints = Parameters("POINTS" )->asShapes(); int id_Points = Parameters("ID_POINTS")->asInt (); CSG_Shapes *pNear = Parameters("NEAR" )->asShapes(); int id_Near = Parameters("ID_NEAR" )->asInt (); CSG_Table *pDistances = Parameters("DISTANCES")->asTable (); double max_Dist = Parameters("MAX_DIST" )->asDouble(); //----------------------------------------------------- if( pNear == NULL ) { pNear = pPoints; id_Near = id_Points; } pDistances->Destroy(); if( pPoints != pNear ) { pDistances->Set_Name(CSG_String::Format(SG_T("%s [%s / %s]"), _TL("Distances"), pPoints->Get_Name(), pNear->Get_Name())); } else { pDistances->Set_Name(CSG_String::Format(SG_T("%s [%s]"), _TL("Distances"), pPoints->Get_Name())); } //----------------------------------------------------- if( Parameters("FORMAT")->asInt() == 1 ) { pDistances->Add_Field("ID_POINT", SG_DATATYPE_String); pDistances->Add_Field("ID_NEAR" , SG_DATATYPE_String); pDistances->Add_Field("DISTANCE", SG_DATATYPE_Double); for(int iPoint=0; iPoint<pPoints->Get_Count() && Set_Progress(iPoint, pPoints->Get_Count()); iPoint++) { TSG_Point Point = pPoints->Get_Shape(iPoint)->Get_Point(0); for(int iNear=0; iNear<pNear->Get_Count(); iNear++) { if( pPoints != pNear || iPoint != iNear ) { double Distance = SG_Get_Distance(Point, pNear->Get_Shape(iNear)->Get_Point(0)); if( Distance <= max_Dist || max_Dist <= 0.0 ) { CSG_Table_Record *pRecord = pDistances->Add_Record(); pRecord->Set_Value(0, GET_ID(pPoints, id_Points, iPoint)); pRecord->Set_Value(1, GET_ID(pNear , id_Near , iNear )); pRecord->Set_Value(2, Distance); } } } } } //----------------------------------------------------- else // Matrix { int iNear; pDistances->Add_Field("ID_POINT", SG_DATATYPE_String); for(iNear=0; iNear<pNear->Get_Count(); iNear++) { pDistances->Add_Field(GET_ID(pNear, id_Near, iNear), SG_DATATYPE_Double); } for(int iPoint=0; iPoint<pPoints->Get_Count() && Set_Progress(iPoint, pPoints->Get_Count()); iPoint++) { TSG_Point Point = pPoints->Get_Shape(iPoint)->Get_Point(0); CSG_Table_Record *pRecord = pDistances->Add_Record(); pRecord->Set_Value(0, GET_ID(pPoints, id_Points, iPoint)); for(iNear=0; iNear<pNear->Get_Count(); iNear++) { pRecord->Set_Value(1 + iNear, SG_Get_Distance(Point, pNear->Get_Shape(iNear)->Get_Point(0))); } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CTL_Merge::On_Execute(void) { CSG_String Target = Parameters("TARGET")->asString(); CSG_Strings Files; if( !Parameters("FILES")->asFilePath()->Get_FilePaths(Files) || Files.Get_Count() <= 1 ) { Error_Set(SG_T("no files to merge")); return( false ); } //----------------------------------------------------- int i, j; CSG_Table Merge; if( !Merge.Create(Files[0], TABLE_FILETYPE_Text) ) { Merge.Destroy(); Merge.Add_Field(SG_T("ORIGINAL") , SG_DATATYPE_String); Merge.Add_Field(SG_T("TRANSLATION") , SG_DATATYPE_String); } Merge.Save(Target, TABLE_FILETYPE_Text); //----------------------------------------------------- for(i=1; i<Files.Get_Count() && Process_Get_Okay(); i++) { CSG_Translator Translator(Target, false); CSG_Table Add(Target, TABLE_FILETYPE_Text); if( Merge.Create(Files[i], TABLE_FILETYPE_Text) ) { for(j=0; j<Merge.Get_Count() && Set_Progress(j, Merge.Get_Count()); j++) { if( !Translator.Get_Translation(Merge[j].asString(0), true) ) { Add.Add_Record(Merge.Get_Record(j)); } } if( Add.Get_Count() > Translator.Get_Count() ) { Add.Save(Target, TABLE_FILETYPE_Text); } } } //----------------------------------------------------- for(i=Merge.Get_Count()-1; i>0 && Process_Get_Okay(); i--) { if( !SG_STR_CMP(Merge[i].asString(0), Merge[i - 1].asString(0)) ) { Merge.Del_Record(i); } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CTL_Extract::On_Execute(void) { //----------------------------------------------------- CSG_Table Elements; Elements.Add_Field(SG_T("TEXT"), SG_DATATYPE_String); Elements.Add_Field(SG_T("FILE"), SG_DATATYPE_String); int nFiles = Read_Directory(Parameters("DIRECTORY")->asString(), Elements); if( nFiles <= 0 ) { Error_Set(SG_T("no source code files found")); return( false ); } Message_Add(CSG_String::Format("\n%s: %d", SG_T("number of scanned files"), nFiles), false); if( Elements.Get_Count() <= 0 ) { Error_Set(SG_T("no translatable text elements found")); return( false ); } Message_Add(CSG_String::Format("\n%s: %d", SG_T("number of translatable elements"), Elements.Get_Count()), false); //----------------------------------------------------- Process_Set_Text(SG_T("collecting elements")); CSG_String Text; bool bLocation = Parameters("LOCATION" )->asBool(); CSG_Table *pTarget = Parameters("TARGET")->asTable(); pTarget->Destroy(); pTarget->Set_Name(SG_T("Translatable Elements")); pTarget->Add_Field("TEXT" , SG_DATATYPE_String); pTarget->Add_Field("TRANSLATION", SG_DATATYPE_String); if( bLocation ) { pTarget->Add_Field("FILE" , SG_DATATYPE_String); } Elements.Set_Index(0, TABLE_INDEX_Ascending); for(int i=0; i<Elements.Get_Count() && Set_Progress(i, Elements.Get_Count()); i++) { if( i == 0 || Text.Cmp(Elements.Get_Record_byIndex(i)->asString(0)) ) { Text = Elements.Get_Record_byIndex(i)->asString(0); CSG_Table_Record *pRecord = pTarget->Add_Record(); pRecord->Set_Value(0, Text); if( bLocation ) { pRecord->Set_Value(2, Elements.Get_Record_byIndex(i)->asString(1)); } } } //----------------------------------------------------- return( true ); }