//--------------------------------------------------------- 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_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 ); }
//--------------------------------------------------------- bool CSG_Table::_Save_Text(const CSG_String &File_Name, bool bHeadline, const SG_Char *Separator) { int iField, iRecord; CSG_File Stream; if( Get_Field_Count() > 0 ) { if( Stream.Open(File_Name, SG_FILE_W, false) ) { for(iField=0; iField<Get_Field_Count(); iField++) { Stream.Printf(SG_T("%s%s"), Get_Field_Name(iField), iField < Get_Field_Count() - 1 ? Separator : SG_T("\n")); } for(iRecord=0; iRecord<Get_Record_Count() && SG_UI_Process_Set_Progress(iRecord, Get_Record_Count()); iRecord++) { for(iField=0; iField<Get_Field_Count(); iField++) { Stream.Printf(SG_T("%s"), Get_Record(iRecord)->asString(iField)); Stream.Printf(SG_T("%s"), iField < Get_Field_Count() - 1 ? Separator : SG_T("\n")); } } SG_UI_Process_Set_Ready(); 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_PointCloud::_Save(const CSG_String &File_Name) { CSG_File Stream; SG_UI_Msg_Add(CSG_String::Format(SG_T("%s: %s..."), _TL("Save point cloud"), File_Name.c_str()), true); CSG_String sFile_Name = SG_File_Make_Path(NULL, File_Name, SG_T("spc")); if( Stream.Open(sFile_Name, SG_FILE_W, true) == false ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("unable to create file.")); return( false ); } int i, iBuffer, nPointBytes = m_nPointBytes - 1; Stream.Write((void *)PC_FILE_VERSION, 6); Stream.Write(&nPointBytes , sizeof(int)); Stream.Write(&m_nFields , sizeof(int)); for(i=0; i<m_nFields; i++) { Stream.Write(&m_Field_Type[i], sizeof(TSG_Data_Type)); iBuffer = (int)m_Field_Name[i]->Length(); if( iBuffer >= 1024 - 1 ) iBuffer = 1024 - 1; Stream.Write(&iBuffer, sizeof(int)); Stream.Write((void *)m_Field_Name[i]->b_str(), sizeof(char), iBuffer); } _Set_Shape(m_Shapes_Index); for(i=0; i<Get_Count() && SG_UI_Process_Set_Progress(i, Get_Count()); i++) { Stream.Write(m_Points[i] + 1, nPointBytes); } Set_Modified(false); Set_File_Name(sFile_Name, true); Save_MetaData(File_Name); Get_Projection().Save(SG_File_Make_Path(NULL, File_Name, SG_T("prj")), SG_PROJ_FMT_WKT); SG_UI_Msg_Add(_TL("okay"), false, SG_UI_MSG_STYLE_SUCCESS); SG_UI_Process_Set_Ready(); return( true ); }
//--------------------------------------------------------- bool CSG_Shapes::Save(const CSG_String &File_Name, int Format) { SG_UI_Msg_Add(CSG_String::Format(SG_T("%s: %s..."), _TL("Save shapes"), File_Name.c_str()), true); if( _Save_ESRI(File_Name) ) { Set_Modified(false); Set_File_Name(File_Name, true); SG_UI_Process_Set_Ready(); SG_UI_Msg_Add(_TL("okay"), false, SG_UI_MSG_STYLE_SUCCESS); return( true ); } SG_UI_Process_Set_Ready(); SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); return( false ); }
//--------------------------------------------------------- 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_Table::_Save_Text(const CSG_String &File_Name, bool bHeadline, const SG_Char *Separator) { int iField, iRecord; CSG_File Stream; if( Get_Field_Count() > 0 ) { if( Stream.Open(File_Name, SG_FILE_W, false) ) { for(iField=0; iField<Get_Field_Count(); iField++) { Stream.Printf(SG_T("%s%s"), Get_Field_Name(iField), iField < Get_Field_Count() - 1 ? Separator : SG_T("\n")); } for(iRecord=0; iRecord<Get_Record_Count() && SG_UI_Process_Set_Progress(iRecord, Get_Record_Count()); iRecord++) { for(iField=0; iField<Get_Field_Count(); iField++) { if( !Get_Record(iRecord)->is_NoData(iField) ) { switch( Get_Field_Type(iField) ) { case SG_DATATYPE_String: case SG_DATATYPE_Date: Stream.Printf(SG_T("\"%s\""), Get_Record(iRecord)->asString(iField)); break; default: Stream.Printf(SG_T("%s") , Get_Record(iRecord)->asString(iField)); break; } } Stream.Printf(SG_T("%s"), iField < Get_Field_Count() - 1 ? Separator : SG_T("\n")); } } SG_UI_Process_Set_Ready(); 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")); } }
//--------------------------------------------------------- bool CSG_Grid::_Load_ASCII(CSG_File &Stream, TSG_Grid_Memory_Type Memory_Type, bool bFlip) { int x, y, iy, dy; double Value; if( Stream.is_Open() && m_System.is_Valid() && m_Type != SG_DATATYPE_Undefined && _Memory_Create(Memory_Type) ) { Set_File_Type(GRID_FILE_FORMAT_ASCII); if( bFlip ) { y = Get_NY() - 1; dy = -1; } else { y = 0; dy = 1; } //------------------------------------------------- for(iy=0; iy<Get_NY() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy) { for(x=0; x<Get_NX(); x++) { SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%lf"), &Value); Set_Value(x, y, Value); } } SG_UI_Process_Set_Ready(); return( true ); } return( false ); }
//--------------------------------------------------------- bool CSG_Grid::_Save_ASCII(CSG_File &Stream, int xA, int yA, int xN, int yN, bool bFlip) { int x, y, ix, iy, dy; if( Stream.is_Open() && is_Valid() ) { Set_File_Type(GRID_FILE_FORMAT_ASCII); if( bFlip ) { y = yA + yN - 1; dy = -1; } else { y = yA; dy = 1; } //------------------------------------------------- for(iy=0; iy<yN && SG_UI_Process_Set_Progress(iy, yN); iy++, y+=dy) { for(ix=0, x=xA; ix<xN; ix++, x++) { Stream.Printf(SG_T("%lf "), asDouble(x, y)); } Stream.Printf(SG_T("\n")); } SG_UI_Process_Set_Ready(); return( true ); } return( false ); }
//--------------------------------------------------------- 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")); } }
//--------------------------------------------------------- bool CSG_Shapes::Create(const CSG_String &File_Name) { Destroy(); SG_UI_Msg_Add(CSG_String::Format("%s: %s...", _TL("Load shapes"), File_Name.c_str()), true); //----------------------------------------------------- bool bResult = File_Name.BeforeFirst(':').Cmp("PGSQL") && SG_File_Exists(File_Name) && _Load_ESRI(File_Name); if( bResult ) { Set_File_Name(File_Name, true); } //----------------------------------------------------- else if( File_Name.BeforeFirst(':').Cmp("PGSQL") == 0 ) // database source { CSG_String s(File_Name); s = s.AfterFirst(':'); CSG_String Host (s.BeforeFirst(':')); s = s.AfterFirst(':'); CSG_String Port (s.BeforeFirst(':')); s = s.AfterFirst(':'); CSG_String DBName(s.BeforeFirst(':')); s = s.AfterFirst(':'); CSG_String Table (s.BeforeFirst(':')); CSG_Tool *pTool = SG_Get_Tool_Library_Manager().Get_Tool("db_pgsql", 0); // CGet_Connections if( pTool != NULL ) { SG_UI_ProgressAndMsg_Lock(true); //--------------------------------------------- CSG_Table Connections; CSG_String Connection = DBName + " [" + Host + ":" + Port + "]"; pTool->Settings_Push(); if( pTool->On_Before_Execution() && SG_TOOL_PARAMETER_SET("CONNECTIONS", &Connections) && pTool->Execute() ) // CGet_Connections { for(int i=0; !bResult && i<Connections.Get_Count(); i++) { if( !Connection.Cmp(Connections[i].asString(0)) ) { bResult = true; } } } pTool->Settings_Pop(); //--------------------------------------------- if( bResult && (bResult = (pTool = SG_Get_Tool_Library_Manager().Get_Tool("db_pgsql", 20)) != NULL) == true ) // CPGIS_Shapes_Load { pTool->Settings_Push(); bResult = pTool->On_Before_Execution() && SG_TOOL_PARAMETER_SET("CONNECTION", Connection) && SG_TOOL_PARAMETER_SET("TABLES" , Table) && SG_TOOL_PARAMETER_SET("SHAPES" , this) && pTool->Execute(); pTool->Settings_Pop(); } SG_UI_ProgressAndMsg_Lock(false); } } //----------------------------------------------------- if( bResult ) { Set_Modified(false); Set_Update_Flag(); SG_UI_Process_Set_Ready(); SG_UI_Msg_Add(_TL("okay"), false, SG_UI_MSG_STYLE_SUCCESS); return( true ); } for(int iShape=Get_Count()-1; iShape>=0; iShape--) // be kind, keep at least those shapes that have been loaded successfully { if( !Get_Shape(iShape)->is_Valid() ) { Del_Shape(iShape); } } if( Get_Count() <= 0 ) { Destroy(); } SG_UI_Process_Set_Ready(); SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); return( false ); }
//--------------------------------------------------------- bool CSG_TIN::_Triangulate(void) { bool bResult; int i, j, n, nTriangles; CSG_TIN_Node **Nodes; TTIN_Triangle *Triangles; //----------------------------------------------------- _Destroy_Edges(); _Destroy_Triangles(); //----------------------------------------------------- Nodes = (CSG_TIN_Node **)SG_Malloc((Get_Node_Count() + 3) * sizeof(CSG_TIN_Node *)); for(i=0; i<Get_Node_Count(); i++) { Nodes[i] = Get_Node(i); Nodes[i] ->_Del_Relations(); } //----------------------------------------------------- qsort(Nodes, Get_Node_Count(), sizeof(CSG_TIN_Node *), SG_TIN_Compare); for(i=0, j=0, n=Get_Node_Count(); j<n; i++) // remove duplicates { Nodes[i] = Nodes[j++]; while( j < n && Nodes[i]->Get_X() == Nodes[j]->Get_X() && Nodes[i]->Get_Y() == Nodes[j]->Get_Y() ) { Del_Node(Nodes[j++]->Get_Index(), false); } } //----------------------------------------------------- for(i=Get_Node_Count(); i<Get_Node_Count()+3; i++) { Nodes[i] = new CSG_TIN_Node(this, 0); } //----------------------------------------------------- Triangles = (TTIN_Triangle *)SG_Malloc(3 * Get_Node_Count() * sizeof(TTIN_Triangle)); if( (bResult = _Triangulate(Nodes, Get_Node_Count(), Triangles, nTriangles)) == true ) { for(i=0; i<nTriangles && SG_UI_Process_Set_Progress(i, nTriangles); i++) { _Add_Triangle(Nodes[Triangles[i].p1], Nodes[Triangles[i].p2], Nodes[Triangles[i].p3]); } } SG_Free(Triangles); //----------------------------------------------------- for(i=Get_Node_Count(); i<Get_Node_Count()+3; i++) { delete(Nodes[i]); } SG_Free(Nodes); SG_UI_Process_Set_Ready(); return( bResult ); }
//--------------------------------------------------------- bool CSG_PointCloud::_Load(const CSG_String &File_Name) { TSG_Data_Type Type; char ID[6]; int i, iBuffer, nPointBytes, nFields; char Name[1024]; CSG_File Stream; SG_UI_Msg_Add(CSG_String::Format(SG_T("%s: %s..."), _TL("Load point cloud"), File_Name.c_str()), true); //----------------------------------------------------- if( !Stream.Open(File_Name, SG_FILE_R, true) ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("file could not be opened.")); return( false ); } if( !Stream.Read(ID, 6) || strncmp(ID, PC_FILE_VERSION, 5) != 0 ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("incompatible file.")); return( false ); } if( !Stream.Read(&nPointBytes, sizeof(int)) || nPointBytes < (int)(3 * sizeof(float)) ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("incompatible file.")); return( false ); } if( !Stream.Read(&nFields, sizeof(int)) || nFields < 3 ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("incompatible file.")); return( false ); } //----------------------------------------------------- Destroy(); for(i=0; i<nFields; i++) { if( !Stream.Read(&Type , sizeof(TSG_Data_Type)) || !Stream.Read(&iBuffer , sizeof(int)) || !(iBuffer > 0 && iBuffer < 1024) || !Stream.Read(Name , iBuffer) ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("incompatible file.")); return( false ); } if( ID[5] == '0' ) // Data Type Definition changed!!! { switch( Type ) { default: Type = SG_DATATYPE_Undefined; break; case 1: Type = SG_DATATYPE_Char; break; case 2: Type = SG_DATATYPE_Short; break; case 3: Type = SG_DATATYPE_Int; break; case 4: Type = SG_DATATYPE_Long; break; case 5: Type = SG_DATATYPE_Float; break; case 6: Type = SG_DATATYPE_Double; break; } } Name[iBuffer] = '\0'; if( !_Add_Field(CSG_String((const char *)Name), Type) ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("incompatible file.")); return( false ); } } if( m_nPointBytes != nPointBytes + 1 ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("incompatible file.")); return( false ); } //----------------------------------------------------- sLong fLength = Stream.Length(); while( _Inc_Array() && Stream.Read(m_Cursor + 1, nPointBytes) && SG_UI_Process_Set_Progress((double)Stream.Tell(), (double)fLength) ) {} _Dec_Array(); Set_File_Name(File_Name, true); Load_MetaData(File_Name); if( 0 > Get_Count() ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("no records in file.")); return( false ); } //----------------------------------------------------- SG_UI_Process_Set_Ready(); Get_Projection().Load(SG_File_Make_Path(NULL, File_Name, SG_T("prj")), SG_PROJ_FMT_WKT); SG_UI_Msg_Add(_TL("okay"), false, SG_UI_MSG_STYLE_SUCCESS); 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 CSG_Table::_Load_Text(const CSG_String &File_Name, bool bHeadline, const SG_Char *Separator) { int i, iField, fLength; CSG_String sLine, sField; CSG_File Stream; CSG_Table Table; //----------------------------------------------------- if( Stream.Open(File_Name, SG_FILE_R, false) == false ) { return( false ); } if( !Stream.Read_Line(sLine) ) { return( false ); } //----------------------------------------------------- sLine += Separator; while( (i = sLine.Find(Separator)) >= 0 ) { sField = bHeadline ? sLine.Left(i) : CSG_String::Format(SG_T("FIELD_%02d"), Table.Get_Field_Count() + 1); if( sField[0] == SG_T('\"') && sField[(int)(sField.Length() - 1)] == SG_T('\"') ) // remove quota { sField = sField.AfterFirst('\"').BeforeLast('\"'); } Table.Add_Field(sField, SG_DATATYPE_String); sLine.Remove(0, i + 1); } //----------------------------------------------------- TSG_Data_Type *Type = new TSG_Data_Type[Table.Get_Field_Count()]; for(iField=0; iField<Table.Get_Field_Count(); iField++) { Type[iField] = SG_DATATYPE_Int; } if( !bHeadline ) { Stream.Seek_Start(); } fLength = Stream.Length(); while( Stream.Read_Line(sLine) && sLine.Length() > 0 && SG_UI_Process_Set_Progress(Stream.Tell(), fLength) ) { CSG_Table_Record *pRecord = Table._Add_Record(); sLine += Separator; for(iField=0; iField<Table.Get_Field_Count(); iField++) { if( (i = sLine.Find(Separator)) >= 0 ) { sField = sLine.Left(i); if( sField[0] == SG_T('\"') && sField[(int)(sField.Length() - 1)] == SG_T('\"') ) // remove quota { sField = sField.AfterFirst('\"').BeforeLast('\"'); } if( Type[iField] != SG_DATATYPE_String ) { double Value; if( SG_SSCANF(sField, SG_T("%lf"), &Value) != 1 ) { Type[iField] = SG_DATATYPE_String; } else if( Type[iField] != SG_DATATYPE_Double && Value - (int)Value != 0.0 ) { Type[iField] = SG_DATATYPE_Double; } } pRecord->Set_Value(iField, sField); sLine.Remove(0, i + 1); } else { break; } } } //----------------------------------------------------- if( Table.Get_Count() > 0 ) { for(iField=0; iField<Table.Get_Field_Count(); iField++) { Add_Field(Table.Get_Field_Name(iField), Type[iField]); } for(int iRecord=0; iRecord<Table.Get_Count() && SG_UI_Process_Set_Progress(iRecord, Table.Get_Count()); iRecord++) { CSG_Table_Record *pRecord = _Add_Record(); for(iField=0; iField<Get_Field_Count(); iField++) { switch( Get_Field_Type(iField) ) { default: pRecord->Set_Value(iField, Table[iRecord].asString(iField)); break; case SG_DATATYPE_Int: pRecord->Set_Value(iField, Table[iRecord].asInt (iField)); break; case SG_DATATYPE_Double: pRecord->Set_Value(iField, Table[iRecord].asDouble(iField)); break; } } } } delete[](Type); SG_UI_Process_Set_Ready(); return( Get_Field_Count() > 0 ); }
//--------------------------------------------------------- bool CSG_Grid::_Load_Binary(CSG_File &Stream, TSG_Data_Type File_Type, bool bFlip, bool bSwapBytes) { char *Line, *pValue; int x, y, i, iy, dy, nxBytes, nValueBytes; if( Stream.is_Open() && is_Valid() ) { Set_File_Type(GRID_FILE_FORMAT_Binary); if( bFlip ) { y = Get_NY() - 1; dy = -1; } else { y = 0; dy = 1; } //------------------------------------------------- if( File_Type == SG_DATATYPE_Bit ) { nxBytes = Get_NX() / 8 + 1; if( m_Type == File_Type && m_Memory_Type == GRID_MEMORY_Normal ) { for(iy=0; iy<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy) { Stream.Read(m_Values[y], sizeof(char), nxBytes); } } else { Line = (char *)SG_Malloc(nxBytes); for(iy=0; iy<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy) { Stream.Read(Line, sizeof(char), nxBytes); for(x=0, pValue=Line; x<Get_NX(); pValue++) { for(i=0; i<8 && x<Get_NX(); i++, x++) { Set_Value(x, y, (*pValue & m_Bitmask[i]) == 0 ? 0.0 : 1.0); } } } SG_Free(Line); } } //------------------------------------------------- else { nValueBytes = SG_Data_Type_Get_Size(File_Type); nxBytes = Get_NX() * nValueBytes; if( m_Type == File_Type && m_Memory_Type == GRID_MEMORY_Normal && !bSwapBytes ) { for(iy=0; iy<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy) { Stream.Read(m_Values[y], sizeof(char), nxBytes); } } else { Line = (char *)SG_Malloc(nxBytes); for(iy=0; iy<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy) { Stream.Read(Line, sizeof(char), nxBytes); for(x=0, pValue=Line; x<Get_NX(); x++, pValue+=nValueBytes) { if( bSwapBytes ) { _Swap_Bytes(pValue, nValueBytes); } switch( File_Type ) { default: break; case SG_DATATYPE_Byte: Set_Value(x, y, *(BYTE *)pValue); break; case SG_DATATYPE_Char: Set_Value(x, y, *(char *)pValue); break; case SG_DATATYPE_Word: Set_Value(x, y, *(WORD *)pValue); break; case SG_DATATYPE_Short: Set_Value(x, y, *(short *)pValue); break; case SG_DATATYPE_DWord: Set_Value(x, y, *(DWORD *)pValue); break; case SG_DATATYPE_Int: Set_Value(x, y, *(int *)pValue); break; case SG_DATATYPE_Float: Set_Value(x, y, *(float *)pValue); break; case SG_DATATYPE_Double: Set_Value(x, y, *(double *)pValue); break; } } } SG_Free(Line); } } //------------------------------------------------- SG_UI_Process_Set_Ready(); return( true ); } return( false ); }
//--------------------------------------------------------- bool CSG_Grid::_Load_Surfer(const CSG_String &File_Name, TSG_Grid_Memory_Type Memory_Type) { bool bResult = false; char Identifier[4]; short sValue; int x, y, NX, NY; float *fLine; double dValue, xMin, yMin, Cellsize; CSG_File Stream; if( Stream.Open(File_Name, SG_FILE_R, true) ) { Stream.Read(Identifier, sizeof(char), 4); //------------------------------------------------- // Binary... if( !strncmp(Identifier, "DSBB", 4) ) { Stream.Read(&sValue , sizeof(short)); NX = sValue; Stream.Read(&sValue , sizeof(short)); NY = sValue; Stream.Read(&xMin , sizeof(double)); Stream.Read(&dValue , sizeof(double)); // XMax Cellsize = (dValue - xMin) / (NX - 1.0); Stream.Read(&yMin , sizeof(double)); Stream.Read(&dValue , sizeof(double)); // YMax... //DY = (dValue - yMin) / (NY - 1.0); // we could check, if cellsizes (x/y) equal... Stream.Read(&dValue , sizeof(double)); // ZMin... Stream.Read(&dValue , sizeof(double)); // ZMax... //--------------------------------------------- if( !Stream.is_EOF() && Create(SG_DATATYPE_Float, NX, NY, Cellsize, xMin, yMin, Memory_Type) ) { bResult = true; fLine = (float *)SG_Malloc(Get_NX() * sizeof(float)); for(y=0; y<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { Stream.Read(fLine, sizeof(float), Get_NX()); for(x=0; x<Get_NX(); x++) { Set_Value(x, y, fLine[x]); } } SG_Free(fLine); } } //------------------------------------------------- // ASCII... else if( !strncmp(Identifier, "DSAA", 4) ) { SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%d %d") , &NX , &NY); SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%lf %lf"), &xMin , &dValue); Cellsize = (dValue - xMin) / (NX - 1.0); SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%lf %lf"), &yMin , &dValue); //DY = (dValue - yMin) / (NY - 1.0); SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%lf %lf"), &dValue, &dValue); //--------------------------------------------- if( !Stream.is_EOF() && Create(SG_DATATYPE_Float, NX, NY, Cellsize, xMin, yMin, Memory_Type) ) { bResult = true; for(y=0; y<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(x=0; x<Get_NX(); x++) { SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%lf"), &dValue); Set_Value(x, y, dValue); } } } } //------------------------------------------------- SG_UI_Process_Set_Ready(); } return( bResult ); }
//--------------------------------------------------------- bool CSG_Table::_Load_DBase(const CSG_String &File_Name) { int iField; CSG_Table_DBase dbf; CSG_Table_Record *pRecord; //----------------------------------------------------- if( dbf.Open(File_Name) ) { Destroy(); for(iField=0; iField<dbf.Get_FieldCount(); iField++) { TSG_Data_Type Type; switch( dbf.Get_FieldType(iField) ) { case DBF_FT_LOGICAL: Type = SG_DATATYPE_Char; break; case DBF_FT_CHARACTER: default: Type = SG_DATATYPE_String; break; case DBF_FT_DATE: Type = SG_DATATYPE_Date; break; case DBF_FT_NUMERIC: Type = dbf.Get_FieldDecimals(iField) > 0 ? SG_DATATYPE_Double : SG_DATATYPE_Long; break; } Add_Field(SG_STR_MBTOSG(dbf.Get_FieldName(iField)), Type); } //------------------------------------------------- if( dbf.Move_First() && dbf.Get_Record_Count() > 0 ) { m_nRecords = m_nBuffer = dbf.Get_Record_Count(); m_Records = (CSG_Table_Record **)SG_Malloc(m_nRecords * sizeof(CSG_Table_Record *)); for(int iRecord=0; iRecord<m_nRecords && SG_UI_Process_Set_Progress(iRecord, m_nRecords); iRecord++) { m_Records[iRecord] = pRecord = _Get_New_Record(iRecord); for(iField=0; iField<Get_Field_Count(); iField++) { switch( Get_Field_Type(iField) ) { case SG_DATATYPE_Char: pRecord->Set_Value(iField, SG_STR_MBTOSG(dbf.asString(iField)) ); break; case SG_DATATYPE_String: default: pRecord->Set_Value(iField, SG_STR_MBTOSG(dbf.asString(iField)) ); break; case SG_DATATYPE_Date: pRecord->Set_Value(iField, dbf.asDouble(iField) ); break; case SG_DATATYPE_Long: pRecord->Set_Value(iField, dbf.asInt(iField) ); break; case SG_DATATYPE_Double: pRecord->Set_Value(iField, dbf.asDouble(iField) ); break; } } dbf.Move_Next(); } SG_UI_Process_Set_Ready(); Set_Modified(false); Set_Update_Flag(); _Stats_Invalidate(); } return( true ); } return( false ); }
//--------------------------------------------------------- bool CSG_Table::_Save_DBase(const CSG_String &File_Name) { int iField, iRecord, nBytes; CSG_Table_DBase dbf; //----------------------------------------------------- CSG_Table_DBase::TFieldDesc *dbfFields = new CSG_Table_DBase::TFieldDesc[Get_Field_Count()]; for(iField=0; iField<Get_Field_Count(); iField++) { strncpy(dbfFields[iField].Name, SG_STR_SGTOMB(Get_Field_Name(iField)), 11); switch( Get_Field_Type(iField) ) { case SG_DATATYPE_String: default: dbfFields[iField].Type = DBF_FT_CHARACTER; dbfFields[iField].Width = (BYTE)((nBytes = Get_Field_Length(iField)) > 255 ? 255 : nBytes); break; case SG_DATATYPE_Date: dbfFields[iField].Type = DBF_FT_DATE; dbfFields[iField].Width = (BYTE)8; break; case SG_DATATYPE_Char: dbfFields[iField].Type = DBF_FT_CHARACTER; dbfFields[iField].Width = (BYTE)1; break; case SG_DATATYPE_Short: case SG_DATATYPE_Int: case SG_DATATYPE_Long: case SG_DATATYPE_Color: dbfFields[iField].Type = DBF_FT_NUMERIC; dbfFields[iField].Width = (BYTE)16; dbfFields[iField].Decimals = (BYTE)0; break; case SG_DATATYPE_Float: case SG_DATATYPE_Double: dbfFields[iField].Type = DBF_FT_NUMERIC; dbfFields[iField].Width = (BYTE)16; dbfFields[iField].Decimals = (BYTE)8; break; } } if( !dbf.Open(File_Name, Get_Field_Count(), dbfFields) ) { delete[](dbfFields); SG_UI_Msg_Add_Error(LNG("[ERR] dbase file could not be opened")); return( false ); } delete[](dbfFields); //----------------------------------------------------- for(iRecord=0; iRecord<Get_Record_Count() && SG_UI_Process_Set_Progress(iRecord, Get_Record_Count()); iRecord++) { CSG_Table_Record *pRecord = Get_Record(iRecord); dbf.Add_Record(); for(iField=0; iField<Get_Field_Count(); iField++) { switch( dbf.Get_FieldType(iField) ) { case DBF_FT_DATE: case DBF_FT_CHARACTER: dbf.Set_Value(iField, SG_STR_SGTOMB(pRecord->asString(iField))); break; case DBF_FT_NUMERIC: dbf.Set_Value(iField, pRecord->asDouble(iField)); break; } } dbf.Flush_Record(); } SG_UI_Process_Set_Ready(); return( true ); }
//--------------------------------------------------------- bool CSG_Grid::_Save_Binary(CSG_File &Stream, int xA, int yA, int xN, int yN, TSG_Data_Type File_Type, bool bFlip, bool bSwapBytes) { char *Line, *pValue; int x, y, i, ix, iy, dy, axBytes, nxBytes, nValueBytes; //----------------------------------------------------- if( Stream.is_Open() && m_System.is_Valid() && m_Type != SG_DATATYPE_Undefined ) { Set_File_Type(GRID_FILE_FORMAT_Binary); if( bFlip ) { y = yA + yN - 1; dy = -1; } else { y = yA; dy = 1; } //------------------------------------------------- if( File_Type == SG_DATATYPE_Bit ) { nxBytes = xN / 8 + 1; if( m_Type == File_Type && m_Memory_Type == GRID_MEMORY_Normal && xA % 8 == 0 ) { axBytes = xA / 8; for(iy=0; iy<yN && SG_UI_Process_Set_Progress(iy, yN); iy++, y+=dy) { Stream.Write((char *)m_Values[y] + axBytes, sizeof(char), nxBytes); } } else { Line = (char *)SG_Malloc(nxBytes); for(iy=0; iy<yN && SG_UI_Process_Set_Progress(iy, yN); iy++, y+=dy) { for(ix=0, x=xA, pValue=Line; ix<xN; pValue++) { for(i=0; i<8 && ix<xN; i++, ix++, x++) { *pValue = asChar(x, y) != 0.0 ? *pValue | m_Bitmask[i] : *pValue & (~m_Bitmask[i]); } } Stream.Write(Line, sizeof(char), nxBytes); } SG_Free(Line); } } //------------------------------------------------- else { nValueBytes = SG_Data_Type_Get_Size(File_Type); nxBytes = xN * nValueBytes; if( m_Type == File_Type && m_Memory_Type == GRID_MEMORY_Normal && !bSwapBytes ) { axBytes = xA * nValueBytes; for(iy=0; iy<yN && SG_UI_Process_Set_Progress(iy, yN); iy++, y+=dy) { Stream.Write((char *)m_Values[y] + axBytes, sizeof(char), nxBytes); } } else { Line = (char *)SG_Malloc(nxBytes); for(iy=0; iy<yN && SG_UI_Process_Set_Progress(iy, yN); iy++, y+=dy) { for(ix=0, x=xA, pValue=Line; ix<xN; ix++, x++, pValue+=nValueBytes) { switch( File_Type ) { default: break; case SG_DATATYPE_Byte: *(BYTE *)pValue = asChar (x, y); break; case SG_DATATYPE_Char: *(char *)pValue = asChar (x, y); break; case SG_DATATYPE_Word: *(WORD *)pValue = asShort (x, y); break; case SG_DATATYPE_Short: *(short *)pValue = asShort (x, y); break; case SG_DATATYPE_DWord: *(DWORD *)pValue = asInt (x, y); break; case SG_DATATYPE_Int: *(int *)pValue = asInt (x, y); break; case SG_DATATYPE_Float: *(float *)pValue = asFloat (x, y); break; case SG_DATATYPE_Double: *(double *)pValue = asDouble (x, y); break; } if( bSwapBytes ) { _Swap_Bytes(pValue, nValueBytes); } } Stream.Write(Line, sizeof(char), nxBytes); } SG_Free(Line); } } //------------------------------------------------- SG_UI_Process_Set_Ready(); return( true ); } return( false ); }