Create Feature and store to different FGDB

869
6
08-15-2013 09:06 AM
ConorMoloney
New Contributor III
I'm creating a feature and I want to store it to a specific geodatabase. At the moment I'm using feature.Store() but it's not storing it to where I want. How do I specify a location?

I want to store it to C:\College Stuff\Dissertation\2009 stuff\Geo\Geo\Full_3D.gdb

   Public Sub CreateFeature(ByVal layername1 As String, ByVal x As Double, ByVal y As Double, ByVal FeatureClass1 As IFeatureClass)



        Dim cPoint As IPoint = New ESRI.ArcGIS.Geometry.Point
        ' To individually assign a value to the coordinates, use cPoint.X = x and cPoint.Y = y.
        cPoint.PutCoords(x, y)


        ' Ensure the feature class contains points.
        If FeatureClass1.ShapeType <> esriGeometryType.esriGeometryPoint Then
            Return
        End If

        ' Build the feature.
        Dim feature As IFeature = FeatureClass1.CreateFeature()
        feature.Shape = cPoint

        ' Commit the new feature to the geodatabase.
        feature.Store()

    End Sub
0 Kudos
6 Replies
JohnHauck
Occasional Contributor II
IFeature::Store will store the feature in whatever feature class you used to call IFeatureClass::CreateFeature. If you are looking to store in a particular database you will need to open the feature class from that database.
0 Kudos
ConorMoloney
New Contributor III
Apologies for my ignorance, I'm new to this. How do I know what feature class I used to call IFeatureClass::CreatFeature? And how do I open the feature class from the database I want?

This is where I call CreateFeature, about half way down.

Public Sub go()
        initlicence()

        'MonitorThread = New Thread(AddressOf Monitor)
        'MonitorThread.Priority = ThreadPriority.BelowNormal
        'MonitorThread.Start()

        ' convertjunctionfile(0.002910881)

        Dim featureclass1 As IFeatureClass = GetFeatureClass("StopsInput")
        Dim featureclassCensus As IFeatureClass = GetFeatureClass("census_enumerator_areas")
        'Dim featureclassPOW As IFeatureClass = GetFeatureClass("pow_area_point")
        Dim featureclassED As IFeatureClass = GetFeatureClass("electoral_divisions")

        Dim EDindex As Integer = -1
        Dim censusindex As Integer = -1
        Dim eastingsindex As Integer = -1
        Dim northingsindex As Integer = -1
        For j = 0 To header.Length - 1

            Select Case header(j)
                Case "Residence_ED"
                    EDindex = j
                Case "Residence_2006_Enumeration_Area"
                    censusindex = j
                Case "Fuzz_East_250"
                    eastingsindex = j
                Case "Fuzz_North_250"
                    northingsindex = j

            End Select
        Next

        'check headers have been read in corrrectly
        If (EDindex = -1 Or censusindex = -1 Or eastingsindex = -1 Or northingsindex = -1) Then
            MsgBox("Error reading in input file header")
        End If

        'MsgBox("total header length: " + header.Length.ToString)
        'MsgBox("EDindex" + EDindex.ToString)
        ' MsgBox("censusindex" + censusindex.ToString)
        ' MsgBox("eastingsindex" + eastingsindex.ToString)
        MsgBox("northingsindex" + northingsindex.ToString)

        Dim route1length As Integer

        Dim startOID As Integer = 0
        Dim inputVals() As String
        Dim startpoint As ESRI.ArcGIS.Geometry.Point
        Dim endpoint As ESRI.ArcGIS.Geometry.Point = New ESRI.ArcGIS.Geometry.Point
        Dim route1() As Integer
        Dim routeClass As RouteClass = New RouteClass
        Dim crowflies As Double = Nothing
        routeClass.Init()

        Dim OutputDatabase1 As OutputDatabase = New OutputDatabase
        Dim numPrevProcessed As Integer = 0

        If continue_from_prev_from_form = True Then
            OutputDatabase1.continueFromOld()
            numPrevProcessed = OutputDatabase1.getNumberProcessed()
            'MsgBox("num prev processed: " + numPrevProcessed.ToString)
            'This loop cycles past any previously processed details
            For m As Integer = 1 To numPrevProcessed
                TextFromFile1.Read()
            Next
        Else 'Initialise database using header information

            OutputDatabase1.init(header)
            MsgBox("OutputGeodatabase")
        End If

        'initialise variables

        For Me.person_number = (numPrevProcessed + 1) To (number_to_process_fromform + numPrevProcessed + 1)


            'updates global person number, for error reporting purposes
            person_no_global = person_number

            'Clears any previous route stops from the screen
            DeleteRows(featureclass1)

            inputVals = TextFromFile1.Read()
            MsgBox(inputVals.ToString)

            'This if statement exits the loop if the reader reaches the end of the file
            If inputVals Is Nothing Then
                TextFromFile1.errorwriter("End of input file. All entries processed", person_number)
                Exit For

            End If

            'Conditions to check that input file info is all present and intact
            If (IsNumeric(inputVals(censusindex)) Or IsNumeric(inputVals(EDindex))) And IsNumeric(inputVals(eastingsindex)) And IsNumeric(inputVals(northingsindex)) Then
                MsgBox("1")

                If (Not IsNumeric(inputVals(censusindex))) Or inputVals(censusindex) = "*" Then
                    startpoint = FindCentroid(featureclassED, inputVals(EDindex), False)
                    MsgBox("2")
                Else
                    startpoint = FindCentroid(featureclassCensus, inputVals(censusindex), True)
                    MsgBox("startpoint: " + startpoint.ToString)
                    MsgBox("3")
                    'If point can't be found from census area then use 
                    If startpoint Is Nothing Then
                        startpoint = FindCentroid(featureclassED, inputVals(EDindex), False)
                        MsgBox("4")
                    End If

                End If

                'initialise startOID and route
                startOID = Nothing
                route1 = Nothing
                route1length = 0
                crowflies = Nothing
                Command1.resultDistance = 0

                'condition in case startpoint can't be found
                If Not startpoint Is Nothing Then

                    MsgBox("11")
                    MsgBox("person: " + person_no_global.ToString)
                    'Dim endpoint As ESRI.ArcGIS.Geometry.Point = FindPOW(featureclassPOW, 400)
                    endpoint.X = inputVals(eastingsindex) + (Rnd(person_number + 0.2) - 0.5) * 2 * 125
                    endpoint.Y = inputVals(northingsindex) + (Rnd(person_number + 0.3) - 0.5) * 2 * 125

                   CreateFeature("StopsInput", startpoint.X, startpoint.Y, featureclass1)

                    CreateFeature("StopsInput", endpoint.X, endpoint.Y, featureclass1)
                    'selectall()

                    'Call routing functions
                    MsgBox("6 " + startpoint.X.ToString)
                    'route1 = Router()
                    route1 = routeClass.SolveRoute()
                    MsgBox("7")
                    If Not route1 Is Nothing Then

                        MsgBox("Route Nothing")
                        startOID = OutputDatabase1.WriteToRouteTableRowField(person_number, route1)
                        route1length = route1.Length
                    Else
                        route1length = 0
                        MsgBox("route length")
                    End If

                    crowflies = crowFliesDistance(startpoint, endpoint)
                    MsgBox("8")

                End If

                OutputDatabase1.WriteToMainTableRowField(person_number, startOID, inputVals, route1length, crowflies, resultDistance)
                MsgBox("9")

            Else
                OutputDatabase1.WriteToMainTableRowField(person_number, Nothing, inputVals, 0, 0, 0)
                MsgBox("10")

                TextFromFile1.errorwriter("Missing info (ED/census or POW co-ords for routing. Person no", person_number)

            End If


            'updates progress bar every 10 people
            'If person_number Mod 10 = 0 Then

            'progressNum = CType(System.Math.Abs(((person_number - numPrevProcessed) * 100) / number_to_process_fromform), Integer)
            'If progressNum <= 100 Then
            'UpdateUI(progressNum)
            'End If

            If person_number Mod 100 = 0 Then
                'OPTIONAL GARBAGE COLLECTOR CALL
                'Used to force system to clean out unused areas of ram every 100 cycles
                System.GC.Collect()
            End If
            'End If
            'progressbox1.ProgressBar1.Value = 49 + person_number * 50
            'progressbox1.Refresh()

            'routeprogress.Invoke(New UpdateUIDelegate() ), New Object() {50 + person_number * 50})
            MsgBox(person_number)
        Next person_number

        'routeClass.SaveLayerToDisk(routeClass.naLayerStore, System.Environment.CurrentDirectory + "\RouteGEN.lyr")
        'routeprogress
        System.GC.Collect()

        'errorwriter("Finished successfully", person_number)
        'Adds Route layer to display
        'Dim mxDocument As IMxDocument = GetMxDocumentFromArcMap(m_application)
        'Dim activeView As IActiveView = mxDocument.ActiveView
        'AddLayerFromFile(activeView, routeClass.SHAPE_WORKSPACE + "RouteGEN.lyr")

        'ESRI License Initializer generated code.
        'Do not make any call to ArcObjects after ShutDownApplication()
        OutputDatabase1 = Nothing
        Try

            'progressbox1.Close()
            MonitorThread.Abort()
            'progressbox1 = Nothing
        Catch ex As Exception

        End Try
        m_AOLicenseInitializer.ShutdownApplication()

        MsgBox("Routing done: " + (person_number - 1).ToString + " routes calculated in total. Finished at: " + TimeOfDay.ToLongTimeString())



    End Sub
0 Kudos
ConorMoloney
New Contributor III
Could anyone offer any more help on this? Do I use a function to connect to the GDB like this one

 Public Shared Function FileGdbWorkspaceFromPath(ByVal FGDB_WORKSPACE As String) As IWorkspace
        Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
        Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)
        Return workspaceFactory.OpenFromFile(FGDB_WORKSPACE, 0)
    End Function


If so, how would I go about calling that to create the feature there?
0 Kudos
LeoDonahue
Occasional Contributor III
0 Kudos
JohnHauck
Occasional Contributor II
You are getting the instance of the feature class from your "GetFeatureClass" method in your sample code.

With your "FileGdbWorkspaceFromPath" example you are just connecting to the workspace...from the workspace you would then open the feature class...looks like you are using one named "StopsInput" in your initial example. So you would just cast the workspace you get back from "FileGdbWorkspaceFromPath" to IFeatureWorkspace and call IFeatureWorkspace::OpenFeatureClass passing in the name. Then you would call IFeatureClass::CreateFeature...set whatever values and then call IFeature::Store. This will commit the change to this particular feature class from the workspace it was opened from.
0 Kudos
ConorMoloney
New Contributor III
Thanks for the help. I am only a beginner modifying someone's code so I don't seem to able to do it. My GetFeatureClass method gets a named feature class from a layer in the active map document. Originally the code gets a shapefile that has been added to the map but what I want it to do is get a featureclass that has been added to  the map.

Public Function GetFeatureClass(ByVal layername1 As String) As IFeatureClass
        Dim mxDocument As IMxDocument
        mxDocument = m_application.Document
        Dim map As IMap = mxDocument.FocusMap
        Dim check As Boolean = False

        Dim layerCounter As Integer
        Dim FeatureClass1 As IFeatureClass = Nothing
        Dim featureLayer As IFeatureLayer
        Dim currentLayer As ILayer
        For layerCounter = 0 To map.LayerCount - 1 Step layerCounter + 1
            currentLayer = map.Layer(layerCounter)

            If (TypeOf currentLayer Is IFeatureLayer) And (currentLayer.Name = layername1) Then


                featureLayer = currentLayer

                FeatureClass1 = featureLayer.FeatureClass

                check = True

                layerCounter = map.LayerCount
            End If
        Next

        If check = False Then
            MsgBox("cannot find InputStops layer")
            Return Nothing
        End If

        Return FeatureClass1
    End Function


I've had to start writing my thesis so haven't had as much time as I hoped to modify the code
0 Kudos