-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdr2lev.java
238 lines (184 loc) · 7.54 KB
/
sdr2lev.java
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// sdr2lev.java
// convert SDR2x file to 'lev' format
import java.io.*;
import java.util.*;
import java.text.*;
public class sdr2lev{
protected BufferedReader in;
protected LinkedList<String> oRawData, observationRows;
protected String sampleRow = " 10.00000 10.00000";
// specification of digital level
protected double maxDist, minDist, staffLength;
// constructor
public sdr2lev(String sFileName){
try{
in = new BufferedReader(new FileReader(sFileName));
Properties prop = new Properties();
prop.load(new BufferedReader(new FileReader("config.txt")));
maxDist = Double.parseDouble(prop.getProperty("maxDist"));
minDist = Double.parseDouble(prop.getProperty("minDist"));
staffLength = Double.parseDouble(prop.getProperty("staffLength"));
// test coding
// System.out.println(maxDist + "\t" + minDist + "\t" + staffLength);
}catch(Exception ex){
System.out.println(ex);
System.exit(0);
}
}
public void filterRawDate(){
try{
oRawData = new LinkedList<String>();
String sLine = null;
while((sLine=in.readLine())!=null){
// skip the 'mumbel jumble'
if(sLine.startsWith("63LV")){
// System.out.println(sLine);
oRawData.add(sLine);
}
}
}catch(Exception ex){
System.out.println(ex);
System.exit(0);
}
}
public void printRawDate(){
for(int index=0; index<oRawData.size(); index++){
System.out.println(oRawData.get(index));
}
}
protected String getDescription(String sLine){
// columns 29-44 Description (BS=backsight)
return sLine.substring(28, 30);
}
protected String getPointId(String sLine){
return cutLeadingZeros(sLine.substring(4, 8));
}
protected String cutLeadingZeros(String sLine){
if(sLine.startsWith("0")){
return cutLeadingZeros(sLine.substring(1));
}else{
return new String(sLine);
}
}
protected String getDistance(String sLine){
// columns 9~18 Distance
double dist = Double.parseDouble(sLine.substring(8, 18));
DecimalFormat df = new DecimalFormat("0.00");
return df.format(dist);
}
protected String getStaffReading(String sLine){
// columns 19~28 staff reading
double staffReading = Double.parseDouble(sLine.substring(18, 28));
DecimalFormat df = new DecimalFormat("0.00000");
return df.format(staffReading);
}
public boolean checkRawData(){
boolean result = true;
if(!getDescription(oRawData.getFirst()).equalsIgnoreCase("BS")){
System.out.println("Error! First element is not Back Sight!");
result = false;
}
if(!getDescription(oRawData.getLast()).equalsIgnoreCase("FS") && !getDescription(oRawData.getLast()).equalsIgnoreCase("FI")){
System.out.println("Error! Last element is not Fore Sight!");
result = false;
return result;
}
for(int index=0; index<oRawData.size()-1; index++){
if(getDescription(oRawData.get(index)).equalsIgnoreCase("BS")){
// two consecutive BS
if(getDescription(oRawData.get(index+1)).equalsIgnoreCase("BS")){
System.out.println("Error! two consecutive BS!");
result = false;
}
}
if(getDescription(oRawData.get(index)).equalsIgnoreCase("IS")){
if(getDescription(oRawData.get(index+1)).equalsIgnoreCase("BS")){
System.out.println("Error! a BS was found after an IS!");
result = false;
}
}
if(getDescription(oRawData.get(index)).equalsIgnoreCase("FS") || getDescription(oRawData.get(index)).equalsIgnoreCase("FI")){
if(!getDescription(oRawData.get(index+1)).equalsIgnoreCase("BS")){
System.out.println("Error! a FS was not followed by a BS!");
result = false;
}
}
}
return result;
}
void packaging(){
// pack the observations into ESCS *.lev format
observationRows = new LinkedList<String>();
String firstRow = insertBS(oRawData.getFirst(), "BS" + sampleRow);
String lastRow = insertFS(oRawData.getLast(), "FS" + sampleRow);
// test coding
System.out.println(firstRow);
// System.out.println(lastRow);
int index = 1;
while(index<oRawData.size()-1){
String sRawData = oRawData.get(index);
if(getDescription(sRawData).equalsIgnoreCase("FS") || getDescription(sRawData).equalsIgnoreCase("FI")){
String fieldBookRow = insertFS(sRawData, "CP" + sampleRow);
index++;
sRawData = oRawData.get(index);
fieldBookRow = insertBS(sRawData, fieldBookRow);
// test coding
System.out.println(fieldBookRow);
}
if(getDescription(sRawData).equalsIgnoreCase("IS")){
String fieldBookRow = insertIS(sRawData, "IS" + sampleRow);
// test coding
System.out.println(fieldBookRow);
}
index++;
}
// test coding
System.out.println(lastRow);
}
protected String insertFS(String bsObservation, String orginalFieldBookRow){
StringBuffer fieldBookRow = new StringBuffer(orginalFieldBookRow);
String pointId = getPointId(bsObservation);
fieldBookRow.replace(3, 3 + pointId.length(), pointId);
String dist = getDistance(bsObservation);
fieldBookRow.replace(55, 55 + dist.length(), dist);
String staffReading = getStaffReading(bsObservation);
fieldBookRow.replace(45, 45 + staffReading.length(), staffReading);
return fieldBookRow.toString();
}
protected String insertBS(String bsObservation, String orginalFieldBookRow){
StringBuffer fieldBookRow = new StringBuffer(orginalFieldBookRow);
String pointId = getPointId(bsObservation);
fieldBookRow.replace(3, 3 + pointId.length(), pointId);
String dist = getDistance(bsObservation);
fieldBookRow.replace(16, 16 + dist.length(), dist);
String staffReading = getStaffReading(bsObservation);
fieldBookRow.replace(23, 23 + staffReading.length(), staffReading);
return fieldBookRow.toString();
}
protected String insertIS(String bsObservation, String orginalFieldBookRow){
StringBuffer fieldBookRow = new StringBuffer(orginalFieldBookRow);
String pointId = getPointId(bsObservation);
fieldBookRow.replace(3, 3 + pointId.length(), pointId);
String dist = getDistance(bsObservation);
fieldBookRow.replace(55, 55 + dist.length(), dist);
String staffReading = getStaffReading(bsObservation);
fieldBookRow.replace(33, 33 + staffReading.length(), staffReading);
return fieldBookRow.toString();
}
public static void main(String[] args)throws Exception{
if(args.length<1){
System.out.println("usage : java sdr2lev FileName");
System.exit(0);
}
sdr2lev oLevFile = new sdr2lev(args[0]);
oLevFile.filterRawDate();
if(oLevFile.checkRawData()){
// oLevFile.printRawDate();
oLevFile.packaging();
}
// String sDesc = sLine.substring(28, 30);
// String sPointId = sLine.substring(4, 8);
// String sDist = sLine.substring(8, 18);
// String sRead = sLine.substring(18, 28);
}
}