-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoTiffHelpers.cs
93 lines (79 loc) · 2.75 KB
/
GeoTiffHelpers.cs
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
//https://www.linkedin.com/in/panagiotis-kalogeropoulos-3a2a95270/
// This section made by Panagiotis kalogeropoulos.
// Using MaxRev-GDAL.
// Recommended on the official GDAL docs: https://gdal.org/api/csharp/index.html#getting-gdal-for-c
// TODO: Check https://github.com/MaxRev-Dev/gdal.netcore for details
using DotSpatial.Data;
using MaxRev.Gdal.Core;
using OSGeo.GDAL;
using System;
using System.IO;
namespace GeoTiffHelpers
{
public class GeoTiffHelpers
{
//Driver code
public static void convertGeotiffToAsc(string geotiffFilePath, string outputPath)
{
// Register GDAL drivers
// REQUIRED, DONT FORGET
GdalBase.ConfigureAll();
Band band = GetBand(geotiffFilePath);
if (band != null)
{
float[] arr = GetRasterAsArray(band);
OutputToFile(outputPath, band, arr);
Console.WriteLine("GeoTIFF conversion to text completed.");
}
else
{
Console.WriteLine("Failed to open GeoTIFF file.");
}
}
private static float[] GetRasterAsArray(Band band)
{
int width = band.XSize;
int height = band.YSize;
int size = width * height;
float[] data = new float[size];
var arr = band.ReadRaster(0, 0, width, height, data, width, height, 0, 0);
if (arr != CPLErr.CE_None)
{
throw new NullException();
}
return data;
}
private static Band GetBand(string fileDir)
{
Dataset dataset = Gdal.Open(fileDir, Access.GA_ReadOnly);
Band band = dataset.GetRasterBand(1);
if (band == null)
{
throw new NullException();
}
return band;
}
private static void OutputToFile(string fileDir, Band band, float[] data)
{
using var writer = new StreamWriter(fileDir);
int width = band.XSize;
int height = band.YSize;
int size = width * height;
writer.WriteLine($"NCOLS {width}");
writer.WriteLine($"NROWS {height}");
writer.WriteLine($"XLLCORNER {"idfk"}");
writer.WriteLine($"YLLCORNER {"idfk either"}");
writer.WriteLine($"CELLSIZE {"not that either"}");
writer.WriteLine($"NODATA_VALUE {"y'already know"}");
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
writer.Write(data[(y * width) + x]);
writer.Write(" ");
}
writer.WriteLine();
}
}
}
}