//--------------------------------------------------------- bool CGrid_Colors_Fit::On_Execute(void) { int iColor; long aC, bC; double aZ, bZ, dColor, zMin, zRange; CSG_Colors Colors_Old, Colors_New; CSG_Grid *pGrid; pGrid = Parameters("GRID")->asGrid(); Colors_New.Set_Count(Parameters("COUNT")->asInt()); switch( Parameters("SCALE")->asInt() ) { case 0: default: zMin = pGrid->Get_ZMin(); zRange = pGrid->Get_ZMax() - zMin; break; case 1: zMin = Parameters("RANGE")->asRange()->Get_LoVal(); zRange = Parameters("RANGE")->asRange()->Get_HiVal() - zMin; break; } DataObject_Get_Colors(pGrid, Colors_Old); if( Colors_Old.Get_Count() > 1 && pGrid->Get_ZRange() > 0.0 && zRange != 0.0 ) { dColor = 100.0 / Colors_Old.Get_Count(); aZ = 0.0; aC = Colors_Old.Get_Color(0); for(iColor=1; iColor<Colors_Old.Get_Count()-1; iColor++) { bZ = aZ; bC = aC; aZ = (pGrid->Get_Percentile(iColor * dColor) - zMin) / zRange; aC = Colors_Old.Get_Color(iColor); _Set_Colors(Colors_New, bZ, bC, aZ, aC); } bZ = aZ; bC = aC; aZ = 1.0; aC = Colors_Old.Get_Color(Colors_Old.Get_Count() - 1); _Set_Colors(Colors_New, bZ, bC, aZ, aC); DataObject_Set_Colors (pGrid, Colors_New); DataObject_Update (pGrid, zMin, zMin + zRange); return( true ); } return( false ); }
//--------------------------------------------------------- bool CGrid_Invert::On_Execute(void) { CSG_Grid *pGrid = Parameters("INVERSE")->asGrid(); if( pGrid == NULL ) { pGrid = Parameters("GRID")->asGrid(); } else if( pGrid != Parameters("GRID")->asGrid() ) { pGrid->Create(*Parameters("GRID")->asGrid()); pGrid->Set_Name(CSG_String::Format("%s [%s]", pGrid->Get_Name(), Parameters("METHOD")->asString())); } //----------------------------------------------------- double zMin = pGrid->Get_ZMin(); double zMax = pGrid->Get_ZMax(); for(int y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { if( !pGrid->is_NoData(x, y) ) { pGrid->Set_Value(x, y, zMax - (pGrid->asDouble(x, y) - zMin)); } } } //----------------------------------------------------- if( pGrid == Parameters("GRID")->asGrid() ) { DataObject_Update(pGrid); } return( true ); }
//--------------------------------------------------------- bool CPanSharp_PCA::On_Execute(void) { //----------------------------------------------------- bool bResult; CSG_Parameters Tool_Parms; CSG_Table Eigen; //----------------------------------------------------- // get the principle components for the low resolution bands SG_RUN_TOOL_KEEP_PARMS(bResult, "statistics_grid", 8, Tool_Parms, SG_TOOL_PARAMETER_SET("GRIDS" , Parameters("GRIDS" )) && SG_TOOL_PARAMETER_SET("METHOD" , Parameters("METHOD")) && SG_TOOL_PARAMETER_SET("EIGEN" , &Eigen) && SG_TOOL_PARAMETER_SET("COMPONENTS", 0) // get all components ); if( !bResult ) { return( false ); } //----------------------------------------------------- CSG_Parameter_Grid_List *pPCA = Tool_Parms.Get_Parameter("PCA")->asGridList(); int i, n = pPCA->Get_Count(); CSG_Grid *PCA = new CSG_Grid[n]; CSG_Grid *pPan = Parameters("PAN")->asGrid(); //----------------------------------------------------- // replace first principle component with the high resolution panchromatic band Process_Set_Text(_TL("Replace first PC with PAN")); double Offset_Pan, Offset, Scale; if( Parameters("PAN_MATCH")->asInt() == 0 ) // scale PAN band to fit first PC histogram { Offset_Pan = pPan->Get_ZMin(); Offset = pPCA->asGrid(0)->Get_ZMin(); Scale = pPCA->asGrid(0)->Get_ZRange() / pPan->Get_ZRange(); } else { Offset_Pan = pPan->Get_Mean(); Offset = pPCA->asGrid(0)->Get_Mean(); Scale = pPCA->asGrid(0)->Get_StdDev() / pPan->Get_StdDev(); } PCA[0].Create(*Get_System()); for(int y=0; y<Get_NY() && Set_Progress(y); y++) { #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { if( pPan->is_NoData(x, y) ) { PCA[0].Set_NoData(x, y); } else { PCA[0].Set_Value(x, y, Offset + Scale * (pPan->asDouble(x, y) - Offset_Pan)); } } } //----------------------------------------------------- // resample all other PCs to match the high resolution of the PAN band TSG_Grid_Resampling Resampling = Get_Resampling(Parameters("RESAMPLING")->asInt()); for(i=1; i<n; i++) { Process_Set_Text(CSG_String::Format("%s: %s ...", _TL("Resampling"), pPCA->asGrid(i)->Get_Name())); PCA[i].Create(*Get_System()); PCA[i].Assign(pPCA->asGrid(i), Resampling); delete(pPCA->asGrid(i)); // PCA tool was unmanaged, so we have to delete the output } delete(pPCA->asGrid(0)); pPCA->Del_Items(); for(i=0; i<n; i++) { pPCA->Add_Item(&PCA[i]); } //----------------------------------------------------- // inverse principle component rotation for the high resolution bands SG_RUN_TOOL_KEEP_PARMS(bResult, "statistics_grid", 10, Tool_Parms, SG_TOOL_PARAMETER_SET("PCA" , Tool_Parms("PCA")) && SG_TOOL_PARAMETER_SET("GRIDS", Parameters("SHARPEN")) && SG_TOOL_PARAMETER_SET("EIGEN", &Eigen) ); delete[](PCA); if( !bResult ) { return( false ); } CSG_Parameter_Grid_List *pHiRes = Parameters("SHARPEN")->asGridList(); CSG_Parameter_Grid_List *pLoRes = Parameters("GRIDS" )->asGridList(); CSG_Parameter_Grid_List *pGrids = Tool_Parms("GRIDS" )->asGridList(); if( !Parameters("OVERWRITE")->asBool() ) { pHiRes->Del_Items(); } for(i=0; i<pLoRes->Get_Count() && i<pGrids->Get_Count(); i++) { if( pHiRes->asGrid(i) ) { pHiRes->asGrid(i)->Assign(pGrids->asGrid(i)); delete(pGrids->asGrid(i)); } else { pHiRes->Add_Item(pGrids->asGrid(i)); } pHiRes->asGrid(i)->Set_Name(pLoRes->asGrid(i)->Get_Name()); } return( true ); }
//--------------------------------------------------------- bool CPanSharp_IHS::On_Execute(void) { //----------------------------------------------------- TSG_Grid_Resampling Resampling = Get_Resampling(Parameters("RESAMPLING")->asInt()); //----------------------------------------------------- int y; CSG_Grid *pPan = Parameters("PAN")->asGrid(); //----------------------------------------------------- Process_Set_Text(CSG_String::Format("%s: %s ...", _TL("Resampling"), Parameters("R")->asGrid()->Get_Name())); CSG_Grid *pR = Parameters("R_SHARP")->asGrid(); pR->Assign (Parameters("R")->asGrid(), Resampling); pR->Set_Name(Parameters("R")->asGrid()->Get_Name()); Process_Set_Text(CSG_String::Format("%s: %s ...", _TL("Resampling"), Parameters("G")->asGrid()->Get_Name())); CSG_Grid *pG = Parameters("G_SHARP")->asGrid(); pG->Assign (Parameters("G")->asGrid(), Resampling); pG->Set_Name(Parameters("G")->asGrid()->Get_Name()); Process_Set_Text(CSG_String::Format("%s: %s ...", _TL("Resampling"), Parameters("B")->asGrid()->Get_Name())); CSG_Grid *pB = Parameters("B_SHARP")->asGrid(); pB->Assign (Parameters("B")->asGrid(), Resampling); pB->Set_Name(Parameters("B")->asGrid()->Get_Name()); //----------------------------------------------------- Process_Set_Text(_TL("RGB to IHS")); double rMin = pR->Get_ZMin(), rRange = pR->Get_ZRange(); double gMin = pG->Get_ZMin(), gRange = pG->Get_ZRange(); double bMin = pB->Get_ZMin(), bRange = pB->Get_ZRange(); for(y=0; y<pPan->Get_NY() && Set_Progress(y, pPan->Get_NY()); y++) { #pragma omp parallel for for(int x=0; x<pPan->Get_NX(); x++) { bool bNoData = true; if( pPan->is_NoData(x, y) || pR->is_NoData(x, y) || pG->is_NoData(x, y) || pB->is_NoData(x, y) ) { pR->Set_NoData(x, y); pG->Set_NoData(x, y); pB->Set_NoData(x, y); } else { double r = (pR->asDouble(x, y) - rMin) / rRange; if( r < 0.0 ) r = 0.0; else if( r > 1.0 ) r = 1.0; double g = (pG->asDouble(x, y) - gMin) / gRange; if( g < 0.0 ) g = 0.0; else if( g > 1.0 ) g = 1.0; double b = (pB->asDouble(x, y) - bMin) / bRange; if( b < 0.0 ) b = 0.0; else if( b > 1.0 ) b = 1.0; double h, s, i = r + g + b; if( i <= 0.0 ) { h = 0.0; s = 0.0; } else { if( r == g && g == b ) { h = 0.0; } else if( b < r && b < g ) { h = (g - b) / (i - 3 * b) ; } else if( r < g && r < b ) { h = (b - r) / (i - 3 * r) + 1; } else { h = (r - g) / (i - 3 * g) + 2; } if ( 0.0 <= h && h < 1.0 ) { s = (i - 3 * b) / i; } else if( 1.0 <= h && h < 2.0 ) { s = (i - 3 * r) / i; } else { s = (i - 3 * g) / i; } } pR->Set_Value(x, y, i); pG->Set_Value(x, y, s); pB->Set_Value(x, y, h); } } } //----------------------------------------------------- double Offset_Pan, Offset, Scale; if( Parameters("PAN_MATCH")->asInt() == 0 ) { Offset_Pan = pPan->Get_ZMin(); Offset = pR->Get_ZMin(); Scale = pR->Get_ZRange() / pPan->Get_ZRange(); } else { Offset_Pan = pPan->Get_Mean(); Offset = pR->Get_Mean(); Scale = pR->Get_StdDev() / pPan->Get_StdDev(); } //----------------------------------------------------- Process_Set_Text(_TL("IHS to RGB")); for(y=0; y<pPan->Get_NY() && Set_Progress(y, pPan->Get_NY()); y++) { #pragma omp parallel for for(int x=0; x<pPan->Get_NX(); x++) { if( !pR->is_NoData(x, y) ) { double i = Offset + Scale * (pPan->asDouble(x, y) - Offset_Pan); double s = pG ->asDouble(x, y); double h = pB ->asDouble(x, y); double r, g, b; if ( 0.0 <= h && h < 1.0 ) { r = i * (1 + 2 * s - 3 * s * h) / 3; g = i * (1 - s + 3 * s * h) / 3; b = i * (1 - s ) / 3; } else if( 1.0 <= h && h < 2.0 ) { r = i * (1 - s ) / 3; g = i * (1 + 2 * s - 3 * s * (h - 1)) / 3; b = i * (1 - s + 3 * s * (h - 1)) / 3; } else { r = i * (1 - s + 3 * s * (h - 2)) / 3; g = i * (1 - s ) / 3; b = i * (1 + 2 * s - 3 * s * (h - 2)) / 3; } pR->Set_Value(x, y, rMin + r * rRange); pG->Set_Value(x, y, gMin + g * gRange); pB->Set_Value(x, y, bMin + b * bRange); } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- 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 CSurfer_Export::On_Execute(void) { const char ID_BINARY[] = "DSBB"; bool bNoData; short sValue; int x, y; float *fLine; double dValue; FILE *Stream; CSG_String fName; CSG_Grid *pGrid; //----------------------------------------------------- pGrid = Parameters("GRID") ->asGrid(); fName = Parameters("FILE") ->asString(); bNoData = Parameters("NODATA") ->asBool(); switch( Parameters("FORMAT")->asInt() ) { //----------------------------------------------------- case 0: // Surfer 6 - Binary... if( (Stream = fopen(fName.b_str(), "wb")) != NULL ) { fwrite(ID_BINARY, 4, sizeof(char ), Stream); sValue = (short)pGrid->Get_NX(); fwrite(&sValue , 1, sizeof(short ), Stream); sValue = (short)pGrid->Get_NY(); fwrite(&sValue , 1, sizeof(short ), Stream); dValue = pGrid->Get_XMin(); fwrite(&dValue , 1, sizeof(double), Stream); dValue = pGrid->Get_XMax(); fwrite(&dValue , 1, sizeof(double), Stream); dValue = pGrid->Get_YMin(); fwrite(&dValue , 1, sizeof(double), Stream); dValue = pGrid->Get_YMax(); fwrite(&dValue , 1, sizeof(double), Stream); dValue = pGrid->Get_ZMin(); fwrite(&dValue , 1, sizeof(double), Stream); dValue = pGrid->Get_ZMax(); fwrite(&dValue , 1, sizeof(double), Stream); //--------------------------------------------- fLine = (float *)SG_Malloc(pGrid->Get_NX() * sizeof(float)); for(y=0; y<pGrid->Get_NY() && Set_Progress(y, pGrid->Get_NY()); y++) { for(x=0; x<pGrid->Get_NX(); x++) { fLine[x] = bNoData && pGrid->is_NoData(x, y) ? NODATAVALUE : pGrid->asFloat(x, y); } fwrite(fLine, pGrid->Get_NX(), sizeof(float), Stream); } SG_Free(fLine); fclose(Stream); return( true ); } break; //----------------------------------------------------- case 1: // Surfer - ASCII... if( (Stream = fopen(fName.b_str(), "w")) != NULL ) { fprintf(Stream, "DSAA\n" ); fprintf(Stream, "%d %d\n", pGrid->Get_NX() , pGrid->Get_NY() ); fprintf(Stream, "%f %f\n", pGrid->Get_XMin(), pGrid->Get_XMax() ); fprintf(Stream, "%f %f\n", pGrid->Get_YMin(), pGrid->Get_YMax() ); fprintf(Stream, "%f %f\n", pGrid->Get_ZMin(), pGrid->Get_ZMax() ); //--------------------------------------------- for(y=0; y<pGrid->Get_NY() && Set_Progress(y, pGrid->Get_NY()); y++) { for(x=0; x<pGrid->Get_NX(); x++) { if( bNoData && pGrid->is_NoData(x, y) ) { fprintf(Stream, "1.70141e38 "); } else { fprintf(Stream, "%f ", pGrid->asFloat(x, y)); } } fprintf(Stream, "\n"); } fclose(Stream); return( true ); } break; } //----------------------------------------------------- return( false ); }