Attribute VB_Name = "modTerrain" 'The number of points in each cell Dim points(0 To gridSize - 1, 0 To gridSize - 1) As Integer Dim grid(0 To gridSize - 1, 0 To gridSize - 1) As Double Public Sub setpoint(east As Double, north As Double, masl As Double, minEast As Double, maxEast As Double, _ minNorth As Double, maxNorth As Double) Dim e As Integer Dim n As Integer Dim tempE As Double Dim tempN As Double Dim range As Double 'Easting first range = maxEast - minEast range = 1.000001 * range tempE = (east - minEast) / range tempE = gridSize * tempE e = Int(tempE) 'now northing range = maxNorth - minNorth range = 1.000001 * range tempN = (north - minNorth) / range tempN = gridSize * tempN n = Int(tempN) If grid(e, n) > 0 Then grid(e, n) = ((grid(e, n) * points(e, n)) + masl) / (points(e, n) + 1) Else grid(e, n) = masl End If points(e, n) = points(e, n) + 1 End Sub Private Sub countpoints() Dim e As Integer Dim n As Integer Dim count As Long count = 0 For e = 0 To gridSize - 1 For n = 0 To gridSize - 1 count = count + points(e, n) Next n Next e count = 0 For e = 0 To gridSize - 1 For n = 0 To gridSize - 1 If grid(e, n) > 0 Then count = count + 1 End If Next n Next e End Sub Public Sub makeGrid() Dim i As Integer Dim minN As Double Dim minE As Double Dim maxN As Double Dim maxE As Double Let minN = 1000000 Let minE = 1000000 Let maxN = 0 Let maxE = 0 Let minN = getMin(Northing) Let minE = getMin(Easting) Let maxN = getMax(Northing) Let maxE = getMax(Easting) frmProgress.ProgressBar1.Min = 0 frmProgress.ProgressBar1.Max = RecordCounter frmProgress.ProgressBar1.Value = frmProgress.ProgressBar1.Min frmProgress.ProgressBar1.Visible = True frmProgress.txtName.Text = "Generating Grid" frmProgress.Show For i = 1 To RecordCounter Call setpoint(Easting(i), Northing(i), masl(i), minE, maxE, minN, maxN) frmProgress.ProgressBar1.Value = frmProgress.ProgressBar1.Value + 1 frmProgress.Refresh Next i frmProgress.Hide End Sub Public Function getMin(arr() As Double) As Double Dim result As Double Dim i As Integer result = arr(1) For i = 2 To RecordCounter If arr(i) < result Then result = arr(i) End If Next i getMin = result End Function Public Function countNorth(n As Integer) As Integer 'Counts the number of points east for a given northing Dim count As Integer Dim i As Integer Let count = 0 For i = 0 To (gridSize - 1) 'Move through the grid If grid(i, n) > 0 Then 'test if a value occurs at each grid point Let count = count + 1 End If Next i countNorth = count End Function Public Function countEast(e As Integer) As Integer 'Counts the number of points east for a given northing Dim count As Integer Dim i As Integer Let count = 0 For i = 0 To (gridSize - 1) 'Move through the grid If grid(e, i) > 0 Then 'test if a value occurs at each grid point Let count = count + 1 End If Next i countEast = count End Function Public Function NextE(n As Integer, Start As Integer) As Integer 'Finds the Next Point along one grid line heading east in the grid Dim i As Integer Dim result As Integer Let result = -1 'Do While i < gridSize And grid(i, N) = 0 'Test if a value found and if the edge of the grid is reached ' i = i + 1 'Loop For i = Start To gridSize - 1 If grid(i, n) > 0 Then result = i Exit For End If Next i NextE = result End Function Public Function NextN(e As Integer, Start As Integer) As Integer 'Finds the Next Point along one grid line heading north in the grid Dim i As Integer Dim result As Integer Let result = -1 'Do While grid(E, i) = 0 And i < gridSize 'Test if a value found and if the edge of the grid is reached ' i = i + 1 'Loop For i = Start To gridSize - 1 If grid(e, i) > 0 Then result = i Exit For End If Next i NextN = result End Function Public Sub inTerPolate() 'Estimate the values of cells in the grid between the measured points 'Firstly in an easterly direction then northerly 'Negate the interpolated points so they can be determine from the measured ones Dim e As Integer Dim n As Integer Dim i As Integer Dim iFrom As Integer Dim iTo As Integer Dim STeps As Integer Dim Interval As Integer Dim HeightDiff As Double Dim temp As Double frmProgress.ProgressBar1.Min = 0 frmProgress.ProgressBar1.Max = gridSize + gridSize frmProgress.ProgressBar1.Value = frmProgress.ProgressBar1.Min frmProgress.ProgressBar1.Visible = True frmProgress.Show 'East first For e = 0 To gridSize - 1 count = countEast(e) If count > 1 Then Let STeps = 0 Let iFrom = 0 Do Let iFrom = NextN(e, iFrom) Let iTo = NextN(e, iFrom + 1) STeps = STeps + 1 Interval = iTo - iFrom HeightDiff = grid(e, iTo) - grid(e, iFrom) For i = iFrom + 1 To iTo - 1 grid(e, i) = (i - iFrom) * HeightDiff / Interval + grid(e, iFrom) Let grid(e, i) = -grid(e, i) Next i iFrom = iTo Loop While STeps < count - 1 End If frmProgress.ProgressBar1.Value = frmProgress.ProgressBar1.Value + 1 frmProgress.Refresh Next e 'next north For n = 0 To gridSize - 1 count = countNorth(n) If count > 1 Then Let STeps = 0 Let iFrom = 0 Do Let iFrom = NextE(n, iFrom) Let iTo = NextE(n, iFrom + 1) STeps = STeps + 1 Interval = iTo - iFrom HeightDiff = grid(iTo, n) - grid(iFrom, n) For i = iFrom + 1 To iTo - 1 Let temp = grid(i, n) grid(i, n) = (i - iFrom) * HeightDiff / Interval + grid(iFrom, n) Let grid(i, n) = -grid(i, n) If Abs(temp) > 0 Then 'Due to the fact points heights are geing averaged there might be a 'difference in height between the north and east grid lines 'so we are averaging out the height at the intersections Let grid(i, n) = (Abs(temp) + Abs(grid(i, n))) / 2 Let grid(i, n) = -grid(i, n) End If Next i iFrom = iTo Loop While STeps < count - 1 End If frmProgress.ProgressBar1.Value = frmProgress.ProgressBar1.Value + 1 frmProgress.Refresh Next n frmProgress.Hide End Sub Public Sub resetPoints() 'resets the negative values (used to differenciate calulated values) Dim count As Long Dim e As Integer Dim n As Integer For e = 0 To gridSize - 1 For n = 0 To gridSize - 1 grid(e, n) = Abs(grid(e, n)) If (grid(e, n) > 0) Then count = count + 1 End If Next n Next e End Sub Public Function getMax(arr() As Double) As Double Dim result As Double Dim i As Integer result = arr(1) For i = 2 To RecordCounter If arr(i) > result Then result = arr(i) End If Next i getMax = result End Function Private Sub MapTerrain(minMASL As Double, maxMASL As Double) 'MapTerrain scale the measured data so that they correspond 'to the height range of MASL. Dim e As Integer Dim n As Integer Dim rangeMASL As Double Let rangeMASL = maxMASL - minMASL For e = 0 To gridSize - 1 For n = 0 To gridSize - 1 If grid(e, n) > 0 Then Terrain(e, n) = _ Int(255 * (grid(e, n) - minMASL) / rangeMASL) If (Terrain(e, n) > 255) Then MsgBox "Terrain too big", vbOKOnly End If If (Terrain(e, n) < 0) Then MsgBox "Terrain too small", vbOKOnly End If Else Terrain(e, n) = 0 End If Next n Next e End Sub Public Sub genTerrain() Dim minMASL As Double Dim maxMASL As Double minMASL = getMin(masl) maxMASL = getMax(masl) frmProgress.txtName.Text = "Interpolating Points Pass 1 of 2" Call inTerPolate Call resetPoints 'functions are recalled to cover any new points exposed frmProgress.txtName.Text = "Interpolating Points Pass 2 of 2" Call inTerPolate Call resetPoints Call MapTerrain(minMASL, maxMASL) End Sub