Friday 28 April 2017

arcgis 10.1 - Getting feature class from File Geodatabase in ArcObjects using VB.NET?


How do you open up a File GDB and read out a simple feature class?


The snippet for retreiving a shapefile from a regular folder on disk works, and I suppose I can use the same code but with a different kind of workspace.


''' 
''' Get the FeatureClass from a Shapefile on disk (hard drive).
'''

''' A System.String that is the directory where the shapefile is located. Example: "C:\data\USA"
''' A System.String that is the shapefile name. Note: the shapefile extension's (.shp, .shx, .dbf, etc.) is not provided! Example: "States"

''' An IFeatureClass interface. Nothing (VB.NET) or null (C#) is returned if unsuccessful.
'''
Public Function GetFeatureClassFromShapefileOnDisk(ByVal string_ShapefileDirectory As System.String, ByVal string_ShapefileName As System.String) As ESRI.ArcGIS.Geodatabase.IFeatureClass

Dim directoryInfo_check As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(string_ShapefileDirectory)
If directoryInfo_check.Exists Then

'We have a valid directory, proceed

Dim fileInfo_check As System.IO.FileInfo = New System.IO.FileInfo(string_ShapefileDirectory + "\" + string_ShapefileName + ".shp")

If fileInfo_check.Exists Then

'We have a valid shapefile, proceed

Dim workspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory = New ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass
Dim workspace As ESRI.ArcGIS.Geodatabase.IWorkspace = workspaceFactory.OpenFromFile(string_ShapefileDirectory, 0)
Dim featureWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace = CType(workspace, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace) ' Explict Cast
Dim featureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass = featureWorkspace.OpenFeatureClass(string_ShapefileName)

Return featureClass

Else

'Not valid shapefile
Return Nothing
End If

Else

' Not valid directory
Return Nothing


End If

End Function

Answer



Assuming your project is setup correctly, with all references added and compiles without errors. Using Visual Studio Express 2013, ArcGIS 10.3 and targeting .Net framework 3.5


Also ensure you add ArcObjects Library References to:



  • DataSourcesGDB

  • GeoDatabase


  • Carto


Public Sub New()
On Error GoTo Trap

Dim sPathFGDB As String
Dim sFCName As String
Dim pFC As IFeatureClass
Dim pWorkSpace As IFeatureWorkspace
Dim pWorkspaceFactory As IWorkspaceFactory = New ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory


sPathFGDB = InputBox("Enter path to FGDB:", "Path to FGDB", "C:\Users\jakub.sisak\Documents\ArcGIS\Default.gdb") 'enter a valid path including the ".gdb" extension
pWorkSpace = pWorkspaceFactory.OpenFromFile(sPathFGDB, 0)
sFCName = InputBox("Enter Feature Class Name:", "Feature Class Name", "Closure_Plan_GA_Footprint_DE") 'enter a valid feature class name
pFC = pWorkSpace.OpenFeatureClass(sFCName)

Dim pLayer As IFeatureLayer
pLayer = New FeatureLayer
pLayer.FeatureClass = pFC
pLayer.Name = pFC.AliasName


Dim pMxDoc As ESRI.ArcGIS.ArcMapUI.IMxDocument = My.ArcMap.Document
pMxDoc.FocusMap.AddLayer(pLayer)
pMxDoc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, pLayer, Nothing)

Exit Sub
Trap:
MsgBox(Err.Number & ": " & Err.Description)

End Sub

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...