//--------------------------------------------------------- CSG_String SG_File_Make_Path(const SG_Char *Directory, const SG_Char *Name, const SG_Char *Extension) { wxFileName fn; fn.AssignDir(Directory && *Directory ? Directory : SG_File_Get_Path(Name).c_str()); if( Extension && *Extension ) { fn.SetName (SG_File_Get_Name(Name, false).c_str()); fn.SetExt (Extension); } else { fn.SetFullName (SG_File_Get_Name(Name, true).c_str()); } return( fn.GetFullPath().c_str() ); }
//--------------------------------------------------------- bool CGPX_Import::On_Execute(void) { CSG_Shapes *pWay; CSG_MetaData GPX; //----------------------------------------------------- m_Name = Parameters("FILE") ->asString(); m_pShapes = Parameters("SHAPES") ->asShapesList(); m_bTime = Parameters("TIME") ->asBool(); //----------------------------------------------------- if( !GPX.Create(m_Name) || GPX.Get_Name().CmpNoCase(SG_T("gpx")) ) { return( false ); } //----------------------------------------------------- pWay = SG_Create_Shapes(SHAPE_TYPE_Point, m_Name); m_Name = SG_File_Get_Name(m_Name, false); m_pShapes->Del_Items(); //----------------------------------------------------- for(int i=0; i<GPX.Get_Children_Count(); i++) { CSG_MetaData *pChild = GPX.Get_Child(i); if( pChild->Get_Name().CmpNoCase(SG_T("wpt")) == 0 ) { Add_Point(pChild, pWay); } else if( pChild->Get_Name().CmpNoCase(SG_T("rte")) == 0 ) { Add_Route(pChild); } else if( pChild->Get_Name().CmpNoCase(SG_T("trk")) == 0 ) { Add_Track(pChild); } } //----------------------------------------------------- if( pWay->Get_Count() > 0 ) { m_pShapes->Add_Item(pWay); } else { delete(pWay); } return( m_pShapes->Get_Count() > 0 ); }
//--------------------------------------------------------- bool CLandsat_Import::On_Execute(void) { CSG_Strings Files; if( !Parameters("FILES")->asFilePath()->Get_FilePaths(Files) || Files.Get_Count() <= 0 ) { return( false ); } //----------------------------------------------------- CSG_Parameter_Grid_List *pBands = Parameters("BANDS")->asGridList(); pBands->Del_Items(); for(int i=0; i<Files.Get_Count(); i++) { Message_Add(CSG_String::Format(SG_T("%s: %s"), _TL("loading"), SG_File_Get_Name(Files[i], false).c_str())); CSG_Grid *pBand = Get_Band(Files[i]); if( pBand ) { pBands->Add_Item(pBand); DataObject_Add(pBand); DataObject_Set_Colors(pBand, 11, SG_COLORS_BLACK_WHITE); } } //----------------------------------------------------- if( Parameters("SHOW_RGB")->is_Enabled() && Parameters("SHOW_RGB")->asBool() ) { CSG_Grid *pR = pBands->asGrid(Parameters("SHOW_R")->asInt()); CSG_Grid *pG = pBands->asGrid(Parameters("SHOW_G")->asInt()); CSG_Grid *pB = pBands->asGrid(Parameters("SHOW_B")->asInt()); if( pR && pG && pB ) { DataObject_Set_Parameter(pR, "COLORS_TYPE" , 5); // _TL("RGB Overlay") // CLASSIFY_OVERLAY DataObject_Set_Parameter(pR, "OVERLAY_MODE", 0); // _TL("red=this, green=1, blue=2") DataObject_Set_Parameter(pR, "OVERLAY_G" , pG); DataObject_Set_Parameter(pR, "OVERLAY_B" , pB); DataObject_Update(pR, true); } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- 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 ); }
//--------------------------------------------------------- int CLandsat_Import::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter) { if( !SG_STR_CMP(pParameter->Get_Identifier(), "FILES") ) { CSG_Strings Files; pParameter->asFilePath()->Get_FilePaths(Files); if( Files.Get_Count() < 3 ) { pParameters->Set_Enabled("SHOW_RGB", false); } else { pParameters->Set_Enabled("SHOW_RGB", true); CSG_String Choices; for(int i=0; i<Files.Get_Count(); i++) { Choices += SG_File_Get_Name(Files[i], false) + "|"; } int iR = pParameters->Get_Parameter("SHOW_R")->asChoice()->Get_Count() > 1 ? pParameters->Get_Parameter("SHOW_R")->asInt() : 2; int iG = pParameters->Get_Parameter("SHOW_G")->asChoice()->Get_Count() > 1 ? pParameters->Get_Parameter("SHOW_G")->asInt() : 1; int iB = pParameters->Get_Parameter("SHOW_B")->asChoice()->Get_Count() > 1 ? pParameters->Get_Parameter("SHOW_B")->asInt() : 0; pParameters->Get_Parameter("SHOW_R")->asChoice()->Set_Items(Choices); pParameters->Get_Parameter("SHOW_R")->Set_Value(iR); pParameters->Get_Parameter("SHOW_G")->asChoice()->Set_Items(Choices); pParameters->Get_Parameter("SHOW_G")->Set_Value(iG); pParameters->Get_Parameter("SHOW_B")->asChoice()->Set_Items(Choices); pParameters->Get_Parameter("SHOW_B")->Set_Value(iB); } } if( !SG_STR_CMP(pParameter->Get_Identifier(), "PROJECTION") ) { pParameters->Set_Enabled("INTERPOLATION", pParameter->asInt() == 2); } if( !SG_STR_CMP(pParameter->Get_Identifier(), "SHOW_RGB") ) { pParameters->Set_Enabled("SHOW_R", pParameter->asBool()); pParameters->Set_Enabled("SHOW_G", pParameter->asBool()); pParameters->Set_Enabled("SHOW_B", pParameter->asBool()); } return( 1 ); }
bool DLG_Save(wxString &File_Path, int ID_DLG) { wxString def_Dir, def_Name; def_Name = SG_File_Get_Name(File_Path, true).w_str(); def_Dir = SG_File_Get_Path(File_Path).w_str(); if( !wxDirExists(def_Dir) ) { CONFIG_Read(CONFIG_GROUP_FILE_DLG, DLG_Get_FILE_Config(ID_DLG), def_Dir); } if( DLG_Save(File_Path, DLG_Get_FILE_Caption(ID_DLG), def_Dir, def_Name, DLG_Get_FILE_Filter(ID_DLG)) ) { CONFIG_Write(CONFIG_GROUP_FILE_DLG, DLG_Get_FILE_Config(ID_DLG), SG_File_Get_Path(File_Path).w_str()); return( true ); } return( false ); }
//--------------------------------------------------------- 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 ); }
//--------------------------------------------------------- bool CGrid_Table_Import::On_Execute(void) { bool bDown; int x, y, nx, ny; double dxy, xmin, ymin, zFactor, zNoData; TSG_Data_Type data_type; CSG_String FileName, Unit; CSG_Grid *pGrid; CSG_Table Table; CSG_Table_Record *pRecord; //----------------------------------------------------- FileName = Parameters("FILE_DATA") ->asString(); dxy = Parameters("DXY") ->asDouble(); xmin = Parameters("XMIN") ->asDouble(); ymin = Parameters("YMIN") ->asDouble(); bDown = Parameters("TOPDOWN") ->asInt() == 1; Unit = Parameters("UNIT") ->asString(); zFactor = Parameters("ZFACTOR") ->asDouble(); zNoData = Parameters("NODATA") ->asDouble(); switch( Parameters("DATA_TYPE")->asInt() ) { default: data_type = SG_DATATYPE_Undefined; break; // not handled case 0: data_type = SG_DATATYPE_Byte; break; // 1 Byte Integer (unsigned) case 1: data_type = SG_DATATYPE_Char; break; // 1 Byte Integer (signed) case 2: data_type = SG_DATATYPE_Word; break; // 2 Byte Integer (unsigned) case 3: data_type = SG_DATATYPE_Short; break; // 2 Byte Integer (signed) case 4: data_type = SG_DATATYPE_DWord; break; // 4 Byte Integer (unsigned) case 5: data_type = SG_DATATYPE_Int; break; // 4 Byte Integer (signed) case 6: data_type = SG_DATATYPE_Float; break; // 4 Byte Floating Point case 7: data_type = SG_DATATYPE_Double; break; // 8 Byte Floating Point } //----------------------------------------------------- if( Table.Create(FileName) && (nx = Table.Get_Field_Count()) > 0 && (ny = Table.Get_Record_Count()) > 0 ) { pGrid = SG_Create_Grid(data_type, nx, ny, dxy, xmin, ymin); for(y=0; y<ny && Set_Progress(y, ny); y++) { pRecord = Table.Get_Record(bDown ? ny - 1 - y : y); for(x=0; x<nx; x++) { pGrid->Set_Value(x, y, pRecord->asDouble(x)); } } pGrid->Set_Unit (Unit); pGrid->Set_ZFactor (zFactor); pGrid->Set_NoData_Value (zNoData); pGrid->Set_Name (SG_File_Get_Name(FileName, false)); Parameters("GRID")->Set_Value(pGrid); return( true ); } //----------------------------------------------------- return( false ); }
//--------------------------------------------------------- bool CSTL_Import::On_Execute(void) { int Method; DWORD iFacette, nFacettes; TSTL_Point p[3]; CSG_String sFile, sHeader; CSG_File Stream; //----------------------------------------------------- sFile = Parameters("FILE") ->asString(); Method = Parameters("METHOD") ->asInt(); r_sin_x = sin(Parameters("ROT_X")->asDouble() * M_DEG_TO_RAD); r_sin_y = sin(Parameters("ROT_Y")->asDouble() * M_DEG_TO_RAD); r_sin_z = sin(Parameters("ROT_Z")->asDouble() * M_DEG_TO_RAD); r_cos_x = cos(Parameters("ROT_X")->asDouble() * M_DEG_TO_RAD); r_cos_y = cos(Parameters("ROT_Y")->asDouble() * M_DEG_TO_RAD); r_cos_z = cos(Parameters("ROT_Z")->asDouble() * M_DEG_TO_RAD); //----------------------------------------------------- if( !Stream.Open(sFile) ) { return( false ); } if( !Stream.Read(sHeader, 80) ) { return( false ); } Message_Add(sHeader); if( !Stream.Read(&nFacettes, sizeof(nFacettes)) ) { return( false ); } Message_Add(CSG_String::Format(SG_T("%s: %d"), _TL("Number of Facettes"), nFacettes)); //----------------------------------------------------- switch( Method ) { //----------------------------------------------------- case 0: { // Point Cloud CSG_Rect Extent; if( Get_Extent(Stream, Extent, nFacettes) ) { CSG_PRQuadTree Points(Extent); CSG_PointCloud *pPoints = SG_Create_PointCloud(); Parameters("POINTS")->Set_Value(pPoints); pPoints->Set_Name(SG_File_Get_Name(sFile, false)); pPoints->Add_Field((const char *)NULL, SG_DATATYPE_Undefined); for(iFacette=0; iFacette<nFacettes && !Stream.is_EOF() && Set_Progress(iFacette, nFacettes); iFacette++) { if( Read_Facette(Stream, p) ) { for(int i=0; i<3; i++) { if( Points.Add_Point(p[i].x, p[i].y, p[i].z) ) { pPoints->Add_Point(p[i].x, p[i].y, p[i].z); } } } } } break; } //----------------------------------------------------- case 1: { // Point Cloud (centered) CSG_PointCloud *pPoints = SG_Create_PointCloud(); Parameters("POINTS")->Set_Value(pPoints); pPoints->Set_Name(SG_File_Get_Name(sFile, false)); pPoints->Add_Field((const char *)NULL, SG_DATATYPE_Undefined); for(iFacette=0; iFacette<nFacettes && !Stream.is_EOF() && Set_Progress(iFacette, nFacettes); iFacette++) { if( Read_Facette(Stream, p) ) { pPoints->Add_Point( (p[0].x + p[1].x + p[2].x) / 3.0, (p[0].y + p[1].y + p[2].y) / 3.0, (p[0].z + p[1].z + p[2].z) / 3.0 ); } } break; } //----------------------------------------------------- case 2: { // Points CSG_Shapes *pPoints = SG_Create_Shapes(SHAPE_TYPE_Point, SG_File_Get_Name(sFile, false)); pPoints->Add_Field(SG_T("Z"), SG_DATATYPE_Float); Parameters("SHAPES")->Set_Value(pPoints); for(iFacette=0; iFacette<nFacettes && !Stream.is_EOF() && Set_Progress(iFacette, nFacettes); iFacette++) { if( Read_Facette(Stream, p) ) { CSG_Shape *pPoint = pPoints->Add_Shape(); pPoint->Add_Point( (p[0].x + p[1].x + p[2].x) / 3.0, (p[0].y + p[1].y + p[2].y) / 3.0 ); pPoint->Set_Value(0, (p[0].z + p[1].z + p[2].z) / 3.0 ); } } break; } //----------------------------------------------------- case 3: { // Raster CSG_Rect Extent; if( Get_Extent(Stream, Extent, nFacettes) ) { int nx, ny; double d; nx = Parameters("GRID_RES")->asInt(); d = Extent.Get_XRange() / nx; ny = 1 + (int)(Extent.Get_YRange() / d); m_pGrid = SG_Create_Grid(SG_DATATYPE_Float, nx, ny, d, Extent.Get_XMin(), Extent.Get_YMin()); m_pGrid->Set_Name(SG_File_Get_Name(sFile, false)); m_pGrid->Set_NoData_Value(-99999); m_pGrid->Assign_NoData(); Parameters("GRID")->Set_Value(m_pGrid); //--------------------------------------------- for(iFacette=0; iFacette<nFacettes && !Stream.is_EOF() && Set_Progress(iFacette, nFacettes); iFacette++) { if( Read_Facette(Stream, p) ) { TSG_Point_Z Point[3]; for(int i=0; i<3; i++) { Point[i].x = (p[i].x - m_pGrid->Get_XMin()) / m_pGrid->Get_Cellsize(); Point[i].y = (p[i].y - m_pGrid->Get_YMin()) / m_pGrid->Get_Cellsize(); Point[i].z = p[i].z; } Set_Triangle(Point); } } } break; } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CCRU_Table_Import::On_Execute(void) { //----------------------------------------------------- CSG_File File; if( !File.Open(Parameters("FILE")->asString(), SG_FILE_R, false) ) { Error_Fmt("%s [%s]", _TL("could not open file"), Parameters("FILE")->asString()); return( false ); } //----------------------------------------------------- CSG_String sLine; if( !File.Read_Line(sLine) ) { Error_Fmt("%s [%s]", _TL("failed to read header"), Parameters("FILE")->asString()); return( false ); } //----------------------------------------------------- int nx, ny, nMonths; double Cellsize, xMin, yMin, xMax, yMax; if( !File.Scan(Cellsize) || !File.Scan(xMin ) || !File.Scan(yMin ) || !File.Scan(xMax ) || !File.Scan(yMax ) || !File.Scan(nx ) || !File.Scan(ny ) || !File.Scan(nMonths ) ) { Error_Fmt("%s [%s]", _TL("failed to read header"), Parameters("FILE")->asString()); return( false ); } //----------------------------------------------------- CSG_Grid_System System(Cellsize, xMin, yMin, nx, ny); if( !System.is_Valid() || System.Get_XMax() != xMax || System.Get_YMax() != yMax ) { Error_Fmt("%s [%s]", _TL("failed to read header"), Parameters("FILE")->asString()); return( false ); } //----------------------------------------------------- bool bShift = Parameters("SHIFT")->asBool(); if( bShift ) { System.Assign(Cellsize, xMin - 180.0, yMin, nx, ny); } //----------------------------------------------------- CSG_String Name = SG_File_Get_Name(Parameters("FILE")->asString(), false); Parameters("GRIDS")->asGridList()->Del_Items(); for(int iMonth=0; iMonth<nMonths && !File.is_EOF() && Process_Get_Okay(); iMonth++) { Process_Set_Text("%s %d", _TL("Band"), 1 + iMonth); CSG_Grid *pGrid = SG_Create_Grid(System, SG_DATATYPE_Short); pGrid->Fmt_Name("%s_%02d", Name.c_str(), 1 + iMonth); pGrid->Set_NoData_Value(-9999); pGrid->Get_Projection().Set_GCS_WGS84(); Parameters("GRIDS")->asGridList()->Add_Item(pGrid); //------------------------------------------------- for(int y=0; y<ny && !File.is_EOF() && Set_Progress(y, ny); y++) { if( File.Read_Line(sLine) && sLine.Length() >= 5. * nx ) { for(int x=0, xx=bShift?nx/2:x, yy=ny-1-y; x<nx; x++, xx++) { double z; CSG_String s = sLine.Mid(x * 5, 5); if( s.asDouble(z) ) { pGrid->Set_Value(xx % nx, yy, z); } else { pGrid->Set_NoData(xx % nx, yy); } } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CPointCloud_From_Text_File::On_Execute(void) { CSG_String fileName; int iField, iType; TSG_Data_Type Type; CSG_String Name, Types, s; CSG_PointCloud *pPoints; CSG_Parameters P; CSG_Parameter *pNode; int xField, yField, zField, nAttribs, max_iField; bool bSkipHeader; char fieldSep; std::vector<int> vCol; std::ifstream tabStream; std::string tabLine; double lines; long cntPt, cntInvalid; double x, y, z, value; //----------------------------------------------------- fileName = Parameters("FILE") ->asString(); xField = Parameters("XFIELD") ->asInt() - 1; yField = Parameters("YFIELD") ->asInt() - 1; zField = Parameters("ZFIELD") ->asInt() - 1; nAttribs = Parameters("ATTRIBS") ->asInt(); bSkipHeader = Parameters("SKIP_HEADER") ->asBool(); switch (Parameters("FIELDSEP")->asInt()) { default: case 0: fieldSep = '\t'; break; case 1: fieldSep = ' '; break; case 2: fieldSep = ','; break; } Types.Printf(SG_T("%s|%s|%s|%s|%s|"), _TL("1 byte integer"), _TL("2 byte integer"), _TL("4 byte integer"), _TL("4 byte floating point"), _TL("8 byte floating point") ); P.Set_Name(_TL("Attribute Field Properties")); for(iField=1; iField<=nAttribs; iField++) { s.Printf(SG_T("NODE_%03d") , iField); pNode = P.Add_Node(NULL, s, CSG_String::Format(SG_T("%d. %s"), iField, _TL("Field")), _TL("")); s.Printf(SG_T("FIELD_%03d"), iField); P.Add_String(pNode, s, _TL("Name"), _TL(""), s); s.Printf(SG_T("COLUMN_%03d"), iField); P.Add_Value(pNode, s, _TL("Attribute is Column ..."), _TL(""), PARAMETER_TYPE_Int, iField+3, 1, true); s.Printf(SG_T("TYPE_%03d") , iField); P.Add_Choice(pNode, s, _TL("Type"), _TL(""), Types, 3); } //----------------------------------------------------- if( nAttribs > 0 ) { if( Dlg_Parameters(&P, _TL("Field Properties")) ) { pPoints = SG_Create_PointCloud(); pPoints->Set_Name(SG_File_Get_Name(fileName, false)); Parameters("POINTS")->Set_Value(pPoints); for(iField=0; iField<nAttribs; iField++) { Name = P(CSG_String::Format(SG_T("FIELD_%03d" ), iField + 1).c_str())->asString(); iType = P(CSG_String::Format(SG_T("TYPE_%03d" ), iField + 1).c_str())->asInt(); vCol.push_back(P(CSG_String::Format(SG_T("COLUMN_%03d"), iField + 1).c_str())->asInt() - 1); switch( iType ) { default: case 0: Type = SG_DATATYPE_Char; break; case 1: Type = SG_DATATYPE_Short; break; case 2: Type = SG_DATATYPE_Int; break; case 3: Type = SG_DATATYPE_Float; break; case 4: Type = SG_DATATYPE_Double; break; } pPoints->Add_Field(Name, Type); } } else return( false ); } else { pPoints = SG_Create_PointCloud(); pPoints->Create(); pPoints->Set_Name(SG_File_Get_Name(fileName, false)); Parameters("POINTS")->Set_Value(pPoints); } max_iField = M_GET_MAX(xField, yField); max_iField = M_GET_MAX(max_iField, zField); for( unsigned int i=0; i<vCol.size(); i++ ) { if( max_iField < vCol.at(i) ) max_iField = vCol.at(i); } // open input stream //--------------------------------------------------------- tabStream.open(fileName.b_str(), std::ifstream::in); if( !tabStream ) { SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unable to open input file!"))); return (false); } tabStream.seekg(0, std::ios::end); // get length of file lines = (double)tabStream.tellg(); tabStream.seekg(0, std::ios::beg); std::getline(tabStream, tabLine); // as a workaround we assume the number of lines from the length of the first line lines = lines / (double)tabStream.tellg(); if( !bSkipHeader ) { tabStream.clear(); // let's forget we may have reached the EOF tabStream.seekg(0, std::ios::beg); // and rewind to the beginning } // import //--------------------------------------------------------- cntPt = cntInvalid = 0; SG_UI_Process_Set_Text(CSG_String::Format(_TL("Importing data ..."))); while( std::getline(tabStream, tabLine) ) { std::istringstream stream(tabLine); std::vector<std::string> tabCols; std::string tabEntry; if( cntPt%10000 == 0 ) SG_UI_Process_Set_Progress((double)cntPt, lines); cntPt++; while( std::getline(stream, tabEntry, fieldSep) ) // read every column in this line and fill vector { if (tabEntry.length() == 0) continue; tabCols.push_back(tabEntry); } if ((int)tabCols.size() < max_iField - 1 ) { SG_UI_Msg_Add(CSG_String::Format(_TL("WARNING: Skipping misformatted line (%.0f)!"), cntPt), true); cntInvalid++; continue; } x = strtod(tabCols[xField].c_str(), NULL); y = strtod(tabCols[yField].c_str(), NULL); z = strtod(tabCols[zField].c_str(), NULL); pPoints->Add_Point(x, y, z); for( int i=0; i<nAttribs; i++ ) { value = strtod(tabCols.at(vCol.at(i)).c_str(), NULL); pPoints->Set_Attribute(i, value); } } // finalize //--------------------------------------------------------- tabStream.close(); CSG_Parameters sParms; DataObject_Get_Parameters(pPoints, sParms); if (sParms("COLORS_ATTRIB") && sParms("COLORS_TYPE") && sParms("METRIC_COLORS") && sParms("METRIC_ZRANGE") && sParms("COLORS_AGGREGATE")) { sParms("COLORS_AGGREGATE")->Set_Value(3); // highest z sParms("COLORS_TYPE")->Set_Value(2); // graduated color sParms("METRIC_COLORS")->asColors()->Set_Count(255); // number of colors sParms("COLORS_ATTRIB")->Set_Value(2); // z attrib sParms("METRIC_ZRANGE")->asRange()->Set_Range(pPoints->Get_Minimum(2),pPoints->Get_Maximum(2)); DataObject_Set_Parameters(pPoints, sParms); DataObject_Update(pPoints); } if (cntInvalid > 0) SG_UI_Msg_Add(CSG_String::Format(_TL("WARNING: Skipped %d invalid points!"), cntInvalid), true); SG_UI_Msg_Add(CSG_String::Format(_TL("%d points sucessfully imported."), (cntPt-cntInvalid)), true); return( true ); }
//--------------------------------------------------------- bool CPointCloud_From_Text_File::On_Execute(void) { //----------------------------------------------------- CSG_File Stream; if( !Stream.Open(Parameters("FILE")->asString(), SG_FILE_R, false) ) { Error_Set(_TL("Unable to open input file!")); return( false ); } //----------------------------------------------------- int xField = Parameters("XFIELD")->asInt() - 1; int yField = Parameters("YFIELD")->asInt() - 1; int zField = Parameters("ZFIELD")->asInt() - 1; char Separator; switch( Parameters("SEPARATOR")->asInt() ) { default: Separator = '\t'; break; case 1: Separator = ' '; break; case 2: Separator = ','; break; } //----------------------------------------------------- CSG_String sLine; CSG_Strings Values; if( !Stream.Read_Line(sLine) ) { Error_Set(_TL("Empty file!")); return( false ); } if( Parameters("SKIP_HEADER")->asBool() ) // header contains field names { CSG_String_Tokenizer tokValues(sLine, Separator); // read each field name for later use while( tokValues.Has_More_Tokens() ) { Values += tokValues.Get_Next_Token(); } } else { Stream.Seek_Start(); } //----------------------------------------------------- CSG_PointCloud *pPoints = SG_Create_PointCloud(); pPoints->Set_Name(SG_File_Get_Name(Parameters("FILE")->asString(), false)); Parameters("POINTS")->Set_Value(pPoints); CSG_Array_Int Fields; //----------------------------------------------------- if( SG_UI_Get_Window_Main() ) { CSG_Parameters &Fields = *Parameters("FIELDSPECS")->asParameters(); int nFields = Fields.Get_Count() / 2; CSG_String Names, Types; for(int iField=0; iField<nFields; iField++) { Names += CSG_String::Format("%s;", Fields(CSG_String::Format("NAME%03d", iField))->asString()); Types += CSG_String::Format("%d;", Fields(CSG_String::Format("TYPE%03d", iField))->asInt ()); } Parameters("FIELDNAMES")->Set_Value(Names); Parameters("FIELDTYPES")->Set_Value(Types); } { TSG_Data_Type Type = SG_DATATYPE_Float; // default CSG_String_Tokenizer tokFields(Parameters("FIELDS" )->asString(), ";"); CSG_String_Tokenizer tokTypes (Parameters("FIELDTYPES")->asString(), ";"); CSG_String_Tokenizer tokNames (Parameters("FIELDNAMES")->asString(), ";"); while( tokFields.Has_More_Tokens() ) { int iField; if( !tokFields.Get_Next_Token().asInt(iField) || iField < 1 ) { Error_Set(_TL("Error parsing attribute field index")); return( false ); } Fields += iField - 1; CSG_String Name; if( tokNames.Has_More_Tokens() ) { Name = tokNames.Get_Next_Token(); Name.Trim(true); Name.Trim(false); } if( Name.is_Empty() ) { if( iField - 1 < Values.Get_Count() ) { Name = Values[iField - 1]; } else { Name.Printf("FIELD%02d", iField); } } if( tokTypes.Has_More_Tokens() ) { Get_Data_Type(Type, tokTypes.Get_Next_Token()); } pPoints->Add_Field(Name, Type); } } //----------------------------------------------------- Process_Set_Text(_TL("Importing data ...")); int nLines = 0; sLong Length = Stream.Length(); while( Stream.Read_Line(sLine) ) { nLines++; if( pPoints->Get_Count() % 10000 == 0 && !Set_Progress((double)Stream.Tell(), (double)Length) ) { return( true ); // user break } //------------------------------------------------- CSG_String_Tokenizer tokValues(sLine, Separator); Values.Clear(); while( tokValues.Has_More_Tokens() ) // read every column in this line and fill vector { Values += tokValues.Get_Next_Token(); } //------------------------------------------------- double x, y, z; if( xField >= Values.Get_Count() || !Values[xField].asDouble(x) || yField >= Values.Get_Count() || !Values[yField].asDouble(y) || zField >= Values.Get_Count() || !Values[zField].asDouble(z) ) { Message_Fmt("\n%s: %s [%d]", _TL("Warning"), _TL("Skipping misformatted line"), nLines); continue; } pPoints->Add_Point(x, y, z); //------------------------------------------------- for(int iAttribute=0; iAttribute<pPoints->Get_Attribute_Count(); iAttribute++) { if( Fields[iAttribute] >= Values.Get_Count() ) { pPoints->Set_NoData(3 + iAttribute); } else switch( pPoints->Get_Attribute_Type(iAttribute) ) { case SG_DATATYPE_String: pPoints->Set_Attribute(iAttribute, Values[Fields[iAttribute]]); break; default: { double Value; if( Values[Fields[iAttribute]].asDouble(Value) ) { pPoints->Set_Attribute(iAttribute, Value); } else { pPoints->Set_NoData(3 + iAttribute); } } break; } } } //----------------------------------------------------- DataObject_Set_Parameter(pPoints, "DISPLAY_VALUE_AGGREGATE", 3); // highest z DataObject_Set_Parameter(pPoints, "COLORS_TYPE" , 3); // graduated colors DataObject_Set_Parameter(pPoints, "METRIC_ATTRIB" , 2); // z attrib DataObject_Set_Parameter(pPoints, "METRIC_ZRANGE", pPoints->Get_Minimum(2), pPoints->Get_Maximum(2)); DataObject_Update(pPoints); //----------------------------------------------------- if( nLines > pPoints->Get_Count() ) { Message_Add(" ", true); Message_Fmt("%s: %d %s", _TL("Warning"), nLines - pPoints->Get_Count(), _TL("invalid points have been skipped")); } Message_Add(" ", true); Message_Fmt("%d %s", pPoints->Get_Count(), _TL("points have been imported with success")); return( true ); }
//--------------------------------------------------------- bool CGrid_Table_Import::On_Execute(void) { //----------------------------------------------------- CSG_Table Table; if( !Table.Create(Parameters("FILE")->asString()) ) { Error_Fmt("%s [%s]", _TL("could not open table file"), Parameters("FILE")->asString()); return( false ); } //----------------------------------------------------- int nx, ny, nHeadLines = Parameters("HEADLINES")->asInt(); if( (nx = Table.Get_Field_Count()) < 1 || (ny = Table.Get_Record_Count()) < 1 ) { Error_Fmt("%s [%s]", _TL("no data in table file"), Parameters("FILE")->asString()); return( false ); } //----------------------------------------------------- TSG_Data_Type Type; switch( Parameters("DATA_TYPE")->asInt() ) { case 0: Type = SG_DATATYPE_Byte ; break; // 1 Byte Integer (unsigned) case 1: Type = SG_DATATYPE_Char ; break; // 1 Byte Integer (signed) case 2: Type = SG_DATATYPE_Word ; break; // 2 Byte Integer (unsigned) case 3: Type = SG_DATATYPE_Short ; break; // 2 Byte Integer (signed) case 4: Type = SG_DATATYPE_DWord ; break; // 4 Byte Integer (unsigned) case 5: Type = SG_DATATYPE_Int ; break; // 4 Byte Integer (signed) default: Type = SG_DATATYPE_Float ; break; // 4 Byte Floating Point case 7: Type = SG_DATATYPE_Double; break; // 8 Byte Floating Point } //----------------------------------------------------- CSG_Grid *pGrid = SG_Create_Grid(Type, nx, ny, Parameters("CELLSIZE")->asDouble(), Parameters("XMIN" )->asDouble(), Parameters("YMIN" )->asDouble() ); pGrid->Set_Name (SG_File_Get_Name(Parameters("FILE")->asString(), false)); pGrid->Set_Unit (Parameters("UNIT" )->asString()); pGrid->Set_NoData_Value(Parameters("NODATA" )->asDouble()); pGrid->Set_Scaling (Parameters("ZFACTOR")->asDouble()); Parameters("GRID")->Set_Value(pGrid); //----------------------------------------------------- bool bDown = Parameters("TOPDOWN")->asInt() == 1; for(int y=0; y<ny && Set_Progress(y, ny); y++) { CSG_Table_Record *pRecord = Table.Get_Record(y + nHeadLines); for(int x=0, yy=bDown?ny-1-y:y; x<nx; x++) { pGrid->Set_Value(x, yy, pRecord->asDouble(x)); } } //----------------------------------------------------- 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 DLG_Open(wxString &File_Path, const wxString &Caption, const wxString &Filter) { return( DLG_Open(File_Path, Caption, SG_File_Get_Path(File_Path).w_str(), SG_File_Get_Name(File_Path, true).w_str(), Filter) ); }
bool DLG_Save(wxString &File_Path, const wxChar *Caption, const wxChar *Filter) { return( DLG_Save(File_Path, Caption, SG_File_Get_Path(File_Path), SG_File_Get_Name(File_Path, true), Filter) ); }
//--------------------------------------------------------- void CWKSP_Module_Manager::_Make_HTML_Docs(void) { CSG_Parameters Options(NULL, LNG("Create HTML Documentation"), LNG("")); Options.Add_FilePath(NULL, "DIR", LNG("Choose Documentation Folder"), LNG(""), NULL, NULL, true, true); if( !DLG_Parameters(&Options) ) { return; } //----------------------------------------------------- bool bDirectory; CSG_File Stream_Module, Stream_Lib, Stream_Libs, Stream_List; wxString LibName, Directory, Main, s; wxFileName FileName; MSG_General_Add(wxString::Format(wxT("%s..."), LNG("Creating module documentation files")), true, true); bDirectory = wxDirExists(Options("DIR")->asString()); Directory = bDirectory ? Options("DIR")->asString() : SG_File_Get_Path(g_pSAGA->Get_App_Path()).c_str(); //----------------------------------------------------- FileName.AssignDir (Directory); FileName.SetExt (wxT("html")); FileName.SetName (wxT("index")); Stream_Libs.Open(FileName.GetFullPath().c_str(), SG_FILE_W, false); Stream_Libs.Printf(SG_T("<html><head><title>SAGA - System for Automated Geoscientific Analyses</title></head><body>")); Stream_Libs.Printf(SG_T("<h1><a href=\"http://www.saga-gis.org\">SAGA - System for Automated Geoscientific Analyses</a></h1>")); Stream_Libs.Printf(SG_T("<h2>%s</h2>\n<ul>\n"), LNG("Module Library Descriptions")); Main = FileName.GetFullPath(); //----------------------------------------------------- for(int i=0; i<Get_Count() && PROGRESSBAR_Set_Position(i, Get_Count()); i++) { LibName = SG_File_Get_Name(Get_Library(i)->Get_File_Name(), false).c_str(); FileName.AssignDir (bDirectory ? Directory.c_str() : SG_File_Get_Path(Get_Library(i)->Get_File_Name()).c_str()); FileName.AppendDir (LibName); FileName.SetExt (wxT("html")); if( wxDirExists(FileName.GetPath()) || wxMkdir(FileName.GetPath()) ) { //--------------------------------------------- // create a frame FileName.SetName(wxT("index")); if( Stream_Lib.Open(FileName.GetFullPath().c_str(), SG_FILE_W, false) ) { if( Stream_Libs.is_Open() ) { s = Get_FilePath_Relative(Directory.c_str(), FileName.GetFullPath().c_str()).c_str(); if( s[0] == '\\' ) s = s.AfterFirst('\\'); if(s[0]=='/') s = s.AfterFirst('/'); Stream_Libs.Printf(wxT("<li><a href=\"%s\">%s</a></li>\n"), s.c_str(), Get_Library(i)->Get_Name().c_str()); } Stream_Lib.Printf(SG_T("<html><head><title>SAGA - System for Automated Geoscientific Analyses</title></head>")); Stream_Lib.Printf(SG_T("<frameset cols=\"200,*\" frameborder=\"0\" framespacing=\"0\" border=\"0\">")); Stream_Lib.Printf(SG_T(" <frame frameborder=\"0\" noresize src=\"modules.html\" name=\"MODULES\">")); Stream_Lib.Printf(SG_T(" <frame frameborder=\"0\" noresize src=\"%s.html\" name=\"CONTENT\">") , LibName.c_str()); Stream_Lib.Printf(SG_T("</frameset></html>")); } //--------------------------------------------- // write the modules if( bDirectory ) s = wxT("./../index"); else s = Get_FilePath_Relative(Main.c_str(), FileName.GetFullPath().c_str()).c_str(); if( s[0] == '\\' ) s = s.AfterFirst('\\'); if(s[0]=='/') s = s.AfterFirst('/'); FileName.SetName(wxT("modules")); Stream_List.Open(FileName.GetFullPath().c_str(), SG_FILE_W, false); Stream_List.Printf(SG_T("<body bgcolor=\"#CCCCCC\">")); Stream_List.Printf(SG_T("<b><a target=\"_top\" href=\"http://www.saga-gis.org\">SAGA</a></b><hr>")); Stream_List.Printf(SG_T("<b><a target=\"_top\" href=\"%s.html\">%s</a></b><hr>"), s.c_str(), LNG("Library Overview")); Stream_List.Printf(SG_T("<b><a target=\"CONTENT\" href=\"%s.html\">%s</a></b><hr><ul>"), LibName.c_str(), Get_Library(i)->Get_Name().c_str()); FileName.SetName(LibName); if( Stream_Lib.Open(FileName.GetFullPath().c_str(), SG_FILE_W, false) ) { Stream_Lib.Printf(wxT("%s<hr><ul>"), Get_Library(i)->Get_Description().c_str()); for(int j=0; j<Get_Library(i)->Get_Count(); j++) { FileName.SetName(wxString::Format(wxT("%s_%02d"), LibName.c_str(), Get_Library(i)->Get_Module(j)->Get_Index())); if( Stream_Module.Open(FileName.GetFullPath().c_str(), SG_FILE_W, false) ) { Stream_Module.Printf(wxT("%s"), Get_Library(i)->Get_Module(j)->Get_Description().c_str()); Stream_Lib .Printf(wxT("<li><a target=\"CONTENT\" href=\"%s\">%s</a></li>"), FileName.GetFullName().c_str(), Get_Library(i)->Get_Module(j)->Get_Name().c_str()); Stream_List.Printf(wxT("<li><a target=\"CONTENT\" href=\"%s\">%s</a></li>"), FileName.GetFullName().c_str(), Get_Library(i)->Get_Module(j)->Get_Name().c_str()); } } } } } //----------------------------------------------------- if( Stream_Libs.is_Open() ) { Stream_Libs.Printf(wxT("</ul>")); } PROCESS_Set_Okay(true); MSG_General_Add(LNG("okay"), false, false, SG_UI_MSG_STYLE_SUCCESS); }
//--------------------------------------------------------- bool CSurfer_Import::On_Execute(void) { //----------------------------------------------------- CSG_String File = Parameters("FILE")->asString(); FILE *Stream = fopen(File.b_str(), "rb"); if( !Stream ) { Error_Set(_TL("failed to open file")); return( false ); } CSG_Grid *pGrid = NULL; char Id[4]; fread(Id, 1, 4 * sizeof(char), Stream); //----------------------------------------------------- if( !strncmp(Id, "DSRB", 4) ) // Surfer 7: Binary... { long lValue, nx, ny; double dValue, dx, dy, xmin, ymin; fread(&lValue, 1, sizeof(long), Stream); // SectionSize... fread(&lValue, 1, sizeof(long), Stream); // Version fread(&lValue, 1, sizeof(long), Stream); if( lValue == 0x44495247 ) // Grid-Header... { fread(&lValue, 1, sizeof(long ), Stream); // SectionSize... fread(&ny , 1, sizeof(long ), Stream); fread(&nx , 1, sizeof(long ), Stream); fread(&xmin , 1, sizeof(double), Stream); fread(&ymin , 1, sizeof(double), Stream); fread(&dx , 1, sizeof(double), Stream); fread(&dy , 1, sizeof(double), Stream); fread(&dValue, 1, sizeof(double), Stream); fread(&dValue, 1, sizeof(double), Stream); fread(&dValue, 1, sizeof(double), Stream); // Rotation (unused)... fread(&dValue, 1, sizeof(double), Stream); // Blank Value... fread(&lValue, 1, sizeof(long ), Stream); // ???... if( lValue == 0x41544144 ) // Load Binary Double... { fread(&lValue, 1, sizeof(long), Stream); // SectionSize... //----------------------------------------- if( !feof(Stream) && (pGrid = SG_Create_Grid(SG_DATATYPE_Double, nx, ny, dx, xmin, ymin)) != NULL ) { double *Line = (double *)SG_Malloc(pGrid->Get_NX() * sizeof(double)); for(int y=0; y<pGrid->Get_NY() && !feof(Stream) && Set_Progress(y, pGrid->Get_NY()); y++) { fread(Line, pGrid->Get_NX(), sizeof(double), Stream); for(int x=0; x<pGrid->Get_NX(); x++) { pGrid->Set_Value(x, y, Line[x]); } } SG_Free(Line); } } } } //----------------------------------------------------- else if( !strncmp(Id, "DSBB", 4) ) // Surfer 6: Binary... { short nx, ny; double dValue, dx, dy, xmin, ymin; fread(&nx , 1, sizeof(short ), Stream); fread(&ny , 1, sizeof(short ), Stream); fread(&xmin , 1, sizeof(double), Stream); fread(&dx , 1, sizeof(double), Stream); dx = (dx - xmin) / (nx - 1.0); // XMax fread(&ymin , 1, sizeof(double), Stream); fread(&dy , 1, sizeof(double), Stream); dy = (dy - ymin) / (ny - 1.0); // YMax... fread(&dValue, 1, sizeof(double), Stream); // ZMin... fread(&dValue, 1, sizeof(double), Stream); // ZMax... //------------------------------------------------- if( !feof(Stream) && (pGrid = SG_Create_Grid(SG_DATATYPE_Float, nx, ny, dx, xmin, ymin)) != NULL ) { float *Line = (float *)SG_Malloc(pGrid->Get_NX() * sizeof(float)); for(int y=0; y<pGrid->Get_NY() && !feof(Stream) && Set_Progress(y, pGrid->Get_NY()); y++) { fread(Line, pGrid->Get_NX(), sizeof(float), Stream); for(int x=0; x<pGrid->Get_NX(); x++) { pGrid->Set_Value(x, y, Line[x]); } } SG_Free(Line); } } //----------------------------------------------------- else if( !strncmp(Id, "DSAA", 4) ) // Surfer 6: ASCII... { int nx, ny; double dx, dy, xmin, ymin, dValue; fscanf(Stream, "%d %d" , &nx , &ny ); fscanf(Stream, "%lf %lf", &xmin , &dx ); dx = (dx - xmin) / (nx - 1.0); fscanf(Stream, "%lf %lf", &ymin , &dy ); dy = (dy - ymin) / (ny - 1.0); fscanf(Stream, "%lf %lf", &dValue, &dValue); //------------------------------------------------- if( !feof(Stream) && (pGrid = SG_Create_Grid(SG_DATATYPE_Float, nx, ny, dx, xmin, ymin)) != NULL ) { for(int y=0; y<pGrid->Get_NY() && !feof(Stream) && Set_Progress(y, pGrid->Get_NY()); y++) { for(int x=0; x<pGrid->Get_NX(); x++) { fscanf(Stream, "%lf", &dValue); pGrid->Set_Value(x, y, dValue); } } } } //----------------------------------------------------- fclose(Stream); if( pGrid ) { pGrid->Set_Name(SG_File_Get_Name(File, false)); pGrid->Set_NoData_Value(Parameters("NODATA")->asInt() == 0 ? NODATAVALUE : Parameters("NODATA_VAL")->asDouble()); Parameters("GRID")->Set_Value(pGrid); return( true ); } return( false ); }
//--------------------------------------------------------- bool CLAS_Import::On_Execute(void) { CSG_Parameter_PointCloud_List *pPointsList; bool bValidity; CSG_Strings Files; int RGBrange; int cntInvalid = 0; bValidity = Parameters("VALID")->asBool(); RGBrange = Parameters("RGB_RANGE")->asInt(); //----------------------------------------------------- if( !Parameters("FILES")->asFilePath()->Get_FilePaths(Files) ) { return( false ); } //----------------------------------------------------- pPointsList = Parameters("POINTS")->asPointCloudList(); pPointsList ->Del_Items(); for(int i=0; i<Files.Get_Count(); i++) { SG_UI_Msg_Add(CSG_String::Format(_TL("Parsing %s ... "), SG_File_Get_Name(Files[i], true).c_str()), true); std::ifstream ifs; ifs.open(Files[i].b_str(), std::ios::in | std::ios::binary); if( !ifs ) { SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unable to open LAS file!"))); continue; } //----------------------------------------------------- // Check if LAS version is supported liblas::LASReader *pReader; try { pReader = new liblas::LASReader(ifs); } catch(std::exception &e) { SG_UI_Msg_Add_Error(CSG_String::Format(_TL("LAS header exception: %s"), e.what())); ifs.close(); return( false ); } catch(...) { SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unknown LAS header exception!"))); ifs.close(); return( false ); } delete (pReader); ifs.clear(); //----------------------------------------------------- liblas::LASReader reader(ifs); liblas::LASHeader const& header = reader.GetHeader(); //----------------------------------------------------- int nFields, iField[VAR_Count]; CSG_PointCloud *pPoints = SG_Create_PointCloud(); pPoints->Set_Name(SG_File_Get_Name(Files[i], false)); nFields = 3; ADD_FIELD("T", VAR_T, _TL("gps-time") , SG_DATATYPE_Double); // SG_DATATYPE_Long ADD_FIELD("i", VAR_i, _TL("intensity") , SG_DATATYPE_Float); // SG_DATATYPE_Word ADD_FIELD("a", VAR_a, _TL("scan angle") , SG_DATATYPE_Float); // SG_DATATYPE_Byte ADD_FIELD("r", VAR_r, _TL("number of the return") , SG_DATATYPE_Int); ADD_FIELD("c", VAR_c, _TL("classification") , SG_DATATYPE_Int); // SG_DATATYPE_Byte ADD_FIELD("u", VAR_u, _TL("user data") , SG_DATATYPE_Double); // SG_DATATYPE_Byte ADD_FIELD("n", VAR_n, _TL("number of returns of given pulse") , SG_DATATYPE_Int); ADD_FIELD("R", VAR_R, _TL("red channel color") , SG_DATATYPE_Int); // SG_DATATYPE_Word ADD_FIELD("G", VAR_G, _TL("green channel color") , SG_DATATYPE_Int); ADD_FIELD("B", VAR_B, _TL("blue channel color") , SG_DATATYPE_Int); ADD_FIELD("e", VAR_e, _TL("edge of flight line flag") , SG_DATATYPE_Char); ADD_FIELD("d", VAR_d, _TL("direction of scan flag") , SG_DATATYPE_Char); ADD_FIELD("p", VAR_p, _TL("point source ID") , SG_DATATYPE_Int); // SG_DATATYPE_Word ADD_FIELD("C", VAR_C, _TL("rgb color") , SG_DATATYPE_Int); //----------------------------------------------------- int iPoint = 0; try { while( reader.ReadNextPoint() ) { if (iPoint % 100000) SG_UI_Process_Set_Progress(iPoint, header.GetPointRecordsCount()); liblas::LASPoint const& point = reader.GetPoint(); if( bValidity ) { if( !point.IsValid() ) { cntInvalid++; continue; } } pPoints->Add_Point(point.GetX(), point.GetY(), point.GetZ()); if( iField[VAR_T] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_T], point.GetTime()); if( iField[VAR_i] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_i], point.GetIntensity()); if( iField[VAR_a] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_a], point.GetScanAngleRank()); if( iField[VAR_r] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_r], point.GetReturnNumber()); if( iField[VAR_c] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_c], point.GetClassification()); if( iField[VAR_u] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_u], point.GetUserData()); if( iField[VAR_n] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_n], point.GetNumberOfReturns()); if( iField[VAR_R] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_R], point.GetColor().GetRed()); if( iField[VAR_G] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_G], point.GetColor().GetGreen()); if( iField[VAR_B] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_B], point.GetColor().GetBlue()); if( iField[VAR_e] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_e], point.GetFlightLineEdge()); if( iField[VAR_d] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_d], point.GetScanDirection()); if( iField[VAR_p] > 0 ) pPoints->Set_Value(iPoint, iField[VAR_p], point.GetPointSourceID()); if( iField[VAR_C] > 0 ) { double r, g, b; r = point.GetColor().GetRed(); g = point.GetColor().GetGreen(); b = point.GetColor().GetBlue(); if (RGBrange == 0) // 16 bit { r = r / 65535 * 255; g = g / 65535 * 255; b = b / 65535 * 255; } pPoints->Set_Value(iPoint, iField[VAR_C], SG_GET_RGB(r, g, b)); } iPoint++; } } catch(std::exception &e) { SG_UI_Msg_Add_Error(CSG_String::Format(_TL("LAS reader exception: %s"), e.what())); ifs.close(); return( false ); } catch(...) { SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unknown LAS reader exception!"))); ifs.close(); return( false ); } ifs.close(); pPointsList->Add_Item(pPoints); DataObject_Add(pPoints); //----------------------------------------------------- CSG_Parameters sParms; DataObject_Get_Parameters(pPoints, sParms); if (sParms("METRIC_ATTRIB") && sParms("COLORS_TYPE") && sParms("METRIC_COLORS") && sParms("METRIC_ZRANGE") && sParms("DISPLAY_VALUE_AGGREGATE")) { sParms("DISPLAY_VALUE_AGGREGATE")->Set_Value(3); // highest z sParms("COLORS_TYPE")->Set_Value(2); // graduated color sParms("METRIC_COLORS")->asColors()->Set_Count(255); // number of colors sParms("METRIC_ATTRIB")->Set_Value(2); // z attrib sParms("METRIC_ZRANGE")->asRange()->Set_Range(pPoints->Get_Minimum(2),pPoints->Get_Maximum(2)); } DataObject_Set_Parameters(pPoints, sParms); SG_UI_Msg_Add(_TL("okay"), false); } //----------------------------------------------------- if( bValidity && cntInvalid > 0 ) SG_UI_Msg_Add(CSG_String::Format(_TL("WARNING: %d invalid points skipped!"), cntInvalid), true); return( true ); }
//--------------------------------------------------------- bool CGrid_Export::On_Execute(void) { //----------------------------------------------------- int y, iy, Method; double dTrans; CSG_Grid *pGrid, *pShade, Grid, Shade; //----------------------------------------------------- pGrid = Parameters("GRID" )->asGrid(); pShade = Parameters("SHADE" )->asGrid(); Method = Parameters("COLOURING" )->asInt (); dTrans = Parameters("SHADE_TRANS" )->asDouble() / 100.0; if( !pGrid ) { return( false ); } //----------------------------------------------------- if( Method == 5 ) // same as in graphical user interface { if( !SG_UI_DataObject_asImage(pGrid, &Grid) ) { Error_Set("could not retrieve colour coding from graphical user interface."); return( false ); } } else { double zMin, zScale; CSG_Colors Colors; CSG_Table LUT; if( SG_UI_Get_Window_Main() ) { Colors.Assign(Parameters("COL_PALETTE")->asColors()); } else { Colors.Set_Palette( Parameters("COL_PALETTE")->asInt (), Parameters("COL_REVERT" )->asBool(), Parameters("COL_COUNT" )->asInt () ); } switch( Method ) { case 0: // stretch to grid's standard deviation zMin = pGrid->Get_Mean() - Parameters("STDDEV")->asDouble() * pGrid->Get_StdDev(); zScale = Colors.Get_Count() / (2 * Parameters("STDDEV")->asDouble() * pGrid->Get_StdDev()); break; case 1: // stretch to grid's value range zMin = pGrid->Get_ZMin(); zScale = Colors.Get_Count() / pGrid->Get_ZRange(); break; case 2: // stretch to specified value range zMin = Parameters("STRETCH")->asRange()->Get_LoVal(); if( zMin >= (zScale = Parameters("STRETCH")->asRange()->Get_HiVal()) ) { Error_Set(_TL("invalid user specified value range.")); return( false ); } zScale = Colors.Get_Count() / (zScale - zMin); break; case 3: // lookup table if( !Parameters("LUT")->asTable() || Parameters("LUT")->asTable()->Get_Field_Count() < 5 ) { Error_Set(_TL("invalid lookup table.")); return( false ); } LUT.Create(*Parameters("LUT")->asTable()); break; case 4: // rgb coded values break; } //------------------------------------------------- Grid.Create(*Get_System(), SG_DATATYPE_Int); for(y=0, iy=Get_NY()-1; y<Get_NY() && Set_Progress(y); y++, iy--) { #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { double z = pGrid->asDouble(x, y); if( Method == 3 ) // lookup table { int i, iColor = -1; for(i=0; iColor<0 && i<LUT.Get_Count(); i++) { if( z == LUT[i][3] ) { Grid.Set_Value(x, iy, LUT[iColor = i].asInt(0)); } } for(i=0; iColor<0 && i<LUT.Get_Count(); i++) { if( z >= LUT[i][3] && z <= LUT[i][4] ) { Grid.Set_Value(x, iy, LUT[iColor = i].asInt(0)); } } if( iColor < 0 ) { Grid.Set_NoData(x, iy); } } else if( pGrid->is_NoData(x, y) ) { Grid.Set_NoData(x, iy); } else if( Method == 4 ) // rgb coded values { Grid.Set_Value(x, iy, z); } else { int i = (int)(zScale * (z - zMin)); Grid.Set_Value(x, iy, Colors[i < 0 ? 0 : i >= Colors.Get_Count() ? Colors.Get_Count() - 1 : i]); } } } } //----------------------------------------------------- if( !pShade || pShade->Get_ZRange() <= 0.0 ) { pShade = NULL; } else { double dMinBright, dMaxBright; dMinBright = Parameters("SHADE_BRIGHT")->asRange()->Get_LoVal() / 100.0; dMaxBright = Parameters("SHADE_BRIGHT")->asRange()->Get_HiVal() / 100.0; if( dMinBright >= dMaxBright ) { SG_UI_Msg_Add_Error(_TL("Minimum shade brightness must be lower than maximum shade brightness!")); return( false ); } int nColors = 100; CSG_Colors Colors(nColors, SG_COLORS_BLACK_WHITE, true); //------------------------------------------------- Shade.Create(*Get_System(), SG_DATATYPE_Int); for(y=0, iy=Get_NY()-1; y<Get_NY() && Set_Progress(y); y++, iy--) { #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { if( pShade->is_NoData(x, y) ) { Shade.Set_NoData(x, iy); } else { Shade.Set_Value (x, iy, Colors[(int)(nColors * (dMaxBright - dMinBright) * (pShade->asDouble(x, y) - pShade->Get_ZMin()) / pShade->Get_ZRange() + dMinBright)]); } } } } //----------------------------------------------------- wxImage Image(Get_NX(), Get_NY()); if( Grid.Get_NoData_Count() > 0 ) { Image.SetAlpha(); } for(y=0; y<Get_NY() && Set_Progress(y); y++) { #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { if( Grid.is_NoData(x, y) || (pShade != NULL && Shade.is_NoData(x, y)) ) { if( Image.HasAlpha() ) { Image.SetAlpha(x, y, wxIMAGE_ALPHA_TRANSPARENT); } Image.SetRGB(x, y, 255, 255, 255); } else { if( Image.HasAlpha() ) { Image.SetAlpha(x, y, wxIMAGE_ALPHA_OPAQUE); } int r, g, b, c = Grid.asInt(x, y); r = SG_GET_R(c); g = SG_GET_G(c); b = SG_GET_B(c); if( pShade ) { c = Shade.asInt(x, y); r = dTrans * r + SG_GET_R(c) * (1.0 - dTrans); g = dTrans * g + SG_GET_G(c) * (1.0 - dTrans); b = dTrans * b + SG_GET_B(c) * (1.0 - dTrans); } Image.SetRGB(x, y, r, g, b); } } } //------------------------------------------------- CSG_String fName(Parameters("FILE")->asString()); if( !SG_File_Cmp_Extension(fName, SG_T("bmp")) && !SG_File_Cmp_Extension(fName, SG_T("jpg")) && !SG_File_Cmp_Extension(fName, SG_T("pcx")) && !SG_File_Cmp_Extension(fName, SG_T("png")) && !SG_File_Cmp_Extension(fName, SG_T("tif")) ) { fName = SG_File_Make_Path(NULL, fName, SG_T("png")); Parameters("FILE")->Set_Value(fName); } //----------------------------------------------------- wxImageHandler *pImgHandler = NULL; if( !SG_UI_Get_Window_Main() ) { 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; #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.SaveFile(fName.c_str()) ) { Error_Set(CSG_String::Format(SG_T("%s [%s]"), _TL("could not save image file"), fName.c_str())); return( false ); } pGrid->Get_Projection().Save(SG_File_Make_Path(NULL, fName, SG_T("prj")), SG_PROJ_FMT_WKT); //----------------------------------------------------- CSG_File Stream; if( SG_File_Cmp_Extension(fName, SG_T("bmp")) ) Stream.Open(SG_File_Make_Path(NULL, fName, SG_T("bpw")), SG_FILE_W, false); else if( SG_File_Cmp_Extension(fName, SG_T("jpg")) ) Stream.Open(SG_File_Make_Path(NULL, fName, SG_T("jgw")), SG_FILE_W, false); else if( SG_File_Cmp_Extension(fName, SG_T("pcx")) ) Stream.Open(SG_File_Make_Path(NULL, fName, SG_T("pxw")), SG_FILE_W, false); else if( SG_File_Cmp_Extension(fName, SG_T("png")) ) Stream.Open(SG_File_Make_Path(NULL, fName, SG_T("pgw")), SG_FILE_W, false); else if( SG_File_Cmp_Extension(fName, SG_T("tif")) ) Stream.Open(SG_File_Make_Path(NULL, fName, SG_T("tfw")), SG_FILE_W, false); if( Stream.is_Open() ) { Stream.Printf(SG_T("%.10f\n%f\n%f\n%.10f\n%.10f\n%.10f\n"), pGrid->Get_Cellsize(), 0.0, 0.0, -pGrid->Get_Cellsize(), pGrid->Get_XMin(), pGrid->Get_YMax() ); } //----------------------------------------------------- if( Parameters("FILE_KML")->asBool() ) { CSG_MetaData KML; KML.Set_Name("kml"); KML.Add_Property("xmlns", "http://www.opengis.net/kml/2.2"); // CSG_MetaData *pFolder = KML.Add_Child("Folder"); // pFolder->Add_Child("name" , "Raster exported from SAGA"); // pFolder->Add_Child("description", "System for Automated Geoscientific Analyses - www.saga-gis.org"); // CSG_MetaData *pOverlay = pFolder->Add_Child("GroundOverlay"); CSG_MetaData *pOverlay = KML.Add_Child("GroundOverlay"); pOverlay->Add_Child("name" , pGrid->Get_Name()); pOverlay->Add_Child("description", pGrid->Get_Description()); pOverlay->Add_Child("Icon" )->Add_Child("href", SG_File_Get_Name(fName, true)); pOverlay->Add_Child("LatLonBox" ); pOverlay->Get_Child("LatLonBox" )->Add_Child("north", pGrid->Get_YMax()); pOverlay->Get_Child("LatLonBox" )->Add_Child("south", pGrid->Get_YMin()); pOverlay->Get_Child("LatLonBox" )->Add_Child("east" , pGrid->Get_XMax()); pOverlay->Get_Child("LatLonBox" )->Add_Child("west" , pGrid->Get_XMin()); KML.Save(fName, SG_T("kml")); } //----------------------------------------------------- if( !SG_UI_Get_Window_Main() && pImgHandler != NULL) { wxImage::RemoveHandler(pImgHandler->GetName()); } //----------------------------------------------------- return( true ); }
bool CGPX2SHP::On_Execute(void) { CSG_String sCmd; CSG_String sFile = Parameters("FILE")->asString(); CSG_String sBasePath = Parameters("BASEPATH")->asString(); CSG_String sShapefile; bool bWaypoints = Parameters("WAYPOINTS")->asBool(); bool bTrackpoints = Parameters("TRACKPOINTS")->asBool(); bool bRoutes = Parameters("ROUTES")->asBool(); bool bAdd = Parameters("ADD")->asBool(); CSG_Shapes *pShapes; sCmd = sBasePath + SG_T("\\gpx2shp "); if (bWaypoints) { sCmd += SG_T("-w "); }//if if (bTrackpoints) { sCmd += SG_T("-t "); }//if if (bRoutes) { sCmd += SG_T("-r "); }//if sCmd += sFile; system(sCmd.b_str()); if( bAdd ) { CSG_String sDir(SG_File_Get_Path(sFile)), sName(SG_File_Get_Name(sFile, false)); //------------------------------------------------- sFile = SG_File_Make_Path(sDir, sName + SG_T("_wpt"), SG_T("shp")); pShapes = SG_Create_Shapes(sFile); if( pShapes->is_Valid() ) DataObject_Add(pShapes, false); else delete(pShapes); //------------------------------------------------- sFile = SG_File_Make_Path(sDir, sName + SG_T("_trk"), SG_T("shp")); pShapes = SG_Create_Shapes(sFile); if( pShapes->is_Valid() ) DataObject_Add(pShapes, false); else delete(pShapes); //------------------------------------------------- sFile = SG_File_Make_Path(sDir, sName + SG_T("_rte"), SG_T("shp")); pShapes = SG_Create_Shapes(sFile); if( pShapes->is_Valid() ) DataObject_Add(pShapes, false); else delete(pShapes); }//if return true; }//method
//--------------------------------------------------------- bool CWRF_Export::Save(const CSG_String &Directory, CSG_Parameter_Grid_List *pGrids) { //----------------------------------------------------- // 00001-00600.00001-00600 // 01234567890123456789012 int xOffset = m_Index.m_TILE_BDR + (int)(0.5 + (Get_XMin() - m_Index.m_KNOWN_LON) / Get_Cellsize()); int yOffset = m_Index.m_TILE_BDR + (int)(0.5 + (Get_YMin() - m_Index.m_KNOWN_LAT) / Get_Cellsize()); CSG_String Name = SG_File_Get_Name(Directory, true); Name.Printf(SG_T("%05d-%05d.%05d-%05d"), xOffset + 1, xOffset + m_Index.m_TILE_X, yOffset + 1, yOffset + m_Index.m_TILE_Y); //----------------------------------------------------- CSG_File Stream; if( !Stream.Open(SG_File_Make_Path(Directory, Name), SG_FILE_W) ) { Error_Set(_TL("data file could not be openend")); return( false ); } //----------------------------------------------------- char *pLine, *pValue; int x, y, nBytes_Line; nBytes_Line = Get_NX() * m_Index.m_WORDSIZE; pLine = (char *)SG_Malloc(nBytes_Line); //----------------------------------------------------- for(int z=0; z<pGrids->Get_Count() && Process_Get_Okay(); z++) { CSG_Grid *pGrid = pGrids->asGrid(z); //------------------------------------------------- for(y=0; y<pGrid->Get_NY() && !Stream.is_EOF() && Set_Progress(y, pGrid->Get_NY()); y++) { int yy = m_Index.m_ROW_ORDER == VAL_TOP_BOTTOM ? pGrid->Get_NY() - 1 - y : y; for(x=0, pValue=pLine; x<pGrid->Get_NX(); x++, pValue+=m_Index.m_WORDSIZE) { if( m_Index.m_SIGNED ) { switch( m_Index.m_WORDSIZE ) { case 1: *((signed char *)pValue) = (signed char )pGrid->asInt(x, yy); break; case 2: *((signed short *)pValue) = (signed short )pGrid->asInt(x, yy); break; case 4: *((signed int *)pValue) = (signed int )pGrid->asInt(x, yy); break; } } else { switch( m_Index.m_WORDSIZE ) { case 1: *((unsigned char *)pValue) = (unsigned char )pGrid->asInt(x, yy); break; case 2: *((unsigned short *)pValue) = (unsigned short)pGrid->asInt(x, yy); break; case 4: *((unsigned int *)pValue) = (unsigned int )pGrid->asInt(x, yy); break; } } if( m_Index.m_ENDIAN == VAL_ENDIAN_BIG ) { SG_Swap_Bytes(pValue, m_Index.m_WORDSIZE); } } Stream.Write(pLine, sizeof(char), nBytes_Line); } } //----------------------------------------------------- SG_Free(pLine); return( true ); }
//--------------------------------------------------------- bool CWASP_MAP_Import::On_Execute(void) { int n, Method, nLength; double z, dz, zMin, rLeft, rRight; FILE *Stream; TSG_Point p, pu[2], pm[2]; CSG_String fName, sLine; CSG_Shape *pLine; CSG_Shapes *pLines; //----------------------------------------------------- pLines = Parameters("SHAPES") ->asShapes(); fName = Parameters("FILE") ->asString(); Method = Parameters("METHOD") ->asInt(); //----------------------------------------------------- if( (Stream = fopen(fName.b_str(), "r")) != NULL ) { fseek(Stream, 0, SEEK_END); nLength = ftell(Stream); fseek(Stream, 0, SEEK_SET); pLines->Create(SHAPE_TYPE_Line, SG_File_Get_Name(fName, false)); switch( Method ) { case 0: // elevation pLines->Add_Field("Z" , SG_DATATYPE_Double); break; case 1: // roughness pLines->Add_Field("RLEFT" , SG_DATATYPE_Double); pLines->Add_Field("RRIGHT" , SG_DATATYPE_Double); break; case 2: // elevation and roughness pLines->Add_Field("Z" , SG_DATATYPE_Double); pLines->Add_Field("RLEFT" , SG_DATATYPE_Double); pLines->Add_Field("RRIGHT" , SG_DATATYPE_Double); break; } // 1) Text string identifying the terrain map: + ... SG_Read_Line(Stream, sLine); // 2) Fixed point #1 in user and metric [m] coordinates: // X1(user) Y1(user) X1(metric) Y1(metric) fscanf(Stream, "%lf %lf %lf %lf", &pu[0].x, &pu[0].y, &pm[0].x, &pm[0].y); // 3) Fixed point #2 in user and metric [m] coordinates: // X2(user) Y2(user) X2(metric) Y2(metric) fscanf(Stream, "%lf %lf %lf %lf", &pu[1].x, &pu[1].y, &pm[1].x, &pm[1].y); // 4) Scaling factor and offset for height scale (Z): // Zmetric = {scaling factor}(Zuser + {offset}) fscanf(Stream, "%lf %lf", &dz, &zMin); while( !feof(Stream) && Set_Progress(ftell(Stream), nLength) ) { pLine = NULL; switch( Method ) { case 0: // elevation // 5a) Height contour: elevation (Z) and number of points (n) in line: // Z n fscanf(Stream, "%lf %d", &z, &n); if( !feof(Stream) && n > 1 ) { pLine = pLines->Add_Shape(); pLine->Set_Value(0, zMin + dz * z); } break; case 1: // roughness // 5b) Roughness change line: // roughness lengths to the left (z0l) and right (z0r) side of the line, // respectively, and number of points: // z0l z0r n fscanf(Stream, "%lf %lf %d", &rLeft, &rRight, &n); if( !feof(Stream) && n > 1 ) { pLine = pLines->Add_Shape(); pLine->Set_Value(0, rLeft); pLine->Set_Value(1, rRight); } break; case 2: // elevation and roughness // 5c) Roughness and contour line: // roughness lengths to the left and right of the line, // respectively, elevation and number of points: // z0l z0r Z n fscanf(Stream, "%lf %lf %lf %d", &rLeft, &rRight, &z, &n); if( !feof(Stream) && n > 1 ) { pLine = pLines->Add_Shape(); pLine->Set_Value(0, zMin + dz * z); pLine->Set_Value(1, rLeft); pLine->Set_Value(2, rRight); } break; } // 6–) Cartesian coordinates (X, Y) of line described in 5a, 5b or 5c: // X1 Y1 [... Xn Yn] // Xn+1 Yn+1 // ... where [] embrace optional numbers and n is > 0 for(int i=0; i<n && !feof(Stream) && Process_Get_Okay(false); i++) { fscanf(Stream, "%lf %lf", &p.x, &p.y); pLine->Add_Point(p); } } fclose(Stream); return( true ); } return( false ); }
//--------------------------------------------------------- bool CXYZ_Import::On_Execute(void) { int nx, ny, nValues, fLength; double x, y, z, xMin, yMin, xMax, yMax, Cellsize; CSG_File Stream; CSG_String FileName, sLine; CSG_Grid *pGrid, *pCount; FileName = Parameters("FILENAME")->asString(); Cellsize = Parameters("CELLSIZE")->asDouble(); switch( Parameters("SEPARATOR")->asInt() ) { case 0: m_Separator = SG_T(' '); break; case 1: m_Separator = SG_T('\t'); break; case 2: m_Separator = SG_T(','); break; case 3: m_Separator = SG_T(';'); break; } if( Cellsize > 0.0 && Stream.Open(FileName, SG_FILE_R, false) ) { if( Parameters("CAPTION")->asBool() ) { Stream.Read_Line(sLine); } fLength = Stream.Length(); nValues = 0; xMin = xMax = 0; yMin = yMax = 0; while( Read_Values(Stream, x, y, z) && Set_Progress(Stream.Tell(), fLength) ) { if( nValues == 0 ) { xMin = xMax = x; yMin = yMax = y; } else { if( xMin > x ) xMin = x; else if( xMax < x ) xMax = x; if( yMin > y ) yMin = y; else if( yMax < y ) yMax = y; } nValues++; } //------------------------------------------------- if( Process_Get_Okay() && xMin < xMax && yMin < yMax ) { nx = 1 + (int)((xMax - xMin) / Cellsize); ny = 1 + (int)((yMax - yMin) / Cellsize); Parameters("GRID" )->Set_Value(pGrid = SG_Create_Grid(SG_DATATYPE_Float, nx, ny, Cellsize, xMin, yMin)); Parameters("COUNT")->Set_Value(pCount = SG_Create_Grid(SG_DATATYPE_Byte , nx, ny, Cellsize, xMin, yMin)); if( pGrid && pCount ) { pGrid ->Set_Name(FileName = SG_File_Get_Name(FileName, false)); pCount ->Set_Name(CSG_String::Format(SG_T("%s [%s]"), FileName.c_str(), _TL("Count"))); Stream.Seek_Start(); if( Parameters("CAPTION")->asBool() ) { Stream.Read_Line(sLine); } while( Read_Values(Stream, x, y, z) && Set_Progress(Stream.Tell(), fLength) ) { if( pGrid->Get_System().Get_World_to_Grid(nx, ny, x, y) ) { pGrid ->Add_Value(nx, ny, z); pCount->Add_Value(nx, ny, 1.0); } } for(ny=0; ny<pGrid->Get_NY() && Set_Progress(ny, pGrid->Get_NY()); ny++) { for(nx=0; nx<pGrid->Get_NX(); nx++) { nValues = pCount->asInt(nx, ny); if( nValues == 0 ) { pGrid->Set_NoData(nx, ny); } else if( nValues > 1 ) { pGrid->Mul_Value(nx, ny, 1.0 / nValues); } } } return( true ); } } } return( false ); }
//--------------------------------------------------------- bool CDXF_Import::On_Execute(void) { CSG_String fName = Parameters("FILE")->asString(); Parameters("TABLES")->asTableList() ->Del_Items(); Parameters("SHAPES")->asShapesList()->Del_Items(); m_Filter = Parameters("FILTER") ->asInt(); m_dArc = Parameters("DCIRCLE") ->asDouble() * M_DEG_TO_RAD; //----------------------------------------------------- if( SG_File_Exists(fName) ) { m_pLayers = SG_Create_Table(); m_pLayers ->Set_Name(CSG_String::Format(SG_T("%s [%s]"), SG_File_Get_Name(fName, false).c_str(), _TL("Layers"))); m_pLayers ->Add_Field("LAYER" , SG_DATATYPE_String); m_pLayers ->Add_Field("FLAGS" , SG_DATATYPE_Int); m_pBlocks = SG_Create_Table(); m_pBlocks ->Set_Name(CSG_String::Format(SG_T("%s [%s]"), SG_File_Get_Name(fName, false).c_str(), _TL("Blocks"))); m_pBlocks ->Add_Field("BLOCK" , SG_DATATYPE_String); m_pBlocks ->Add_Field("FLAGS" , SG_DATATYPE_Int); m_pBlocks ->Add_Field("X" , SG_DATATYPE_Double); m_pBlocks ->Add_Field("Y" , SG_DATATYPE_Double); m_pBlocks ->Add_Field("Z" , SG_DATATYPE_Double); m_pPoints = SG_Create_Shapes(SHAPE_TYPE_Point , CSG_String::Format(SG_T("%s [%s]"), SG_File_Get_Name(fName, false).c_str(), _TL("Points"))); m_pPoints ->Add_Field("LAYER" , SG_DATATYPE_String); m_pPoints ->Add_Field("Z" , SG_DATATYPE_Double); m_pLines = SG_Create_Shapes(SHAPE_TYPE_Line , CSG_String::Format(SG_T("%s [%s]"), SG_File_Get_Name(fName, false).c_str(), _TL("Lines"))); m_pLines ->Add_Field("LAYER" , SG_DATATYPE_String); m_pLines ->Add_Field("Z1" , SG_DATATYPE_Double); m_pLines ->Add_Field("Z2" , SG_DATATYPE_Double); m_pPolyLines = SG_Create_Shapes(SHAPE_TYPE_Line , CSG_String::Format(SG_T("%s [%s]"), SG_File_Get_Name(fName, false).c_str(), _TL("Polylines"))); m_pPolyLines ->Add_Field("LAYER" , SG_DATATYPE_String); m_pPolyLines ->Add_Field("FLAGS" , SG_DATATYPE_Int); m_pPolygons = SG_Create_Shapes(SHAPE_TYPE_Polygon , CSG_String::Format(SG_T("%s [%s]"), SG_File_Get_Name(fName, false).c_str(), _TL("Polygons"))); m_pPolygons ->Add_Field("LAYER" , SG_DATATYPE_String); m_pPolygons ->Add_Field("FLAGS" , SG_DATATYPE_Int); m_pCircles = SG_Create_Shapes(SHAPE_TYPE_Line , CSG_String::Format(SG_T("%s [%s]"), SG_File_Get_Name(fName, false).c_str(), _TL("Circles"))); m_pCircles ->Add_Field("LAYER" , SG_DATATYPE_String); m_pCircles ->Add_Field("FLAGS" , SG_DATATYPE_Int); m_pTriangles = SG_Create_Shapes(SHAPE_TYPE_Polygon , CSG_String::Format(SG_T("%s [%s]"), SG_File_Get_Name(fName, false).c_str(), _TL("Triangles"))); m_pTriangles ->Add_Field("LAYER" , SG_DATATYPE_String); m_pTriangles ->Add_Field("THICK" , SG_DATATYPE_Int); m_pTriangles ->Add_Field("Z1" , SG_DATATYPE_Double); m_pTriangles ->Add_Field("Z2" , SG_DATATYPE_Double); m_pTriangles ->Add_Field("Z3" , SG_DATATYPE_Double); m_pText = SG_Create_Shapes(SHAPE_TYPE_Point , CSG_String::Format(SG_T("%s [%s]"), SG_File_Get_Name(fName, false).c_str(), _TL("Text"))); m_pText ->Add_Field("LAYER" , SG_DATATYPE_String); m_pText ->Add_Field("Z" , SG_DATATYPE_Double); m_pText ->Add_Field("TEXT" , SG_DATATYPE_String); m_pText ->Add_Field("HEIGHT", SG_DATATYPE_Int); m_pText ->Add_Field("ANGLE" , SG_DATATYPE_Double); m_pText ->Add_Field("APX" , SG_DATATYPE_Double); m_pText ->Add_Field("APY" , SG_DATATYPE_Double); m_pText ->Add_Field("APZ" , SG_DATATYPE_Double); m_pText ->Add_Field("SCALE" , SG_DATATYPE_Double); m_pText ->Add_Field("HJUST" , SG_DATATYPE_Int); m_pText ->Add_Field("VJUST" , SG_DATATYPE_Int); m_pText ->Add_Field("STYLE" , SG_DATATYPE_String); m_pText ->Add_Field("FLAGS" , SG_DATATYPE_Int); //------------------------------------------------- m_Offset.x = 0.0; m_Offset.y = 0.0; m_Offset.z = 0.0; m_pPolyLine = NULL; DL_Dxf *pDXF = new DL_Dxf(); pDXF->in(fName.b_str(), this); delete(pDXF); //------------------------------------------------- ADD_RESULT("TABLES", m_pLayers); ADD_RESULT("TABLES", m_pBlocks); ADD_RESULT("SHAPES", m_pPoints); ADD_RESULT("SHAPES", m_pLines); ADD_RESULT("SHAPES", m_pPolyLines); ADD_RESULT("SHAPES", m_pPolygons); ADD_RESULT("SHAPES", m_pCircles); ADD_RESULT("SHAPES", m_pTriangles); ADD_RESULT("SHAPES", m_pText); } //----------------------------------------------------- return( Parameters("SHAPES")->asShapesList()->Get_Count() > 0 ); }
//--------------------------------------------------------- bool CPointCloud_Create_Tileshape_From_SPCVF::On_Execute(void) { CSG_String sFileName; CSG_Shapes *pShapes; CSG_MetaData SPCVF; CSG_String sPathSPCVF, sFilePath; int iPoints; double dBBoxXMin, dBBoxYMin, dBBoxXMax, dBBoxYMax; //----------------------------------------------------- sFileName = Parameters("FILENAME")->asString(); pShapes = Parameters("TILE_SHP")->asShapes(); //----------------------------------------------------- if( !SPCVF.Create(sFileName) || SPCVF.Get_Name().CmpNoCase(SG_T("SPCVFDataset")) ) { SG_UI_Msg_Add_Error(_TL("Please provide a valid *.scpvf file!")); return( false ); } //----------------------------------------------------- CSG_String sMethodPaths; SPCVF.Get_Property(SG_T("Paths"), sMethodPaths); if( !sMethodPaths.CmpNoCase(SG_T("absolute")) ) { sPathSPCVF = SG_T(""); } else if( !sMethodPaths.CmpNoCase(SG_T("relative")) ) { sPathSPCVF = SG_File_Get_Path(sFileName); sPathSPCVF.Replace(SG_T("\\"), SG_T("/")); } else { SG_UI_Msg_Add_Error(_TL("Encountered invalid path description in *.spcvf file!")); return( false ); } //----------------------------------------------------- pShapes->Destroy(); pShapes->Add_Field(_TL("ID"), SG_DATATYPE_Int); pShapes->Add_Field(_TL("Filepath"), SG_DATATYPE_String); pShapes->Add_Field(_TL("File"), SG_DATATYPE_String); pShapes->Add_Field(_TL("Points"), SG_DATATYPE_Int); pShapes->Set_Name(CSG_String::Format(_TL("Tileshape_%s"), SG_File_Get_Name(sFileName, false).c_str())); //----------------------------------------------------- CSG_MetaData *pDatasets = SPCVF.Get_Child(SG_T("Datasets")); for(int i=0; i<pDatasets->Get_Children_Count(); i++) { CSG_MetaData *pDataset = pDatasets->Get_Child(i); CSG_MetaData *pBBox = pDataset->Get_Child(SG_T("BBox")); pDataset->Get_Property(SG_T("File"), sFilePath); pDataset->Get_Property(SG_T("Points"), iPoints); pBBox->Get_Property(SG_T("XMin"), dBBoxXMin); pBBox->Get_Property(SG_T("YMin"), dBBoxYMin); pBBox->Get_Property(SG_T("XMax"), dBBoxXMax); pBBox->Get_Property(SG_T("YMax"), dBBoxYMax); //----------------------------------------------------- CSG_Shape *pShape = pShapes->Add_Shape(); pShape->Add_Point(dBBoxXMin, dBBoxYMin); pShape->Add_Point(dBBoxXMin, dBBoxYMax); pShape->Add_Point(dBBoxXMax, dBBoxYMax); pShape->Add_Point(dBBoxXMax, dBBoxYMin); pShape->Set_Value(0, i + 1); if( sPathSPCVF.Length() == 0 ) // absolute paths { pShape->Set_Value(1, sFilePath.BeforeLast('/')); pShape->Set_Value(2, sFilePath.AfterLast('/')); } else // relative paths { pShape->Set_Value(1, sPathSPCVF); pShape->Set_Value(2, sFilePath); } pShape->Set_Value(3, iPoints); } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CPointCloud_From_Text_File::On_Execute(void) { CSG_String fileName; int iField, iType; CSG_String Name, Types, s; CSG_PointCloud *pPoints; CSG_Parameters P; CSG_Parameter *pNode; int xField, yField, zField, nAttribs; bool bSkipHeader; char fieldSep; std::vector<int> vCol; std::ifstream tabStream; std::string tabLine; double lines; long cntPt, cntInvalid; double x, y, z; //----------------------------------------------------- fileName = Parameters("FILE") ->asString(); xField = Parameters("XFIELD") ->asInt() - 1; yField = Parameters("YFIELD") ->asInt() - 1; zField = Parameters("ZFIELD") ->asInt() - 1; bSkipHeader = Parameters("SKIP_HEADER") ->asBool(); switch (Parameters("FIELDSEP")->asInt()) { default: case 0: fieldSep = '\t'; break; case 1: fieldSep = ' '; break; case 2: fieldSep = ','; break; } pPoints = SG_Create_PointCloud(); pPoints->Create(); pPoints->Set_Name(SG_File_Get_Name(fileName, false)); Parameters("POINTS")->Set_Value(pPoints); DataObject_Add(pPoints); //----------------------------------------------------- if (SG_UI_Get_Window_Main()) { nAttribs = Parameters("ATTRIBS") ->asInt(); Types.Printf(SG_T("%s|%s|%s|%s|%s|"), _TL("1 byte integer"), _TL("2 byte integer"), _TL("4 byte integer"), _TL("4 byte floating point"), _TL("8 byte floating point") ); P.Set_Name(_TL("Attribute Field Properties")); for(iField=1; iField<=nAttribs; iField++) { s.Printf(SG_T("NODE_%03d") , iField); pNode = P.Add_Node(NULL, s, CSG_String::Format(SG_T("%d. %s"), iField, _TL("Field")), _TL("")); s.Printf(SG_T("FIELD_%03d"), iField); P.Add_String(pNode, s, _TL("Name"), _TL(""), s); s.Printf(SG_T("COLUMN_%03d"), iField); P.Add_Value(pNode, s, _TL("Attribute is Column ..."), _TL(""), PARAMETER_TYPE_Int, iField+3, 1, true); s.Printf(SG_T("TYPE_%03d") , iField); P.Add_Choice(pNode, s, _TL("Type"), _TL(""), Types, 3); } //----------------------------------------------------- if( nAttribs > 0 ) { if( Dlg_Parameters(&P, _TL("Field Properties")) ) { for(iField=0; iField<nAttribs; iField++) { Name = P(CSG_String::Format(SG_T("FIELD_%03d" ), iField + 1).c_str())->asString(); iType = P(CSG_String::Format(SG_T("TYPE_%03d" ), iField + 1).c_str())->asInt(); vCol.push_back(P(CSG_String::Format(SG_T("COLUMN_%03d"), iField + 1).c_str())->asInt() - 1); pPoints->Add_Field(Name, Get_Data_Type(iType)); } } else return( false ); } } else // CMD { CSG_String sFields, sNames, sTypes; CSG_String token; int iValue; std::vector<int> vTypes; sFields = Parameters("FIELDS")->asString(); sNames = Parameters("FIELDNAMES")->asString(); sTypes = Parameters("FIELDTYPES")->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 < 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_datatypes(sTypes, ";", SG_TOKEN_STRTOK); while( tkz_datatypes.Has_More_Tokens() ) { token = tkz_datatypes.Get_Next_Token(); if( token.Length() == 0 ) break; if( !token.asInt(iValue) ) { SG_UI_Msg_Add_Error(_TL("Error parsing field type: can't convert to number")); return( false ); } vTypes.push_back(iValue); } CSG_String_Tokenizer tkz_datanames(sNames, ";", SG_TOKEN_STRTOK); int iter = 0; while( tkz_datanames.Has_More_Tokens() ) { token = tkz_datanames.Get_Next_Token(); if( token.Length() == 0 ) break; pPoints->Add_Field(token, Get_Data_Type(vTypes.at(iter))); iter++; } if( vCol.size() != vTypes.size() || (int)vCol.size() != iter ) { SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Number of arguments for attribute fields (%d), names (%d) and types (%d) do not match!"), vCol.size(), iter, vTypes.size())); return( false ); } } // open input stream //--------------------------------------------------------- tabStream.open(fileName.b_str(), std::ifstream::in); if( !tabStream ) { SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unable to open input file!"))); return (false); } tabStream.seekg(0, std::ios::end); // get length of file lines = (double)tabStream.tellg(); tabStream.seekg(0, std::ios::beg); std::getline(tabStream, tabLine); // as a workaround we assume the number of lines from the length of the first line lines = lines / (double)tabStream.tellg(); if( !bSkipHeader ) { tabStream.clear(); // let's forget we may have reached the EOF tabStream.seekg(0, std::ios::beg); // and rewind to the beginning } // import //--------------------------------------------------------- cntPt = cntInvalid = 0; SG_UI_Process_Set_Text(CSG_String::Format(_TL("Importing data ..."))); while( std::getline(tabStream, tabLine) ) { std::istringstream stream(tabLine); std::vector<std::string> tabCols; std::string tabEntry; if( cntPt%10000 == 0 ) { SG_UI_Process_Set_Progress((double)cntPt, lines); } cntPt++; while( std::getline(stream, tabEntry, fieldSep) ) // read every column in this line and fill vector { if (tabEntry.length() == 0) continue; tabCols.push_back(tabEntry); } if ((int)tabCols.size() < (vCol.size() + 3) ) { SG_UI_Msg_Add(CSG_String::Format(_TL("WARNING: Skipping misformatted line: %d!"), cntPt), true); cntInvalid++; continue; } //parse line tokens std::vector<double> fieldValues; fieldValues.resize(vCol.size()); x = strtod(tabCols[xField].c_str(), NULL); y = strtod(tabCols[yField].c_str(), NULL); z = strtod(tabCols[zField].c_str(), NULL); for( int i=0; i<(int)vCol.size(); i++ ) { fieldValues[i] = strtod(tabCols.at(vCol.at(i)).c_str(), NULL); } pPoints->Add_Point(x, y, z); for( int i=0; i<(int)vCol.size(); i++ ) { pPoints->Set_Attribute(i, fieldValues[i]); } } // finalize //--------------------------------------------------------- tabStream.close(); CSG_Parameters sParms; DataObject_Get_Parameters(pPoints, sParms); if (sParms("METRIC_ATTRIB") && sParms("COLORS_TYPE") && sParms("METRIC_COLORS") && sParms("METRIC_ZRANGE") && sParms("DISPLAY_VALUE_AGGREGATE")) { sParms("DISPLAY_VALUE_AGGREGATE")->Set_Value(3); // highest z sParms("COLORS_TYPE")->Set_Value(2); // graduated color sParms("METRIC_COLORS")->asColors()->Set_Count(255); // number of colors sParms("METRIC_ATTRIB")->Set_Value(2); // z attrib sParms("METRIC_ZRANGE")->asRange()->Set_Range(pPoints->Get_Minimum(2),pPoints->Get_Maximum(2)); DataObject_Set_Parameters(pPoints, sParms); DataObject_Update(pPoints); } if (cntInvalid > 0) SG_UI_Msg_Add(CSG_String::Format(SG_T("%s: %d %s"), _TL("WARNING"), cntInvalid, _TL("invalid points have been skipped")), true); SG_UI_Msg_Add(CSG_String::Format(SG_T("%d %s"), (cntPt-cntInvalid), _TL("points have been imported with success")), true); return( true ); }
//--------------------------------------------------------- bool CDirect_Georeferencing_WorldFile::On_Execute(void) { //----------------------------------------------------- int nx = Parameters("NX")->asInt(); int ny = Parameters("NY")->asInt(); if( !m_Georeferencer.Set_Transformation(Parameters, nx, ny) ) { return( false ); } //----------------------------------------------------- CSG_String File = Parameters("FILE")->asString(); if( File.is_Empty() ) { return( false ); } //----------------------------------------------------- CSG_File Stream; if( !Stream.Open(File, SG_FILE_W, false) ) { return( false ); } //----------------------------------------------------- CSG_Matrix R(m_Georeferencer.Get_Transformation()); R *= 0.001 * Parameters("Z")->asDouble() / Parameters("CFL")->asDouble() * Parameters("PXSIZE")->asDouble(); TSG_Point p = m_Georeferencer.Image_to_World(0, ny); Stream.Printf(SG_T("%.10f\n%.10f\n%.10f\n%.10f\n%.10f\n%.10f\n"), R[0][0], // A: pixel size in the x-direction in map units/pixel R[1][0], // D: rotation about y-axis -R[0][1], // B: rotation about x-axis -R[1][1], // E: pixel size in the y-direction in map units, almost always negative p.x, // X: top left pixel center p.y // Y: top left pixel center ); //----------------------------------------------------- CSG_Shapes *pExtents = Parameters("EXTENT")->asShapes(); if( pExtents ) { pExtents->Create(SHAPE_TYPE_Polygon, SG_File_Get_Name(File, false)); pExtents->Add_Field(_TL("NAME"), SG_DATATYPE_String); CSG_Shape *pExtent = pExtents->Add_Shape(); p = m_Georeferencer.Image_to_World( 0, 0); pExtent->Add_Point(p.x, p.y); p = m_Georeferencer.Image_to_World( 0, ny); pExtent->Add_Point(p.x, p.y); p = m_Georeferencer.Image_to_World(nx, ny); pExtent->Add_Point(p.x, p.y); p = m_Georeferencer.Image_to_World(nx, 0); pExtent->Add_Point(p.x, p.y); pExtent->Set_Value(0, SG_File_Get_Name(File, false)); } //----------------------------------------------------- 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 CWRF_Import::Load(const CSG_String &File) { //----------------------------------------------------- // 00001-00600.00001-00600 // 01234567890123456789012 CSG_String Name = SG_File_Get_Name(File, true); if( Name.Length() != 23 || Name[5] != SG_T('-') || Name[11] != SG_T('.') || Name[17] != SG_T('-') ) { Error_Set(_TL("invalid geogrid file name")); return( false ); } int xOffset = Name.asInt() - 1; int yOffset = Name.AfterFirst(SG_T('.')).asInt() - 1; //----------------------------------------------------- CSG_File Stream; if( !Stream.Open(File, SG_FILE_R) ) { Error_Set(_TL("data file could not be openend")); return( false ); } //----------------------------------------------------- TSG_Data_Type Type; switch( m_Index.m_WORDSIZE ) { default: Error_Set(_TL("invalid word size")); return( false ); case 1: Type = m_Index.m_SIGNED == false ? SG_DATATYPE_Byte : SG_DATATYPE_Char; break; case 2: Type = m_Index.m_SIGNED == false ? SG_DATATYPE_Word : SG_DATATYPE_Short; break; case 4: Type = m_Index.m_SIGNED == false ? SG_DATATYPE_DWord : SG_DATATYPE_Int; break; } //----------------------------------------------------- char *pLine, *pValue; int x, y, nBytes_Line; nBytes_Line = (m_Index.m_TILE_X + 2 * m_Index.m_TILE_BDR) * m_Index.m_WORDSIZE; pLine = (char *)SG_Malloc(nBytes_Line); //----------------------------------------------------- for(int z=m_Index.m_TILE_Z_START; z<=m_Index.m_TILE_Z_END && !Stream.is_EOF() && Process_Get_Okay(); z++) { CSG_Grid *pGrid = SG_Create_Grid( Type, m_Index.m_TILE_X + 2 * m_Index.m_TILE_BDR, m_Index.m_TILE_Y + 2 * m_Index.m_TILE_BDR, m_Index.m_DX, m_Index.m_KNOWN_LON + (xOffset - m_Index.m_TILE_BDR) * m_Index.m_DX, m_Index.m_KNOWN_LAT + (yOffset - m_Index.m_TILE_BDR) * m_Index.m_DY ); pGrid->Set_Name (CSG_String::Format(SG_T("%s_%02d"), SG_File_Get_Name(File, false).c_str(), z)); pGrid->Set_Description (m_Index.m_DESCRIPTION); pGrid->Set_Unit (m_Index.m_UNITS); pGrid->Set_NoData_Value (m_Index.m_MISSING_VALUE); pGrid->Set_Scaling (m_Index.m_SCALE_FACTOR); Parameters("GRIDS")->asGridList()->Add_Item(pGrid); //------------------------------------------------- for(y=0; y<pGrid->Get_NY() && !Stream.is_EOF() && Set_Progress(y, pGrid->Get_NY()); y++) { int yy = m_Index.m_ROW_ORDER == VAL_TOP_BOTTOM ? pGrid->Get_NY() - 1 - y : y; Stream.Read(pLine, sizeof(char), nBytes_Line); for(x=0, pValue=pLine; x<pGrid->Get_NX(); x++, pValue+=m_Index.m_WORDSIZE) { if( m_Index.m_ENDIAN == VAL_ENDIAN_BIG ) { SG_Swap_Bytes(pValue, m_Index.m_WORDSIZE); } switch( pGrid->Get_Type() ) { case SG_DATATYPE_Byte: pGrid->Set_Value(x, yy, *(unsigned char *)pValue); break; // 1 Byte Integer (unsigned) case SG_DATATYPE_Char: pGrid->Set_Value(x, yy, *(signed char *)pValue); break; // 1 Byte Integer (signed) case SG_DATATYPE_Word: pGrid->Set_Value(x, yy, *(unsigned short *)pValue); break; // 2 Byte Integer (unsigned) case SG_DATATYPE_Short: pGrid->Set_Value(x, yy, *(signed short *)pValue); break; // 2 Byte Integer (signed) case SG_DATATYPE_DWord: pGrid->Set_Value(x, yy, *(unsigned int *)pValue); break; // 4 Byte Integer (unsigned) case SG_DATATYPE_Int: pGrid->Set_Value(x, yy, *(signed int *)pValue); break; // 4 Byte Integer (signed) } } } } //----------------------------------------------------- SG_Free(pLine); return( true ); }