How to get point spacing


Placed inside ArcMap's VBA, this routine estimates the average point spacing of ASCII or LAS formatted point files in a folder. Output is written to the Immediate window.

How to use

  1. Paste the desired function code into ArcMap's VB Editor.
  2. Reference the ESRI GeoDatabaseExtensions Object Library.
  3. Reference the Miscrosoft Scripting Runtime Library.
  4. Run the function from a calling procedure which sends and receives the required parameters.
  5. Read the output written to the Immediate window in VB.
[VBA]
' sInputFolder is the path to the folder containing the files.
' sFileExt is the file suffix used by the point files. 'LAS' is treated as the
'   LAS LiDAR format.
' bIsXYZ indicates the file format when sFileExt is not equal to 'LAS'. When TRUE
'   the format is ASCII XYZ, when FALSE it's 3D ASCII GENERATE.

Public Sub GetPointSpacing(sInputFolder As String, sFileExt As String, Optional bIsXYZ As Boolean = True)
    On Error GoTo EH
    
    Dim pFSO As FileSystemObject
    Set pFSO = New FileSystemObject
    
    Dim pFolder As Folder
    Set pFolder = pFSO.GetFolder(sInputFolder)
    
    Dim pFiles As Files
    Set pFiles = pFolder.Files
    
    sFileExt = UCase(sFileExt)
    
    Dim pImporter As ITerrainDataImporter
    If (sFileExt = "LAS") Then
        Set pImporter = New TerrainLasDataImporter
        Dim pLasImporter As ITerrainLasDataImporter
        Set pLasImporter = pImporter
        pLasImporter.AddReturnNumber esriTerrainLasReturnAll
    Else
        Set pImporter = New TerrainAsciiDataImporter
        Dim pAsciiImporter As ITerrainAsciiDataImporter
        Set pAsciiImporter = pImporter
        If (bIsXYZ) Then
            pAsciiImporter.FileFormat = esriTerrainAsciiDataFormatXYZ
        Else
            pAsciiImporter.FileFormat = esriTerrainAsciiDataFormatGenerate
        End If
    End If
    
    ' Declare an array (Spacings) where the index represents the spacing,
    ' to the nearest whole number, and the value at the index represents the
    ' number of files observered with that spacing.
    Const cMax As Long = 1024 ' Represents the absolute maximum spacing (feet or meters)
    Dim Spacings(cMax) As Long ' this script will attempt to report.
    
    Dim pFile As File
    For Each pFile In pFiles
        
        If (Len(pFile.ShortName) > 3) Then
            Dim sSuffix As String
            sSuffix = Right(pFile.ShortName, 3)
        Else
            sSuffix = ""
        End If
        
        If (UCase(sSuffix) = sFileExt) Then
            pImporter.AddFile pFile.Path
            
            Dim pEnv As IEnvelope
            Set pEnv = pImporter.GetDataExtent(Nothing)
            
            Dim pArea As IArea
            Set pArea = pEnv
            
            Dim lPointCount As Long
            lPointCount = pImporter.GetPointCount
            
            ' round to nearest whole number
            If (lPointCount > 0) Then
                Dim lSpacing As Long
                lSpacing = Sqr(pArea.Area / lPointCount)
                
                If (lSpacing <= cMax) Then
                    Spacings(lSpacing) = Spacings(lSpacing) + 1
                End If
            End If
            
        End If
        
        pImporter.SetEmpty
        
        If (Not pLasImporter Is Nothing) Then
            pLasImporter.AddReturnNumber esriTerrainLasReturnAll
        End If
        
    Next
    
    ' Report histogram - number of files per spacing. Only report where observations
    ' are encountered.
    Dim lAveSpacing As Long
    lAveSpacing = 0
    Dim i As Long
    For i = 0 To cMax
        Dim lFileCount As Long
        lFileCount = Spacings(i)
        If (lFileCount > 0) Then
            Debug.Print "Average spacing: " & i & "; Number of files: "; lFileCount
        End If
    Next i
    
    Exit Sub
EH:
    MsgBox Err.Description
End Sub