//--------------------------------------------------------- inline bool CSTL_Import::Read_Facette(CSG_File &Stream, TSTL_Point p[3]) { WORD Attribute; if( Stream.Read(p + 0, sizeof(TSTL_Point)) && Stream.Read(p + 0, sizeof(TSTL_Point)) && Stream.Read(p + 1, sizeof(TSTL_Point)) && Stream.Read(p + 2, sizeof(TSTL_Point)) && Stream.Read(&Attribute, sizeof(Attribute)) ) { Rotate(p[0]); Rotate(p[1]); Rotate(p[2]); return( true ); } return( false ); }
//--------------------------------------------------------- bool CSG_MetaData::Load_JSON(const CSG_String &File) { CSG_File Stream; CSG_String JSON; if( Stream.Open(File, SG_FILE_R, false) && Stream.Read(JSON, (size_t)Stream.Length()) > 0 ) { return( from_JSON(JSON) ); } return( false ); }
//--------------------------------------------------------- int CTL_Extract::Read_File(const SG_Char *File, CSG_Table &Elements) { //----------------------------------------------------- CSG_File Stream; if( !Stream.Open(File, SG_FILE_R, false) ) { return( 0 ); } Process_Set_Text(CSG_String::Format("%s: %s", SG_T("scanning"), File)); //----------------------------------------------------- CSG_String String, Text; if( !Stream.Read(String, Stream.Length()) ) { return( 0 ); } //----------------------------------------------------- const SG_Char Function[2][4] = { SG_T("_TL"), SG_T("_TW") }; const SG_Char *p = String; bool bLong = Parameters("LONG")->asBool(); while( *p != '\0' ) { if( !(p[0] == Function[0][0] && p[1] == Function[0][1] && p[2] == Function[0][2]) && !(p[0] == Function[1][0] && p[1] == Function[1][1] && p[2] == Function[1][2] && bLong) ) { p ++; } else { p += Read_Text(p, Text); if( Text.Length() > 0 ) { CSG_Table_Record *pRecord = Elements.Add_Record(); pRecord->Set_Value(0, Text); pRecord->Set_Value(1, File); } } } return( 1 ); }
//--------------------------------------------------------- bool CCMD_Module::_Get_Parameters(CSG_Parameters *pParameters) { if( !pParameters || m_CMD.Parse(false) != 0 ) { return( false ); } //----------------------------------------------------- int i; for(i=0; i<pParameters->Get_Count(); i++) { CSG_Parameter *pParameter = pParameters->Get_Parameter(i); if( pParameter->is_Input() ) { // nop now, loading options first } else if( pParameter->is_Output() ) { if( pParameter->is_DataObject() && pParameter->is_Optional() && !pParameter->asDataObject() && m_CMD.Found(_Get_ID(pParameter)) ) { pParameter->Set_Value(DATAOBJECT_CREATE); } } else if( pParameter->is_Option() && !pParameter->is_Information() ) { long l; double d; wxString s; switch( pParameter->Get_Type() ) { default: break; case PARAMETER_TYPE_Parameters: _Get_Parameters(pParameter->asParameters()); break; case PARAMETER_TYPE_Bool: pParameter->Set_Value(m_CMD.Found(_Get_ID(pParameter)) ? 1 : 0); break; case PARAMETER_TYPE_Int: if( m_CMD.Found(_Get_ID(pParameter), &l) ) { pParameter->Set_Value((int)l); } break; case PARAMETER_TYPE_Choice: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { if( s.ToLong(&l) ) { pParameter->Set_Value((int)l); } else { pParameter->Set_Value(CSG_String(&s)); } } break; case PARAMETER_TYPE_Double: case PARAMETER_TYPE_Degree: if( m_CMD.Found(_Get_ID(pParameter), &s) && s.ToDouble(&d) ) { pParameter->Set_Value(d); } break; case PARAMETER_TYPE_Range: if( m_CMD.Found(_Get_ID(pParameter, wxT("MIN")), &s) && s.ToDouble(&d) ) { pParameter->asRange()->Set_LoVal(d); } if( m_CMD.Found(_Get_ID(pParameter, wxT("MAX")), &s) && s.ToDouble(&d) ) { pParameter->asRange()->Set_HiVal(d); } break; case PARAMETER_TYPE_String: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { pParameter->Set_Value(CSG_String(&s)); } break; case PARAMETER_TYPE_Text: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { CSG_File Stream; if( Stream.Open(CSG_String(&s)) ) { CSG_String t; Stream.Read(t, Stream.Length()); pParameter->Set_Value(t.c_str()); } else { pParameter->Set_Value(CSG_String(&s)); } } break; case PARAMETER_TYPE_FilePath: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { if( pParameter->asFilePath()->is_Multiple() ) { s.Prepend(wxT("\"")); s.Replace(wxT(";"), wxT("\" \"")); s.Append (wxT("\"")); } pParameter->Set_Value(CSG_String(&s)); } break; case PARAMETER_TYPE_FixedTable: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { CSG_Table Table(&s); pParameter->asTable()->Assign_Values(&Table); } break; case PARAMETER_TYPE_Grid_System: if( pParameter->Get_Children_Count() == 0 ) { long nx, ny; double d, x, y; if( !m_CMD.Found(_Get_ID(pParameter, wxT("NX")), &nx) || !m_CMD.Found(_Get_ID(pParameter, wxT("NY")), &ny) || !m_CMD.Found(_Get_ID(pParameter, wxT( "X")), &s) || !s.ToDouble(&x) || !m_CMD.Found(_Get_ID(pParameter, wxT( "Y")), &s) || !s.ToDouble(&y) || !m_CMD.Found(_Get_ID(pParameter, wxT( "D")), &s) || !s.ToDouble(&d) ) { pParameter->asGrid_System()->Assign(-1, 0.0, 0.0, 0, 0); } else { pParameter->asGrid_System()->Assign(d, x, y, (int)nx, (int)ny); } } break; } } } m_pModule->Update_Parameter_States(); //----------------------------------------------------- for(i=0; i<pParameters->Get_Count(); i++) { CSG_Parameter *pParameter = pParameters->Get_Parameter(i); if( pParameter->is_Input() ) { if( !_Load_Input(pParameters->Get_Parameter(i)) ) { CMD_Print_Error(pParameters->Get_Parameter(i)->Get_Name()); return( false ); } } else if( pParameter->is_Option() && !pParameter->is_Information() ) { long l; wxString s; switch( pParameter->Get_Type() ) { case PARAMETER_TYPE_Table_Field: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { if( s.ToLong(&l) ) { pParameter->Set_Value((int)l); } else { pParameter->Set_Value(CSG_String(&s)); } } break; case PARAMETER_TYPE_Table_Fields: if( m_CMD.Found(_Get_ID(pParameter), &s) ) { pParameter->Set_Value(CSG_String(&s)); } break; } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CBSL_Interpreter::On_Execute(void) { //----------------------------------------------------- Parameters("OUTPUT")->asGridList()->Del_Items(); g_bProgress = Parameters("PROGRESS")->asBool(); if( m_bFile ) { CSG_File Stream; if( !Stream.Open(Parameters("BSL")->asString(), SG_FILE_R, false) ) { return( false ); } Stream.Read(m_BSL, Stream.Length()); } else { m_BSL = Parameters("BSL")->asString(); } //----------------------------------------------------- if( !Parse_Vars(false) ) { return( false ); } //----------------------------------------------------- CSG_Parameters Input(this, _TL("Input"), _TL(""), SG_T("INPUT"), true); FindMemoryGrids(); for(T_InputText::iterator it=InputGrids.begin(); it!=InputGrids.end(); it++) { CSG_String sName(it->c_str()); Input.Add_Grid(NULL, sName, sName, _TL(""), PARAMETER_INPUT, true); } DeleteVarList(); DeleteAnweisungList(AnweisungList); if( Dlg_Parameters(&Input, _TL("Input")) == false ) { return( false ); } //----------------------------------------------------- if( !Parse_Vars(true) ) { return( false ); } //----------------------------------------------------- g_pInterpreter = this; if( GetMemoryGrids(&Input) ) { try { ausfuehren_anweisung(AnweisungList); } catch(BBFehlerAusfuehren x) { if( x.Text == "" ) Message_Add(_TL("unknown error: execution")); else Message_Add(CSG_String::Format(SG_T("error: %s\n"), CSG_String(x.Text.c_str()).c_str())); } catch(BBFehlerUserbreak x) { if( x.Text == "" ) Message_Add(_TL("unknown error: user break")); else Message_Add(CSG_String::Format(SG_T("error: %s\n"), CSG_String(x.Text.c_str()).c_str())); } } g_pInterpreter = NULL; DeleteVarList(); DeleteAnweisungList(AnweisungList); return( true ); }
//--------------------------------------------------------- bool CMOLA_Import::On_Execute(void) { bool bDown; int xa, xb, y, yy, NX, NY; short *sLine; double D, xMin, yMin; CSG_File Stream; TSG_Data_Type Type; CSG_Grid *pGrid; CSG_String fName, sName; //----------------------------------------------------- pGrid = NULL; switch( Parameters("TYPE")->asInt() ) { case 0: Type = SG_DATATYPE_Short; break; case 1: default: Type = SG_DATATYPE_Float; break; } bDown = Parameters("ORIENT")->asInt() == 1; //----------------------------------------------------- // MEGpxxnyyyrv // 012345678901 // p indicates the product type (A for areoid, C for counts, R for // radius, and T for topography) // xx is the latitude of the upper left corner of the image // n indicates whether the latitude is north (N) or south (S) // yyy is the east longitude of the upper left corner of the image // r is the map resolution using the pattern // c = 4 pixel per degree // e = 16 pixel per degree // f = 32 pixel per degree // g = 64 pixel per degree // h = 128 pixel per degree // (This convention is consistent with that used for the Mars Digital // Image Model [MDIM] archives.) // v is a letter indicating the product version. fName = SG_File_Get_Name(Parameters("FILE")->asString(), false); fName.Make_Upper(); if( fName.Length() < 12 ) { return( false ); } //----------------------------------------------------- switch( fName[3] ) { default: return( false ); case 'A': sName.Printf(SG_T("MOLA: Areoid v%c") , fName[11]); break; case 'C': sName.Printf(SG_T("MOLA: Counts v%c") , fName[11]); break; case 'R': sName.Printf(SG_T("MOLA: Radius v%c") , fName[11]); break; case 'T': sName.Printf(SG_T("MOLA: Topography v%c") , fName[11]); break; } //----------------------------------------------------- switch( fName[10] ) { default: return( false ); case 'C': // 1/4th degree... D = 1.0 / 4.0; NX = 4 * 360; NY = 4 * 180; yMin = - 90.0; xMin = -180.0; break; case 'D': // 1/8th degree... D = 1.0 / 8.0; NX = 8 * 360; NY = 8 * 180; yMin = - 90.0; xMin = -180.0; break; case 'E': // 1/16th degree... D = 1.0 / 16.0; NX = 16 * 360; NY = 16 * 180; yMin = - 90.0; xMin = -180.0; break; case 'F': // 1/32th degree... D = 1.0 / 32.0; NX = 32 * 360; NY = 32 * 180; yMin = - 90.0; xMin = -180.0; break; case 'G': // 1/64th degree... D = 1.0 / 64.0; NX = 64 * 180; NY = 64 * 90; yMin = (fName[6] == 'S' ? -1.0 : 1.0) * fName.Right(8).asInt(); yMin = bDown ? yMin - NY * D : -yMin; xMin = fName.Right(5).asInt(); if( xMin >= 180.0 ) { xMin -= 360.0; } break; case 'H': // 1/128th degree... D = 1.0 / 128.0; NX = 128 * 90; NY = 128 * 44; yMin = (fName[6] == 'S' ? -1.0 : 1.0) * fName.Right(8).asInt(); yMin = bDown ? yMin - NY * D : -yMin; xMin = fName.Right(5).asInt(); if( xMin >= 180.0 ) { xMin -= 360.0; } break; } //----------------------------------------------------- if( Stream.Open(Parameters("FILE")->asString(), SG_FILE_R, true) ) { if( (pGrid = SG_Create_Grid(Type, NX, NY, D, xMin + D / 2.0, yMin + D / 2.0)) != NULL ) { pGrid->Set_Name(sName); pGrid->Set_NoData_Value(-999999); pGrid->Get_Projection().Create(SG_T("+proj=lonlat +units=m +a=3396200.000000 +b=3376200.000000"), SG_PROJ_FMT_Proj4); //--------------------------------------------- sLine = (short *)SG_Malloc(NX * sizeof(short)); for(y=0; y<NY && !Stream.is_EOF() && Set_Progress(y, NY); y++) { yy = bDown ? NY - 1 - y : y; Stream.Read(sLine, NX, sizeof(short)); if( fName[10] == 'G' || fName[10] == 'H' ) { for(xa=0; xa<NX; xa++) { SG_Swap_Bytes(sLine + xa, sizeof(short)); pGrid->Set_Value(xa, yy, sLine[xa]); } } else { for(xa=0, xb=NX/2; xb<NX; xa++, xb++) { SG_Swap_Bytes(sLine + xa, sizeof(short)); SG_Swap_Bytes(sLine + xb, sizeof(short)); pGrid->Set_Value(xa, yy, sLine[xb]); pGrid->Set_Value(xb, yy, sLine[xa]); } } } //--------------------------------------------- SG_Free(sLine); Parameters("GRID")->Set_Value(pGrid); } } return( pGrid != NULL ); }
//--------------------------------------------------------- bool 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 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 CSG_PointCloud::_Load(const CSG_String &File_Name) { TSG_Data_Type Type; char ID[6]; int i, iBuffer, nPointBytes, nFields; char Name[1024]; CSG_File Stream; SG_UI_Msg_Add(CSG_String::Format(SG_T("%s: %s..."), _TL("Load point cloud"), File_Name.c_str()), true); //----------------------------------------------------- if( !Stream.Open(File_Name, SG_FILE_R, true) ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("file could not be opened.")); return( false ); } if( !Stream.Read(ID, 6) || strncmp(ID, PC_FILE_VERSION, 5) != 0 ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("incompatible file.")); return( false ); } if( !Stream.Read(&nPointBytes, sizeof(int)) || nPointBytes < (int)(3 * sizeof(float)) ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("incompatible file.")); return( false ); } if( !Stream.Read(&nFields, sizeof(int)) || nFields < 3 ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("incompatible file.")); return( false ); } //----------------------------------------------------- Destroy(); for(i=0; i<nFields; i++) { if( !Stream.Read(&Type , sizeof(TSG_Data_Type)) || !Stream.Read(&iBuffer , sizeof(int)) || !(iBuffer > 0 && iBuffer < 1024) || !Stream.Read(Name , iBuffer) ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("incompatible file.")); return( false ); } if( ID[5] == '0' ) // Data Type Definition changed!!! { switch( Type ) { default: Type = SG_DATATYPE_Undefined; break; case 1: Type = SG_DATATYPE_Char; break; case 2: Type = SG_DATATYPE_Short; break; case 3: Type = SG_DATATYPE_Int; break; case 4: Type = SG_DATATYPE_Long; break; case 5: Type = SG_DATATYPE_Float; break; case 6: Type = SG_DATATYPE_Double; break; } } Name[iBuffer] = '\0'; if( !_Add_Field(CSG_String((const char *)Name), Type) ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("incompatible file.")); return( false ); } } if( m_nPointBytes != nPointBytes + 1 ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("incompatible file.")); return( false ); } //----------------------------------------------------- sLong fLength = Stream.Length(); while( _Inc_Array() && Stream.Read(m_Cursor + 1, nPointBytes) && SG_UI_Process_Set_Progress((double)Stream.Tell(), (double)fLength) ) {} _Dec_Array(); Set_File_Name(File_Name, true); Load_MetaData(File_Name); if( 0 > Get_Count() ) { SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE); SG_UI_Msg_Add_Error(_TL("no records in file.")); return( false ); } //----------------------------------------------------- SG_UI_Process_Set_Ready(); Get_Projection().Load(SG_File_Make_Path(NULL, File_Name, SG_T("prj")), SG_PROJ_FMT_WKT); SG_UI_Msg_Add(_TL("okay"), false, SG_UI_MSG_STYLE_SUCCESS); 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 ); }