-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileoutput.c
executable file
·89 lines (70 loc) · 2.29 KB
/
fileoutput.c
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
#include "bathymetrictools.h"
/*
* This file contains:
* - File output related functions
*/
/*
* Simple output filepath parser
*/
void parsePath(char *inputfp, char *addon, char *ret) {
int leninput = strlen(inputfp);
int index = 0;
while (index < leninput) {
if (inputfp[index] == '.') {
ret[index] = '\0';
break;
}
ret[index] = inputfp[index];
index++;
}
strcat(ret, addon);
}
/*
* Converts FloatSurface 2D float** arrays to (1D) float* arrays
* for file output
*/
float *convertFloatArray(struct FloatSurface *input) {
int index = 0;
float *ret = CPLMalloc(sizeof(float) * input->rows * input->cols);
for (int row = 0; row < input->rows; row++) {
for (int col = 0; col < input->cols; col++) {
ret[index] = input->array[row][col];
index++;
}
}
return ret;
}
/*
* Writes FloatSurface to a GeoTIFF file
* - Uses GDAL for I/O
*/
void writeSurfaceToFile(struct FloatSurface *input, const char *outputpath) {
printf("Exporting file..");
fflush(stdout);
GDALAllRegister();
const char *format = "GTiff";
GDALDriverH driver = GDALGetDriverByName(format);
char **papszOptions = NULL;
char outputfp[1000];
// Parse output filename if NULL was passed as parameter
if (outputpath == NULL) {
parsePath(input->inputfp, "_smoothed_surface.tif", outputfp);
} else {
strcpy(outputfp, outputpath);
}
papszOptions = CSLSetNameValue(papszOptions, "COMPRESS", "DEFLATE" );
GDALDatasetH outdataset = GDALCreate(driver, outputfp, input->cols, input->rows, 1, GDT_Float32, papszOptions);
GDALRasterBandH outband = GDALGetRasterBand(outdataset, 1);
GDALSetGeoTransform(outdataset, input->geotransform);
GDALSetProjection(outdataset, input->projection);
float *datalist = convertFloatArray(input);
char ret = GDALRasterIO(outband, GF_Write, 0, 0, input->cols, input->rows, datalist, input->cols, input->rows, GDT_Float32, 0, 0);
if (ret != 0) {
printf("Export was not successful.\n");
}
GDALSetRasterNoDataValue(outband, input->nodata);
free(datalist);
GDALClose(outdataset);
printf("Done. Surface exported to file: %s\n\n", outputfp);
fflush(stdout);
}