This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
forked from screwt/openglobusddm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
HgtFilesGrid.cpp
110 lines (93 loc) · 2.98 KB
/
HgtFilesGrid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "HgtFilesGrid.h"
#include <iostream>
HgtFilesGrid::HgtFilesGrid()
{
}
HgtFilesGrid::~HgtFilesGrid()
{
for( int i = 0; i <= 180; i++ ){
for( int j = 0; j < 360; j++ ){
if( this->hgtFilesGrid[i][j].fileName )
delete[] this->hgtFilesGrid[i][j].fileName;
}
}
delete[] this->dataStack;
}
void HgtFilesGrid::Init( int maxLoadedFiles, const char* filesPath )
{
this->loadedFiles = 0;
this->MAX_LOADED_FILES = maxLoadedFiles;
this->dataStack = new Heights[maxLoadedFiles];
for( int i = 0; i < maxLoadedFiles; i++ )
this->dataStack[i].pFileFlag = NULL;
for( int i = 0; i <= 180; i++ ){
for( int j = 0; j <= 360; j++ ){
FILE *fp;
std::string fileName(filesPath);
char demFileName[12];
fileName.append(HgtFormat::crdtodem(90 - i, -180 + j, demFileName));
fp = fopen(fileName.c_str(), "rb");
if( fp == NULL ) {
this->hgtFilesGrid[i][j].fileName = NULL;
} else {
std::cout << "loaded: " << fileName << "\n";
int length = fileName.length();
this->hgtFilesGrid[i][j].fileName = new char[length + 1];
strcpy(hgtFilesGrid[i][j].fileName, fileName.c_str());
//strcpy_s( hgtFilesGrid[i][j].fileName, length + 1, fileName.c_str() );
fclose(fp);
}
this->hgtFilesGrid[i][j].pHeightData = NULL;
this->hgtFilesGrid[i][j].isLoaded = false;
}
}
for( int i = 0; i <= 180; i++ ){
this->hgtFilesGrid[i][360] = this->hgtFilesGrid[i][0];
}
}
bool HgtFilesGrid::IsExists(int i, int j){
return this->hgtFilesGrid[i][j].fileName ? true : false;
}
const char* HgtFilesGrid::GetFileName( int i, int j) {
return this->hgtFilesGrid[i][j].fileName;
}
signed short HgtFilesGrid::GetHeight( int iSquare, int jSquare, int i, int j )
{
if( this->hgtFilesGrid[iSquare][jSquare].isLoaded )
{
return this->hgtFilesGrid[iSquare][jSquare].pHeightData->height[i][j];
}
else
{
if( this->hgtFilesGrid[iSquare][jSquare].fileName ) {
FILE *fp;
fp = fopen(this->hgtFilesGrid[iSquare][jSquare].fileName, "rb+");
if( fp == NULL ) {
return 0;
}
if( this->loadedFiles >= this->MAX_LOADED_FILES ) {
this->loadedFiles = 0;
}
if( this->dataStack[this->loadedFiles].pFileFlag )
this->dataStack[this->loadedFiles].pFileFlag->isLoaded = false;
int size = this->NCols * this->NRows;
short int *buf = new short int[size];
fread( buf, sizeof(short int), size, fp);
fclose(fp);
for( int i=0; i < size; i++ ) {
signed short height = (signed short)((buf[i] & 255) << 8 | (buf[i] >> 8) & 255);
this->dataStack[this->loadedFiles].height[i / this->NRows][i % this->NCols] = height;
}
delete[] buf;
this->hgtFilesGrid[iSquare][jSquare].pHeightData = &this->dataStack[this->loadedFiles];
this->dataStack[this->loadedFiles].pFileFlag = &this->hgtFilesGrid[iSquare][jSquare];
this->loadedFiles++;
this->hgtFilesGrid[iSquare][jSquare].isLoaded = true;
return this->hgtFilesGrid[iSquare][jSquare].pHeightData->height[i][j];
}
else
{
return 0;
}
}
}