//--------------------------------------------------------- bool CSG_Grid::_Assign_Interpolated(CSG_Grid *pGrid, TSG_Grid_Interpolation Interpolation) { int x, y; double xPosition, yPosition, z; Set_NoData_Value_Range(pGrid->Get_NoData_Value(), pGrid->Get_NoData_hiValue()); for(y=0, yPosition=Get_YMin(); y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++, yPosition+=Get_Cellsize()) { for(x=0, xPosition=Get_XMin(); x<Get_NX(); x++, xPosition+=Get_Cellsize()) { if( pGrid->Get_Value(xPosition, yPosition, z, Interpolation) ) { Set_Value (x, y, z); } else { Set_NoData(x, y); } } } Get_History() = pGrid->Get_History(); Get_History().Add_Child(SG_T("GRID_OPERATION"), CSG_String::Format(SG_T("%f -> %f"), pGrid->Get_Cellsize(), Get_Cellsize()))->Add_Property(SG_T("NAME"), LNG("Resampling")); SG_UI_Process_Set_Ready(); return( true ); }
//--------------------------------------------------------- CSG_Grid & CSG_Grid::_Operation_Arithmetic(const CSG_Grid &Grid, TSG_Grid_Operation Operation) { if( is_Intersecting(Grid.Get_Extent()) ) { int x, y; double xWorld, yWorld, Value; TSG_Grid_Interpolation Interpolation; Interpolation = Get_Cellsize() == Grid.Get_Cellsize() && fmod(Get_XMin() - Grid.Get_XMin(), Get_Cellsize()) == 0.0 && Get_Cellsize() == Grid.Get_Cellsize() && fmod(Get_YMin() - Grid.Get_YMin(), Get_Cellsize()) == 0.0 ? GRID_INTERPOLATION_NearestNeighbour : GRID_INTERPOLATION_BSpline; for(y=0, yWorld=Get_YMin(); y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++, yWorld+=Get_Cellsize()) { for(x=0, xWorld=Get_XMin(); x<Get_NX(); x++, xWorld+=Get_Cellsize()) { if( !Grid.Get_Value(xWorld, yWorld, Value, Interpolation, true) ) { Set_NoData(x, y); } else switch( Operation ) { case GRID_OPERATION_Addition: Add_Value(x, y, Value); break; case GRID_OPERATION_Subtraction: Add_Value(x, y, -Value); break; case GRID_OPERATION_Multiplication: Mul_Value(x, y, Value); break; case GRID_OPERATION_Division: if( Value != 0.0 ) { Mul_Value(x, y, 1.0 / Value); } else { Set_NoData(x, y); } break; } } } SG_UI_Process_Set_Ready(); //------------------------------------------------- switch( Operation ) { case GRID_OPERATION_Addition: Get_History().Add_Child(SG_T("GRID_OPERATION"), Grid.Get_Name())->Add_Property(SG_T("NAME"), LNG("Addition")); break; case GRID_OPERATION_Subtraction: Get_History().Add_Child(SG_T("GRID_OPERATION"), Grid.Get_Name())->Add_Property(SG_T("NAME"), LNG("Subtraction")); break; case GRID_OPERATION_Multiplication: Get_History().Add_Child(SG_T("GRID_OPERATION"), Grid.Get_Name())->Add_Property(SG_T("NAME"), LNG("Multiplication")); break; case GRID_OPERATION_Division: Get_History().Add_Child(SG_T("GRID_OPERATION"), Grid.Get_Name())->Add_Property(SG_T("NAME"), LNG("Division")); break; } Get_History() += ((CSG_Grid *)&Grid)->Get_History(); } return( *this ); }
//--------------------------------------------------------- bool CSG_Grid::_Assign_MeanValue(CSG_Grid *pGrid, bool bAreaProportional) { if( Get_Cellsize() < pGrid->Get_Cellsize() || is_Intersecting(pGrid->Get_Extent()) == INTERSECTION_None ) { return( false ); } //----------------------------------------------------- int x, y, ix, iy, jx, jy; double px, py, ax, ay, d, w, wx, wy, z; CSG_Matrix S(Get_NY(), Get_NX()), N(Get_NY(), Get_NX()); d = pGrid->Get_Cellsize() / Get_Cellsize(); Set_NoData_Value(pGrid->Get_NoData_Value()); Assign_NoData(); //----------------------------------------------------- if( bAreaProportional == false ) { ax = 0.5 + (pGrid->Get_XMin() - Get_XMin()) / Get_Cellsize(); ay = 0.5 + (pGrid->Get_YMin() - Get_YMin()) / Get_Cellsize(); for(y=0, py=ay; y<pGrid->Get_NY() && SG_UI_Process_Set_Progress(y, pGrid->Get_NY()); y++, py+=d) { if( (iy = (int)floor(py)) >= 0 && iy < Get_NY() ) { for(x=0, px=ax; x<pGrid->Get_NX(); x++, px+=d) { if( !pGrid->is_NoData(x, y) && (ix = (int)floor(px)) >= 0 && ix < Get_NX() ) { S[ix][iy] += pGrid->asDouble(x, y); N[ix][iy] ++; } } } } } //----------------------------------------------------- else // if( bAreaProportional == true ) { ax = ((pGrid->Get_XMin() - 0.5 * pGrid->Get_Cellsize()) - (Get_XMin() - 0.5 * Get_Cellsize())) / Get_Cellsize(); ay = ((pGrid->Get_YMin() - 0.5 * pGrid->Get_Cellsize()) - (Get_YMin() - 0.5 * Get_Cellsize())) / Get_Cellsize(); for(y=0, py=ay; y<pGrid->Get_NY() && SG_UI_Process_Set_Progress(y, pGrid->Get_NY()); y++, py+=d) { if( py > -d || py < Get_NY() ) { iy = (int)floor(py); wy = (py + d) - iy; wy = wy < 1.0 ? 1.0 : wy - 1.0; for(x=0, px=ax; x<pGrid->Get_NX(); x++, px+=d) { if( !pGrid->is_NoData(x, y) && (px > -d && px < Get_NX()) ) { ix = (int)floor(px); wx = (px + d) - ix; wx = wx < 1.0 ? 1.0 : wx - 1.0; z = pGrid->asDouble(x, y); if( iy >= 0 && iy < Get_NY() ) // wy > 0.0 is always true { if( ix >= 0 && ix < Get_NX() ) // wx > 0.0 is always true { w = wx * wy; S[ix][iy] += w * z; N[ix][iy] += w; } if( wx < 1.0 && (jx = ix + 1) >= 0 && jx < Get_NX() ) { w = (1.0 - wx) * wy; S[jx][iy] += w * z; N[jx][iy] += w; } } if( wy < 1.0 && (jy = iy + 1) >= 0 && jy < Get_NY() ) { if( ix >= 0 && ix < Get_NX() ) { w = wx * (1.0 - wy); S[ix][jy] += w * z; N[ix][jy] += w; } if( wx < 1.0 && (jx = ix + 1) >= 0 && jx < Get_NX() ) { w = (1.0 - wx) * (1.0 - wy); S[jx][jy] += w * z; N[jx][jy] += w; } } } } } } } //----------------------------------------------------- for(y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(x=0; x<Get_NX(); x++) { if( N[x][y] ) { Set_Value(x, y, S[x][y] / N[x][y]); } else { Set_NoData(x, y); } } } //----------------------------------------------------- Get_History() = pGrid->Get_History(); Get_History().Add_Child(SG_T("GRID_OPERATION"), CSG_String::Format(SG_T("%f -> %f"), pGrid->Get_Cellsize(), Get_Cellsize()))->Add_Property(SG_T("NAME"), LNG("Resampling")); SG_UI_Process_Set_Ready(); return( true ); }
//--------------------------------------------------------- bool CKriging_Base::On_Execute(void) { bool bResult = false; //----------------------------------------------------- m_Block = Parameters("BLOCK" )->asBool() ? Parameters("DBLOCK")->asDouble() / 2.0 : 0.0; m_bStdDev = Parameters("TQUALITY")->asInt() == 0; m_bLog = Parameters("LOG" )->asBool(); m_pPoints = Parameters("POINTS" )->asShapes(); m_zField = Parameters("ZFIELD" )->asInt(); if( m_pPoints->Get_Count() <= 1 ) { SG_UI_Msg_Add(_TL("not enough points for interpolation"), true); return( false ); } //----------------------------------------------------- CSG_Table Variogram; if( SG_UI_Get_Window_Main() ) { static CVariogram_Dialog dlg; if( dlg.Execute(m_pPoints, m_zField, m_bLog, &Variogram, &m_Model) ) { bResult = true; } } else { int nSkip = Parameters("VAR_NSKIP" )->asInt(); int nClasses = Parameters("VAR_NCLASSES")->asInt(); double maxDistance = Parameters("VAR_MAXDIST" )->asDouble(); m_Model.Set_Formula(Parameters("VAR_MODEL")->asString()); if( CSG_Variogram::Calculate(m_pPoints, m_zField, m_bLog, &Variogram, nClasses, maxDistance, nSkip) ) { m_Model.Clr_Data(); for(int i=0; i<Variogram.Get_Count(); i++) { CSG_Table_Record *pRecord = Variogram.Get_Record(i); m_Model.Add_Data(pRecord->asDouble(CSG_Variogram::FIELD_DISTANCE), pRecord->asDouble(CSG_Variogram::FIELD_VAR_EXP)); } bResult = m_Model.Get_Trend() || m_Model.Get_Parameter_Count() == 0; } } //----------------------------------------------------- if( bResult && (bResult = _Initialise_Grids() && On_Initialize()) ) { Message_Add(CSG_String::Format(SG_T("%s: %s"), _TL("variogram model"), m_Model.Get_Formula(SG_TREND_STRING_Formula_Parameters).c_str()), false); for(int y=0; y<m_pGrid->Get_NY() && Set_Progress(y, m_pGrid->Get_NY()); y++) { #pragma omp parallel for for(int x=0; x<m_pGrid->Get_NX(); x++) { double z, v; if( Get_Value(m_pGrid->Get_System().Get_Grid_to_World(x, y), z, v) ) { Set_Value(x, y, z, v); } else { Set_NoData(x, y); } } } } //----------------------------------------------------- m_Model.Clr_Data(); On_Finalize(); return( bResult ); }