Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. void Grid::loadGrid(const char* filePathGrid)
  2. {
  3. // 2D vector to contain the matrix
  4. vector<vector<float>> data;
  5.  
  6. unsigned nrows, ncols;
  7. double xllcorner, yllcorner;
  8. int cellsize, nodataValue;
  9. const int nRowHeader = 6;
  10. string line, strtmp;
  11.  
  12. ifstream DEMFile;
  13. DEMFile.open(filePathGrid);
  14. if (DEMFile.is_open())
  15. {
  16. // read the header (6 lines)
  17. for (int index = 0; index < nRowHeader; index++)
  18. {
  19. getline(DEMFile, line);
  20. istringstream ss(line);
  21. switch (index)
  22. {
  23. case 0:
  24. while (ss >> strtmp)
  25. {
  26. istringstream(strtmp) >> ncols;
  27. }
  28. break;
  29. case 1:
  30. while (ss >> strtmp)
  31. {
  32. istringstream(strtmp) >> nrows;
  33. }
  34. break;
  35. case 2:
  36. while (ss >> strtmp)
  37. {
  38. istringstream(strtmp) >> xllcorner;
  39. }
  40. break;
  41. case 3:
  42. while (ss >> strtmp)
  43. {
  44. istringstream(strtmp) >> yllcorner;
  45. }
  46. break;
  47. case 4:
  48. while (ss >> strtmp)
  49. {
  50. istringstream(strtmp) >> cellsize;
  51. }
  52. break;
  53. case 5:
  54. while (ss >> strtmp)
  55. {
  56. istringstream(strtmp) >> nodataValue;
  57. }
  58. break;
  59. }
  60. }
  61.  
  62. // Read in the elevation values
  63. if (ncols * nrows > 0)
  64. {
  65. // Set up sizes. (rows x cols)
  66. data.resize(nrows);
  67. for (unsigned row = 0; row < nrows; ++row)
  68. {
  69. data[row].resize(ncols);
  70. }
  71.  
  72. // Load values in
  73. unsigned row = 0;
  74. while (row < nrows)
  75. {
  76. getline(DEMFile, line);
  77. istringstream ss(line);
  78. for (unsigned col =0; col < ncols; col++)
  79. {
  80. ss >> data[row][col];
  81. }
  82. row ++;
  83. }
  84. DEMFile.close();
  85. }
  86. }
  87. else cout << "Unable to open file";
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement