//--------------------------------------------------------- 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 ); }
void Cdodproperror::DisplayFile(CSG_String path) { if (SG_File_Exists(path)) { CSG_File File; if (File.Open(path, SG_FILE_R, false)) { CSG_String Line; while (! File.is_EOF() && File.Read_Line(Line)) { Message_Add(Line); } File.Close(); } else { Message_Add(CSG_String("Unable to open " + path + CSG_String(" for reading"))); } } else { Message_Add(CSG_String("File '" + path + CSG_String("' does not exist"))); } }
//--------------------------------------------------------- bool CSTL_Import::Get_Extent(CSG_File &Stream, CSG_Rect &Extent, int nFacettes) { float xMin = 1, xMax = 0, yMin, yMax; for(int iFacette=0; iFacette<nFacettes && !Stream.is_EOF() && Set_Progress(iFacette, nFacettes); iFacette++) { TSTL_Point p[3]; if( Read_Facette(Stream, p) ) { if( iFacette == 0 ) { xMin = xMax = p[0].x; yMin = yMax = p[0].y; } for(int i=0; i<3; i++) { if( xMin > p[i].x ) { xMin = p[i].x; } else if( xMax < p[i].x ) { xMax = p[i].x; } if( yMin > p[i].y ) { yMin = p[i].y; } else if( yMax < p[i].y ) { yMax = p[i].y; } } } } Extent.Assign(xMin, yMin, xMax, yMax); return( xMin < xMax && yMin < yMax && Stream.Seek(80 + sizeof(nFacettes)) ); }
//--------------------------------------------------------- bool CWKSP_Project::_Compatibility_Load_Map(CSG_File &Stream, const wxString &ProjectDir) { TSG_Rect r; CSG_String sLine, sName; CWKSP_Base_Item *pItem; CWKSP_Map *pMap; while( Stream.Read_Line(sLine) && sLine.Cmp(MAP_ENTRY_BEGIN) && sLine.Cmp(MAP_ENTRIES_END) ); if( !sLine.Cmp(MAP_ENTRY_BEGIN) && Stream.Read_Line(sLine) ) { if( SG_SSCANF(sLine, SG_T("%lf %lf %lf %lf"), &r.xMin, &r.xMax, &r.yMin, &r.yMax) == 4 ) { pMap = NULL; while( Stream.Read_Line(sLine) && sLine.Cmp(MAP_ENTRY_END) ) { if( !sLine.Cmp(MAP_ENTRY_NAME) ) { Stream.Read_Line(sName); } else { sLine = Get_FilePath_Absolute(ProjectDir, sLine.w_str()).wc_str(); if( (pItem = _Get_byFileName(sLine.c_str())) != NULL && ( pItem->Get_Type() == WKSP_ITEM_Grid || pItem->Get_Type() == WKSP_ITEM_Grids || pItem->Get_Type() == WKSP_ITEM_TIN || pItem->Get_Type() == WKSP_ITEM_PointCloud || pItem->Get_Type() == WKSP_ITEM_Shapes) ) { if( pMap == NULL ) { pMap = new CWKSP_Map; } g_pMaps->Add((CWKSP_Layer *)pItem, pMap); } } } if( pMap ) { if( sName.Length() > 0 ) { pMap->Get_Parameter("NAME")->Set_Value(sName); pMap->Parameters_Changed(); } pMap->Set_Extent(r, true); pMap->View_Show(true); } } return( true ); } return( false ); }
//--------------------------------------------------------- bool CWKSP_Project::_Compatibility_Load_Data(CSG_File &Stream, const wxString &ProjectDir) { int Type; CSG_String sLine, sPath; CWKSP_Base_Item *pItem; while( Stream.Read_Line(sLine) && sLine.Cmp(DATA_ENTRY_BEGIN) && sLine.Cmp(DATA_ENTRIES_END) ); if( !sLine.Cmp(DATA_ENTRY_BEGIN) ) { if( Stream.Read_Line(sPath) && Stream.Read_Line(sLine) && sLine.asInt(Type) ) { if( SG_File_Exists(sPath = Get_FilePath_Absolute(ProjectDir, sPath.w_str()).wc_str()) ) { if( (pItem = g_pData->Open(sPath.w_str(), Type)) != NULL ) { if( pItem->Get_Parameters() ) { pItem->Get_Parameters()->Serialize_Compatibility(Stream); _Compatibility_Data((TSG_Data_Type)Type, pItem->Get_Parameters(), ""); pItem->Parameters_Changed(); } } } } return( true ); } return( false ); }
//--------------------------------------------------------- bool CSG_MetaData::Load_JSON(const CSG_String &File) { CSG_File Stream; CSG_String JSON; if( Stream.Open(File, SG_FILE_R, false) && Stream.Read(JSON, (size_t)Stream.Length()) > 0 ) { return( from_JSON(JSON) ); } return( false ); }
//--------------------------------------------------------- int CTL_Extract::Read_File(const SG_Char *File, CSG_Table &Elements) { //----------------------------------------------------- CSG_File Stream; if( !Stream.Open(File, SG_FILE_R, false) ) { return( 0 ); } Process_Set_Text(CSG_String::Format("%s: %s", SG_T("scanning"), File)); //----------------------------------------------------- CSG_String String, Text; if( !Stream.Read(String, Stream.Length()) ) { return( 0 ); } //----------------------------------------------------- const SG_Char Function[2][4] = { SG_T("_TL"), SG_T("_TW") }; const SG_Char *p = String; bool bLong = Parameters("LONG")->asBool(); while( *p != '\0' ) { if( !(p[0] == Function[0][0] && p[1] == Function[0][1] && p[2] == Function[0][2]) && !(p[0] == Function[1][0] && p[1] == Function[1][1] && p[2] == Function[1][2] && bLong) ) { p ++; } else { p += Read_Text(p, Text); if( Text.Length() > 0 ) { CSG_Table_Record *pRecord = Elements.Add_Record(); pRecord->Set_Value(0, Text); pRecord->Set_Value(1, File); } } } return( 1 ); }
//--------------------------------------------------------- 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 ); }
//--------------------------------------------------------- int CSG_Grid::_Load_Native_Get_Key(CSG_File &Stream, CSG_String &Value) { int i; CSG_String sLine; if( Stream.Read_Line(sLine) && (i = sLine.Find('=')) > 0 ) { Value = sLine.AfterFirst('='); Value.Trim(); sLine.Remove(i); for(i=0; i<GRID_FILE_KEY_Count; i++) { CSG_String s(gSG_Grid_File_Key_Names[i]); if( s.Find(sLine.Left(s.Length())) >= 0 ) { return( i ); } } } return( -1 ); }
//--------------------------------------------------------- inline bool CXYZ_Import::Read_Values(CSG_File &Stream, double &x, double &y, double &z) { CSG_String sLine; if( Stream.Read_Line(sLine) ) { sLine.Trim(); if( sLine.asDouble(x) ) { sLine = sLine.AfterFirst(m_Separator); sLine.Trim(); if( sLine.asDouble(y) ) { sLine = sLine.AfterFirst(m_Separator); if( sLine.asDouble(z) ) { 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_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 ); }
//--------------------------------------------------------- inline bool CSTL_Import::Read_Facette(CSG_File &Stream, TSTL_Point p[3]) { WORD Attribute; if( Stream.Read(p + 0, sizeof(TSTL_Point)) && Stream.Read(p + 0, sizeof(TSTL_Point)) && Stream.Read(p + 1, sizeof(TSTL_Point)) && Stream.Read(p + 2, sizeof(TSTL_Point)) && Stream.Read(&Attribute, sizeof(Attribute)) ) { Rotate(p[0]); Rotate(p[1]); Rotate(p[2]); return( true ); } return( false ); }
//--------------------------------------------------------- bool CWKSP_Project::_Compatibility_Load_Data(const wxString &FileName) { CSG_String sLine; CSG_File Stream; if( !Stream.Open(&FileName, SG_FILE_R, true) ) { return( false ); } //------------------------------------------------- while( Stream.Read_Line(sLine) && sLine.Cmp(DATA_ENTRIES_BEGIN) ); if( sLine.Cmp(DATA_ENTRIES_BEGIN) ) { return( false ); } g_pSAGA_Frame->Freeze(); g_pData->Get_Menu_Files()->Set_Update(false); while( _Compatibility_Load_Data(Stream, SG_File_Get_Path(&FileName).w_str()) ); g_pData->Get_Menu_Files()->Set_Update(true); //------------------------------------------------- while( Stream.Read_Line(sLine) && sLine.Cmp(MAP_ENTRIES_BEGIN) ); if( !sLine.Cmp(MAP_ENTRIES_BEGIN) ) { while( _Compatibility_Load_Map(Stream, SG_File_Get_Path(&FileName).w_str()) ); } switch( g_pData->Get_Parameter("PROJECT_MAP_ARRANGE")->asInt() ) { case 1: g_pSAGA_Frame->Tile(wxHORIZONTAL); break; case 2: g_pSAGA_Frame->Tile(wxVERTICAL ); break; } g_pSAGA_Frame->Thaw(); return( true ); }
//--------------------------------------------------------- bool CSurfer_BLN_Export::On_Execute(void) { //----------------------------------------------------- CSG_File Stream; if( !Stream.Open(Parameters("FILE")->asString(), SG_FILE_W) ) { return( false ); } CSG_Shapes *pShapes = Parameters("SHAPES")->asShapes(); if( !pShapes->is_Valid() || pShapes->Get_Count() <= 0 ) { return( false ); } //----------------------------------------------------- int iName = Parameters("BNAME" )->asBool() ? Parameters("NAME")->asInt() : -1; int iDesc = Parameters("BDESC" )->asBool() ? Parameters("DESC")->asInt() : -1; int iZVal = Parameters("BZVAL" )->asBool() ? Parameters("ZVAL")->asInt() : -1; int Flag = 1; //----------------------------------------------------- for(int iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shape *pShape = pShapes->Get_Shape(iShape); for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { Stream.Printf(SG_T("%d,%d"), pShape->Get_Point_Count(iPart), Flag); if( iName >= 0 ) { Stream.Printf(SG_T(",\"%s\""), pShape->asString(iName)); } if( iDesc >= 0 ) { Stream.Printf(SG_T(",\"%s\""), pShape->asString(iDesc)); } Stream.Printf(SG_T("\n")); for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++) { TSG_Point p = pShape->Get_Point(iPoint, iPart); if( iZVal >= 0 ) { Stream.Printf(SG_T("%f,%f,%f\n"), p.x, p.y, pShape->asDouble(iZVal)); } else { Stream.Printf(SG_T("%f,%f\n" ), p.x, p.y); } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CXYZ_Export::On_Execute(void) { bool bExNoData; int x, y, i; TSG_Point p; CSG_File Stream; CSG_String FileName; CSG_Parameter_Grid_List *pGrids; pGrids = Parameters("GRIDS") ->asGridList(); FileName = Parameters("FILENAME")->asString(); bExNoData = Parameters("EX_NODATA")->asBool(); if( pGrids->Get_Count() > 0 && Stream.Open(FileName, SG_FILE_W, false) ) { if( Parameters("CAPTION")->asBool() ) { Stream.Printf(SG_T("\"X\"\t\"Y\"")); for(i=0; i<pGrids->Get_Count(); i++) { Stream.Printf(SG_T("\t\"%s\""), pGrids->asGrid(i)->Get_Name()); } Stream.Printf(SG_T("\n")); } for(y=0, p.y=Get_YMin(); y<Get_NY() && Set_Progress(y); y++, p.y+=Get_Cellsize()) { for(x=0, p.x=Get_XMin(); x<Get_NX(); x++, p.x+=Get_Cellsize()) { if( !bExNoData || (bExNoData && !pGrids->asGrid(0)->is_NoData(x, y)) ) { Stream.Printf(SG_T("%f\t%f"), p.x, p.y); for(i=0; i<pGrids->Get_Count(); i++) { Stream.Printf(SG_T("\t%f"), pGrids->asGrid(i)->asDouble(x, y)); } Stream.Printf(SG_T("\n")); } } } return( true ); } return( false ); }
//--------------------------------------------------------- bool CSG_CURL::Request(const CSG_String &Request, const SG_Char *File) { if( !is_Connected() ) { return( false ); } CSG_File Answer; if( !Answer.Open(File, SG_FILE_W) ) { m_Error.Printf("%s [%s]", _TL("Failed to open file"), File); return( false ); } CSG_String URL = m_Server + "/" + Request; CURL_SET_OPT(1, CURLOPT_URL , URL.b_str()); CURL_SET_OPT(1, CURLOPT_FOLLOWLOCATION, 1L); CURL_SET_OPT(1, CURLOPT_WRITEFUNCTION , _Callback_Write_File); CURL_SET_OPT(1, CURLOPT_WRITEDATA , &Answer); return( _Perform() ); }
//--------------------------------------------------------- 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 CXYZ_Import::On_Execute(void) { CSG_File Stream; if( !Stream.Open(Parameters("FILENAME")->asString(), SG_FILE_R) ) { Error_Set(_TL("file could not be opened")); return( false ); } //----------------------------------------------------- CSG_Shapes *pPoints = Parameters("POINTS")->asShapes(); pPoints->Create(SHAPE_TYPE_Point, SG_File_Get_Name(Parameters("FILENAME")->asString(), false)); pPoints->Add_Field("Z", SG_DATATYPE_Double); //----------------------------------------------------- if( Parameters("HEADLINE")->asBool() ) { CSG_String sLine; if( !Stream.Read_Line(sLine) ) { Error_Set(_TL("could not read headline")); return( false ); } } //----------------------------------------------------- sLong Length = Stream.Length(); double x, y, z; while( Stream.Scan(x) && Stream.Scan(y) && Stream.Scan(z) && Set_Progress((double)Stream.Tell(), (double)Length) ) { CSG_Shape *pPoint = pPoints->Add_Shape(); pPoint->Add_Point(x, y); pPoint->Set_Value(0, z); } return( pPoints->Get_Count() > 0 ); }
//--------------------------------------------------------- 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 ); }
//--------------------------------------------------------- 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); wxFFileOutputStream Stream(File.Get_Stream()); if( Stream.IsOk() && XML.Save(Stream) ) { 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++) { 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 ); }
//--------------------------------------------------------- bool CCMD_Module::_Get_Parameters(CSG_Parameters *pParameters) { if( !pParameters || m_CMD.Parse(false) != 0 ) { return( false ); } //----------------------------------------------------- int i; for(i=0; i<pParameters->Get_Count(); i++) { CSG_Parameter *pParameter = pParameters->Get_Parameter(i); if( pParameter->is_Input() ) { // nop now, loading options first } else if( pParameter->is_Output() ) { if( pParameter->is_DataObject() && pParameter->is_Optional() && !pParameter->asDataObject() && m_CMD.Found(_Get_ID(pParameter)) ) { pParameter->Set_Value(DATAOBJECT_CREATE); } } else if( pParameter->is_Option() && !pParameter->is_Information() ) { long l; double d; wxString s; switch( pParameter->Get_Type() ) { default: break; case PARAMETER_TYPE_Parameters: _Get_Parameters(pParameter->asParameters()); break; case PARAMETER_TYPE_Bool: pParameter->Set_Value(m_CMD.Found(_Get_ID(pParameter)) ? 1 : 0); break; case PARAMETER_TYPE_Int: if( m_CMD.Found(_Get_ID(pParameter), &l) ) { pParameter->Set_Value((int)l); } break; case PARAMETER_TYPE_Choice: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { if( s.ToLong(&l) ) { pParameter->Set_Value((int)l); } else { pParameter->Set_Value(CSG_String(&s)); } } break; case PARAMETER_TYPE_Double: case PARAMETER_TYPE_Degree: if( m_CMD.Found(_Get_ID(pParameter), &s) && s.ToDouble(&d) ) { pParameter->Set_Value(d); } break; case PARAMETER_TYPE_Range: if( m_CMD.Found(_Get_ID(pParameter, wxT("MIN")), &s) && s.ToDouble(&d) ) { pParameter->asRange()->Set_LoVal(d); } if( m_CMD.Found(_Get_ID(pParameter, wxT("MAX")), &s) && s.ToDouble(&d) ) { pParameter->asRange()->Set_HiVal(d); } break; case PARAMETER_TYPE_String: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { pParameter->Set_Value(CSG_String(&s)); } break; case PARAMETER_TYPE_Text: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { CSG_File Stream; if( Stream.Open(CSG_String(&s)) ) { CSG_String t; Stream.Read(t, Stream.Length()); pParameter->Set_Value(t.c_str()); } else { pParameter->Set_Value(CSG_String(&s)); } } break; case PARAMETER_TYPE_FilePath: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { if( pParameter->asFilePath()->is_Multiple() ) { s.Prepend(wxT("\"")); s.Replace(wxT(";"), wxT("\" \"")); s.Append (wxT("\"")); } pParameter->Set_Value(CSG_String(&s)); } break; case PARAMETER_TYPE_FixedTable: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { CSG_Table Table(&s); pParameter->asTable()->Assign_Values(&Table); } break; case PARAMETER_TYPE_Grid_System: if( pParameter->Get_Children_Count() == 0 ) { long nx, ny; double d, x, y; if( !m_CMD.Found(_Get_ID(pParameter, wxT("NX")), &nx) || !m_CMD.Found(_Get_ID(pParameter, wxT("NY")), &ny) || !m_CMD.Found(_Get_ID(pParameter, wxT( "X")), &s) || !s.ToDouble(&x) || !m_CMD.Found(_Get_ID(pParameter, wxT( "Y")), &s) || !s.ToDouble(&y) || !m_CMD.Found(_Get_ID(pParameter, wxT( "D")), &s) || !s.ToDouble(&d) ) { pParameter->asGrid_System()->Assign(-1, 0.0, 0.0, 0, 0); } else { pParameter->asGrid_System()->Assign(d, x, y, (int)nx, (int)ny); } } break; } } } m_pModule->Update_Parameter_States(); //----------------------------------------------------- for(i=0; i<pParameters->Get_Count(); i++) { CSG_Parameter *pParameter = pParameters->Get_Parameter(i); if( pParameter->is_Input() ) { if( !_Load_Input(pParameters->Get_Parameter(i)) ) { CMD_Print_Error(pParameters->Get_Parameter(i)->Get_Name()); return( false ); } } else if( pParameter->is_Option() && !pParameter->is_Information() ) { long l; wxString s; switch( pParameter->Get_Type() ) { case PARAMETER_TYPE_Table_Field: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { if( s.ToLong(&l) ) { pParameter->Set_Value((int)l); } else { pParameter->Set_Value(CSG_String(&s)); } } break; case PARAMETER_TYPE_Table_Fields: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { pParameter->Set_Value(CSG_String(&s)); } break; } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- 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 CBSL_Interpreter::On_Execute(void) { //----------------------------------------------------- Parameters("OUTPUT")->asGridList()->Del_Items(); g_bProgress = Parameters("PROGRESS")->asBool(); if( m_bFile ) { CSG_File Stream; if( !Stream.Open(Parameters("BSL")->asString(), SG_FILE_R, false) ) { return( false ); } Stream.Read(m_BSL, Stream.Length()); } else { m_BSL = Parameters("BSL")->asString(); } //----------------------------------------------------- if( !Parse_Vars(false) ) { return( false ); } //----------------------------------------------------- CSG_Parameters Input(this, _TL("Input"), _TL(""), SG_T("INPUT"), true); FindMemoryGrids(); for(T_InputText::iterator it=InputGrids.begin(); it!=InputGrids.end(); it++) { CSG_String sName(it->c_str()); Input.Add_Grid(NULL, sName, sName, _TL(""), PARAMETER_INPUT, true); } DeleteVarList(); DeleteAnweisungList(AnweisungList); if( Dlg_Parameters(&Input, _TL("Input")) == false ) { return( false ); } //----------------------------------------------------- if( !Parse_Vars(true) ) { return( false ); } //----------------------------------------------------- g_pInterpreter = this; if( GetMemoryGrids(&Input) ) { try { ausfuehren_anweisung(AnweisungList); } catch(BBFehlerAusfuehren x) { if( x.Text == "" ) Message_Add(_TL("unknown error: execution")); else Message_Add(CSG_String::Format(SG_T("error: %s\n"), CSG_String(x.Text.c_str()).c_str())); } catch(BBFehlerUserbreak x) { if( x.Text == "" ) Message_Add(_TL("unknown error: user break")); else Message_Add(CSG_String::Format(SG_T("error: %s\n"), CSG_String(x.Text.c_str()).c_str())); } } g_pInterpreter = NULL; DeleteVarList(); DeleteAnweisungList(AnweisungList); return( true ); }
//--------------------------------------------------------- bool CMOLA_Import::On_Execute(void) { bool bDown; int xa, xb, y, yy, NX, NY; short *sLine; double D, xMin, yMin; CSG_File Stream; TSG_Data_Type Type; CSG_Grid *pGrid; CSG_String fName, sName; //----------------------------------------------------- pGrid = NULL; switch( Parameters("TYPE")->asInt() ) { case 0: Type = SG_DATATYPE_Short; break; case 1: default: Type = SG_DATATYPE_Float; break; } bDown = Parameters("ORIENT")->asInt() == 1; //----------------------------------------------------- // MEGpxxnyyyrv // 012345678901 // p indicates the product type (A for areoid, C for counts, R for // radius, and T for topography) // xx is the latitude of the upper left corner of the image // n indicates whether the latitude is north (N) or south (S) // yyy is the east longitude of the upper left corner of the image // r is the map resolution using the pattern // c = 4 pixel per degree // e = 16 pixel per degree // f = 32 pixel per degree // g = 64 pixel per degree // h = 128 pixel per degree // (This convention is consistent with that used for the Mars Digital // Image Model [MDIM] archives.) // v is a letter indicating the product version. fName = SG_File_Get_Name(Parameters("FILE")->asString(), false); fName.Make_Upper(); if( fName.Length() < 12 ) { return( false ); } //----------------------------------------------------- switch( fName[3] ) { default: return( false ); case 'A': sName.Printf(SG_T("MOLA: Areoid v%c") , fName[11]); break; case 'C': sName.Printf(SG_T("MOLA: Counts v%c") , fName[11]); break; case 'R': sName.Printf(SG_T("MOLA: Radius v%c") , fName[11]); break; case 'T': sName.Printf(SG_T("MOLA: Topography v%c") , fName[11]); break; } //----------------------------------------------------- switch( fName[10] ) { default: return( false ); case 'C': // 1/4th degree... D = 1.0 / 4.0; NX = 4 * 360; NY = 4 * 180; yMin = - 90.0; xMin = -180.0; break; case 'D': // 1/8th degree... D = 1.0 / 8.0; NX = 8 * 360; NY = 8 * 180; yMin = - 90.0; xMin = -180.0; break; case 'E': // 1/16th degree... D = 1.0 / 16.0; NX = 16 * 360; NY = 16 * 180; yMin = - 90.0; xMin = -180.0; break; case 'F': // 1/32th degree... D = 1.0 / 32.0; NX = 32 * 360; NY = 32 * 180; yMin = - 90.0; xMin = -180.0; break; case 'G': // 1/64th degree... D = 1.0 / 64.0; NX = 64 * 180; NY = 64 * 90; yMin = (fName[6] == 'S' ? -1.0 : 1.0) * fName.Right(8).asInt(); yMin = bDown ? yMin - NY * D : -yMin; xMin = fName.Right(5).asInt(); if( xMin >= 180.0 ) { xMin -= 360.0; } break; case 'H': // 1/128th degree... D = 1.0 / 128.0; NX = 128 * 90; NY = 128 * 44; yMin = (fName[6] == 'S' ? -1.0 : 1.0) * fName.Right(8).asInt(); yMin = bDown ? yMin - NY * D : -yMin; xMin = fName.Right(5).asInt(); if( xMin >= 180.0 ) { xMin -= 360.0; } break; } //----------------------------------------------------- if( Stream.Open(Parameters("FILE")->asString(), SG_FILE_R, true) ) { if( (pGrid = SG_Create_Grid(Type, NX, NY, D, xMin + D / 2.0, yMin + D / 2.0)) != NULL ) { pGrid->Set_Name(sName); pGrid->Set_NoData_Value(-999999); pGrid->Get_Projection().Create(SG_T("+proj=lonlat +units=m +a=3396200.000000 +b=3376200.000000"), SG_PROJ_FMT_Proj4); //--------------------------------------------- sLine = (short *)SG_Malloc(NX * sizeof(short)); for(y=0; y<NY && !Stream.is_EOF() && Set_Progress(y, NY); y++) { yy = bDown ? NY - 1 - y : y; Stream.Read(sLine, NX, sizeof(short)); if( fName[10] == 'G' || fName[10] == 'H' ) { for(xa=0; xa<NX; xa++) { SG_Swap_Bytes(sLine + xa, sizeof(short)); pGrid->Set_Value(xa, yy, sLine[xa]); } } else { for(xa=0, xb=NX/2; xb<NX; xa++, xb++) { SG_Swap_Bytes(sLine + xa, sizeof(short)); SG_Swap_Bytes(sLine + xb, sizeof(short)); pGrid->Set_Value(xa, yy, sLine[xb]); pGrid->Set_Value(xb, yy, sLine[xa]); } } } //--------------------------------------------- SG_Free(sLine); Parameters("GRID")->Set_Value(pGrid); } } return( pGrid != NULL ); }
//--------------------------------------------------------- bool CResection::On_Execute(void) { CSG_PointCloud *pPoints; // Input Point Cloud CSG_String fileName; CSG_File *pTabStream = NULL; int n = 6; // Number of unknowns CSG_Vector center(3); CSG_Vector target(3); double c = Parameters("F") ->asDouble(); // Focal Length (mm) double pixWmm = Parameters("W") ->asDouble() / 1000;// Pixel Width (mm) double ppOffsetX = Parameters("ppX") ->asDouble(); // Principal Point Offset X (pixels) double ppOffsetY = Parameters("ppY") ->asDouble(); // Principal Point Offset Y (pixels) pPoints = Parameters("POINTS") ->asPointCloud(); fileName = Parameters("OUTPUT FILE") ->asString(); center[0] = Parameters("Xc") ->asDouble(); center[1] = Parameters("Yc") ->asDouble(); center[2] = Parameters("Zc") ->asDouble(); target[0] = Parameters("Xt") ->asDouble(); target[1] = Parameters("Yt") ->asDouble(); target[2] = Parameters("Zt") ->asDouble(); int pointCount = pPoints->Get_Point_Count(); bool estPPOffsets = false; if ( Parameters("EST_OFFSETS")->asBool() ) { estPPOffsets = true; n = 8; // Increase number of unknowns by 2 } bool applyDistortions = false; CSG_Vector K(3); if ( Parameters("GIVE_DISTORTIONS")->asBool() ) { applyDistortions = true; K[0] = Parameters("K1") ->asDouble(); K[1] = Parameters("K2") ->asDouble(); K[2] = Parameters("K3") ->asDouble(); } double dxapp = center [0] - target [0]; double dyapp = center [1] - target [1]; double dzapp = center [2] - target [2]; double h_d = sqrt (dxapp * dxapp + dyapp * dyapp + dzapp * dzapp); // Distance between Proj. Center & Target (m) double h_dmm = h_d * 1000; // Convert to mm if( fileName.Length() == 0 ) { SG_UI_Msg_Add_Error(_TL("Please provide an output file name!")); return( false ); } pTabStream = new CSG_File(); if( !pTabStream->Open(fileName, SG_FILE_W, false) ) { SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unable to open output file %s!"), fileName.c_str())); delete (pTabStream); return (false); } CSG_Vector rotns = methods::calcRotations(center,target); // Approx. rotations omega, kappa, alpha CSG_String msg = "********* Initial Approximate Values *********"; pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Rotation Angles:"); pTabStream->Write(SG_T("\n") + msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Omega:\t") + SG_Get_String(rotns[0],6,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Kappa:\t") + SG_Get_String(rotns[1],6,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Alpha:\t") + SG_Get_String(rotns[2],6,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Projection Center:"); pTabStream->Write(SG_T("\n") + msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Xc:\t") + SG_Get_String(center[0],4,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Yc:\t") + SG_Get_String(center[1],4,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Zc:\t") + SG_Get_String(center[2],4,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); if (estPPOffsets) { msg = SG_T("Principal Point Offsets:"); pTabStream->Write(SG_T("\n") + msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("ppX:\t") + SG_Get_String(ppOffsetX,5,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("ppY:\t") + SG_Get_String(ppOffsetY,5,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); } double itrNo = 0; CSG_Matrix invN; while (true) { // Begin Iterations itrNo++; double omega = rotns[0]; double kappa = rotns[1]; double alpha = rotns[2]; CSG_Matrix R = methods::calcRotnMatrix(rotns); // Rotation Matrix from approximate values CSG_Matrix E(3,3); // [w1;w2;w3] = E * [dw;dk;da] E[0][0] = -1; E[0][1] = E[1][0] = E[2][0] = 0; E[0][2] = sin(kappa); E[1][1] = -cos(omega); E[1][2] = -sin(omega) * cos(kappa); E[2][1] = sin(omega); E[2][2] = -cos(omega) * cos(kappa); CSG_Matrix N(n,n); // Transpose(Design Matrix) * I * Design Matrix CSG_Vector ATL(n); // Transpose(Design Matrix) * I * Shortened obs. vector double SS = 0; double sigma_naught = 0; for (int i = 0; i < pointCount; i++) { CSG_Vector pqs(3); // Approx. pi, qi, si for (int j = 0; j < 3; j++) { pqs[j] = R[j][0] * (pPoints->Get_X(i) - center[0]) + R[j][1] * (pPoints->Get_Y(i) - center[1]) + R[j][2] * (pPoints->Get_Z(i) - center[2]); } double p_i = pqs[0]; double q_i = pqs[1]; double s_i = pqs[2]; double dR = 0; // Undistorted double x_u = c * p_i / q_i; double y_u = c * s_i / q_i; double c_hat = c; if (applyDistortions) { double r2 = x_u * x_u + y_u * y_u; dR = K[0] * r2 + K[1] * r2 * r2 + K[2] * r2 * r2 * r2; c_hat = c * (1 - dR); } // Approx. image coordinates (with distortions) double x_i = (1 - dR) * x_u + ppOffsetX * pixWmm; double z_i = (1 - dR) * y_u + ppOffsetY * pixWmm; // Shortened obervation vector: dxi & dzi double dx_i = pPoints->Get_Attribute(i,0) * pixWmm - x_i; double dz_i = pPoints->Get_Attribute(i,1) * pixWmm - z_i; SS += pow(dx_i,2) + pow(dz_i,2); /* x_i, z_i in [mm] p_i,q_i,s_i in [m] h_d in [m] c, c_hat in [mm] h_dmm in [mm] */ CSG_Matrix L(3,2); // CSG_Matrix takes columns first and rows second CSG_Matrix V(3,3); CSG_Matrix LR(3,2); CSG_Matrix LVE(3,2); L[0][0] = L[1][2] = c_hat / (1000 * q_i); L[0][2] = L[1][0] = 0; L[0][1] = -x_u * (1 - dR) / (1000 * q_i); L[1][1] = -y_u * (1 - dR) / (1000 * q_i); V[0][0] = V[1][1] = V[2][2] = 0; V[0][1] = s_i / h_d; V[0][2] = -q_i / h_d; V[1][0] = -s_i / h_d; V[1][2] = p_i / h_d; V[2][0] = q_i / h_d; V[2][1] = -p_i / h_d; LVE = ( L * V ) * E; LR = L * R; // Design Matrix (J) CSG_Matrix design(n,2); for(int j = 0; j < 2; j++) { for(int k = 0; k < 3; k++) { design[j][k] = LVE[j][k]; design[j][k+3] = -LR[j][k]; } } if ( estPPOffsets ) { design[0][6] = design[1][7] = 1.0; } // Build Normal Matrix for(int j = 0; j < n; j++) { for(int k = 0; k < n; k++) { N[j][k] += (design[0][j] * design[0][k] + design[1][j] * design[1][k]); } } // Build Tranpose (J) * I * (Shortened obs. vector) for (int m=0; m < n; m++) { ATL[m] += design[0][m] * dx_i + design[1][m] * dz_i; } L.Destroy(); V.Destroy(); LR.Destroy(); LVE.Destroy(); pqs.Destroy(); design.Destroy(); } // end looping over observations // Eigen values and Eigen Vectors CSG_Vector eigenVals(n); CSG_Matrix eigenVecs(n,n); SG_Matrix_Eigen_Reduction(N, eigenVecs, eigenVals, true); // One of the Eigen Values is 0 if (std::any_of(eigenVals.cbegin(), eigenVals.cend(), [] (double i) { return i == 0; })) { msg = "The Normal Matrix has a rank defect. Please measure more points."; pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); break; } double mx = *std::max_element(eigenVals.cbegin(), eigenVals.cend()); double mn = *std::min_element(eigenVals.cbegin(), eigenVals.cend()); // Ratio of Smallest to the Biggest Eigen value is too small if ((mn / mx) < pow(10,-12.0)) { msg = SG_T("Condition of the Matrix of Normal Equations:\t") + CSG_String::Format(SG_T(" %13.5e"), mn/mx); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = "The Normal Matrix is weakly conditioned. Please measure more points."; pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); break; } // Calculate the adjustments double absMax = 0; invN = N.Get_Inverse(); CSG_Vector est_param_incs = invN * ATL; for (int i = 0; i < n; i++) { if (abs(est_param_incs[i]) > absMax) { absMax = abs(est_param_incs[i]); } } if (absMax < thresh) { msg = "Solution has converged."; pTabStream->Write(SG_T("\n") + msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); break; } for (int a = 0; a < 3; a++) { rotns[a] += est_param_incs[a] / h_dmm; // New Approx. rotations omega, kappa, alpha center[a] += est_param_incs[a+3] / 1000; // New Approx. Projection Center } if ( estPPOffsets ) { ppOffsetX += (est_param_incs[6] / pixWmm); // New Approx. Principal Point ppOffsetY += (est_param_incs[7] / pixWmm); } sigma_naught = sqrt(SS / (2 * pointCount - n)); // Writing To Output File & SAGA Console msg = "********* Iteration: " + SG_Get_String(itrNo,0,false) + " *********"; pTabStream->Write(SG_T("\n") + msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = "Sum of Squared Residuals:\t" + SG_Get_String(SS,5,false); pTabStream->Write(SG_T("\n") + msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = "Sigma Naught:\t" + SG_Get_String(sigma_naught,5,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Condition of the Matrix of Normal Equations:\t") + CSG_String::Format(SG_T(" %13.5e"), mn/mx); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); R.Destroy(); E.Destroy(); N.Destroy(); ATL.Destroy(); invN.Destroy(); eigenVals.Destroy(); eigenVecs.Destroy(); est_param_incs.Destroy(); } // end of iterations msg = "********* Final Estimated Parameters *********"; pTabStream->Write(SG_T("\n") + msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Rotation Angles:"); pTabStream->Write(SG_T("\n") + msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Omega:\t") + SG_Get_String(rotns[0],6,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Kappa:\t") + SG_Get_String(rotns[1],6,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Alpha:\t") + SG_Get_String(rotns[2],6,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Projection Center:"); pTabStream->Write(SG_T("\n") + msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Xc:\t") + SG_Get_String(center[0],4,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Yc:\t") + SG_Get_String(center[1],4,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("Zc:\t") + SG_Get_String(center[2],4,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); if (estPPOffsets) { msg = SG_T("Principal Point Offsets:"); pTabStream->Write(SG_T("\n") + msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("ppX:\t") + SG_Get_String(ppOffsetX,5,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); msg = SG_T("ppY:\t") + SG_Get_String(ppOffsetY,5,false); pTabStream->Write(msg + SG_T("\n")); SG_UI_Msg_Add(msg, true); } K.Destroy(); rotns.Destroy(); center.Destroy(); target.Destroy(); pTabStream->Close(); return true; }
//--------------------------------------------------------- bool CPointcloud_To_Text_File::On_Execute(void) { CSG_PointCloud *pPoints; CSG_String fileName; CSG_File *pTabStream = NULL; bool bWriteHeader; CSG_String fieldSep; CSG_Parameters P; CSG_Parameter *pNode; CSG_String s; std::vector<int> vCol, vPrecision; //----------------------------------------------------- pPoints = Parameters("POINTS") ->asPointCloud(); fileName = Parameters("FILE") ->asString(); bWriteHeader = Parameters("WRITE_HEADER")->asBool(); switch (Parameters("FIELDSEP")->asInt()) { default: case 0: fieldSep = "\t"; break; case 1: fieldSep = " "; break; case 2: fieldSep = ","; break; } if( fileName.Length() == 0 ) { SG_UI_Msg_Add_Error(_TL("Please provide an output file name!")); return( false ); } if (SG_UI_Get_Window_Main()) { P.Set_Name(_TL("Check the fields to export")); for(int iField=0; iField<pPoints->Get_Field_Count(); iField++) { s.Printf(SG_T("NODE_%03d") , iField + 1); pNode = P.Add_Node(NULL, s, CSG_String::Format(_TL("%d. %s"), iField + 1, _TL("Field")), _TL("")); s.Printf(SG_T("FIELD_%03d"), iField); P.Add_Value(pNode, s, CSG_String::Format(SG_T("%s"), pPoints->Get_Field_Name(iField)), _TL(""), PARAMETER_TYPE_Bool, false); s.Printf(SG_T("PRECISION_%03d"), iField); P.Add_Value(pNode, s, _TL("Decimal Precision"), _TL(""), PARAMETER_TYPE_Int, 2.0, 0.0, true); } //----------------------------------------------------- if( Dlg_Parameters(&P, _TL("Field Properties")) ) { vCol.clear(); vPrecision.clear(); for(int iField=0; iField<pPoints->Get_Field_Count(); iField++) { if( P(CSG_String::Format(SG_T("FIELD_%03d" ), iField).c_str())->asBool() ) { vCol.push_back(iField); vPrecision.push_back(P(CSG_String::Format(SG_T("PRECISION_%03d" ), iField).c_str())->asInt()); } } } else return( false ); } else // CMD { CSG_String sFields, sPrecision; CSG_String token; int iValue; sFields = Parameters("FIELDS")->asString(); sPrecision = Parameters("PRECISIONS")->asString(); CSG_String_Tokenizer tkz_fields(sFields, ";", SG_TOKEN_STRTOK); while( tkz_fields.Has_More_Tokens() ) { token = tkz_fields.Get_Next_Token(); if( token.Length() == 0 ) break; if( !token.asInt(iValue) ) { SG_UI_Msg_Add_Error(_TL("Error parsing attribute fields: can't convert to number")); return( false ); } iValue -= 1; if( iValue < 0 || iValue > pPoints->Get_Field_Count() - 1 ) { SG_UI_Msg_Add_Error(_TL("Error parsing attribute fields: field index out of range")); return( false ); } else vCol.push_back(iValue); } CSG_String_Tokenizer tkz_precisons(sPrecision.c_str(), ";", SG_TOKEN_STRTOK); while( tkz_precisons.Has_More_Tokens() ) { token = tkz_precisons.Get_Next_Token(); if( token.Length() == 0 ) break; if( !token.asInt(iValue) ) { SG_UI_Msg_Add_Error(_TL("Error parsing attribute fields: can't convert to number")); return( false ); } vPrecision.push_back(iValue); } } if( vCol.size() == 0 ) { SG_UI_Msg_Add_Error(_TL("Please provide at least one column to export!")); return( false ); } if( vCol.size() != vPrecision.size() ) { SG_UI_Msg_Add_Error(_TL("Number of fields and precisions must be equal!")); return( false ); } //----------------------------------------------------- pTabStream = new CSG_File(); if( !pTabStream->Open(fileName, SG_FILE_W, false) ) { SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unable to open output file %s!"), fileName.c_str())); delete (pTabStream); return (false); } if( bWriteHeader ) { CSG_String sHeader; for(size_t i=0; i<vCol.size(); i++) { sHeader += CSG_String::Format(SG_T("%s"), pPoints->Get_Field_Name(vCol.at(i))); if( i < vCol.size()-1 ) sHeader += fieldSep.c_str(); } sHeader += SG_T("\n"); pTabStream->Write(sHeader); } for(int iPoint=0; iPoint<pPoints->Get_Count() && Set_Progress(iPoint, pPoints->Get_Count()); iPoint++) { CSG_String sLine; for(size_t i=0; i<vCol.size(); i++) { switch (pPoints->Get_Field_Type(vCol.at(i))) { case SG_DATATYPE_Double: case SG_DATATYPE_Float: sLine += SG_Get_String(pPoints->Get_Value(iPoint, vCol.at(i)), vPrecision.at(i), false); break; default: sLine += CSG_String::Format(SG_T("%d"), (int)pPoints->Get_Value(iPoint, vCol.at(i))); break; } if( i < vCol.size()-1 ) sLine += fieldSep.c_str(); } sLine += SG_T("\n"); pTabStream->Write(sLine); } pTabStream->Close(); delete (pTabStream); return( true ); }
//--------------------------------------------------------- bool CSTL_Export::On_Execute(void) { bool bBinary; int zField; float v[3]; CSG_String File; CSG_File Stream; CSG_TIN *pTIN; pTIN = Parameters("TIN") ->asTIN(); File = Parameters("FILE") ->asString(); zField = Parameters("ZFIELD") ->asInt(); bBinary = Parameters("BINARY") ->asInt() == 1; if( !Stream.Open(File, SG_FILE_W, bBinary) ) { return( false ); } //----------------------------------------------------- if( bBinary ) { char *sHeader = (char *)SG_Calloc(80, sizeof(char)); DWORD nFacets = pTIN->Get_Triangle_Count(); WORD nBytes = 0; Stream.Write(sHeader , sizeof(char), 80); Stream.Write(&nFacets, sizeof(DWORD)); SG_Free(sHeader); //------------------------------------------------- for(int iTriangle=0; iTriangle<pTIN->Get_Triangle_Count(); iTriangle++) { CSG_TIN_Triangle *pTriangle = pTIN->Get_Triangle(iTriangle); Get_Normal(pTriangle, zField, v); Stream.Write(v, sizeof(float), 3); // facet normal for(int iNode=0; iNode<3; iNode++) { CSG_TIN_Node *pNode = pTriangle->Get_Node(iNode); v[0] = (float)pNode->Get_X(); v[1] = (float)pNode->Get_Y(); v[2] = (float)pNode->asDouble(zField); Stream.Write(v, sizeof(float), 3); } Stream.Write(&nBytes, sizeof(WORD)); } } //----------------------------------------------------- else // ASCII { Stream.Printf(SG_T("solid %s\n"), SG_File_Get_Name(File, false).c_str()); for(int iTriangle=0; iTriangle<pTIN->Get_Triangle_Count(); iTriangle++) { CSG_TIN_Triangle *pTriangle = pTIN->Get_Triangle(iTriangle); Get_Normal(pTriangle, zField, v); Stream.Printf(SG_T(" facet normal %.4f %.4f %.4f\n"), v[0], v[1], v[2]); Stream.Printf(SG_T(" outer loop\n")); for(int iNode=0; iNode<3; iNode++) { CSG_TIN_Node *pNode = pTriangle->Get_Node(iNode); v[0] = (float)pNode->Get_X(); v[1] = (float)pNode->Get_Y(); v[2] = (float)pNode->asDouble(zField); Stream.Printf(SG_T(" vertex %.4f %.4f %.4f\n"), v[0], v[1], v[2]); } Stream.Printf(SG_T(" endloop\n")); Stream.Printf(SG_T(" endfacet\n")); } Stream.Printf(SG_T("endsolid %s\n"), SG_File_Get_Name(File, false).c_str()); } return( true ); }