//--------------------------------------------------------- bool CSG_Grid::Assign(double Value) { if( is_Valid() ) { if( Value == 0.0 && m_Memory_Type == GRID_MEMORY_Normal ) { for(int n=0, m=_Get_nLineBytes(); n<Get_NY(); n++) { memset(m_Values[n], 0, m); } } else { for(int n=0; n<Get_NCells(); n++) { Set_Value(n, Value); } } //------------------------------------------------- Get_History().Destroy(); Get_History().Add_Child(SG_T("GRID_OPERATION"), Value)->Add_Property(SG_T("NAME"), LNG("Assign")); //------------------------------------------------- m_zStats.Invalidate(); Set_Update_Flag(false); return( true ); } return( false ); }
//--------------------------------------------------------- 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(double Value, TSG_Grid_Operation Operation) { //----------------------------------------------------- switch( Operation ) { case GRID_OPERATION_Addition: Get_History().Add_Child(SG_T("GRID_OPERATION"), Value)->Add_Property(SG_T("NAME"), LNG("Addition")); break; case GRID_OPERATION_Subtraction: Get_History().Add_Child(SG_T("GRID_OPERATION"), Value)->Add_Property(SG_T("NAME"), LNG("Subtraction")); Value = -Value; break; case GRID_OPERATION_Multiplication: Get_History().Add_Child(SG_T("GRID_OPERATION"), Value)->Add_Property(SG_T("NAME"), LNG("Multiplication")); break; case GRID_OPERATION_Division: if( Value == 0.0 ) return( *this ); Get_History().Add_Child(SG_T("GRID_OPERATION"), Value)->Add_Property(SG_T("NAME"), LNG("Division")); Value = 1.0 / Value; break; } //----------------------------------------------------- for(int y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(int x=0; x<Get_NX(); x++) { if( !is_NoData(x, y) ) { switch( Operation ) { case GRID_OPERATION_Addition: case GRID_OPERATION_Subtraction: Add_Value(x, y, Value); break; case GRID_OPERATION_Multiplication: case GRID_OPERATION_Division: Mul_Value(x, y, Value); break; } } } } SG_UI_Process_Set_Ready(); return( *this ); }
//--------------------------------------------------------- bool CSG_Grid::_Assign_ExtremeValue(CSG_Grid *pGrid, bool bMaximum) { if( Get_Cellsize() < pGrid->Get_Cellsize() || is_Intersecting(pGrid->Get_Extent()) == INTERSECTION_None ) { return( false ); } //----------------------------------------------------- int x, y, ix, iy; double px, py, ax, ay, d, 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(); //----------------------------------------------------- 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() ) { z = pGrid->asDouble(x, y); if( is_NoData(ix, iy) || (bMaximum == true && z > asDouble(ix, iy)) || (bMaximum == false && z < asDouble(ix, iy)) ) { Set_Value(ix, iy, z); } } } } } //----------------------------------------------------- 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 CSG_Shapes::Assign(CSG_Data_Object *pObject) { int iShape; CSG_Shape *pShape; CSG_Shapes *pShapes; //----------------------------------------------------- if( pObject && pObject->is_Valid() && (pObject->Get_ObjectType() == DATAOBJECT_TYPE_Shapes || pObject->Get_ObjectType() == DATAOBJECT_TYPE_PointCloud) ) { pShapes = (CSG_Shapes *)pObject; Create(pShapes->Get_Type(), pShapes->Get_Name(), pShapes, pShapes->Get_Vertex_Type()); for(iShape=0; iShape<pShapes->Get_Count() && SG_UI_Process_Set_Progress(iShape, pShapes->Get_Count()); iShape++) { pShape = Add_Shape(); pShape->Assign(pShapes->Get_Shape(iShape)); } SG_UI_Process_Set_Ready(); Update(); Get_History() = pObject->Get_History(); return( true ); } return( false ); }
//--------------------------------------------------------- void CSG_Grid::Invert(void) { int x, y; double zMin, zMax; if( is_Valid() && Get_ZRange() > 0.0 ) { zMin = Get_ZMin(); zMax = Get_ZMax(); for(y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(x=0; x<Get_NX(); x++) { if( !is_NoData(x, y) ) { Set_Value(x, y, zMax - (asDouble(x, y) - zMin)); } } } SG_UI_Process_Set_Ready(); Get_History().Add_Child(SG_T("GRID_OPERATION"), LNG("Inversion")); } }
//--------------------------------------------------------- bool CSG_PointCloud::Assign(CSG_Data_Object *pObject) { if( pObject && pObject->is_Valid() && pObject->Get_ObjectType() == Get_ObjectType() ) { Destroy(); CSG_PointCloud *pPointCloud = (CSG_PointCloud *)pObject; Get_History() = pPointCloud->Get_History(); for(int iField=0; iField<pPointCloud->m_nFields; iField++) { _Add_Field(pPointCloud->m_Field_Name[iField]->c_str(), pPointCloud->m_Field_Type[iField]); } for(int iPoint=0; iPoint<pPointCloud->Get_Count(); iPoint++) { if( _Inc_Array() ) { memcpy(m_Points[iPoint] + 1, pPointCloud->m_Points[iPoint] + 1, m_nPointBytes - 1); } } return( true ); } return( false ); }
//--------------------------------------------------------- void CSG_Grid::Normalise(void) { if( is_Valid() ) { Update(); if( m_zStats.Get_StdDev() > 0.0 ) { int x, y; if( (Get_NoData_hiValue() > -NORMALISED_NODATA && Get_NoData_hiValue() < NORMALISED_NODATA) || (Get_NoData_Value () > -NORMALISED_NODATA && Get_NoData_Value () < NORMALISED_NODATA) ) { for(y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(x=0; x<Get_NX(); x++) { if( is_NoData(x, y) ) { Set_Value(x, y, -NORMALISED_NODATA); } } } Set_NoData_Value(-NORMALISED_NODATA); } for(y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(x=0; x<Get_NX(); x++) { if( !is_NoData(x, y) ) { Set_Value(x, y, (asDouble(x, y) - m_zStats.Get_Mean()) / m_zStats.Get_StdDev() ); } } } SG_UI_Process_Set_Ready(); Get_History().Add_Child(SG_T("GRID_OPERATION"), LNG("Normalisation")); } } }
//--------------------------------------------------------- void CSG_Grid::Mirror(void) { int xA, xB, y; double d; if( is_Valid() ) { for(y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(xA=0, xB=Get_NX()-1; xA<xB; xA++, xB--) { d = asDouble(xA, y); Set_Value(xA, y, asDouble(xB, y)); Set_Value(xB, y, d); } } SG_UI_Process_Set_Ready(); Get_History().Add_Child(SG_T("GRID_OPERATION"), LNG("Horizontally mirrored")); } }
//--------------------------------------------------------- void CSG_Grid::DeNormalise(double ArithMean, double Variance) { int x, y; if( is_Valid() ) { Variance = sqrt(Variance); for(y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(x=0; x<Get_NX(); x++) { if( !is_NoData(x, y) ) { Set_Value(x, y, Variance * asDouble(x, y) + ArithMean); } } } SG_UI_Process_Set_Ready(); Get_History().Add_Child(SG_T("GRID_OPERATION"), LNG("Denormalisation")); } }
//--------------------------------------------------------- void CSG_Grid::Flip(void) { int x, yA, yB; double *Line, d; if( is_Valid() ) { Line = (double *)SG_Malloc(Get_NX() * sizeof(double)); for(yA=0, yB=Get_NY()-1; yA<yB && SG_UI_Process_Set_Progress(2 * yA, Get_NY()); yA++, yB--) { for(x=0; x<Get_NX(); x++) { Line[x] = asDouble(x, yA); } for(x=0; x<Get_NX(); x++) { d = Line[x]; Line[x] = asDouble(x, yB); Set_Value(x, yB, d); } for(x=0; x<Get_NX(); x++) { Set_Value(x, yA, Line[x]); } } SG_UI_Process_Set_Ready(); SG_Free(Line); Get_History().Add_Child(SG_T("GRID_OPERATION"), LNG("Vertically mirrored")); } }
//--------------------------------------------------------- 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 ); }