How to create a layer based on XY data


This sample uses a delimited textfile table to create an XY Event layer which is then added to ArcMap. The points displayed by this layer are generated dynamically when the layer is drawn. The same approach may be take to create XY Event layers from any type of standalone table.

How to use

  1. Paste the code into your code file.
  2. Create a new text file named XYSample.txt, then add the data below to the new textfile. If you name the file something else, you will need to adjust the sample code.

    x,y,place
    419289.2644,3744635.9201,Anaheim CA
    392626.0961,3739354.4688,Long Beach CA
    444090.0630,3768354.3989,Ontario CA
    543630.3519,3737391.7820,Palm Springs
    CA 395037.5403,3780560.0343,Pasadena CA
    484233.0769,3768589.0100,Redlands CA
    463278.9584,3755622.1016,Riverside CA
    473051.8917,3777689.9331,San Bernadino CA
    362120.1377,3764646.9229,Santa Monica CA
    373495.0135,3772750.7525,W Hollwood CA
  3. Add the XYSample.txt file to ArcMap as a table.
  4. Run the sample.
[VBA]
Private Sub AddXYEventLayer()
    
    On Error GoTo EH
    
    Dim pDoc As IMxDocument
    Dim pMap As IMap
    Set pDoc = ThisDocument
    Set pMap = pDoc.FocusMap
    
    ' Get the table named XYSample.txt
    Dim pStTabCol As IStandaloneTableCollection
    Dim pStandaloneTable As IStandaloneTable
    Dim intCount As Integer
    Dim pTable As ITable
    Set pStTabCol = pMap
    For intCount = 0 To pStTabCol.StandaloneTableCount - 1
        Set pStandaloneTable = pStTabCol.StandaloneTable(intCount)
        If pStandaloneTable.Name = "XYSample.txt" Then
            Set pTable = pStandaloneTable.Table
            Exit For
        End If
    Next
    If pTable Is Nothing Then
        MsgBox "The table was not found"
        Exit Sub
    End If
    
    ' Get the table name object
    Dim pDataSet As IDataset
    Dim pTableName As IName
    Set pDataSet = pTable
    Set pTableName = pDataSet.FullName
    
    ' Specify the X and Y fields
    Dim pXYEvent2FieldsProperties As IXYEvent2FieldsProperties
    Set pXYEvent2FieldsProperties = New XYEvent2FieldsProperties
    With pXYEvent2FieldsProperties
        .XFieldName = "x"
        .YFieldName = "y"
        .ZFieldName = ""
    End With
    
    ' Specify the projection
    Dim pSpatialReferenceFactory As ISpatialReferenceFactory
    Dim pProjectedCoordinateSystem As IProjectedCoordinateSystem
    Set pSpatialReferenceFactory = New SpatialReferenceEnvironment
    Set pProjectedCoordinateSystem = pSpatialReferenceFactory.CreateProjectedCoordinateSystem(esriSRProjCS_NAD1983UTM_11N)
    
    ' Create the XY name object and set it's properties
    Dim pXYEventSourceName As IXYEventSourceName
    Dim pXYName As IName
    Dim pXYEventSource As IXYEventSource
    Set pXYEventSourceName = New XYEventSourceName
    With pXYEventSourceName
        Set .EventProperties = pXYEvent2FieldsProperties
        Set .SpatialReference = pProjectedCoordinateSystem
        Set .EventTableName = pTableName
    End With
    Set pXYName = pXYEventSourceName
    Set pXYEventSource = pXYName.Open
    
    ' Create a new Map Layer
    Dim pFlayer As IFeatureLayer
    Set pFlayer = New FeatureLayer
    Set pFlayer.FeatureClass = pXYEventSource
    pFlayer.Name = "Sample XY Event layer"
    
    'Add the layer extension (this is done so that when you edit
    'the layer's Source properties and click the Set Data Source
    'button, the Add XY Events Dialog appears)
    Dim pLayerExt As ILayerExtensions
    Dim pRESPageExt As New XYDataSourcePageExtension
    Set pLayerExt = pFlayer
    pLayerExt.AddExtension pRESPageExt
    
    pMap.AddLayer pFlayer
    
    Exit Sub
EH:
    
    MsgBox Err.Number & "  " & Err.Description
    
End Sub