| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package footleg.cavesurvey.converter; |
| 20 |
|
| 21 |
import java.io.File; |
| 22 |
import java.text.DecimalFormat; |
| 23 |
import java.text.ParseException; |
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.Calendar; |
| 26 |
import java.util.Date; |
| 27 |
import java.util.List; |
| 28 |
import java.util.TimeZone; |
| 29 |
|
| 30 |
import footleg.cavesurvey.data.model.CaveSurvey; |
| 31 |
import footleg.cavesurvey.data.reader.CompassParser; |
| 32 |
import footleg.cavesurvey.data.reader.DxfParser; |
| 33 |
import footleg.cavesurvey.data.reader.PocketTopoParser; |
| 34 |
import footleg.cavesurvey.data.reader.SurvexParser; |
| 35 |
import footleg.cavesurvey.data.writer.SurvexWriter; |
| 36 |
import footleg.cavesurvey.data.writer.TopoRobotWriter; |
| 37 |
import footleg.cavesurvey.tools.UtilityFunctions; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class CaveConverter { |
| 52 |
private static final String filePath = "./"; |
| 53 |
private static Date today; |
| 54 |
|
| 55 |
public static String newline = System.getProperty("line.separator"); |
| 56 |
|
| 57 |
public static final String DATA_ORDER_CAT_FROMSTN = "FROM"; |
| 58 |
public static final String DATA_ORDER_CAT_TOSTN = "TO"; |
| 59 |
public static final String DATA_ORDER_CAT_LENGTH = "LENGTH"; |
| 60 |
public static final String DATA_ORDER_CAT_BEARING = "BEARING"; |
| 61 |
public static final String DATA_ORDER_CAT_CLINO = "GRADIENT"; |
| 62 |
public static final String DATA_ORDER_CAT_IGNOREALL = "IGNOREALL"; |
| 63 |
public static final String DATA_ORDER_CAT_FROMDEPTH = "FROMDEPTH"; |
| 64 |
public static final String DATA_ORDER_CAT_TODEPTH = "TODEPTH"; |
| 65 |
public static final String DATA_ORDER_CAT_DEPTHCHANGE = "DEPTHCHANGE"; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static enum CmdlineOpt { |
| 72 |
T, F, D |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static enum CmdlineSplaysOpt { |
| 79 |
Named, None, Default, Anon |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public static enum SurveyDataInputFormats { |
| 86 |
Compass, |
| 87 |
DXF, |
| 88 |
PocketTopo, |
| 89 |
Survex |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static enum SurveyDataOutputFormats { |
| 96 |
Survex, |
| 97 |
Toporobot |
| 98 |
} |
| 99 |
|
| 100 |
private static String dataFormats[][] = {{"Compass","dat"},{"DXF","dxf"},{"PocketTopo","txt"},{"Survex","svx"},{"Toporobot","text"}}; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public static enum LengthUnit { |
| 106 |
Metres, |
| 107 |
Feet, |
| 108 |
Yards |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public static enum BearingUnit { |
| 115 |
Degrees, |
| 116 |
Grads, |
| 117 |
Minutes |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public static enum GradientUnit { |
| 124 |
Degrees, |
| 125 |
Grads, |
| 126 |
Minutes, |
| 127 |
Percent |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public static String getFormatName(SurveyDataInputFormats format) { |
| 136 |
String formatName = ""; |
| 137 |
switch (format) { |
| 138 |
case Compass: |
| 139 |
formatName = dataFormats[0][0]; |
| 140 |
break; |
| 141 |
case DXF: |
| 142 |
formatName = dataFormats[1][0]; |
| 143 |
break; |
| 144 |
case PocketTopo: |
| 145 |
formatName = dataFormats[2][0]; |
| 146 |
break; |
| 147 |
case Survex: |
| 148 |
formatName = dataFormats[3][0]; |
| 149 |
break; |
| 150 |
} |
| 151 |
return formatName; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public static String getFormatName(SurveyDataOutputFormats format) { |
| 160 |
String formatName = ""; |
| 161 |
switch (format) { |
| 162 |
case Survex: |
| 163 |
formatName = dataFormats[3][0]; |
| 164 |
break; |
| 165 |
case Toporobot: |
| 166 |
formatName = dataFormats[4][0]; |
| 167 |
break; |
| 168 |
} |
| 169 |
return formatName; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public static SurveyDataInputFormats inputFormatFromFileExtn( String fileExtn ) { |
| 178 |
SurveyDataInputFormats format = null; |
| 179 |
|
| 180 |
if ( fileExtn.equalsIgnoreCase( dataFormats[0][1] ) ) { |
| 181 |
format = SurveyDataInputFormats.Compass; |
| 182 |
} |
| 183 |
else if ( fileExtn.equalsIgnoreCase( dataFormats[1][1] ) ) { |
| 184 |
format = SurveyDataInputFormats.DXF; |
| 185 |
} |
| 186 |
else if ( fileExtn.equalsIgnoreCase( dataFormats[2][1] ) ) { |
| 187 |
format = SurveyDataInputFormats.PocketTopo; |
| 188 |
} |
| 189 |
else if ( fileExtn.equalsIgnoreCase( dataFormats[3][1] ) ) { |
| 190 |
format = SurveyDataInputFormats.Survex; |
| 191 |
} |
| 192 |
|
| 193 |
return format; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public static void main( String[] args ) { |
| 200 |
|
| 201 |
String inputFilename = "input.svx"; |
| 202 |
String outputFilename = "output.text"; |
| 203 |
SurveyDataInputFormats inputFormat = null; |
| 204 |
SurveyDataOutputFormats outputFormat = null; |
| 205 |
CmdLineLogger logger = new CmdLineLogger(); |
| 206 |
|
| 207 |
|
| 208 |
if ( today == null ) { |
| 209 |
|
| 210 |
Calendar cal = Calendar.getInstance(TimeZone.getDefault()); |
| 211 |
today = cal.getTime(); |
| 212 |
} |
| 213 |
|
| 214 |
if (args.length > 0) { |
| 215 |
|
| 216 |
inputFilename = args[0]; |
| 217 |
if (args.length > 1) { |
| 218 |
|
| 219 |
outputFilename = args[1]; |
| 220 |
if (args.length > 2) { |
| 221 |
|
| 222 |
try { |
| 223 |
inputFormat = UtilityFunctions.inputDataFormatFromLetterCode( parseSingleCharacterArgument( args[2] ) ); |
| 224 |
if (args.length > 3) { |
| 225 |
|
| 226 |
outputFormat = UtilityFunctions.outputDataFormatFromLetterCode( parseSingleCharacterArgument( args[3] ) ); |
| 227 |
|
| 228 |
|
| 229 |
CmdlineSplaysOpt splaysOpt = CmdlineSplaysOpt.Default; |
| 230 |
boolean anonSplaysOpt = false; |
| 231 |
CmdlineOpt genLRUDOpt = CmdlineOpt.F; |
| 232 |
String charSetEncoding = "UTF8"; |
| 233 |
for (int iOpts = 4; iOpts < args.length; iOpts++ ) { |
| 234 |
if ( args[iOpts].compareToIgnoreCase("nosplays") == 0 ) { |
| 235 |
splaysOpt = CmdlineSplaysOpt.None; |
| 236 |
} |
| 237 |
else if ( args[iOpts].compareToIgnoreCase("splays") == 0 ) { |
| 238 |
splaysOpt = CmdlineSplaysOpt.Named; |
| 239 |
} |
| 240 |
if ( args[iOpts].compareToIgnoreCase("anonsplays") == 0 ) { |
| 241 |
anonSplaysOpt = true; |
| 242 |
} |
| 243 |
if ( args[iOpts].compareToIgnoreCase("lrud") == 0 ) { |
| 244 |
genLRUDOpt = CmdlineOpt.T; |
| 245 |
} |
| 246 |
if ( args[iOpts].compareToIgnoreCase("charset") == 0 ) { |
| 247 |
if ( iOpts + 1 < args.length ) { |
| 248 |
charSetEncoding = args[iOpts + 1]; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
if ( ( splaysOpt != CmdlineSplaysOpt.None ) && ( anonSplaysOpt == true ) ) { |
| 255 |
splaysOpt = CmdlineSplaysOpt.Anon; |
| 256 |
} |
| 257 |
|
| 258 |
convertFile(inputFilename, outputFilename, inputFormat, outputFormat, splaysOpt, |
| 259 |
genLRUDOpt, charSetEncoding, logger ); |
| 260 |
|
| 261 |
|
| 262 |
logger.writeLogToFile( filePath + "CaveConverter.log", charSetEncoding ); |
| 263 |
} |
| 264 |
} catch (ParseException e) { |
| 265 |
|
| 266 |
e.printStackTrace(); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
public static void setToday(Date date) { |
| 274 |
CaveConverter.today = date; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
private static char parseSingleCharacterArgument( String argument ) throws ParseException { |
| 285 |
char[] chars = argument.toCharArray(); |
| 286 |
char argChar = 'x'; |
| 287 |
|
| 288 |
if ( chars.length == 1 ) { |
| 289 |
argChar = chars[0]; |
| 290 |
} |
| 291 |
else { |
| 292 |
ParseException e = new ParseException("Failed to parse argument to single character. " + |
| 293 |
"Argument was '" + argument + "'.", 1); |
| 294 |
throw e; |
| 295 |
} |
| 296 |
|
| 297 |
return argChar; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public static void convertFile( String inputFilename, String outputFilename, SurveyDataInputFormats inputFormat, |
| 314 |
SurveyDataOutputFormats outputFormat, CmdlineSplaysOpt splaysOpt, CmdlineOpt generateLRUDOpt, |
| 315 |
String charSetEncoding, Logger logger ) throws ParseException { |
| 316 |
|
| 317 |
CaveSurvey surveyData = null; |
| 318 |
List<String> multiFileRefs = null; |
| 319 |
boolean multifile = false; |
| 320 |
|
| 321 |
|
| 322 |
String msg = "Reading data file '" + inputFilename + "' with format " + getFormatName( inputFormat ) |
| 323 |
+ ". Splays option: "; |
| 324 |
switch ( splaysOpt ) { |
| 325 |
case None: |
| 326 |
msg += "None"; |
| 327 |
break; |
| 328 |
case Anon: |
| 329 |
msg += "Anonymous"; |
| 330 |
break; |
| 331 |
case Default: |
| 332 |
msg += "Default"; |
| 333 |
break; |
| 334 |
case Named: |
| 335 |
msg += "Named to Stations"; |
| 336 |
break; |
| 337 |
} |
| 338 |
if ( inputFormat == SurveyDataInputFormats.Survex ) { |
| 339 |
multifile = true; |
| 340 |
multiFileRefs = new ArrayList<String>(); |
| 341 |
} |
| 342 |
|
| 343 |
logger.logMessage( msg ); |
| 344 |
List<String> fileData = UtilityFunctions.readTextFile( new File( inputFilename ), charSetEncoding, |
| 345 |
multifile, multiFileRefs, logger ); |
| 346 |
|
| 347 |
|
| 348 |
if ( inputFormat == SurveyDataInputFormats.Survex ) { |
| 349 |
|
| 350 |
SurvexParser parser = new SurvexParser(logger); |
| 351 |
surveyData = parser.parseFile( fileData, multiFileRefs ); |
| 352 |
} |
| 353 |
else if ( inputFormat == SurveyDataInputFormats.PocketTopo ) { |
| 354 |
|
| 355 |
PocketTopoParser parser = new PocketTopoParser(logger); |
| 356 |
surveyData = parser.parseFile( fileData ); |
| 357 |
} |
| 358 |
else if ( inputFormat == SurveyDataInputFormats.DXF ) { |
| 359 |
|
| 360 |
DxfParser parser = new DxfParser(logger); |
| 361 |
surveyData = parser.parseFile(fileData, 0); |
| 362 |
} |
| 363 |
else if ( inputFormat == SurveyDataInputFormats.Compass ) { |
| 364 |
|
| 365 |
CompassParser parser = new CompassParser(logger); |
| 366 |
surveyData = parser.parseFile(fileData); |
| 367 |
} |
| 368 |
else { |
| 369 |
|
| 370 |
logger.logMessage("Unsupported input format argument: " + inputFormat + " is not a valid input format."); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
boolean generateLRUD = false; |
| 375 |
if (generateLRUDOpt == CmdlineOpt.T) { |
| 376 |
generateLRUD = true; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
if ( generateLRUD ) { |
| 381 |
logger.logMessage("Generating LRUD data from splays..."); |
| 382 |
surveyData.generateLRUDfromSplays(); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
List<String> outputData = null; |
| 387 |
if ( surveyData != null ) { |
| 388 |
if ( outputFormat == SurveyDataOutputFormats.Survex ) { |
| 389 |
|
| 390 |
SurvexWriter.SplayFormats outputSplays = SurvexWriter.SplayFormats.Flagged; |
| 391 |
if ( splaysOpt == CmdlineSplaysOpt.None ) { |
| 392 |
outputSplays = SurvexWriter.SplayFormats.None; |
| 393 |
} |
| 394 |
else if ( splaysOpt == CmdlineSplaysOpt.Anon ) { |
| 395 |
outputSplays = SurvexWriter.SplayFormats.Anonymous; |
| 396 |
} |
| 397 |
|
| 398 |
SurvexWriter writer = new SurvexWriter(logger); |
| 399 |
outputData = writer.generateSurvexData( surveyData, outputSplays ); |
| 400 |
} |
| 401 |
else if ( outputFormat == SurveyDataOutputFormats.Toporobot ) { |
| 402 |
|
| 403 |
boolean outputSplays = false; |
| 404 |
if ( ( splaysOpt == CmdlineSplaysOpt.Named ) || ( splaysOpt == CmdlineSplaysOpt.Anon ) ) { |
| 405 |
outputSplays = true; |
| 406 |
} |
| 407 |
|
| 408 |
TopoRobotWriter writer = new TopoRobotWriter(logger); |
| 409 |
outputData = writer.generateToporobotData( surveyData, today, outputSplays ); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
else { |
| 417 |
|
| 418 |
logger.logMessage("Unsupported output format argument: " + outputFormat + " is not a valid output format."); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
if ( outputData.size() > 0 ) { |
| 424 |
String outputFilePath = filePath + outputFilename; |
| 425 |
logger.logMessage( "Writing output file: " + outputFilePath ); |
| 426 |
String error = UtilityFunctions.writeTextFile( outputData, outputFilePath, charSetEncoding ); |
| 427 |
if ( error.length() > 0 ) { |
| 428 |
logger.logMessage( error ); |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
public static String padNumber(int num, int padWidth) { |
| 434 |
String numStr = "" + num; |
| 435 |
|
| 436 |
return padString(numStr,padWidth); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public static String padNumber(double num, int decPlaces, int padWidth) { |
| 448 |
String formatStr = "0."; |
| 449 |
for (int i = 0; i < decPlaces; i++) { |
| 450 |
formatStr += '0'; |
| 451 |
} |
| 452 |
DecimalFormat formatter = new DecimalFormat(formatStr); |
| 453 |
|
| 454 |
return padString(formatter.format(num), padWidth); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public static String padString(String data, int padWidth) { |
| 466 |
String padded; |
| 467 |
|
| 468 |
if ( data.length() < padWidth ) { |
| 469 |
|
| 470 |
padded = " " + data; |
| 471 |
|
| 472 |
padded = padded.substring(padded.length() - padWidth); |
| 473 |
} |
| 474 |
else { |
| 475 |
|
| 476 |
padded = data; |
| 477 |
} |
| 478 |
|
| 479 |
return padded; |
| 480 |
} |
| 481 |
|
| 482 |
} |