Debian Codegraph @

src/footleg/cavesurvey/data/writer/CompassWriter.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
24 import footleg.cavesurvey.converter.CaveConverter;
25 import footleg.cavesurvey.converter.Logger;
26 import footleg.cavesurvey.converter.CaveConverter.BearingUnit;
27 import footleg.cavesurvey.converter.CaveConverter.GradientUnit;
28 import footleg.cavesurvey.converter.CaveConverter.LengthUnit;
29 import footleg.cavesurvey.data.model.CaveSurvey;
30 import footleg.cavesurvey.data.model.SurveyLeg;
31 import footleg.cavesurvey.data.model.SurveySeries;
32
33 /**
34 * Writer for Compass file format text data.
35 *
36 * @author Footleg
37 * @version 2017.01.09 (ISO 8601 YYYY.MM.DD)
38 * @since 1.6 (The Java version used)
39 */
40 public class CompassWriter {
41 private List<String> shortNames = new ArrayList<String>();
42 private Logger logger;
43
44 public CompassWriter( Logger logger ) {
45 super();
46 this.logger = logger;
47 }
48
49 /**
50 * Generates Compass format data from a cave survey
51 *
52 * THIS CLASS IS NOT FULLY IMPLEMENTED YET AND WILL NOT GENERATE VALID DATA
53 *
54 * @param surveyData The cave survey model to generate Compass data for
55 * @return Text lines of Compass format data
56 */
57 public List<String> generateCompassData( CaveSurvey surveyData ) {
58 List<String> outputData = new ArrayList<String>();
59 String caveName = "cave";
60
61 //See if we can determine cave name from series name of first series
62 SurveySeries series1 = surveyData.get(0);
63 if ( series1.getSeriesName().length() > 0 ) {
64 //Check for dot separator
65 int pos = series1.getSeriesName().indexOf('.');
66 if ( pos > 0) {
67 //Take name before first separator
68 caveName = series1.getSeriesName().substring(0, pos);
69 }
70 else {
71 //Work backwards until first non-numeric character found
72 for ( int nameIdx = series1.getSeriesName().length() - 1; nameIdx > -1; nameIdx-- ) {
73 //Check for non-numerical character
74 int charCode = series1.getSeriesName().charAt(nameIdx);
75 if ( charCode < 48 || charCode > 57 ) {
76 //Take name prior to this point
77 caveName = series1.getSeriesName().substring(0, nameIdx + 1);
78 nameIdx = 0;
79 }
80 }
81 }
82 }
83
84 //Generate short names for all series
85 for ( int seriesIdx = 0; seriesIdx < surveyData.size(); seriesIdx++ ) {
86 String seriesName = surveyData.get(seriesIdx).getSeriesName();
87 String reducedName = "";
88
89 /* //Remove cave name prefix if found
90 if ( seriesName.substring(0, caveName.length() + 1 ).compareTo( caveName + "." ) == 0 ) {
91 seriesName = seriesName.substring(caveName.length() + 1);
92 }
93 */
94
95 //Look for last part of multi-part names
96 String nameLastPart = seriesName;
97 int lstIdx = seriesName.lastIndexOf('.');
98 if ( lstIdx > -1 && lstIdx < (seriesName.length() - 1) ) {
99 //Take just last part of name
100 nameLastPart = seriesName.substring(lstIdx + 1);
101 }
102
103 //Remove illegal characters
104 for ( int idx = 0; reducedName.length() < 4 && idx < nameLastPart.length(); idx ++ ) {
105 if ( nameLastPart.charAt(idx) != '.' ) {
106 reducedName += nameLastPart.charAt(idx);
107 }
108 }
109
110 int trys = 0;
111 while ( trys < 110 ) {
112 trys++;
113 String testName = " ";
114
115 switch (trys) {
116 case 1:
117 //Try using first 4 chars of name padded with leading spaces
118 testName = reducedName;
119 break;
120 case 2:
121 //Try removing vowels
122 reducedName = "";
123 for ( int idx = 0; reducedName.length() < 4 && idx < nameLastPart.length(); idx ++ ) {
124 if ( nameLastPart.charAt(idx) != '.'
125 && nameLastPart.charAt(idx) != 'a'
126 && nameLastPart.charAt(idx) != 'e'
127 && nameLastPart.charAt(idx) != 'i'
128 && nameLastPart.charAt(idx) != 'o'
129 && nameLastPart.charAt(idx) != 'u'
130 && nameLastPart.charAt(idx) != 'A'
131 && nameLastPart.charAt(idx) != 'E'
132 && nameLastPart.charAt(idx) != 'I'
133 && nameLastPart.charAt(idx) != 'O'
134 && nameLastPart.charAt(idx) != 'U'
135 ) {
136 reducedName += nameLastPart.charAt(idx);
137 }
138 }
139 testName += reducedName;
140 break;
141 case 3:
142 case 4:
143 case 5:
144 case 6:
145 case 7:
146 case 8:
147 case 9:
148 case 10:
149 case 11:
150 //Try adding increment number to end of 3 char name
151 testName += reducedName.substring(0,3) + ( trys - 2 );
152 break;
153 default :
154 //Try adding increment number to end of 2 char name
155 testName += reducedName.substring(0,2) + ( trys - 11 );
156 }
157
158 //Trim to 4 chars
159 testName = testName.substring( testName.length() - 4 );
160
161 //Check if name already used
162 if ( nameAlreadyUsed( testName ) == false ) {
163 //Use this name
164 shortNames.add( testName );
165 trys = 999;
166 }
167 }
168 //Check name was added
169 if (trys != 999) {
170 RuntimeException ex = new RuntimeException("Failed to generate unique short name for series name: " + seriesName );
171 throw ex;
172 }
173 }
174
175 //Loop through all series
176 for ( int seriesIdx = 0; seriesIdx < surveyData.size(); seriesIdx++ ) {
177 SurveySeries series = surveyData.get(seriesIdx);
178 List<String> legsData = new ArrayList<String>();
179 List<String> fixedStnsData = new ArrayList<String>();
180 List<String> passageData = new ArrayList<String>();
181
182 //Write header lines for series
183 outputData.add( caveName );
184 outputData.add( "SURVEY NAME: " + series.getSeriesName() );
185 outputData.add( "SURVEY DATE: " + series.getSurveyDate() );
186 outputData.add( "SURVEY TEAM: ");
187 outputData.add( " ");
188 outputData.add( "DECLINATION: " + series.getCompassCalibration(BearingUnit.Degrees) + " FORMAT: DMMDLRUDLADN CORRECTIONS: 0.00 0.00 0.00");
189 outputData.add( " ");
190 outputData.add( "FROM TO LENGTH BEARING INC LEFT UP DOWN RIGHT FLAGS COMMENTS");
191 /*
192 //Add equates to output
193 for ( int linkIdx = 0; linkIdx < series.getLinks().size(); linkIdx++ ) {
194 SeriesLink link = series.getLinks().get(linkIdx);
195 String equate = "*EQUATE ";
196 if ( link.getLinkedSeries() == -1 ) {
197 //Link to series not in current data file, so create 'false' equate placeholder in file
198 equate = ";" + equate + series.getSeriesName() + "." + link.getLocalStation().getName() + " " +
199 "CAVE" + "." +
200 link.getLocalStation();
201 }
202 else {
203 equate += series.getSeriesName() + "." + link.getLocalStation().getName() + " " +
204 surveyData.get( link.getLinkedSeries() ).getSeriesName() + "." +
205 link.getLinkedStation().getName();
206 }
207 outputData.add( equate );
208 }
209 outputData.add( "");
210 */
211 //Start series block
212 outputData.add( "*BEGIN " + series.getSeriesName() );
213
214 //TODO Write date if present
215 // if ( series.getSurveyDate().length() > 0 ) {
216 // compassData.add( ";Date " + series.getSurveyDate() );
217 // }
218
219 //Write calibration lines
220 if ( series.getTapeCalibration(LengthUnit.Metres) != 0 ) {
221 outputData.add( "*CALIBRATE tape " + series.getTapeCalibration(LengthUnit.Feet) );
222 }
223 if ( series.getCompassCalibration(BearingUnit.Degrees) != 0 ) {
224 outputData.add( "*CALIBRATE declination " + series.getCompassCalibration(BearingUnit.Degrees) );
225 }
226 if ( series.getClinoCalibration(GradientUnit.Degrees) != 0 ) {
227 outputData.add( "*CALIBRATE clino " + series.getClinoCalibration(GradientUnit.Degrees) );
228 }
229 outputData.add( "");
230
231 //Loop through the series legs writing details of each leg found
232 for ( int legIdx = 0; legIdx < series.legCount(); legIdx++ ) {
233 SurveyLeg leg = series.getLegRaw(legIdx);
234
235 //Check for valid leg
236 String fromStn = leg.getFromStn().getName();
237 String toStn = leg.getToStn().getName();
238 if ( leg.getLength( LengthUnit.Metres ) > 0 ) {
239 //Write leg data
240 legsData.add( fromStn + "\t" + toStn + "\t" +
241 CaveConverter.padNumber( leg.getLength(LengthUnit.Feet), 2, 5 ) + "\t" +
242 CaveConverter.padNumber( leg.getCompass(BearingUnit.Degrees), 2, 6 ) + "\t" +
243 CaveConverter.padNumber( leg.getClino(GradientUnit.Degrees), 2, 6 ) + "\t");
244 }
245 else {
246 //Zero length leg
247 //Get to stn name if there is one
248 if ( leg.isSplay() == false ) {
249 //Valid leg, so write as equate
250 legsData.add( "*EQUATE " + fromStn + "\t" + toStn );
251 }
252 }
253
254 //Add FIXed points to fixed stns block for series
255 // if (leg.hasFixedStn() == true) {
256 // String fixedStnLine = "*FIX ";
257 // if ( leg.isFromStnFixed() == true ) {
258 // fixedStnLine += fromStn;
259 // }
260 // else {
261 // fixedStnLine += getActualStationName( series, leg.getToStn() );
262 // }
263 // fixedStnLine += "\t" + leg.getEasting() + "\t" + leg.getNorthing() + "\t" +
264 // leg.getAltitude();
265 // fixedStnsData.add( fixedStnLine );
266 // }
267
268 //Add LRUD to passages block for series
269 if ( leg.getLeft(LengthUnit.Metres) + leg.getRight(LengthUnit.Metres) +
270 leg.getUp(LengthUnit.Metres) + leg.getDown(LengthUnit.Metres) > 0.0 ) {
271 passageData.add( fromStn + "\t" + leg.getLeft(LengthUnit.Feet) + "\t" +
272 leg.getRight(LengthUnit.Feet) + "\t" + leg.getUp(LengthUnit.Feet) + "\t" +
273 leg.getDown(LengthUnit.Feet) + "\t" );
274 }
275 }
276 //Write fixes data block
277 if ( fixedStnsData.size() > 0 ) {
278 outputData.addAll( fixedStnsData );
279 }
280 //Write legs data block
281 outputData.addAll( legsData );
282 //Write passage data block
283 if ( passageData.size() > 0 ) {
284 outputData.add( "");
285 outputData.add( "*data passage station left right up down");
286 outputData.addAll( passageData );
287 }
288 //Close the series
289 outputData.add( "*END " + series.getSeriesName() + "\n");
290 }
291
292 //Close the cave name
293 outputData.add( "*END " + caveName );
294
295 return outputData;
296 }
297
298 /**
299 * Looks up short series name for series from array of names created previously
300 * @param seriesIdx
301 * @return short (4 character) name for series
302 */
303 private String getShortSeriesName( int seriesIdx ) {
304 return shortNames.get(seriesIdx);
305 }
306
307 /**
308 * Looks for short series name in array of names used
309 * @param name
310 * @return true if name has already been used for another series
311 */
312 private boolean nameAlreadyUsed( String name ) {
313 boolean found = false;
314 for ( int i = 0; i < shortNames.size(); i++ ) {
315 if ( shortNames.get(i).compareToIgnoreCase( name ) == 0 ) {
316 found = true;
317 i = shortNames.size();
318 }
319 }
320 return found;
321 }
322
323 }