Debian Codegraph @

src/footleg/cavesurvey/data/writer/SurvexWriter.java

raw symbols SCIP
1 /**
2 * Copyright (C) 2009-2017 Paul Fretwell - aka 'Footleg' ([email protected])
3 *
4 * This file is part of Cave Converter.
5 *
6 * Cave Converter is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Cave Converter is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Cave Converter. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package footleg.cavesurvey.data.writer;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.ListIterator;
24
25 import footleg.cavesurvey.converter.CaveConverter;
26 import footleg.cavesurvey.converter.Logger;
27 import footleg.cavesurvey.converter.CaveConverter.BearingUnit;
28 import footleg.cavesurvey.converter.CaveConverter.GradientUnit;
29 import footleg.cavesurvey.converter.CaveConverter.LengthUnit;
30 import footleg.cavesurvey.data.model.CaveSurvey;
31 import footleg.cavesurvey.data.model.SeriesLink;
32 import footleg.cavesurvey.data.model.SurveyLeg;
33 import footleg.cavesurvey.data.model.SurveySeries;
34 import footleg.cavesurvey.data.model.SurveySeries.ToStnLRUD;
35 import footleg.cavesurvey.data.model.SurveyStation;
36 import footleg.cavesurvey.tools.UtilityFunctions;
37
38 /**
39 * Writer for Survex file format text data.
40 *
41 * @author Footleg
42 * @version 2017.01.10 (ISO 8601 YYYY.MM.DD)
43 * @since 1.6 (The Java version used)
44 *
45 * @to.do
46 * TODO Handle nosurvey legs (currently they are converted to equates in error)
47 * TODO Output entrance flags
48 * TODO Output fix flags
49 * TODO Output calibration comments fields
50 * TODO Output team fields
51 * TODO Output instrument fields
52 * TODO Output units
53 * TODO Indent block contents including nested blocks
54 * TODO Support option to output multiple files using includes and equates in master file and one series per file in child files
55 */
56 public class SurvexWriter {
57 private Logger logger;
58
59 public SurvexWriter( Logger logger ) {
60 super();
61 this.logger = logger;
62 }
63
64 /**
65 * Supported splay formats for output
66 */
67 public static enum SplayFormats {
68 None,
69 Flagged,
70 Anonymous
71 }
72
73 /**
74 * Generates Survex format data from a cave survey
75 *
76 * @param surveyData The cave survey model to generate Survex data for
77 * @param splaysOutput Indicate format of splay legs included in the output.
78 * @return Text lines of Survex format data
79 */
80 public List<String> generateSurvexData( CaveSurvey surveyData, SplayFormats splaysOutput ) {
81 List<String> outputData = new ArrayList<String>();
82
83 logger.logMessage("Generating Survex format data...");
84
85 //Create dummy parent series to pass in neutral calibration settings for top level series
86 SurveySeries parentSeries = new SurveySeries("parent");
87
88 //Loop through all series
89 ListIterator<SurveySeries> seriesIterator = surveyData.listIterator();
90 while ( seriesIterator.hasNext() ) {
91 SurveySeries series = seriesIterator.next();
92
93 outputData.addAll( generateSurvexDataSeries( series, parentSeries, splaysOutput ) );
94 }
95
96 return outputData;
97 }
98
99 /**
100 * Generates Survex format data for a survey series
101 *
102 * @param series Survey series to generate Survex format data from
103 * @param parentSeries Survey series which is the parent of the series being passed in (used for calibrations)
104 * @return Text lines for a Survex format series data block
105 */
106 private List<String> generateSurvexDataSeries( SurveySeries series, SurveySeries parentSeries, SplayFormats splaysOutput ) {
107 List<String> outputData = new ArrayList<String>();
108 List<String> legsData = new ArrayList<String>();
109 List<String> fixedStnsData = new ArrayList<String>();
110
111 //List of passage data blocks. Each block is a child list of the station name and the LRUD data line.
112 List<List<String[]>> passageData = new ArrayList<List<String[]>>();
113
114 boolean duplicateFlagOn = false;
115 boolean surfaceFlagOn = false;
116 boolean splayFlagOn = false;
117 boolean nosurveyFlagOn = false;
118 int splayNameSequence = 0;
119
120 //Start series block
121 outputData.add( "*BEGIN " + substIllegalNameChars( series.getSeriesName() ) );
122
123 //Write comment if present
124 if ( series.getComment().length() > 0 ) {
125 String[] dataItems = UtilityFunctions.parseTripComment( series.getComment() );
126 for (String line : dataItems) {
127 outputData.add( ";" + line );
128 }
129 outputData.add( "" );
130 }
131
132 //Write date if present
133 if ( series.getSurveyDate() != null ) {
134 outputData.add( "*DATE " + UtilityFunctions.dateToString( series.getSurveyDate(), UtilityFunctions.SURVEXDATE_FORMAT) );
135 }
136
137 //Write calibration lines if different to parent series
138 //TODO Output Units for series and calibrations in these units (at same time as measurements in series units)
139 if ( series.getDeclination() != parentSeries.getDeclination() ) {
140 outputData.add( "*CALIBRATE declination " + series.getDeclination() );
141 }
142 if ( series.getTapeCalibration(LengthUnit.Metres) != parentSeries.getTapeCalibration(LengthUnit.Metres) ) {
143 outputData.add( "*CALIBRATE tape " + CaveConverter.padNumber( series.getTapeCalibration(LengthUnit.Metres), 2, 0 ) );
144 }
145 if ( series.getCompassCalibration(BearingUnit.Degrees) != parentSeries.getCompassCalibration(BearingUnit.Degrees) ) {
146 outputData.add( "*CALIBRATE compass " + series.getCompassCalibration(BearingUnit.Degrees) );
147 }
148 if ( series.getClinoCalibration(GradientUnit.Degrees) != parentSeries.getClinoCalibration(GradientUnit.Degrees) ) {
149 outputData.add( "*CALIBRATE clino " + series.getClinoCalibration(GradientUnit.Degrees) );
150 }
151 outputData.add( "" );
152
153 //Alias - for stations if splays output format is anonymous
154 if ( splaysOutput == SplayFormats.Anonymous ) {
155 outputData.add( "*alias station - .." );
156 outputData.add( "" );
157 }
158
159
160 //Add equates to output
161 ListIterator<SeriesLink> linksIterator = series.getLinks().listIterator();
162 while ( linksIterator.hasNext() ) {
163 SeriesLink link = linksIterator.next();
164 String equate = "*EQUATE ";
165
166 String series1Prefix = "";
167 String series2Prefix = "";
168 if ( link.getSeries1().length() > 0 ) {
169 series1Prefix = substIllegalNameChars( link.getSeries1() ) + ".";
170 }
171 if ( link.getSeries2().length() > 0 ) {
172 series2Prefix = substIllegalNameChars( link.getSeries2() ) + ".";
173 }
174 equate += series1Prefix + substIllegalNameChars( link.getStn1().getName() ) + " " +
175 series2Prefix + substIllegalNameChars( link.getStn2().getName() );
176
177 outputData.add( equate );
178 }
179 outputData.add( "");
180
181 //Write data format block if present and different to parent series
182 boolean skipDataOrderLine = false;
183 if ( series.hasDataOrder() ) {
184 if ( parentSeries.hasDataOrder() ) {
185 if ( UtilityFunctions.compareStringLists( series.getDataOrder(), parentSeries.getDataOrder() ) ) {
186 if ( series.getDataOrder2().size() == 0 && parentSeries.getDataOrder2().size() == 0 ) {
187 //Identical to parent and neither is a mix of data formats, so skip redeclaring data order in this series
188 skipDataOrderLine = true;
189 }
190 }
191 }
192 }
193
194 //Add data order header here if no legs in series, so it can apply to child series.
195 //(Series with legs will have data order headers written as needed in the leg processing loop later)
196 if ( ( skipDataOrderLine == false ) && ( series.legCount() == 0 ) && ( series.hasDataOrder() ) ) {
197 outputData.add( "*" + UtilityFunctions.dataFormatLineForSeries( series, false ) );
198 }
199
200 //Loop through the series legs and LRUD data writing details of each leg found
201 int psuedoLegCount = series.legCount() + series.getToStnLRUDs().size();
202 boolean lastLegWasDiving = false;
203 for ( int psuedoLegIdx = 0; psuedoLegIdx < psuedoLegCount; psuedoLegIdx++ ) {
204 //Determine if real leg of psuedo leg representing LRUD data on a toStn
205 SurveyLeg leg;
206 if ( psuedoLegIdx < series.legCount() ) {
207 //Real leg
208 leg = series.getLegRaw(psuedoLegIdx);
209 //If no data order in series, then skip data order headers unless diving data
210 if ( ( series.hasDataOrder() == false ) && ( leg.isDiving() == false ) ) {
211 //No data order in series, so skip it
212 skipDataOrderLine = true;
213 }
214 }
215 else {
216 //Need to generate a psudeo leg to hold the LRUD data for this toStn
217 leg = new SurveyLeg();
218 ToStnLRUD lrud = series.getToStnLRUDs().get( psuedoLegIdx - series.legCount() );
219 leg.setFromStn( lrud.getFromStn() );
220 leg.setLeft( lrud.getLeft(), LengthUnit.Metres );
221 leg.setRight( lrud.getRight(), LengthUnit.Metres );
222 leg.setUp( lrud.getUp(), LengthUnit.Metres );
223 leg.setDown( lrud.getDown(), LengthUnit.Metres );
224 }
225
226 String fromStn = leg.getFromStn().getName();
227 String fromStnVal = substIllegalNameChars( fromStn );
228
229 /* Check for valid leg. We do not want to write output for psuedo legs in a series which
230 * contain only a 'from station' with LRUD data on it,
231 * so only write leg data if a 'to station' exists or leg is a splay
232 */
233 if ( ( leg.isSplay() ) || ( leg.getToStn() != null ) ) {
234 String toStn;
235 if ( leg.getToStn() == null ) {
236 //Splay leg without a 'to' station. Create a station name from the leg from station
237 toStn = leg.getFromStn().getName() + "-" + splayNameSequence++;
238 }
239 else {
240 toStn = leg.getToStn().getName();
241 }
242 String toStnVal = substIllegalNameChars( toStn );
243
244 if ( leg.isNosurvey() || leg.getLength( LengthUnit.Metres ) > 0 ) {
245 //Determine whether to write data type header
246 if ( ( leg.isNosurvey() ) && nosurveyFlagOn == false ) {
247 legsData.add( "*data nosurvey from to");
248 nosurveyFlagOn = true;
249 }
250 else if ( ( leg.isNosurvey() == false ) && nosurveyFlagOn ) {
251 legsData.add( "*" + UtilityFunctions.dataFormatLineForSeries( series, leg.isDiving() ) );
252 nosurveyFlagOn = false;
253 }
254
255 //Determine if any flags need setting
256 String flagsSetting = "";
257 //Determine whether to set or clear duplicate flag
258 if ( ( leg.isDuplicate() ) && duplicateFlagOn == false ) {
259 flagsSetting += " DUPLICATE";
260 duplicateFlagOn = true;
261 }
262 else if ( ( leg.isDuplicate() == false ) && duplicateFlagOn ) {
263 flagsSetting += " NOT DUPLICATE";
264 duplicateFlagOn = false;
265 }
266
267 //Determine whether to set or clear surface flag
268 if ( ( leg.isSurface() ) && surfaceFlagOn == false ) {
269 flagsSetting += " SURFACE";
270 surfaceFlagOn = true;
271 }
272 else if ( ( leg.isSurface() == false ) && surfaceFlagOn ) {
273 flagsSetting += " NOT SURFACE";
274 surfaceFlagOn = false;
275 }
276 if ( splaysOutput == SplayFormats.Flagged ) {
277 //Determine whether to set or clear splays flag
278 if ( ( leg.isSplay() ) && splayFlagOn == false ) {
279 flagsSetting += " SPLAY";
280 splayFlagOn = true;
281 }
282 else if ( ( leg.isSplay() == false ) && splayFlagOn ) {
283 flagsSetting += " NOT SPLAY";
284 splayFlagOn = false;
285 }
286 }
287 //Write flags line if any flags changed
288 if ( flagsSetting.length() > 0 ) {
289 legsData.add( "*FLAGS" + flagsSetting);
290 }
291 if ( ( ( splaysOutput == SplayFormats.None ) && leg.isSplay() ) == false ) {
292 //Leg or a splay to be written
293
294 //Write data order line if first leg, or different format to last leg when secondary data order
295 boolean needDataOrderHeader = false;
296 if ( psuedoLegIdx == 0 ) {
297 //First leg, so write data order line regardless, and set flag for type
298 if ( skipDataOrderLine == false ) {
299 needDataOrderHeader = true;
300 }
301 }
302 else {
303 //Only write data order line if leg type has changed since last leg
304 if ( series.getDataOrder2().size() > 0 ) {
305 if ( lastLegWasDiving != leg.isDiving() ) {
306 needDataOrderHeader = true;
307 }
308 }
309 }
310 //Set flag ready for next leg
311 lastLegWasDiving = leg.isDiving();
312 //Output data order header line if needed before this leg
313 if ( needDataOrderHeader ) {
314 legsData.add( "*" + UtilityFunctions.dataFormatLineForSeries( series, leg.isDiving() ) );
315 }
316 //Determine data order to write out
317 List<String> dataOrder = series.getDataOrder();
318 if ( dataOrder.size() == 0 ) {
319 //No data order specified, so use Survex default order
320 dataOrder.add( CaveConverter.DATA_ORDER_CAT_FROMSTN );
321 dataOrder.add( CaveConverter.DATA_ORDER_CAT_TOSTN );
322 dataOrder.add( CaveConverter.DATA_ORDER_CAT_LENGTH );
323 dataOrder.add( CaveConverter.DATA_ORDER_CAT_BEARING );
324 dataOrder.add( CaveConverter.DATA_ORDER_CAT_CLINO );
325 }
326 else {
327 List<String> dataOrder2 = series.getDataOrder2();
328
329 //Determine if correct data order item for normal or diving in case of mixed data series
330 if ( dataOrder2.size() > 0 ) {
331 if ( UtilityFunctions.dataOrderIsDiving( dataOrder ) != leg.isDiving() ) {
332 //Primary data order does not match required type, so use secondary order
333 dataOrder = dataOrder2;
334 }
335 }
336 }
337
338 String legLine = "";
339
340 //Write data in data order
341 for ( int i = 0; i < dataOrder.size(); i++ ) {
342 String item = dataOrder.get(i);
343 if ( item.compareTo( CaveConverter.DATA_ORDER_CAT_FROMSTN ) == 0 ) {
344 legLine += fromStnVal + "\t";
345 }
346 else if ( item.compareTo( CaveConverter.DATA_ORDER_CAT_TOSTN ) == 0 ) {
347 if ( ( splaysOutput == SplayFormats.Anonymous ) && leg.isSplay() ) {
348 //Anonymous splay, so no name for toStn
349 legLine += "-\t";
350 }
351 else {
352 legLine += toStnVal + "\t";
353 }
354 }
355 else if ( nosurveyFlagOn == false ) {
356 if ( item.compareTo( CaveConverter.DATA_ORDER_CAT_LENGTH ) == 0 ) {
357 legLine += CaveConverter.padNumber( leg.getLength(LengthUnit.Metres), 2, 5 ) + "\t";
358 }
359 else if ( item.compareTo( CaveConverter.DATA_ORDER_CAT_BEARING ) == 0 ) {
360 legLine += CaveConverter.padNumber( leg.getCompass(BearingUnit.Degrees), 2, 6 ) + "\t";
361 }
362 else if ( item.compareTo( CaveConverter.DATA_ORDER_CAT_CLINO ) == 0 ) {
363 legLine += CaveConverter.padNumber( leg.getClino(GradientUnit.Degrees), 2, 6 ) + "\t";
364 }
365 else if ( item.compareTo( CaveConverter.DATA_ORDER_CAT_FROMDEPTH ) == 0 ) {
366 legLine += CaveConverter.padNumber( leg.getFromDepth(LengthUnit.Metres), 2, 5 ) + "\t";
367 }
368 else if ( item.compareTo( CaveConverter.DATA_ORDER_CAT_TODEPTH ) == 0 ) {
369 legLine += CaveConverter.padNumber( leg.getToDepth(LengthUnit.Metres), 2, 5 ) + "\t";
370 }
371 else if ( item.compareTo( CaveConverter.DATA_ORDER_CAT_DEPTHCHANGE ) == 0 ) {
372 legLine += CaveConverter.padNumber( leg.getDepthChange(LengthUnit.Metres), 2, 5 ) + "\t";
373 }
374 else if ( item.compareTo( CaveConverter.DATA_ORDER_CAT_IGNOREALL ) == 0 ) {
375 }
376 }
377 }
378
379 //Add comment to end of line
380 if ( leg.getComment().length() > 0 ) {
381 if ( dataOrder.get( dataOrder.size() - 1 ).compareTo( CaveConverter.DATA_ORDER_CAT_IGNOREALL ) != 0 ) {
382 //Use comment separator
383 legLine += ";";
384 }
385 legLine += leg.getComment();
386 }
387 else {
388 //Trim whitespace off end of line
389 legLine = legLine.trim();
390 }
391
392 legsData.add( legLine );
393 }
394 }
395 else {
396 //Zero length leg
397 if ( leg.isSplay() == false ) {
398 //Valid leg, so write as equate
399 legsData.add( "*EQUATE " + fromStnVal + "\t" + toStnVal );
400 }
401 }
402
403 //Add FIXed points to fixed stns block for series
404 SurveyStation stn = leg.getFromStn();
405 for (int i=0; i < 2; i++) {
406 //Loop twice, to process from and to stations
407 if ( stn.isFixed() == true ) {
408 String fixedStnLine = "*FIX " + substIllegalNameChars( stn.getName() );
409 fixedStnLine += "\t" + stn.getEasting() + "\t" + stn.getNorthing() + "\t" +
410 stn.getAltitude();
411 fixedStnsData.add( fixedStnLine );
412 }
413 //After first pass using FromStn, get toStn if there is one for second pass
414 if ( leg.getToStn() == null ) {
415 break;
416 }
417 stn = leg.getToStn();
418 }
419 }
420
421 //Process LRUD data for non-splay, non-surface legs, including legs with no LRUD data.
422 if ( ( leg.isSplay() == false ) && ( leg.isSurface() == false ) ) {
423 //Start new passage data block if leg does not connect the fromStn containing the LRUD data
424 //to the start or end of another passage data block in the file
425 boolean newBlock = false;
426 int activeBlockIdx = passageData.size() - 1; //Initialise to last block in list
427 boolean addAtFront = false;
428 if ( passageData.size() == 0 ) {
429 //Create first block for first lrud station in series
430 newBlock = true;
431 }
432 else {
433 //Check if station linked to a station at either end of an existing block
434 // We are looking for the toStn at either end of an existing block
435 boolean foundExistingBlock = false;
436
437 for (int idx = 0; idx < passageData.size(); idx++ ) {
438 List<String[]> block = passageData.get(idx);
439
440 if ( leg.getToStn() != null ) {
441 //Check if this block starts with the toStn for the leg
442 if ( ( block.size() == 1 ) && ( block.get(0)[0].equals( leg.getToStn().getName() ) ) ) {
443 //Leg toStn matches only station in block, so leg could go before or after this LRUD
444 //We need to know which direction the leg for the existing LRUD measurements was in
445 if ( block.get(0)[2].equals( leg.getFromStn().getName() ) ) {
446 //This leg follows the one previously added to this block, so add at end
447 activeBlockIdx = idx;
448 addAtFront = false;
449 foundExistingBlock = true;
450 }
451 else {
452 //This leg does NOT follow the one which started this block, so it must go before it
453 activeBlockIdx = idx;
454 addAtFront = true;
455 foundExistingBlock = true;
456 }
457 }
458 else if ( block.get(block.size() - 1)[0].equals( leg.getToStn().getName() ) ) {
459 //Matching toStn at end, so add LRUD to end of this block
460 activeBlockIdx = idx;
461 addAtFront = false;
462 foundExistingBlock = true;
463 }
464 else if ( block.get(0)[0].equals( leg.getToStn().getName() ) ) {
465 //Matching toStn at start, so add LRUD to start of this block
466 activeBlockIdx = idx;
467 addAtFront = true;
468 foundExistingBlock = true;
469 }
470 }
471 if ( foundExistingBlock == false ) {
472 //Need to find another leg which has a toStn matching the fromStn of this leg
473 for (int idx2 = 0; idx2 < series.legCount(); idx2++ ) {
474 SurveyLeg chkLeg = series.getLegRaw(idx2);
475 if ( ( chkLeg.getToStn() != null )
476 && ( chkLeg.getToStn().getName().equals(leg.getFromStn().getName() ) ) ) {
477 //Found a leg which precedes the leg we are adding LRUD data from.
478 //Check if the fromStn of this preceding leg is the last stn in the block
479 if ( block.get(block.size() - 1)[0].equals( chkLeg.getFromStn().getName() ) ) {
480 //Matching fromStn for previous leg at end of block, so add LRUD to end of this block
481 activeBlockIdx = idx;
482 addAtFront = false;
483 foundExistingBlock = true;
484 }
485 // This last case turns out not to be needed as it is generally better to start a new passage data block in this case.
486 // else {
487 // //Check if the preceding leg starts this block
488 // //(but is not already covered going down this block)
489 // int blockIdx2 = 1;
490 // if ( block.size() == 1 ) {
491 // blockIdx2 = 0;
492 // }
493 // if ( ( block.get(0)[0].equals( chkLeg.getFromStn().getName() ) )
494 // && ( block.get(blockIdx2)[0].equals( chkLeg.getToStn().getName() ) == false ) ) {
495 // //Matching toStn at start, so add LRUD to start of this block
496 // activeBlockIdx = idx;
497 // addAtFront = true;
498 // foundExistingBlock = true;
499 // logger.logMessage("Stn " + leg.getFromStn().getName() + " fromStn for leg matched toStn of leg " +
500 // chkLeg.getFromStn().getName() + "-" + chkLeg.getToStn().getName() +
501 // " where fromStn was at start of PD block but leg is not covered in this block");
502 // }
503 // }
504 if ( foundExistingBlock ) {
505 break;
506 }
507 }
508 }
509 }
510 if ( foundExistingBlock ) {
511 break;
512 }
513 }
514
515 if ( foundExistingBlock == false ) {
516 //Create new block for station as it is not connected to any other existing block
517 newBlock = true;
518 }
519 }
520
521 if (newBlock) {
522 passageData.add( new ArrayList<String[]>() );
523 activeBlockIdx = passageData.size() - 1;
524 }
525
526 String[] lrudLine = createLrudLine( leg );
527 if ( addAtFront ) {
528 passageData.get(activeBlockIdx).add(0, lrudLine);
529 }
530 else {
531 passageData.get(activeBlockIdx).add( lrudLine );
532 }
533 }
534 }
535 //Add missing toStns from legs which close loops
536 addMissingPassageDataAtLoopClosureStations( series, passageData );
537
538 //Replace LRUD values with those from the actual legs matching the pairs of stations
539 //for the current and next line in each block and remove single station blocks
540 reprocessPassageDataBlocks( series, passageData );
541
542 //Merge passage data blocks where one block ends with the same station as another starts
543 combinePassageDataBlocks( passageData );
544
545 //Write fixes data block
546 if ( fixedStnsData.size() > 0 ) {
547 outputData.addAll( fixedStnsData );
548 }
549
550 //Write legs data block
551 outputData.addAll( legsData );
552
553 //Turn off splays flag if on
554 if ( splayFlagOn ) {
555 outputData.add( "*FLAGS NOT SPLAY");
556 splayFlagOn = false;
557 }
558
559 //Write passage data block
560 if ( passageData.size() > 0 ) {
561 outputData.add( "");
562 //outputData.addAll( passageData );
563 ListIterator<List<String[]>> blocksIter = passageData.listIterator();
564 while ( blocksIter.hasNext() ) {
565 List<String[]> block = blocksIter.next();
566 outputData.add("*data passage station left right up down");
567 ListIterator<String[]> blockIter = block.listIterator();
568 while ( blockIter.hasNext() ) {
569 String[] line = blockIter.next();
570 //line[0] is fromStn name, and needs illegal characters substituting
571 //line[1] is the LRUD numbers, line[2] is the toStn name and is not output
572 outputData.add( substIllegalNameChars( line[0] ) + "\t" + line[1] );
573 }
574 }
575 }
576
577 //Loop through inner series
578 ListIterator<SurveySeries> seriesIterator = series.getInnerSeriesList().listIterator();
579 while ( seriesIterator.hasNext() ) {
580 SurveySeries innerSeries = seriesIterator.next();
581
582 outputData.addAll( generateSurvexDataSeries( innerSeries, series, splaysOutput ) );
583 }
584
585 //Close the series
586 outputData.add( "*END " + substIllegalNameChars( series.getSeriesName() ) );
587 outputData.add( "" );
588
589 return outputData;
590 }
591
592 /* Replace illegal characters in station or series names with Survex allowed name characters.
593 * This method should store previous conversions in case ambiguous cases might arise, and
594 * use a different character to prevent converted stations becoming ambiguous.
595 * e.g. CF+ and CF' do not want to both be changed to CF_ or CF-, but all occurrences of
596 * CF+ would want to be converted to the same name. We could also check the series for
597 * occurrences of the same name as an existing legal station (so if CF_1 already existed,
598 * we would not want to convert CF+ to CF_1). *
599 */
600 private String substIllegalNameChars( String stnName ) {
601 String result = "";
602
603 for (int i=0; i < stnName.length(); i++){
604 char c = stnName.charAt(i);
605 String sub = String.valueOf(c);
606
607 if ( c < 48 ) {
608 //Check for characters with ascii values below '0'
609 switch (c) {
610 case 33:
611 // 33 !
612 sub = "_ex";
613 break;
614 case 34:
615 // 34 "
616 sub = "_dq";
617 break;
618 case 35:
619 // 35 #
620 sub = "_hs";
621 break;
622 case 36:
623 // 36 $
624 sub = "_dl";
625 break;
626 case 37:
627 // 37 %
628 sub = "_pc";
629 break;
630 case 38:
631 // 38 &
632 sub = "_am";
633 break;
634 case 39:
635 // 39 '
636 sub = "_sq";
637 break;
638 case 40:
639 // 40 (
640 sub = "_ob";
641 break;
642 case 41:
643 // 41 )
644 sub = "_cb";
645 break;
646 case 42:
647 // 42 *
648 sub = "_as";
649 break;
650 case 43:
651 // 43 +
652 sub = "_pl";
653 break;
654 case 44:
655 // 44 ,
656 sub = "_cm";
657 break;
658 case 47:
659 // 47 /
660 sub = "_fs";
661 break;
662 }
663 }
664 else if ( c > 57 ) {
665 if ( c < 65 ) {
666 //Check for characters with ascii values between '9' and 'A'
667 switch (c) {
668 case 58:
669 // 58 :
670 sub = "_co";
671 break;
672 case 59:
673 // 59 ;
674 sub = "_sc";
675 break;
676 case 60:
677 // 60 <
678 sub = "_lt";
679 break;
680 case 61:
681 // 61 =
682 sub = "_eq";
683 break;
684 case 62:
685 // 62 >
686 sub = "_gt";
687 break;
688 case 63:
689 // 63 ?
690 sub = "_qm";
691 break;
692 case 64:
693 // 64 @
694 sub = "_at";
695 break;
696 }
697 }
698 else if ( c > 90 ) {
699 if ( c < 97 ) {
700 //Check for characters with ascii values between 'Z' and 'a'
701 switch (c) {
702 case 91:
703 // 91 [
704 sub = "_os";
705 break;
706 case 92:
707 // 92 \
708 sub = "_bs";
709 break;
710 case 93:
711 // 93 ]
712 sub = "_cs";
713 break;
714 case 94:
715 // 94 ^
716 sub = "_ht";
717 break;
718 case 95:
719 // 95 _
720 //Allowed character
721 //sub = "_";
722 break;
723 case 96:
724 // 96 `
725 sub = "_gr";
726 break;
727 }
728 }
729 else if ( c > 122 ) {
730 //Check for characters with ascii values between 'z' and '....'
731 switch (c) {
732 case 123:
733 // 123 {
734 sub = "_oc";
735 break;
736 case 124:
737 // 124 |
738 sub = "_pi";
739 break;
740 case 125:
741 // 125 }
742 sub = "_cc";
743 break;
744 case 126:
745 // 126 ~
746 sub = "_ti";
747 break;
748 default:
749 //All extended ascii characters, replace with ascii code
750 int code = 0 + c;
751 sub = "_asc" + code;
752 }
753 }
754 }
755 }
756 //TODO More illegal character mapping required
757
758 result += sub;
759 }
760
761 return result;
762 }
763
764 /* Replace LRUD values with those from actual leg represented by current and next station in block
765 * and remove single station blocks. Sometimes at junctions the initial LRUD data put into the block
766 * is from a leg which is not the correct leg for the next station added to the block. So where a
767 * leg can be matched to both the station on the data line, and the next station in the block then
768 * we can update the LRUD data to be from that leg.
769 */
770 private void reprocessPassageDataBlocks( SurveySeries series, List<List<String[]>> passageData ) {
771 //Process all blocks
772 ListIterator<List<String[]>> blocksIter = passageData.listIterator();
773 while ( blocksIter.hasNext() ) {
774 List<String[]> block = blocksIter.next();
775
776 //Remove block if only contains a single station as these are obsolete
777 if ( block.size() < 2 ) {
778 blocksIter.remove();
779 }
780 else {
781 //Update LRUD data for lines in block
782 boolean noLrudDataInBlock = true;
783 //Loop through each pair of stations in the block
784 for ( int blockIdx = 0; blockIdx < ( block.size() - 1 ); blockIdx++ ) {
785 String[] line1 = block.get(blockIdx);
786 String[] line2 = block.get(blockIdx + 1);
787
788 //Look for a leg representing this line1
789 boolean legMatchesPassageData = false;
790 for ( int legIdx = 0; legIdx < series.legCount(); legIdx++ ) {
791 SurveyLeg leg = series.getLegRaw(legIdx);
792 if ( ( leg.isSplay() == false )
793 && ( leg.getToStn() != null )
794 && ( leg.getLeft(LengthUnit.Metres) + leg.getRight(LengthUnit.Metres) +
795 leg.getUp(LengthUnit.Metres) + leg.getDown(LengthUnit.Metres) > 0.0 ) ) {
796 //Leg is a real leg with LRUD data, check for match both ways
797 if ( ( line1[0].equals( leg.getFromStn().getName() ) )
798 && ( line2[0].equals( leg.getToStn().getName() ) ) ) {
799 legMatchesPassageData = true;
800 }
801 //Do not look for reverse leg match because LRUD will be on opposite end of leg to the passage data line we are updating
802 // else if ( ( line2[0].equals( leg.getFromStn().getName() ) )
803 // && ( line1[0].equals( leg.getToStn().getName() ) ) ) {
804 // legMatchesPassageData = true;
805 // }
806 }
807 //If a matching leg was found then regenerate LRUD data for this line using the leg
808 if ( legMatchesPassageData ) {
809 //Replace LRUD data for this line with data from this leg
810 String[] newLine = createLrudLine(leg);
811 if ( line1[1].equals( newLine[1] ) == false ) {
812 block.remove(blockIdx);
813 block.add(blockIdx, newLine);
814 }
815 break; //Exit legs for loop
816 }
817 }
818
819 //Check if the line has any LRUD data if this block has not had any so far
820 if ( ( noLrudDataInBlock )
821 && ( ( line1[1].equals( " 0.00\t 0.00\t 0.00\t 0.00" ) == false )
822 || ( line2[1].equals( " 0.00\t 0.00\t 0.00\t 0.00" ) == false ) )
823 ) {
824 noLrudDataInBlock = false;
825 }
826 }
827
828 //Remove block if no LRUD data on any station
829 if ( noLrudDataInBlock ) {
830 blocksIter.remove();
831 }
832 }
833 }
834 }
835
836
837 /* Combine any passage data blocks which start with the same station as another block ends with.
838 */
839 private void combinePassageDataBlocks( List<List<String[]>> passageData ) {
840 //Process all blocks
841 int outerIdx = 0;
842 while ( outerIdx < passageData.size() ) {
843 List<String[]> block = passageData.get( outerIdx );
844
845 String lastStn = block.get( block.size() - 1 )[0];
846
847 //Iterate through all blocks to see if any start with the last station in this block
848 for ( int innerIdx1 = 0; innerIdx1 < passageData.size(); innerIdx1++ ) {
849 List<String[]> innerBlock = passageData.get( innerIdx1 );
850
851 if ( innerBlock.equals( block ) == false ) {
852 String innerStartStn = innerBlock.get( 0 )[0];
853
854 if ( innerStartStn.equals( lastStn ) ) {
855 boolean removeInner = true;
856 //Append this inner block to the end of the main iterator block
857 for ( int innerIdx2 = 0; innerIdx2 < innerBlock.size(); innerIdx2++ ) {
858 if ( innerIdx2 == 0 ) {
859 //Don't copy first station, but check it matches LRUD of last item in block we are appending to
860 if ( block.get( block.size() - 1 )[1].equals( innerBlock.get( 0 )[1] ) == false ) {
861 logger.logMessage("Unable to merge passage data blocks due to LRUD data mismatch: " +
862 "Block ending " + block.get( block.size() - 1 )[0] + "=" + block.get( block.size() - 1 )[1] + ", " +
863 "Block starting " + innerBlock.get( 0 )[0] + "=" + innerBlock.get( 0 )[1]);
864 removeInner = false;
865 break;
866 }
867 }
868 else {
869 block.add( innerBlock.get( innerIdx2 ) );
870 }
871 }
872 if ( removeInner ) {
873 //Remove the inner block now it has been copied
874 passageData.remove( innerIdx1 );
875 outerIdx--;
876 //Update last station to new end station
877 lastStn = block.get( block.size() - 1 )[0];
878 }
879 }
880 }
881 }
882 outerIdx++;
883 }
884 }
885
886 /* When a leg closes a loop, we need to add the LRUD for the toStn of the loop closure by finding that station
887 * with the LRUD data in another leg. In Stomps, the leg 31-25 has LRUD on 31. But there is no terminal stn25.
888 * Perhaps there should be? Or easier to analyse all passage data blocks here, consolidate blocks which are adjoined
889 * and then look for any legs where the toStn is not represented and add them by copying data from another leg.
890 */
891 private void addMissingPassageDataAtLoopClosureStations( SurveySeries series, List<List<String[]>> passageData ) {
892
893 //Check for legs with LRUD data on the toStn which are not represented in blocks
894 for ( int legIdx = 0; legIdx < series.legCount(); legIdx++ ) {
895 SurveyLeg leg = series.getLegRaw(legIdx);
896 if ( ( leg.isSplay() == false ) && ( leg.getLeft(LengthUnit.Metres) + leg.getRight(LengthUnit.Metres) +
897 leg.getUp(LengthUnit.Metres) + leg.getDown(LengthUnit.Metres) > 0.0 ) ) {
898 //Check for a leg which has LRUD data on the toStn of this leg
899 for ( int legIdx2 = 0; legIdx2 < series.legCount(); legIdx2++ ) {
900 SurveyLeg leg2 = series.getLegRaw(legIdx2);
901 if ( ( leg.isSplay() == false )
902 && ( leg.getToStn() != null )
903 && ( leg.getLeft(LengthUnit.Metres) + leg.getRight(LengthUnit.Metres) +
904 leg.getUp(LengthUnit.Metres) + leg.getDown(LengthUnit.Metres) > 0.0 )
905 && ( leg.getToStn().getName().equals( leg2.getFromStn().getName() ) ) ) {
906 //Check if the leg is represented in a passage data block
907 boolean foundLegInPassData = false;
908 ListIterator<List<String[]>> blocksIter = passageData.listIterator();
909 while ( blocksIter.hasNext() ) {
910 List<String[]> block = blocksIter.next();
911 //Look for pair of lines in this block which represent this leg
912 for ( int blockIdx = 0; blockIdx < ( block.size() - 1 ); blockIdx++ ) {
913 String[] line1 = block.get(blockIdx);
914 String[] line2 = block.get(blockIdx + 1);
915 if ( ( line1[0].equals( leg.getFromStn().getName() ) )
916 && ( line2[0].equals( leg.getToStn().getName() ) ) ) {
917 foundLegInPassData = true;
918 break; //Exit block for loop
919 }
920 else if ( ( line2[0].equals( leg.getFromStn().getName() ) )
921 && ( line1[0].equals( leg.getToStn().getName() ) ) ) {
922 foundLegInPassData = true;
923 break; //Exit block for loop
924 }
925 }
926 if ( foundLegInPassData ) {
927 break; //Exit passage data blocks loop
928 }
929 }
930 if ( foundLegInPassData == false ) {
931 //Leg with toStn LRUD is missing from passage data.
932 //Check if any passage data block starts or ends with one of the stns from the leg
933 //so we can add it
934 ListIterator<List<String[]>> blocksIter2 = passageData.listIterator();
935 while ( blocksIter2.hasNext() ) {
936 List<String[]> blockForAdd = blocksIter2.next();
937 int insertPoint = 0; //0=not found, 1=start, 2=end
938 if ( blockForAdd.get(blockForAdd.size() - 1)[0].equals( leg.getFromStn().getName() ) ) {
939 //Leg from station found at end of block
940 insertPoint = 2;
941 }
942 else if ( blockForAdd.get(0)[0].equals( leg.getFromStn().getName() ) ) {
943 //Leg from station found at start of block
944 insertPoint = 1;
945 }
946 if ( insertPoint > 0 ) {
947 //Loop through all passage data block lines looking for one which matches the toStn
948 ListIterator<List<String[]>> blocksIter3 = passageData.listIterator();
949 while ( blocksIter3.hasNext() ) {
950 List<String[]> blockToCopyFrom = blocksIter3.next();
951 boolean lineAdded = false;
952 ListIterator<String[]> blockIter = blockToCopyFrom.listIterator();
953 while ( blockIter.hasNext() ) {
954 String[] line = blockIter.next();
955
956 if ( line[0].equals( leg.getToStn().getName() ) ) {
957 //Insert suitable LRUD line here
958 if ( insertPoint == 1) {
959 //Insert at start
960 blockForAdd.add(0, line);
961 }
962 else {
963 //Insert at end
964 blockForAdd.add(line);
965 }
966 lineAdded = true;
967 break; //Exit loop through block looking for line to add
968 }
969 }
970
971 if ( lineAdded ) {
972 break; //Exit loop looking for passage data block to add line from
973 }
974 }
975
976 break; //Exit passage data blocks2 loop
977 }
978 }
979 }
980 break; //Exit loop leg2 as leg has been processed
981 }
982 }
983 }
984 }
985
986
987 }
988
989 private String[] createLrudLine( SurveyLeg bestLeg ) {
990 String[] lrudLine = new String[3];
991 lrudLine[0] = bestLeg.getFromStn().getName();
992 lrudLine[1] =
993 CaveConverter.padNumber(bestLeg.getLeft(LengthUnit.Metres),2,5) + "\t" +
994 CaveConverter.padNumber(bestLeg.getRight(LengthUnit.Metres),2,5) + "\t" +
995 CaveConverter.padNumber(bestLeg.getUp(LengthUnit.Metres),2,5) + "\t" +
996 CaveConverter.padNumber(bestLeg.getDown(LengthUnit.Metres),2,5);
997 if ( bestLeg.getToStn() != null ) {
998 lrudLine[2] = bestLeg.getToStn().getName();
999 }
1000 return lrudLine;
1001
1002 }
1003
1004 }