//--------------------------------------------------------- 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 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 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 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 ); }