//--------------------------------------------------------- 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)) ); }
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 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 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 CWASP_MAP_Import::On_Execute(void) { int n, Method, nLength; double z, dz, zMin, rLeft, rRight; CSG_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.Open(fName, SG_FILE_R) ) { nLength = Stream.Length(); 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: + ... Stream.Read_Line(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); pu[0].x = Stream.Scan_Double(); pu[0].y = Stream.Scan_Double(); pm[0].x = Stream.Scan_Double(); pm[0].y = Stream.Scan_Double(); // 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); pu[1].x = Stream.Scan_Double(); pu[1].y = Stream.Scan_Double(); pm[1].x = Stream.Scan_Double(); pm[1].y = Stream.Scan_Double(); // 4) Scaling factor and offset for height scale (Z): // Zmetric = {scaling factor}(Zuser + {offset}) // fscanf(Stream, "%lf %lf", &dz, &zMin); dz = Stream.Scan_Double(); zMin = Stream.Scan_Double(); while( !Stream.is_EOF() && Set_Progress((int)Stream.Tell(), 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); z = Stream.Scan_Double(); n = Stream.Scan_Int (); if( !Stream.is_EOF() && 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); rLeft = Stream.Scan_Double(); rRight = Stream.Scan_Double(); n = Stream.Scan_Int (); if( !Stream.is_EOF() && 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); rLeft = Stream.Scan_Double(); rRight = Stream.Scan_Double(); z = Stream.Scan_Double(); n = Stream.Scan_Int (); if( !Stream.is_EOF() && 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 && !Stream.is_EOF() && Process_Get_Okay(false); i++) { // fscanf(Stream, "%lf %lf", &p.x, &p.y); p.x = Stream.Scan_Double(); p.y = Stream.Scan_Double(); pLine->Add_Point(p); } } return( true ); } return( false ); }
//--------------------------------------------------------- bool CSG_Grid::_Load_Native(const CSG_String &File_Name, TSG_Grid_Memory_Type Memory_Type) { bool bResult, hdr_bFlip, hdr_bSwapBytes; int iType, hdr_Offset, NX, NY; double Cellsize, xMin, yMin; CSG_File Stream; TSG_Data_Type hdr_Type; CSG_Grid_System System; CSG_String File_Data, Value; //----------------------------------------------------- bResult = false; if( Stream.Open(File_Name, SG_FILE_R, false) ) { //------------------------------------------------- // Load Header... hdr_Type = SG_DATATYPE_Undefined; hdr_Offset = 0; hdr_bFlip = false; hdr_bSwapBytes = false; NX = NY = 0; Cellsize = 0.0; xMin = 0.0; yMin = 0.0; //------------------------------------------------- do { switch( _Load_Native_Get_Key(Stream, Value) ) { case GRID_FILE_KEY_NAME: Set_Name (Value); break; case GRID_FILE_KEY_DESCRIPTION: Set_Description (Value); break; case GRID_FILE_KEY_UNITNAME: Set_Unit (Value); break; case GRID_FILE_KEY_CELLCOUNT_X: NX = Value.asInt(); break; case GRID_FILE_KEY_CELLCOUNT_Y: NY = Value.asInt(); break; case GRID_FILE_KEY_POSITION_XMIN: xMin = Value.asDouble(); break; case GRID_FILE_KEY_POSITION_YMIN: yMin = Value.asDouble(); break; case GRID_FILE_KEY_CELLSIZE: Cellsize = Value.asDouble(); break; case GRID_FILE_KEY_Z_FACTOR: m_zFactor = Value.asDouble(); break; case GRID_FILE_KEY_NODATA_VALUE: Set_NoData_Value(Value.asDouble()); break; case GRID_FILE_KEY_DATAFILE_OFFSET: hdr_Offset = Value.asInt(); break; case GRID_FILE_KEY_BYTEORDER_BIG: hdr_bSwapBytes = Value.Find(GRID_FILE_KEY_TRUE) >= 0; break; case GRID_FILE_KEY_TOPTOBOTTOM: hdr_bFlip = Value.Find(GRID_FILE_KEY_TRUE) >= 0; break; case GRID_FILE_KEY_DATAFILE_NAME: if( SG_File_Get_Path(Value).Length() > 0 ) { File_Data = Value; } else { File_Data = SG_File_Make_Path(SG_File_Get_Path(File_Name), Value); } break; case GRID_FILE_KEY_DATAFORMAT: for(iType=0; iType<SG_DATATYPE_Undefined && hdr_Type == SG_DATATYPE_Undefined; iType++) { if( Value.Find(gSG_Data_Type_Identifier[iType]) >= 0 ) { hdr_Type = (TSG_Data_Type)iType; } } break; } } while( !Stream.is_EOF() ); //------------------------------------------------- // Load Data... if( m_System.Assign(Cellsize, xMin, yMin, NX, NY) ) { //--------------------------------------------- // ASCII... if( !SG_Data_Type_is_Numeric(hdr_Type) ) { if( m_Type >= SG_DATATYPE_Undefined ) { m_Type = SG_DATATYPE_Float; } if( Stream.Open(File_Data , SG_FILE_R, false) || Stream.Open(SG_File_Make_Path(NULL, File_Name, SG_T( "dat")) , SG_FILE_R, false) || Stream.Open(SG_File_Make_Path(NULL, File_Name, SG_T("sdat")) , SG_FILE_R, false) ) { Stream.Seek(hdr_Offset); bResult = _Load_ASCII(Stream, Memory_Type, hdr_bFlip); } } //--------------------------------------------- // Binary... else { if( m_Type >= SG_DATATYPE_Undefined ) { m_Type = hdr_Type; } if( (NX = SG_Grid_Cache_Check(m_System, Get_nValueBytes())) > 0 ) { Set_Buffer_Size(NX); if( _Cache_Create(File_Data , hdr_Type, hdr_Offset, hdr_bSwapBytes, hdr_bFlip) || _Cache_Create(SG_File_Make_Path(NULL, File_Name, SG_T( "dat")) , hdr_Type, hdr_Offset, hdr_bSwapBytes, hdr_bFlip) || _Cache_Create(SG_File_Make_Path(NULL, File_Name, SG_T("sdat")) , hdr_Type, hdr_Offset, hdr_bSwapBytes, hdr_bFlip) ) { return( true ); } Memory_Type = GRID_MEMORY_Cache; } if( _Memory_Create(Memory_Type) ) { if( Stream.Open(File_Data , SG_FILE_R, true) || Stream.Open(SG_File_Make_Path(NULL, File_Name, SG_T( "dat")) , SG_FILE_R, true) || Stream.Open(SG_File_Make_Path(NULL, File_Name, SG_T("sdat")) , SG_FILE_R, true) ) { Stream.Seek(hdr_Offset); bResult = _Load_Binary(Stream, hdr_Type, hdr_bFlip, hdr_bSwapBytes); } } } } } return( bResult ); }
//--------------------------------------------------------- bool CSG_Grid::_Load_Surfer(const CSG_String &File_Name, TSG_Grid_Memory_Type Memory_Type) { bool bResult = false; char Identifier[4]; short sValue; int x, y, NX, NY; float *fLine; double dValue, xMin, yMin, Cellsize; CSG_File Stream; if( Stream.Open(File_Name, SG_FILE_R, true) ) { Stream.Read(Identifier, sizeof(char), 4); //------------------------------------------------- // Binary... if( !strncmp(Identifier, "DSBB", 4) ) { Stream.Read(&sValue , sizeof(short)); NX = sValue; Stream.Read(&sValue , sizeof(short)); NY = sValue; Stream.Read(&xMin , sizeof(double)); Stream.Read(&dValue , sizeof(double)); // XMax Cellsize = (dValue - xMin) / (NX - 1.0); Stream.Read(&yMin , sizeof(double)); Stream.Read(&dValue , sizeof(double)); // YMax... //DY = (dValue - yMin) / (NY - 1.0); // we could check, if cellsizes (x/y) equal... Stream.Read(&dValue , sizeof(double)); // ZMin... Stream.Read(&dValue , sizeof(double)); // ZMax... //--------------------------------------------- if( !Stream.is_EOF() && Create(SG_DATATYPE_Float, NX, NY, Cellsize, xMin, yMin, Memory_Type) ) { bResult = true; fLine = (float *)SG_Malloc(Get_NX() * sizeof(float)); for(y=0; y<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { Stream.Read(fLine, sizeof(float), Get_NX()); for(x=0; x<Get_NX(); x++) { Set_Value(x, y, fLine[x]); } } SG_Free(fLine); } } //------------------------------------------------- // ASCII... else if( !strncmp(Identifier, "DSAA", 4) ) { SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%d %d") , &NX , &NY); SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%lf %lf"), &xMin , &dValue); Cellsize = (dValue - xMin) / (NX - 1.0); SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%lf %lf"), &yMin , &dValue); //DY = (dValue - yMin) / (NY - 1.0); SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%lf %lf"), &dValue, &dValue); //--------------------------------------------- if( !Stream.is_EOF() && Create(SG_DATATYPE_Float, NX, NY, Cellsize, xMin, yMin, Memory_Type) ) { bResult = true; for(y=0; y<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(x=0; x<Get_NX(); x++) { SG_FILE_SCANF(Stream.Get_Stream(), SG_T("%lf"), &dValue); Set_Value(x, y, dValue); } } } } //------------------------------------------------- SG_UI_Process_Set_Ready(); } return( bResult ); }
//--------------------------------------------------------- bool CSG_Grid::_Load_Binary(CSG_File &Stream, TSG_Data_Type File_Type, bool bFlip, bool bSwapBytes) { char *Line, *pValue; int x, y, i, iy, dy, nxBytes, nValueBytes; if( Stream.is_Open() && is_Valid() ) { Set_File_Type(GRID_FILE_FORMAT_Binary); if( bFlip ) { y = Get_NY() - 1; dy = -1; } else { y = 0; dy = 1; } //------------------------------------------------- if( File_Type == SG_DATATYPE_Bit ) { nxBytes = Get_NX() / 8 + 1; if( m_Type == File_Type && m_Memory_Type == GRID_MEMORY_Normal ) { for(iy=0; iy<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy) { Stream.Read(m_Values[y], sizeof(char), nxBytes); } } else { Line = (char *)SG_Malloc(nxBytes); for(iy=0; iy<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy) { Stream.Read(Line, sizeof(char), nxBytes); for(x=0, pValue=Line; x<Get_NX(); pValue++) { for(i=0; i<8 && x<Get_NX(); i++, x++) { Set_Value(x, y, (*pValue & m_Bitmask[i]) == 0 ? 0.0 : 1.0); } } } SG_Free(Line); } } //------------------------------------------------- else { nValueBytes = SG_Data_Type_Get_Size(File_Type); nxBytes = Get_NX() * nValueBytes; if( m_Type == File_Type && m_Memory_Type == GRID_MEMORY_Normal && !bSwapBytes ) { for(iy=0; iy<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy) { Stream.Read(m_Values[y], sizeof(char), nxBytes); } } else { Line = (char *)SG_Malloc(nxBytes); for(iy=0; iy<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy) { Stream.Read(Line, sizeof(char), nxBytes); for(x=0, pValue=Line; x<Get_NX(); x++, pValue+=nValueBytes) { if( bSwapBytes ) { _Swap_Bytes(pValue, nValueBytes); } switch( File_Type ) { default: break; case SG_DATATYPE_Byte: Set_Value(x, y, *(BYTE *)pValue); break; case SG_DATATYPE_Char: Set_Value(x, y, *(char *)pValue); break; case SG_DATATYPE_Word: Set_Value(x, y, *(WORD *)pValue); break; case SG_DATATYPE_Short: Set_Value(x, y, *(short *)pValue); break; case SG_DATATYPE_DWord: Set_Value(x, y, *(DWORD *)pValue); break; case SG_DATATYPE_Int: Set_Value(x, y, *(int *)pValue); break; case SG_DATATYPE_Float: Set_Value(x, y, *(float *)pValue); break; case SG_DATATYPE_Double: Set_Value(x, y, *(double *)pValue); break; } } } SG_Free(Line); } } //------------------------------------------------- SG_UI_Process_Set_Ready(); return( true ); } return( false ); }
bool CGridNet::On_Execute(void) { TSG_Data_Type Type; CSG_String InputBasename = CSG_String("input"); CSG_String OutputBasename = CSG_String("output"); CSG_String FLOWD8_INPUT_FileName, FLOWD8_INPUT_FilePath; CSG_String LONGEST_OUTPUT_FileName, LONGEST_OUTPUT_FilePath; CSG_String TOTAL_LENGTH_OUTPUT_FileName, TOTAL_LENGTH_OUTPUT_FilePath; CSG_String ORDER_OUTPUT_FileName, ORDER_OUTPUT_FilePath; CSG_String OUTLET_INPUT_FileName, OUTLET_INPUT_FilePath, MASK_INPUT_FileName, MASK_INPUT_FilePath; CSG_String GDALDriver, sCmd, TempDirPath, TauDEMBinDir, BinaryName, BinaryPath, LogFile; CSG_String LONGESTName, LENGTHName, ORDERName; CSG_Projection Projection; CSG_GDAL_DataSet DataSet; CSG_Grid *FLOWD8_INPUT_Grid, *LONGEST_OUTPUT_Grid, *TOTAL_LENGTH_OUTPUT_Grid, *ORDER_OUTPUT_Grid, *MASK_INPUT_Grid; CSG_Shapes *OUTLET_INPUT_Grid; int Threshold, nproc; FLOWD8_INPUT_Grid = Parameters("FLOWD8_INPUT")->asGrid(); LONGEST_OUTPUT_Grid = Parameters("LONGEST_OUTPUT")->asGrid(); TOTAL_LENGTH_OUTPUT_Grid = Parameters("TOTAL_LENGTH_OUTPUT")->asGrid(); ORDER_OUTPUT_Grid = Parameters("ORDER_OUTPUT")->asGrid(); MASK_INPUT_Grid = Parameters("MASK_INPUT")->asGrid(); OUTLET_INPUT_Grid = Parameters("OUTLET_INPUT")->asShapes(); Threshold = Parameters("THRESHOLD")->asInt(); nproc = Parameters("NPROC")->asInt(); GDALDriver = CSG_String("GTiff"); Get_Projection(Projection); Type = FLOWD8_INPUT_Grid->Get_Type(); //TempDirPath = SG_File_Get_Path_Absolute(CSG_String("taudem_tmp")); TempDirPath = Parameters("TEMP_DIR")->asFilePath()->asString(); FLOWD8_INPUT_FileName = InputBasename + CSG_String("p"); FLOWD8_INPUT_FilePath = SG_File_Make_Path(TempDirPath, FLOWD8_INPUT_FileName, CSG_String("tif")); LONGEST_OUTPUT_FileName = OutputBasename + CSG_String("plen"); LONGEST_OUTPUT_FilePath = SG_File_Make_Path(TempDirPath, LONGEST_OUTPUT_FileName, CSG_String("tif")); TOTAL_LENGTH_OUTPUT_FileName = OutputBasename + CSG_String("tlen"); TOTAL_LENGTH_OUTPUT_FilePath = SG_File_Make_Path(TempDirPath, TOTAL_LENGTH_OUTPUT_FileName, CSG_String("tif")); ORDER_OUTPUT_FileName = OutputBasename + CSG_String("gord"); ORDER_OUTPUT_FilePath = SG_File_Make_Path(TempDirPath, ORDER_OUTPUT_FileName, CSG_String("tif")); OUTLET_INPUT_FileName = InputBasename + CSG_String("o"); OUTLET_INPUT_FilePath = SG_File_Make_Path(TempDirPath, OUTLET_INPUT_FileName, CSG_String("shp")); MASK_INPUT_FileName = InputBasename + CSG_String("wg"); MASK_INPUT_FilePath = SG_File_Make_Path(TempDirPath, MASK_INPUT_FileName, CSG_String("tif")); LogFile = SG_File_Make_Path(TempDirPath, CSG_String("taudem_log.txt")); LogFile = SG_File_Get_Path_Absolute(LogFile); TauDEMBinDir = SG_File_Make_Path(CSG_String("bin"), CSG_String("TauDEM")); TauDEMBinDir = SG_File_Get_Path_Absolute(TauDEMBinDir); // optional flags CSG_String OptionalFlags = CSG_String(""); if (OUTLET_INPUT_Grid != NULL) { OptionalFlags = CSG_String::Format(SG_T(" -o \"%s\""), OUTLET_INPUT_FilePath.c_str()); } if (MASK_INPUT_Grid != NULL) { OptionalFlags = OptionalFlags + CSG_String::Format(SG_T(" -mask \"%s\" -thresh %d"), MASK_INPUT_FilePath.c_str(), Threshold); } BinaryName = CSG_String("GridNet"); BinaryPath = SG_File_Make_Path(TauDEMBinDir, BinaryName); sCmd = CSG_String::Format(SG_T("\"mpiexec -n %d \"%s\" -p \"%s\" -plen \"%s\" -tlen \"%s\" -gord \"%s\" %s > \"%s\" 2>&1\""), nproc, BinaryPath.c_str(), FLOWD8_INPUT_FilePath.c_str(), LONGEST_OUTPUT_FilePath.c_str(), TOTAL_LENGTH_OUTPUT_FilePath.c_str(), ORDER_OUTPUT_FilePath.c_str(), OptionalFlags.c_str(), LogFile.c_str()); LONGESTName = CSG_String("D8 LongestPath"); LENGTHName = CSG_String("D8 TotalLength"); ORDERName = CSG_String("D8 GridOrder"); // make sure temp dir exists if (!SG_Dir_Exists(TempDirPath)) { if (!SG_Dir_Create(TempDirPath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to create temp directory"), TempDirPath.c_str())); } } // Delete old input file if exists if (SG_File_Exists(FLOWD8_INPUT_FilePath)) { if (!SG_File_Delete(FLOWD8_INPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to delete previous input file: "), FLOWD8_INPUT_FilePath.c_str())); return( false ); } } if (OUTLET_INPUT_Grid != NULL) { // Delete old input file if exists if (SG_File_Exists(OUTLET_INPUT_FilePath)) { if (!SG_File_Delete(OUTLET_INPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to delete previous input file: "), OUTLET_INPUT_FilePath.c_str())); return( false ); } } } if (MASK_INPUT_Grid != NULL) { // Delete old input file if exists if (SG_File_Exists(MASK_INPUT_FilePath)) { if (!SG_File_Delete(MASK_INPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to delete previous input file: "), MASK_INPUT_FilePath.c_str())); return( false ); } } } // Delete old output files if (SG_File_Exists(LONGEST_OUTPUT_FilePath)) { if (!SG_File_Delete(LONGEST_OUTPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to delete previous output file: "), LONGEST_OUTPUT_FilePath.c_str())); return( false ); } } // Delete old output files if (SG_File_Exists(TOTAL_LENGTH_OUTPUT_FilePath)) { if (!SG_File_Delete(TOTAL_LENGTH_OUTPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to delete previous output file: "), TOTAL_LENGTH_OUTPUT_FilePath.c_str())); return( false ); } } // Delete old output files if (SG_File_Exists(ORDER_OUTPUT_FilePath)) { if (!SG_File_Delete(ORDER_OUTPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to delete previous output file: "), ORDER_OUTPUT_FilePath.c_str())); return( false ); } } // save input file if( !DataSet.Open_Write(FLOWD8_INPUT_FilePath, GDALDriver, CSG_String(""), Type, 1, *Get_System(), Projection) ) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to open file for writing: "), FLOWD8_INPUT_FilePath.c_str())); return( false ); } DataSet.Write(0, FLOWD8_INPUT_Grid); if( !DataSet.Close() ) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to close file after writing: "), FLOWD8_INPUT_FilePath.c_str())); return( false ); } // save mask grid if (MASK_INPUT_Grid != NULL) { if( !DataSet.Open_Write(MASK_INPUT_FilePath, GDALDriver, CSG_String(""), Type, 1, *Get_System(), Projection) ) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to open file for writing: "), MASK_INPUT_FilePath.c_str())); return( false ); } DataSet.Write(0, MASK_INPUT_Grid); if( !DataSet.Close() ) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to close file after writing: "), MASK_INPUT_FilePath.c_str())); return( false ); } } // save outlet shapefile if (OUTLET_INPUT_Grid != NULL) { CSG_OGR_DataSource DataSource; CSG_String OGRDriver = CSG_String("ESRI Shapefile"); if( !DataSource.Create(OUTLET_INPUT_FilePath, OGRDriver) ) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to open file for writing: "), OUTLET_INPUT_FilePath.c_str())); return( false ); } DataSource.Write(OUTLET_INPUT_Grid, OGRDriver); } // Run TauDEM GridNet Message_Add(CSG_String("Executing ") + sCmd); // run process if (system(sCmd.b_str()) != 0) { Error_Set(CSG_String::Format(SG_T("Error executing '%s' see Execution log for details"), BinaryName.c_str())); // read log output CSG_File File; if (File.Open(LogFile, 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 " + LogFile + CSG_String(" for reading"))); } return( false ); } // Open new output tif file if( !DataSet.Open_Read(LONGEST_OUTPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to open generated file: "), LONGEST_OUTPUT_FilePath.c_str())); return( false ); } else { LONGEST_OUTPUT_Grid->Assign(DataSet.Read(0)); LONGEST_OUTPUT_Grid->Set_Name(LONGESTName); Parameters("LONGEST_OUTPUT")->Set_Value(LONGEST_OUTPUT_Grid); CSG_Colors colors; DataObject_Get_Colors(FLOWD8_INPUT_Grid, colors); DataObject_Set_Colors(LONGEST_OUTPUT_Grid, colors); DataObject_Update(LONGEST_OUTPUT_Grid, false); } // Open new output tif file if( !DataSet.Open_Read(TOTAL_LENGTH_OUTPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to open generated file: "), TOTAL_LENGTH_OUTPUT_FilePath.c_str())); return( false ); } else { TOTAL_LENGTH_OUTPUT_Grid->Assign(DataSet.Read(0)); TOTAL_LENGTH_OUTPUT_Grid->Set_Name(LENGTHName); Parameters("TOTAL_LENGTH_OUTPUT")->Set_Value(TOTAL_LENGTH_OUTPUT_Grid); CSG_Colors colors; DataObject_Get_Colors(FLOWD8_INPUT_Grid, colors); DataObject_Set_Colors(TOTAL_LENGTH_OUTPUT_Grid, colors); DataObject_Update(TOTAL_LENGTH_OUTPUT_Grid, false); } // Open new output tif if( !DataSet.Open_Read(ORDER_OUTPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to open generated file: "), ORDER_OUTPUT_FilePath.c_str())); return( false ); } else { ORDER_OUTPUT_Grid->Assign(DataSet.Read(0)); ORDER_OUTPUT_Grid->Set_Name(ORDERName); Parameters("ORDER_OUTPUT")->Set_Value(ORDER_OUTPUT_Grid); CSG_Colors colors; DataObject_Get_Colors(FLOWD8_INPUT_Grid, colors); DataObject_Set_Colors(ORDER_OUTPUT_Grid, colors); DataObject_Update(ORDER_OUTPUT_Grid, false); } return( true ); }
bool CStreamNet::On_Execute(void) { // Inputs and Output Strings CSG_String InputBasename = CSG_String("input"); CSG_String OutputBasename = CSG_String("output"); CSG_String FEL_INPUT_FileName, FEL_INPUT_FilePath; CSG_String FLOWD8_INPUT_FileName, FLOWD8_INPUT_FilePath; CSG_String AREAD8_INPUT_FileName, AREAD8_INPUT_FilePath; CSG_String SRC_INPUT_FileName, SRC_INPUT_FilePath; CSG_String ORD_OUTPUT_FileName, ORD_OUTPUT_FilePath, ORD_OUTPUT_Name; CSG_String W_OUTPUT_FileName, W_OUTPUT_FilePath, W_OUTPUT_Name; CSG_String NET_OUTPUT_FileName, NET_OUTPUT_FilePath, NET_OUTPUT_Name; CSG_String TREE_OUTPUT_FileName, TREE_OUTPUT_FilePath, TREE_OUTPUT_Name; CSG_String COORD_OUTPUT_FileName, COORD_OUTPUT_FilePath, COORD_OUTPUT_Name; CSG_String OUTLET_INPUT_FileName, OUTLET_INPUT_FilePath; // Data Objects CSG_Grid *FEL_INPUT_Grid, *FLOWD8_INPUT_Grid, *SRC_INPUT_Grid, *AREAD8_INPUT_Grid, *ORD_OUTPUT_Grid, *W_OUTPUT_Grid; CSG_Shapes *OUTLET_INPUT_Shapes, *NET_OUTPUT_Shapes; CSG_Table *TREE_OUTPUT_Table, *COORD_OUTPUT_Table; // Misc TSG_Data_Type Type; CSG_String GDALDriver, sCmd, TempDirPath, TauDEMBinDir, BinaryName, BinaryPath, LogFile; CSG_Projection Projection; CSG_GDAL_DataSet DataSet; CSG_OGR_DataSource OGRDataSource; CSG_String OGRDriver = CSG_String("ESRI Shapefile"); bool sw; int nproc; // Grab inputs FEL_INPUT_Grid = Parameters("FEL_INPUT")->asGrid(); FLOWD8_INPUT_Grid = Parameters("FLOWD8_INPUT")->asGrid(); SRC_INPUT_Grid = Parameters("SRC_INPUT")->asGrid(); AREAD8_INPUT_Grid = Parameters("AREAD8_INPUT")->asGrid(); ORD_OUTPUT_Grid = Parameters("ORD_OUTPUT")->asGrid(); W_OUTPUT_Grid = Parameters("W_OUTPUT")->asGrid(); nproc = Parameters("NPROC")->asInt(); OUTLET_INPUT_Shapes = Parameters("OUTLET_INPUT")->asShapes(); NET_OUTPUT_Shapes = Parameters("NET_OUTPUT")->asShapes(); TREE_OUTPUT_Table = Parameters("TREE_OUTPUT")->asTable(); COORD_OUTPUT_Table = Parameters("COORD_OUTPUT")->asTable(); sw = Parameters("SW")->asBool(); GDALDriver = CSG_String("GTiff"); Get_Projection(Projection); Type = FEL_INPUT_Grid->Get_Type(); //TempDirPath = SG_File_Get_Path_Absolute(CSG_String("taudem_tmp")); TempDirPath = Parameters("TEMP_DIR")->asFilePath()->asString(); FEL_INPUT_FileName = InputBasename + CSG_String("fel"); FEL_INPUT_FilePath = SG_File_Make_Path(TempDirPath, FEL_INPUT_FileName, CSG_String("tif")); FLOWD8_INPUT_FileName = InputBasename + CSG_String("p"); FLOWD8_INPUT_FilePath = SG_File_Make_Path(TempDirPath, FLOWD8_INPUT_FileName, CSG_String("tif")); SRC_INPUT_FileName = InputBasename + CSG_String("src"); SRC_INPUT_FilePath = SG_File_Make_Path(TempDirPath, SRC_INPUT_FileName, CSG_String("tif")); AREAD8_INPUT_FileName = InputBasename + CSG_String("ad8"); AREAD8_INPUT_FilePath = SG_File_Make_Path(TempDirPath, AREAD8_INPUT_FileName, CSG_String("tif")); ORD_OUTPUT_FileName = OutputBasename + CSG_String("ord"); ORD_OUTPUT_FilePath = SG_File_Make_Path(TempDirPath, ORD_OUTPUT_FileName, CSG_String("tif")); ORD_OUTPUT_Name = CSG_String("NetworkOrder"); W_OUTPUT_FileName = OutputBasename + CSG_String("w"); W_OUTPUT_FilePath = SG_File_Make_Path(TempDirPath, W_OUTPUT_FileName, CSG_String("tif")); W_OUTPUT_Name = CSG_String("WatershedIDs"); OUTLET_INPUT_FileName = InputBasename + CSG_String("o"); OUTLET_INPUT_FilePath = SG_File_Make_Path(TempDirPath, OUTLET_INPUT_FileName, CSG_String("shp")); NET_OUTPUT_FileName = OutputBasename + CSG_String("net"); NET_OUTPUT_FilePath = SG_File_Make_Path(TempDirPath, NET_OUTPUT_FileName, CSG_String("shp")); NET_OUTPUT_Name = CSG_String("Channel Network"); TREE_OUTPUT_FileName = OutputBasename + CSG_String("tree"); TREE_OUTPUT_FilePath = SG_File_Make_Path(TempDirPath, TREE_OUTPUT_FileName, CSG_String("dat")); TREE_OUTPUT_Name = CSG_String("Channel Network Tree"); COORD_OUTPUT_FileName = OutputBasename + CSG_String("coord"); COORD_OUTPUT_FilePath = SG_File_Make_Path(TempDirPath, COORD_OUTPUT_FileName, CSG_String("dat")); COORD_OUTPUT_Name = CSG_String("Channel Network Coords"); LogFile = SG_File_Make_Path(TempDirPath, CSG_String("taudem_log.txt")); LogFile = SG_File_Get_Path_Absolute(LogFile); TauDEMBinDir = SG_File_Make_Path(CSG_String("bin"), CSG_String("TauDEM")); TauDEMBinDir = SG_File_Get_Path_Absolute(TauDEMBinDir); // options CSG_String OptionalFlags = CSG_String(""); if (OUTLET_INPUT_Shapes != NULL) { OptionalFlags = CSG_String::Format(SG_T("-o \"%s\" "), OUTLET_INPUT_FilePath.c_str()); } if (sw) { OptionalFlags = OptionalFlags + CSG_String::Format(SG_T("-sw")); } // exec commnad BinaryName = CSG_String("StreamNet"); // D8 BinaryPath = SG_File_Make_Path(TauDEMBinDir, BinaryName); sCmd = CSG_String::Format(SG_T("\"mpiexec -n %d \"%s\" -fel \"%s\" -p \"%s\" -ad8 \"%s\" -src \"%s\" -ord \"%s\" -tree \"%s\" -coord \"%s\" -net \"%s\" -w \"%s\" %s >\"%s\" 2>&1\""), nproc, BinaryPath.c_str(), FEL_INPUT_FilePath.c_str(), FLOWD8_INPUT_FilePath.c_str(), AREAD8_INPUT_FilePath.c_str(), SRC_INPUT_FilePath.c_str(), ORD_OUTPUT_FilePath.c_str(), TREE_OUTPUT_FilePath.c_str(), COORD_OUTPUT_FilePath.c_str(), NET_OUTPUT_FilePath.c_str(), W_OUTPUT_FilePath.c_str(), OptionalFlags.c_str(), LogFile.c_str()); // make sure temp dir exists if (!SG_Dir_Exists(TempDirPath)) { if (!SG_Dir_Create(TempDirPath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to create temp directory"), TempDirPath.c_str())); } } CSG_String FilePaths [10] = {FEL_INPUT_FilePath, FLOWD8_INPUT_FilePath, SRC_INPUT_FilePath, AREAD8_INPUT_FilePath, ORD_OUTPUT_FilePath, W_OUTPUT_FilePath, OUTLET_INPUT_FilePath, NET_OUTPUT_FilePath, TREE_OUTPUT_FilePath, COORD_OUTPUT_FilePath}; for (int i = 0; i < 10; i++) { CSG_String FilePath = FilePaths[i]; // Delete old file if exists if (SG_File_Exists(FilePath)) { if (!SG_File_Delete(FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to delete existing file: "), FilePath.c_str())); return( false ); } } } // SAVE TIFFS CSG_String TIFF_INPUT_FilePaths [4] = {FEL_INPUT_FilePath, FLOWD8_INPUT_FilePath, SRC_INPUT_FilePath, AREAD8_INPUT_FilePath}; CSG_Grid* TIFF_INPUT_Grids [4] = {FEL_INPUT_Grid, FLOWD8_INPUT_Grid, SRC_INPUT_Grid, AREAD8_INPUT_Grid}; for (int i = 0; i < 4; i++) { CSG_String FilePath = TIFF_INPUT_FilePaths[i]; CSG_Grid* Grid = TIFF_INPUT_Grids[i]; if( !DataSet.Open_Write(FilePath, GDALDriver, CSG_String(""), Type, 1, *Get_System(), Projection) ) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to open file for writing: "), FilePath.c_str())); return( false ); } DataSet.Write(0, Grid); if( !DataSet.Close() ) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to close file after writing: "), FilePath.c_str())); return( false ); } } if (OUTLET_INPUT_Shapes != NULL) { // save outlet shapefile CSG_String OGRDriver = CSG_String("ESRI Shapefile"); if( !OGRDataSource.Create(OUTLET_INPUT_FilePath, OGRDriver) ) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to open file for writing: "), OUTLET_INPUT_FilePath.c_str())); return( false ); } OGRDataSource.Write(OUTLET_INPUT_Shapes, OGRDriver); OGRDataSource.Destroy(); } // Run TauDEM StreamNet Message_Add(CSG_String("Executing ") + sCmd); if (system(sCmd.b_str()) != 0) { Error_Set(CSG_String::Format(SG_T("Error executing '%s' see Execution log for details"), BinaryName.c_str())); // read log output CSG_File File; if (File.Open(LogFile, 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 " + LogFile + CSG_String(" for reading"))); } return( false ); } // Load output tiffs if( !DataSet.Open_Read(ORD_OUTPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to open output file: "), ORD_OUTPUT_FilePath.c_str())); return( false ); } else { ORD_OUTPUT_Grid->Assign(DataSet.Read(0)); ORD_OUTPUT_Grid->Set_Name(ORD_OUTPUT_Name); Parameters("ORD_OUTPUT")->Set_Value(ORD_OUTPUT_Grid); } if( !DataSet.Open_Read(W_OUTPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to open output file: "), W_OUTPUT_FilePath.c_str())); return( false ); } else { W_OUTPUT_Grid->Assign(DataSet.Read(0)); W_OUTPUT_Grid->Set_Name(W_OUTPUT_Name); Parameters("W_OUTPUT")->Set_Value(W_OUTPUT_Grid); CSG_Colors colors; DataObject_Get_Colors(SRC_INPUT_Grid, colors); DataObject_Set_Colors(ORD_OUTPUT_Grid, colors); DataObject_Update(ORD_OUTPUT_Grid, false); } // load output shapefile if( !OGRDataSource.Create(NET_OUTPUT_FilePath) ) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Failed to open file for reading: "), NET_OUTPUT_FilePath.c_str())); return( false ); } NET_OUTPUT_Shapes->Assign(OGRDataSource.Read(0, 0)); NET_OUTPUT_Shapes->Set_Name(NET_OUTPUT_Name); OGRDataSource.Destroy(); // load table data if (!SG_File_Exists(COORD_OUTPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Output file does not exist: "), COORD_OUTPUT_FilePath.c_str())); return false; } else { COORD_OUTPUT_Table->Destroy(); COORD_OUTPUT_Table->Set_Name(COORD_OUTPUT_Name); // create table fields COORD_OUTPUT_Table->Add_Field(SG_T("X"), SG_DATATYPE_Double); COORD_OUTPUT_Table->Add_Field(SG_T("Y"), SG_DATATYPE_Double); COORD_OUTPUT_Table->Add_Field(SG_T("Terminal Distance"), SG_DATATYPE_Double); COORD_OUTPUT_Table->Add_Field(SG_T("Elevation"), SG_DATATYPE_Double); COORD_OUTPUT_Table->Add_Field(SG_T("Contributing Area"), SG_DATATYPE_Double); // read table data CSG_File File; if (File.Open(COORD_OUTPUT_FilePath, SG_FILE_R, false)) { CSG_String Line; // determine number of lines while (! File.is_EOF() && File.Read_Line(Line)) { Line.Trim(); if (Line.Length() == 0) { break; } else { CSG_Table_Record *Record = COORD_OUTPUT_Table->Add_Record(); for (int i = 0; i < COORD_OUTPUT_Table->Get_Field_Count(); i++) { Record->Set_Value(i, Line.asDouble()); Line = Line.AfterFirst('\t'); Line.Trim(); } } } File.Close(); } else { Message_Add(CSG_String("Unable to open " + COORD_OUTPUT_FilePath + CSG_String(" for reading"))); } } if (!SG_File_Exists(TREE_OUTPUT_FilePath)) { Error_Set(CSG_String::Format(SG_T("%s: '%s' "), _TL("Output file does not exist: "), TREE_OUTPUT_FilePath.c_str())); return false; } else { TREE_OUTPUT_Table->Destroy(); TREE_OUTPUT_Table->Set_Name(TREE_OUTPUT_Name); // create table fields TREE_OUTPUT_Table->Add_Field(SG_T("Link"), SG_DATATYPE_Int); TREE_OUTPUT_Table->Add_Field(SG_T("Start Point"), SG_DATATYPE_Int); TREE_OUTPUT_Table->Add_Field(SG_T("End Point"), SG_DATATYPE_Int); TREE_OUTPUT_Table->Add_Field(SG_T("Next (Downstream) Link"), SG_DATATYPE_Int); TREE_OUTPUT_Table->Add_Field(SG_T("First Previous (Upstream) Link"), SG_DATATYPE_Int); TREE_OUTPUT_Table->Add_Field(SG_T("Second Previous (Upstream) Link"), SG_DATATYPE_Int); TREE_OUTPUT_Table->Add_Field(SG_T("Strahler Order"), SG_DATATYPE_Int); TREE_OUTPUT_Table->Add_Field(SG_T("Monitoring Point ID"), SG_DATATYPE_Int); TREE_OUTPUT_Table->Add_Field(SG_T("Link Network Magnitude"), SG_DATATYPE_Int); // read table data CSG_File File; if (File.Open(TREE_OUTPUT_FilePath, SG_FILE_R, false)) { CSG_String Line; // determine number of lines while (! File.is_EOF() && File.Read_Line(Line)) { Line.Trim(); if (Line.Length() == 0) { break; } else { CSG_Table_Record *Record = TREE_OUTPUT_Table->Add_Record(); for (int i = 0; i < TREE_OUTPUT_Table->Get_Field_Count(); i++) { Record->Set_Value(i, Line.asDouble()); Line = Line.AfterFirst('\t'); Line.Trim(); } } } File.Close(); } else { Message_Add(CSG_String("Unable to open " + TREE_OUTPUT_FilePath + CSG_String(" for reading"))); } } return( true ); }
//--------------------------------------------------------- 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 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 ); }