Select to view content in your preferred language

.dbf to XY Event to shapefile

767
1
06-09-2010 09:16 AM
RuchiraWelikala
Regular Contributor
Hi All,
I'm trying to run this code and seems to be getting an error.  I think the problem is with the "esriDataSourcesOleDB.OLEDBWorkspaceFactory".  Can someone confirm this?  What should I use to import the data as an XY Event?  

Thanks,

Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.DataSourcesFile
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geometry

Imports System.IO
Imports ESRI.ArcGIS.Carto


Module mod_AddShapefile

    Public Sub AddShapeFile(ByVal MxDoc As IMxDocument, ByVal m_App As IApplication, _
                            ByVal TableName As String, ByVal FilePath As String)
        Try
            ' Part 1:  Define the workspace for the updated dbf stored locally.
            Dim pWorkspaceName As IWorkspaceName = New WorkspaceName
            Dim pTableName As ITableName = New TableName
            Dim pDatasetName As IDatasetName = pTableName

            ' Define the input tables's workspace
            pWorkspaceName.PathName = FilePath
            pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesOleDB.OLEDBWorkspaceFactory"

            pDatasetName.Name = TableName & ".dbf"
            pDatasetName.WorkspaceName = pWorkspaceName

            ' Part 2:  Define the XY Events source Name object
            Dim pXYEvent2FieldsProperties As IXYEvent2FieldsProperties = New XYEvent2FieldsProperties
            Dim pSpatialReferenceFactory As ISpatialReferenceFactory = New SpatialReferenceEnvironment
            Dim pGCS As IGeographicCoordinateSystem
            Dim pXYEventSourceName As IXYEventSourceName = New XYEventSourceName

            ' Set the event to fields properties.
            With pXYEvent2FieldsProperties
                .XFieldName = "SUR_LONG83"
                .YFieldName = "SUR_LAT83"
                .ZFieldName = ""
            End With
            ' Set the spatial reference
            pGCS = pSpatialReferenceFactory.CreateGeographicCoordinateSystem(esriSRGeoCSType.esriSRGeoCS_NAD1983)

            ' Specify the event source and its properties
            With pXYEventSourceName
                .EventProperties = pXYEvent2FieldsProperties
                .SpatialReference = pGCS
                .EventTableName = pTableName
            End With

            ' Part 3:  Create a layer from the event source
            Dim pXYEventSource As IXYEventSource
            Dim pFLayer As IFeatureLayer = New FeatureLayer
            Dim pName As IName = pXYEventSourceName
            pXYEventSource = pName.Open
            pFLayer.FeatureClass = pXYEventSource
            pFLayer.Name = "Events"

            ' Set the output shapefile
            Dim pShapeName As IDatasetName = New FeatureClassName
            Dim pShapeWSFactory As IWorkspaceFactory = New ShapefileWorkspaceFactory
            Dim pShapeWS As IDataset = pShapeWSFactory.OpenFromFile(FilePath, m_App.hWnd)
            Dim pShapeWSName As IWorkspaceName = pShapeWS.FullName

            pShapeName.Name = TableName & "_shape"
            pShapeName.WorkspaceName = pShapeWSName

            Dim Fshp As New FileInfo(FilePath & "\" & TableName & "_shape.shp")
            Dim Fdbf As New FileInfo(FilePath & "\" & TableName & "_shape.dbf")
            Dim Fprj As New FileInfo(FilePath & "\" & TableName & "_shape.prj")
            Dim Fsbn As New FileInfo(FilePath & "\" & TableName & "_shape.sbn")
            Dim Fsbx As New FileInfo(FilePath & "\" & TableName & "_shape.sbx")
            Dim Fshx As New FileInfo(FilePath & "\" & TableName & "_shape.shx")
            If Fshp.Exists = True Then
                Fshp.Delete()
                Fdbf.Delete()
                Fprj.Delete()
                Fsbn.Delete()
                Fsbx.Delete()
                Fshx.Delete()
            End If

            ' Convert the data to shapefile
            Dim pFDConverter As IFeatureDataConverter = New FeatureDataConverter
            pFDConverter.ConvertFeatureClass(pName, Nothing, Nothing, pShapeName, Nothing, Nothing, "", 1000, m_App.hWnd)

            If MsgBox("Shapefile created.  Add to current map?", MsgBoxStyle.YesNo, "PetroGIS") = MsgBoxResult.Yes Then
                Dim pFeatureWorkspace As IFeatureWorkspace = pShapeWS
                Dim pNLayer As IFeatureLayer = New FeatureLayer
                pNLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pShapeName.Name)
                pNLayer.Name = pShapeName.Name & "_upt"
                ' Add the feature layer to the focus map
                MxDoc.AddLayer(pNLayer)
                MxDoc.ActiveView.Refresh()
            Else
                Exit Sub
            End If
        Catch Ex As Exception
            MsgBox(Ex.Message, MsgBoxStyle.Critical, "PetroGIS")
        End Try
    End Sub

End Module
0 Kudos
1 Reply
RuchiraWelikala
Regular Contributor
Figured it out! 
instead of using "esriDataSourcesOleDB.OLEDBWorkspaceFactory"
, you have to use "esriDataSourcesFile.ShapefileWorkspaceFactory".
0 Kudos