-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclsInputFileVisiontac.vb
More file actions
352 lines (288 loc) · 12.3 KB
/
clsInputFileVisiontac.vb
File metadata and controls
352 lines (288 loc) · 12.3 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
Imports System.IO
Imports System.Math
Imports Microsoft.VisualBasic.FileIO
Public Class clsInputFileVisiontac
Inherits clsInputFile
Dim dtRaw As DataTable = New DataTable
Public Sub New(ByVal strFile As String)
FilePath = strFile
'Read the visiontac file
Dim strFields() As String
Dim strValues() As String
Dim strField As String
'If the file doesn't exist, search the current import folder and sub-folders
'for a file of that name. Temporary workaround added 6th June 2012 to find
'tracks from drop-box where dropbox is stored on a different path to that on
'the computer where it was saved.
If Not File.Exists(strFile) Then
strFile = SearchFolderForFile(frmOptions.txtImportFolder.Text, strFile)
End If
If File.Exists(strFile) Then
Dim tfp As TextFieldParser = New TextFieldParser(strFile)
tfp.TextFieldType = FieldType.Delimited
tfp.Delimiters = New String() {","}
'Get headers
strFields = tfp.ReadFields()
'Sometimes Visiontac can create empty files with no header line or
'anything. Account for that here.
Dim bVisiontacFieldsOK As Boolean = True
If Not strFields Is Nothing Then
'Check that expected Visiontac fields present - if
'not then set error message
Dim listFields As List(Of String) = New List(Of String)
listFields.Add("INDEX")
listFields.Add("TAG")
listFields.Add("DATE")
listFields.Add("TIME")
listFields.Add("LATITUDE N/S")
listFields.Add("LONGITUDE E/W")
listFields.Add("HEIGHT")
listFields.Add("SPEED")
listFields.Add("HEADING")
listFields.Add("VOX")
'For Each strField In strFields
' If Not strField.Trim = "" Then
' If Not listFields.Contains(strField.Trim("""")) Then
' bVisiontacFieldsOK = False
' ErrorMessage = "Expected visiontac fields missing"
' Exit For
' End If
' End If
'Next
For Each strField In listFields
If Not strField.Trim = "" Then
If Not strFields.Contains(strField.Trim("""")) Then
bVisiontacFieldsOK = False
ErrorMessage = "Expected visiontac fields missing"
Exit For
End If
End If
Next
End If
If bVisiontacFieldsOK And Not strFields Is Nothing Then 'Can happen if file empty
'If data table headers not already created, then do so
If dtRaw.Columns.Count = 0 Then
For Each strField In strFields
dtRaw.Columns.Add(strField)
Next
End If
'Read the rest
Do While Not tfp.EndOfData
strValues = tfp.ReadFields()
Try
dtRaw.Rows.Add(strValues)
Catch ex As Exception
'MessageBox.Show("There was a problem importing a row")
End Try
Loop
End If
tfp.Close()
End If
End Sub
Public Overrides Function GetPotentialRecords() As DataTable
Dim dt As DataTable = GetEmptyRecordsDatatable()
If dtRaw.Columns.Count = 0 Then
'This can happen if input file was empty
Return dt
End If
Dim rowRaw As DataRow
Dim row As DataRow
Dim rowsTagged As DataRow()
If frmOptions.rbVistiotacVoiceTags.Checked Then
rowsTagged = dtRaw.Select("TAG = 'V'")
Else
rowsTagged = dtRaw.Select("not TAG = 'T'")
End If
Dim strDate As String
Dim strTime As String
For Each rowRaw In rowsTagged
row = dt.NewRow()
row("FileLon") = GetLonFromVisiontac(rowRaw("LONGITUDE E/W"))
row("FileLat") = GetLatFromVisiontac(rowRaw("LATITUDE N/S"))
row("Filename") = Path.GetFileName(FilePath)
row("FileIndex") = rowRaw("INDEX").Trim(vbNullChar)
row("DateTimeKey") = rowRaw("DATE") & rowRaw("TIME")
'We assume the dates and times in Visiontac files are UTC
strDate = rowRaw("DATE")
strDate = strDate.Substring(4, 2) & "/" & strDate.Substring(2, 2) & "/20" & strDate.Substring(0, 2)
strTime = rowRaw("TIME")
If strTime.Length = 5 Then
strTime = "0" & strTime
End If
strTime = strTime.Substring(0, 2) & ":" & strTime.Substring(2, 2)
strDate = cfun.UTC2LocalTime(DateTime.Parse(strDate & " " & strTime & ":00"), cfun.RecDateTimeType.RecDate)
strTime = cfun.UTC2LocalTime(DateTime.Parse(strDate & " " & strTime & ":00"), cfun.RecDateTimeType.RecTime)
row("RecDate") = strDate
row("RecTime") = strTime
If Not cfun.HasNoValue(rowRaw("VOX")) Then
row("VoiceFile") = rowRaw("VOX").Replace(Chr(0), "")
End If
dt.Rows.Add(row)
Next
Return dt
End Function
Public Overrides Function GetTracks() As DataTable()
'There's only a single track in each Visiontac file
Dim rowRaw As DataRow
Dim row As DataRow
Dim strDate As String
Dim strTime As String
Dim dt(0) As DataTable
dt(0) = GetEmptyTrackDatatable()
For Each rowRaw In dtRaw.Rows
row = dt(0).NewRow()
row("Lon") = GetLonFromVisiontac(rowRaw("LONGITUDE E/W"))
row("Lat") = GetLatFromVisiontac(rowRaw("LATITUDE N/S"))
'We assume the dates and times in Visiontac files are UTC
strDate = rowRaw("DATE")
strDate = strDate.Substring(4, 2) & "/" & strDate.Substring(2, 2) & "/20" & strDate.Substring(0, 2)
strTime = rowRaw("TIME")
strTime = strTime.Substring(0, 2) & ":" & strTime.Substring(2, 2)
strDate = cfun.UTC2LocalTime(DateTime.Parse(strDate & " " & strTime & ":00"), cfun.RecDateTimeType.RecDate)
strTime = cfun.UTC2LocalTime(DateTime.Parse(strDate & " " & strTime & ":00"), cfun.RecDateTimeType.RecTime)
row("Date") = strDate
row("Time") = strTime
dt(0).Rows.Add(row)
Next
Return dt
End Function
Public Overrides Function LocationFromTime(ByVal dtetim As Date) As DataTable
Dim dt As DataTable = New DataTable
Return dt
End Function
Public Overrides Function TimeFromLocation(ByVal lat As Double, ByVal lon As Double) As DataTable
Dim dt As DataTable = New DataTable
Return dt
End Function
Public Overrides Function TrackToPoint(ByVal ref As String, ByVal dte As String, ByVal time As String, ByVal intPoints As Integer) As DataTable
Dim dt As DataTable = New DataTable
dt.Columns.Add("Lat")
dt.Columns.Add("Lon")
'For Visiontac, the ref should have a value
If Not ref = "" Then
Dim iIndex As Integer = CInt(ref)
Dim i As Integer
Dim strLat As String
Dim strLon As String
Dim row As DataRow
Dim strFilter As String
Dim x0 As Double
Dim y0 As Double
Dim x1 As Double
Dim y1 As Double
Dim n As Integer = 0
Dim rowNew As DataRow
For i = iIndex To 1 Step -1
strFilter = "INDEX='" & i.ToString() & "'"
row = dtRaw.Select(strFilter)(0)
If i = iIndex Then
strLat = row.Item("LATITUDE N/S").ToString()
strLon = row.Item("LONGITUDE E/W").ToString()
x0 = GetLonFromVisiontac(strLon)
y0 = GetLatFromVisiontac(strLat)
End If
strLat = row.Item("LATITUDE N/S").ToString()
strLon = row.Item("LONGITUDE E/W").ToString()
rowNew = dt.NewRow()
rowNew("Lat") = GetLatFromVisiontac(strLat)
rowNew("Lon") = GetLonFromVisiontac(strLon)
dt.Rows.Add(rowNew)
n = n + 1
If intPoints = 0 Then
'If the distance between this point and the record point is greater than 20m
'and more than three points have been considered then that's enough
x1 = GetEastingFromVisiontac(strLon, strLat)
y1 = GetNorthingFromVisiontac(strLon, strLat)
If Sqrt((x0 - x1) ^ 2 + (y0 - y1) ^ 2) > 20 And n > 3 Then
Exit For
End If
Else
If intPoints = n Then
Exit For
End If
End If
Next
End If
Return dt
End Function
Public Overrides Function AllTrackPoints() As DataTable
Dim dt As DataTable = New DataTable
dt.Columns.Add("Lat")
dt.Columns.Add("Lon")
Dim strLat As String
Dim strLon As String
Dim row As DataRow
Dim rowNew As DataRow
For Each row In dtRaw.Rows
strLat = row.Item("LATITUDE N/S").ToString()
strLon = row.Item("LONGITUDE E/W").ToString()
rowNew = dt.NewRow()
rowNew("Lat") = GetLatFromVisiontac(strLat)
rowNew("Lon") = GetLonFromVisiontac(strLon)
dt.Rows.Add(rowNew)
Next
Return dt
End Function
Private Function GetLonFromVisiontac(ByVal strLon As String) As Double
strLon = strLon.Trim
If strLon.Substring(strLon.Length - 1, 1) = "E" Then
strLon = strLon.Substring(0, strLon.Length - 1)
Else
strLon = "-" & strLon.Substring(0, strLon.Length - 1)
End If
Return Val(strLon)
End Function
Private Function GetLatFromVisiontac(ByVal strLat As String) As Double
strLat = strLat.Trim
If strLat.Substring(strLat.Length - 1, 1) = "N" Then
strLat = strLat.Substring(0, strLat.Length - 1)
Else
strLat = "-" & strLat.Substring(0, strLat.Length - 1)
End If
Return Val(strLat)
End Function
Private Function GetNorthingFromVisiontac(ByVal strLon As String, ByVal strLat As String)
Dim dblLon As Double
Dim dblLat As Double
Dim strGR As String = ""
strLon = strLon.Trim
If strLon.Substring(strLon.Length - 1, 1) = "E" Then
strLon = strLon.Substring(0, strLon.Length - 1)
Else
strLon = "-" & strLon.Substring(0, strLon.Length - 1)
End If
dblLon = Val(strLon)
strLat = strLat.Trim
If strLat.Substring(strLat.Length - 1, 1) = "N" Then
strLat = strLat.Substring(0, strLat.Length - 1)
Else
strLat = "-" & strLat.Substring(0, strLat.Length - 1)
End If
dblLat = Val(strLat)
Dim objGridRef As GridRef = New GridRef
objGridRef.MakePrefixArrays()
Return objGridRef.LatWGS842Northing(dblLat, dblLon, 100)
End Function
Private Function GetEastingFromVisiontac(ByVal strLon As String, ByVal strLat As String)
Dim dblLon As Double
Dim dblLat As Double
Dim strGR As String = ""
strLon = strLon.Trim
If strLon.Substring(strLon.Length - 1, 1) = "E" Then
strLon = strLon.Substring(0, strLon.Length - 1)
Else
strLon = "-" & strLon.Substring(0, strLon.Length - 1)
End If
dblLon = Val(strLon)
strLat = strLat.Trim
If strLat.Substring(strLat.Length - 1, 1) = "N" Then
strLat = strLat.Substring(0, strLat.Length - 1)
Else
strLat = "-" & strLat.Substring(0, strLat.Length - 1)
End If
dblLat = Val(strLat)
Dim objGridRef As GridRef = New GridRef
objGridRef.MakePrefixArrays()
Return objGridRef.LongWGS842Easting(dblLat, dblLon, 100)
End Function
End Class