//--------------------------------------------------------- inline bool CXYZ_Import::Read_Values(CSG_File &Stream, double &x, double &y, double &z) { CSG_String sLine; if( Stream.Read_Line(sLine) ) { sLine.Trim(); if( sLine.asDouble(x) ) { sLine = sLine.AfterFirst(m_Separator); sLine.Trim(); if( sLine.asDouble(y) ) { sLine = sLine.AfterFirst(m_Separator); if( sLine.asDouble(z) ) { return( true ); } } } } return( false ); }
//--------------------------------------------------------- bool CSG_Shapes_OGIS_Converter::from_WKText(const CSG_String &Text, CSG_Shape *pShape) { pShape->Del_Parts(); CSG_String Type = Text.BeforeFirst('('); Type.Trim(true); Type.Trim(false); switch( pShape->Get_Type() ) { case SHAPE_TYPE_Point: if( !Type.CmpNoCase(SG_OGIS_TYPE_STR_Point) ) { return( _WKT_Read_Point(Text.AfterFirst('(').BeforeFirst(')'), pShape, 0) ); } break; case SHAPE_TYPE_Points: if( !Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiPoint) ) { return( _WKT_Read_Parts(Text, pShape) ); } break; case SHAPE_TYPE_Line: if( !Type.CmpNoCase(SG_OGIS_TYPE_STR_Line) ) { return( _WKT_Read_Points(Text, pShape) ); } if( !Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiLine) ) { return( _WKT_Read_Parts(Text, pShape) ); } break; case SHAPE_TYPE_Polygon: if( !Type.CmpNoCase(SG_OGIS_TYPE_STR_Polygon) ) { return( _WKT_Read_Parts(Text, pShape) ); } if( !Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiPolygon) ) { return( _WKT_Read_Polygon(Text, pShape) ); } break; default: break; } return( false ); }
//--------------------------------------------------------- int CSG_Grid::_Load_Native_Get_Key(CSG_File &Stream, CSG_String &Value) { int i; CSG_String sLine; if( Stream.Read_Line(sLine) && (i = sLine.Find('=')) > 0 ) { Value = sLine.AfterFirst('='); Value.Trim(); sLine.Remove(i); for(i=0; i<GRID_FILE_KEY_Count; i++) { CSG_String s(gSG_Grid_File_Key_Names[i]); if( s.Find(sLine.Left(s.Length())) >= 0 ) { return( i ); } } } return( -1 ); }
//--------------------------------------------------------- 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 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 ); }