//--------------------------------------------------------- bool CSG_MetaData::Load(CSG_File &File) { Destroy(); wxXmlDocument XML; if( File.is_Reading() && XML.Load(*((wxInputStream *)File.Get_Stream())) ) { _Load(XML.GetRoot()); return( true ); } return( false ); }
//--------------------------------------------------------- bool CSG_MetaData::Load(CSG_File &File) { Destroy(); wxXmlDocument XML; wxFFileInputStream Stream(File.Get_Stream()); if( Stream.IsOk() && XML.Load(Stream) ) { _Load(XML.GetRoot()); return( true ); } return( false ); }
//--------------------------------------------------------- bool CSG_MetaData::Save(CSG_File &File) const { wxXmlDocument XML; wxXmlNode *pRoot = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, Get_Name().c_str()); XML.SetRoot(pRoot); _Save(pRoot); if( File.is_Writing() && XML.Save(*((wxOutputStream *)File.Get_Stream())) ) { return( true ); } return( false ); }
//--------------------------------------------------------- bool CSG_MetaData::Save(CSG_File &File) const { wxXmlDocument XML; wxXmlNode *pRoot = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, Get_Name().c_str()); XML.SetRoot(pRoot); _Save(pRoot); wxFFileOutputStream Stream(File.Get_Stream()); if( Stream.IsOk() && XML.Save(Stream) ) { return( true ); } return( false ); }
//--------------------------------------------------------- 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 CGrid_Import::On_Execute(void) { bool bTransform; int x, y, yy, Method; double ax, ay, dx, dy, rx, ry, xMin, yMin, Cellsize; CSG_Colors Colors; CSG_String fImage, fWorld, Name; CSG_Grid *pImage; CSG_File Stream; wxImage Image; wxImageHistogram Histogram; //----------------------------------------------------- fImage = Parameters("FILE") ->asString(); Method = Parameters("METHOD") ->asInt(); Name = SG_File_Get_Name(fImage, false); //----------------------------------------------------- wxImageHandler *pImgHandler = NULL; if( !SG_UI_Get_Window_Main() ) { CSG_String fName = SG_File_Get_Name(fImage, true); if( SG_File_Cmp_Extension(fName, SG_T("jpg")) ) pImgHandler = new wxJPEGHandler; else if( SG_File_Cmp_Extension(fName, SG_T("pcx")) ) pImgHandler = new wxPCXHandler; else if( SG_File_Cmp_Extension(fName, SG_T("tif")) ) pImgHandler = new wxTIFFHandler; else if( SG_File_Cmp_Extension(fName, SG_T("gif")) ) pImgHandler = new wxGIFHandler; else if( SG_File_Cmp_Extension(fName, SG_T("pnm")) ) pImgHandler = new wxPNMHandler; else if( SG_File_Cmp_Extension(fName, SG_T("xpm")) ) pImgHandler = new wxXPMHandler; #ifdef _SAGA_MSW else if( SG_File_Cmp_Extension(fName, SG_T("bmp")) ) pImgHandler = new wxBMPHandler; #endif else // if( SG_File_Cmp_Extension(fName, SG_T("png")) ) pImgHandler = new wxPNGHandler; wxImage::AddHandler(pImgHandler); } if( !Image.LoadFile(fImage.c_str()) ) { return( false ); } //----------------------------------------------------- if( SG_File_Cmp_Extension(fImage, SG_T("bmp")) ) { fWorld = SG_File_Make_Path(NULL, fImage, SG_T("bpw")); } else if( SG_File_Cmp_Extension(fImage, SG_T("jpg")) ) { fWorld = SG_File_Make_Path(NULL, fImage, SG_T("jgw")); } else if( SG_File_Cmp_Extension(fImage, SG_T("png")) ) { fWorld = SG_File_Make_Path(NULL, fImage, SG_T("pgw")); } else if( SG_File_Cmp_Extension(fImage, SG_T("tif")) ) { fWorld = SG_File_Make_Path(NULL, fImage, SG_T("tfw")); } else { fWorld = SG_File_Make_Path(NULL, fImage, SG_T("world")); } bTransform = false; xMin = 0.0; yMin = 0.0; Cellsize = 1.0; if( Stream.Open(fWorld, SG_FILE_R, false) && fscanf(Stream.Get_Stream(), "%lf %lf %lf %lf %lf %lf ", &dx, &ry, &rx, &dy, &ax, &ay) == 6 ) { if( dx != -dy || rx != 0.0 || ry != 0.0 ) { bTransform = true; } else { xMin = ax; yMin = ay + dy * (Image.GetHeight() - 1); Cellsize = dx; } } //----------------------------------------------------- // color look-up table... if( Method == 0 && (yy = Image.ComputeHistogram(Histogram)) <= 256 ) { Colors.Set_Count(yy); for(wxImageHistogram::iterator i=Histogram.begin(); i!=Histogram.end(); ++i) { Colors.Set_Color(i->second.index, SG_GET_R(i->first), SG_GET_G(i->first), SG_GET_B(i->first)); } pImage = SG_Create_Grid(yy <= 2 ? SG_DATATYPE_Bit : SG_DATATYPE_Byte, Image.GetWidth(), Image.GetHeight(), Cellsize, xMin, yMin); for(y=0; y<pImage->Get_NY() && Set_Progress(y, pImage->Get_NY()); y++) { yy = bTransform ? y : pImage->Get_NY() - 1 - y; for(x=0; x<pImage->Get_NX(); x++) { pImage->Set_Value(x, y, Histogram[SG_GET_RGB(Image.GetRed(x, yy), Image.GetGreen(x, yy), Image.GetBlue(x, yy))].index); } } if( bTransform ) { Set_Transformation(&pImage, ax, ay, dx, dy, rx, ry); } pImage->Set_Name(Name); pImage->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj"))); Parameters("OUT_GRID")->Set_Value(pImage); DataObject_Set_Colors(pImage, Colors); DataObject_Update(pImage, 0, Colors.Get_Count() - 1); } //----------------------------------------------------- else // true color... { pImage = SG_Create_Grid(SG_DATATYPE_Int, Image.GetWidth(), Image.GetHeight(), Cellsize, xMin, yMin); pImage ->Set_Name(Name); for(y=0; y<pImage->Get_NY() && Set_Progress(y, pImage->Get_NY()); y++) { yy = bTransform ? y : pImage->Get_NY() - 1 - y; for(x=0; x<pImage->Get_NX(); x++) { pImage->Set_Value(x, y, SG_GET_RGB(Image.GetRed(x, yy), Image.GetGreen(x, yy), Image.GetBlue(x, yy))); } } if( bTransform ) { Set_Transformation(&pImage, ax, ay, dx, dy, rx, ry); } //------------------------------------------------- if( Method != 1 ) // true color... { pImage->Get_Projection().Load(fImage, SG_PROJ_FMT_WKT); pImage->Set_Name(Name); pImage->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj"))); Parameters("OUT_GRID")->Set_Value(pImage); DataObject_Set_Colors(pImage, 100, SG_COLORS_BLACK_WHITE); DataObject_Set_Parameter(pImage, "COLORS_TYPE", 6); // Color Classification Type: RGB } //------------------------------------------------- else // split channels... { CSG_Grid *pR, *pG, *pB; pR = SG_Create_Grid(pImage->Get_System(), SG_DATATYPE_Byte); pG = SG_Create_Grid(pImage->Get_System(), SG_DATATYPE_Byte); pB = SG_Create_Grid(pImage->Get_System(), SG_DATATYPE_Byte); for(y=0; y<pImage->Get_NY() && Set_Progress(y, pImage->Get_NY()); y++) { for(x=0; x<pImage->Get_NX(); x++) { pR->Set_Value(x, y, SG_GET_R(pImage->asInt(x, y))); pG->Set_Value(x, y, SG_GET_G(pImage->asInt(x, y))); pB->Set_Value(x, y, SG_GET_B(pImage->asInt(x, y))); } } pR->Get_Projection().Load(fImage, SG_PROJ_FMT_WKT); pG->Get_Projection().Load(fImage, SG_PROJ_FMT_WKT); pB->Get_Projection().Load(fImage, SG_PROJ_FMT_WKT); pR->Set_Name(CSG_String::Format(SG_T("%s [R]"), Name.c_str())); pG->Set_Name(CSG_String::Format(SG_T("%s [G]"), Name.c_str())); pB->Set_Name(CSG_String::Format(SG_T("%s [B]"), Name.c_str())); pR->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj"))); pG->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj"))); pB->Get_Projection().Load(SG_File_Make_Path(NULL, fImage, SG_T("prj"))); Parameters("OUT_RED") ->Set_Value(pR); Parameters("OUT_GREEN") ->Set_Value(pG); Parameters("OUT_BLUE") ->Set_Value(pB); DataObject_Set_Colors(pR, 100, SG_COLORS_BLACK_RED); DataObject_Set_Colors(pG, 100, SG_COLORS_BLACK_GREEN); DataObject_Set_Colors(pB, 100, SG_COLORS_BLACK_BLUE); } } //----------------------------------------------------- if( !SG_UI_Get_Window_Main() && pImgHandler != NULL) { wxImage::RemoveHandler(pImgHandler->GetName()); } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- 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 ); }