-
Notifications
You must be signed in to change notification settings - Fork 2
/
Options.cs
205 lines (163 loc) · 7.56 KB
/
Options.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
namespace Gdal4Hgt {
/// <summary>
/// Optionen und Argumente werden zweckmäßigerweise in eine (programmabhängige) Klasse gekapselt.
/// Erzeugen des Objektes und Evaluate() sollten in einem try-catch-Block erfolgen.
/// </summary>
public class Options {
// alle Optionen sind i.A. 'read-only'
/// <summary>
/// Pfad zu den zu konvertierenden HGT-Dateien bzw. Name einer HGT-Datei
/// </summary>
public List<string> HgtPath { get; private set; }
/// <summary>
/// Name der zu konvertierenden TIF-Datei
/// </summary>
public string RasterFilename { get; private set; }
/// <summary>
/// Pfad für die zu erzeugenden Dateien
/// </summary>
public string DestinationPath { get; private set; }
/// <summary>
/// Wert für 'nodata'-Punkte
/// </summary>
public int NoDataValue { get; private set; }
/// <summary>
/// Ergebnis komprimieren
/// </summary>
public bool Compress { get; private set; }
/// <summary>
/// Info über Input liefern
/// </summary>
public bool Info { get; private set; }
/// <summary>
/// Ausgabeziel ev. überschreiben
/// </summary>
public bool OutputOverwrite { get; private set; }
/// <summary>
/// VRT-Datei(en) erzeugen
/// </summary>
public bool WithVRT { get; private set; }
FSoftUtils.CmdlineOptions cmd;
enum MyOptions {
HgtPath,
RasterFilename,
DstPath,
Compression,
NoDataValue,
VrtFilename,
Info,
OutputOverwrite,
Help,
}
public Options() {
Init();
cmd = new FSoftUtils.CmdlineOptions();
// Definition der Optionen
cmd.DefineOption((int)MyOptions.HgtPath, "hgt", "", "hgt-filename or path to hgt files to convert (multiple usable)", FSoftUtils.CmdlineOptions.OptionArgumentType.String, int.MaxValue);
cmd.DefineOption((int)MyOptions.RasterFilename, "raster", "r", "tiff- or vrt-filename to convert", FSoftUtils.CmdlineOptions.OptionArgumentType.String);
cmd.DefineOption((int)MyOptions.DstPath, "dstpath", "d", "destination path", FSoftUtils.CmdlineOptions.OptionArgumentType.String);
cmd.DefineOption((int)MyOptions.VrtFilename, "vrt", "v", "create additional vrt-file(s) (one for ever number of pixels in HGT's; , default 'true')", FSoftUtils.CmdlineOptions.OptionArgumentType.Boolean);
cmd.DefineOption((int)MyOptions.Compression, "compr", "c", "compress the result (default 'true')", FSoftUtils.CmdlineOptions.OptionArgumentType.Boolean);
cmd.DefineOption((int)MyOptions.OutputOverwrite, "overwrite", "O", "overwrite destination files (without arg 'true', default 'false')", FSoftUtils.CmdlineOptions.OptionArgumentType.BooleanOrNot);
cmd.DefineOption((int)MyOptions.Info, "info", "", "only show infos for input files (without arg 'true', default 'false')", FSoftUtils.CmdlineOptions.OptionArgumentType.BooleanOrNot);
cmd.DefineOption((int)MyOptions.NoDataValue, "nodata", "", "value for 'nodata' (default " + Hgt.HGTReaderWriter.NoValue.ToString() + ")", FSoftUtils.CmdlineOptions.OptionArgumentType.Integer);
cmd.DefineOption((int)MyOptions.Help, "help", "?", "this help", FSoftUtils.CmdlineOptions.OptionArgumentType.Nothing);
}
/// <summary>
/// Standardwerte setzen
/// </summary>
void Init() {
HgtPath = new List<string>();
RasterFilename = "";
DestinationPath = ".";
WithVRT = true;
Compress = true;
Info = false;
OutputOverwrite = false;
NoDataValue = int.MinValue;
}
/// <summary>
/// Auswertung der Optionen
/// </summary>
/// <param name="args"></param>
public void Evaluate(string[] args) {
if (args == null) return;
List<string> InputArray_Tmp = new List<string>();
try {
cmd.Parse(args);
foreach (MyOptions opt in Enum.GetValues(typeof(MyOptions))) { // jede denkbare Option testen
int optcount = cmd.OptionAssignment((int)opt); // Wie oft wurde diese Option verwendet?
if (optcount > 0)
switch (opt) {
case MyOptions.HgtPath:
for (int i = 0; i < optcount; i++) {
string tmp = cmd.StringValue((int)opt, i).Trim();
if (tmp.Length > 0)
HgtPath.Add(tmp);
}
break;
case MyOptions.RasterFilename:
RasterFilename = cmd.StringValue((int)opt).Trim();
break;
case MyOptions.DstPath:
DestinationPath = cmd.StringValue((int)opt).Trim();
break;
case MyOptions.VrtFilename:
WithVRT = cmd.BooleanValue((int)opt);
break;
case MyOptions.Compression:
Compress = cmd.BooleanValue((int)opt);
break;
case MyOptions.OutputOverwrite:
if (cmd.ArgIsUsed((int)opt))
OutputOverwrite = cmd.BooleanValue((int)opt);
else
OutputOverwrite = true;
break;
case MyOptions.Info:
if (cmd.ArgIsUsed((int)opt))
Info = cmd.BooleanValue((int)opt);
else
Info = true;
break;
case MyOptions.NoDataValue:
int iTmp = cmd.IntegerValue((int)opt);
if ((int)(iTmp & 0xFFFF0000) != 0) {
throw new Exception("valid values are " + short.MinValue.ToString() + " .. " + short.MaxValue.ToString());
}
NoDataValue = (short)(iTmp & 0xFFFF);
break;
case MyOptions.Help:
ShowHelp();
break;
}
}
//TestParameter = new string[cmd.Parameters.Count];
//cmd.Parameters.CopyTo(TestParameter);
if (HgtPath.Count == 0 && RasterFilename == "")
throw new Exception("don't know, what to do (use at least '" + cmd.OptionName((int)MyOptions.HgtPath) + "' or '" + cmd.OptionName((int)MyOptions.RasterFilename) + "')");
if (cmd.Parameters.Count > 0)
throw new Exception("args not permitted");
} catch (Exception ex) {
Console.Error.WriteLine(ex.Message);
ShowHelp();
throw new Exception("Error on prog-options.");
}
}
/// <summary>
/// Hilfetext für Optionen ausgeben
/// </summary>
/// <param name="cmd"></param>
public void ShowHelp() {
List<string> help = cmd.GetHelpText();
for (int i = 0; i < help.Count; i++) Console.Error.WriteLine(help[i]);
Console.Error.WriteLine();
Console.Error.WriteLine("Zusatzinfos:");
Console.Error.WriteLine("Für '--' darf auch '/' stehen und für '=' auch ':' oder Leerzeichen.");
Console.Error.WriteLine("Argumente mit ';' werden an diesen Stellen in Einzelargumente aufgetrennt.");
// ...
}
}
}