-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.cpp
More file actions
executable file
·110 lines (90 loc) · 2.63 KB
/
utils.cpp
File metadata and controls
executable file
·110 lines (90 loc) · 2.63 KB
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 "utils.h"
#include "gdal_priv.h"
#include "dem.h"
#include <string>
//create a new GeoTIFF file
bool CreateGeoTIFF(char* path,int height, int width,void* pData, GDALDataType type, double* geoTransformArray6Eles,
double* min, double* max, double* mean, double* stdDev, double nodatavalue)
{
GDALDataset *poDataset;
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");
GDALDriver* poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
char **papszOptions = NULL;
poDataset = poDriver->Create(path,width, height, 1, type,
papszOptions );
if (geoTransformArray6Eles != NULL)
poDataset->SetGeoTransform(geoTransformArray6Eles);
GDALRasterBand* poBand;
poBand= poDataset->GetRasterBand(1);
poBand->SetNoDataValue(nodatavalue);
if (min != NULL && max != NULL && mean != NULL && stdDev != NULL)
{
poBand->SetStatistics(*min, *max, *mean, *stdDev);
}
poBand->RasterIO( GF_Write, 0, 0, width, height,
pData, width, height, type, 0, 0 );
GDALClose( (GDALDatasetH) poDataset );
return true;
}
//read a DEM GeoTIFF file
bool readTIFF(const char* path, GDALDataType type, CDEM& dem, double* geoTransformArray6Eles)
{
GDALDataset *poDataset;
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");
poDataset = (GDALDataset* )GDALOpen(path, GA_ReadOnly);
if (poDataset == NULL)
{
printf("Failed to read the GeoTIFF file\n");
return false;
}
GDALRasterBand* poBand;
poBand = poDataset->GetRasterBand(1);
GDALDataType dataType = poBand->GetRasterDataType();
if (dataType != type)
{
return false;
}
if (geoTransformArray6Eles == NULL)
{
printf("Transformation parameters can not be NULL\n");
return false;
}
memset(geoTransformArray6Eles, 0, 6);
poDataset->GetGeoTransform(geoTransformArray6Eles);
dem.SetWidth(poBand->GetXSize());
dem.SetHeight(poBand->GetYSize());
if (!dem.Allocate()) return false;
poBand->RasterIO(GF_Read, 0, 0, dem.Get_NX(), dem.Get_NY(),
(void *)dem.getDEMdata(), dem.Get_NX(), dem.Get_NY(), dataType, 0, 0);
GDALClose((GDALDatasetH)poDataset);
return true;
}
/*
* neighbor index
* 5 6 7
* 4 0
* 3 2 1
*/
int ix[8] = { 0, 1, 1, 1, 0,-1,-1,-1 };
int iy[8] = { 1, 1, 0,-1,-1,-1, 0, 1 };
void setNoData(unsigned char* data, int length, unsigned char noDataValue)
{
if (data == NULL || length == 0)
{
return;
}
for (int i = 0; i < length; i++)
{
data[i] = noDataValue;
}
}
void setNoData(float* data, int length, float noDataValue)
{
for (int i = 0; i < length; i++)
{
data[i] = noDataValue;
}
}
const unsigned char value[8] = {128, 64, 32, 16, 8, 4, 2, 1};