//--------------------------------------------------------- inline bool CXYZ_Import::Read_Values(CSG_File &Stream, double &x, double &y, double &z) { CSG_String sLine; if( Stream.Read_Line(sLine) ) { sLine.Trim(); if( sLine.asDouble(x) ) { sLine = sLine.AfterFirst(m_Separator); sLine.Trim(); if( sLine.asDouble(y) ) { sLine = sLine.AfterFirst(m_Separator); if( sLine.asDouble(z) ) { return( true ); } } } } return( false ); }
//--------------------------------------------------------- double SG_Degree_To_Double(const CSG_String &String) { double d, h, s, sig; sig = 1.0; d = h = s = 0.0; if( String.BeforeFirst('\xb0').asDouble(d) ) { if( d < 0.0 ) { sig = -1.0; d = -d; } String.AfterFirst('\xb0').asDouble(h); String.AfterFirst('\'' ).asDouble(s); } else { String.asDouble(d); } return( sig * (d + h / 60.0 + s / (60.0 * 60.0)) ); }
//--------------------------------------------------------- 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 CSG_MetaData::Get_Property(const CSG_String &Name, double &Value) const { CSG_String s; return( Get_Property(Name, s) && s.asDouble(Value) ); }
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 ); }