Simple code to create a feature class and a feature

771
3
Jump to solution
10-27-2023 12:34 AM
mody_buchbinder
Occasional Contributor III

Does anybody have a sample in C# that create a GDB, create a feature class in it and add a point to this feature class.

Without using the XML as the example do?

Thanks

0 Kudos
1 Solution

Accepted Solutions
lmatteo
New Contributor III

What about this ?

using System;
using System.IO;
using Esri.FileGDB;

namespace CreateFileGDB
{
class Program
{
static void Main(string[] args)
{
// Define the path to your file geodatabase
string gdbPath = @"C:\Path\To\Your\Geodatabase.gdb";

try
{
// Create a new File Geodatabase
using (Geodatabase geodatabase = Geodatabase.Open(gdbPath, GeodatabaseOpenMode.Create))
{
// Create a point feature class
using (FeatureClassDescription fcDescription = new FeatureClassDescription())
{
Field[] fields = fcDescription.RequiredFields;
GeometryDef geometryDef = fcDescription.DefaultGeometryDef;
using (FeatureClass featureClass = geodatabase.CreateFeatureClass("MyPointFC", fields, geometryDef))
{
// Create a point feature and add it to the feature class
using (RowBuffer rowBuffer = featureClass.CreateRowBuffer())
{
using (PointShapeBuffer shapeBuffer = new PointShapeBuffer(geometryDef))
{
shapeBuffer.SetPoint(0, 100.0, 50.0);
rowBuffer.SetGeometry(shapeBuffer);

// Set attribute values
rowBuffer["Name"] = "FirstPoint";

using (Row row = featureClass.CreateRow(rowBuffer))
{
Console.WriteLine("Point feature with attributes created.");
}
}
}
}
}
}
}
catch (GdbError e)
{
Console.WriteLine("Error: " + e.Message);
}
}
}
}

View solution in original post

0 Kudos
3 Replies
lmatteo
New Contributor III

Hello,
Make sure to replace "C:\Path\To\Your\Geodatabase.gdb" with the path where you want to create your file geodatabase. This script creates a file geodatabase, a point feature class, and adds a point feature with attributes to it. I'm not using C, but I guess it should work. Let me know 😉

 

using System;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesFile;

namespace CreateGeodatabase
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Initialize the license
            IAoInitialize aoInit = new AoInitialize();
            esriLicenseStatus licStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);

            if (licStatus != esriLicenseStatus.esriLicenseCheckedOut)
            {
                Console.WriteLine("License initialization failed, exiting.");
                return;
            }

            try
            {
                // Create a file geodatabase
                IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
                IWorkspaceName workspaceName = workspaceFactory.Create(@"C:\Path\To\Your\Geodatabase.gdb", "MyGDB", null, 0);
                IName name = (IName)workspaceName;
                IWorkspace workspace = (IWorkspace)name.Open();

                // Create a point feature class
                IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
                IFields fields = new FieldsClass();
                IFieldsEdit fieldsEdit = (IFieldsEdit)fields;

                IField field = new FieldClass();
                IFieldEdit fieldEdit = (IFieldEdit)field;
                fieldEdit.Name_2 = "Name";
                fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                fieldsEdit.AddField(field);

                IGeometryDef geometryDef = new GeometryDefClass();
                IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
                geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
                fieldsEdit.GeometryDef_2 = geometryDef;

                featureWorkspace.CreateFeatureClass("MyPointFC", fields, null, null, esriFeatureType.esriFTSimple, "Shape", "");

                // Create a point feature and add it to the feature class
                IFeatureClass pointFeatureClass = featureWorkspace.OpenFeatureClass("MyPointFC");
                IFeatureBuffer featureBuffer = pointFeatureClass.CreateFeatureBuffer();
                IFeatureCursor featureCursor = pointFeatureClass.Insert(true);

                // Create a point geometry and set it on the feature
                IPoint point = new PointClass();
                point.PutCoords(100.0, 50.0);
                featureBuffer.Shape = point;

                // Set attribute values
                int nameFieldIndex = featureBuffer.Fields.FindField("Name");
                featureBuffer.set_Value(nameFieldIndex, "FirstPoint");

                featureCursor.InsertFeature(featureBuffer);
                featureCursor.Flush();

                Console.WriteLine("Point feature with attributes created.");

                Marshal.ReleaseComObject(workspace);
                Marshal.ReleaseComObject(workspaceFactory);
                Marshal.ReleaseComObject(aoInit);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }
    }
}​

 


 

0 Kudos
mody_buchbinder
Occasional Contributor III

Thanks but I look for code with geodatabase API that does not need a license

0 Kudos
lmatteo
New Contributor III

What about this ?

using System;
using System.IO;
using Esri.FileGDB;

namespace CreateFileGDB
{
class Program
{
static void Main(string[] args)
{
// Define the path to your file geodatabase
string gdbPath = @"C:\Path\To\Your\Geodatabase.gdb";

try
{
// Create a new File Geodatabase
using (Geodatabase geodatabase = Geodatabase.Open(gdbPath, GeodatabaseOpenMode.Create))
{
// Create a point feature class
using (FeatureClassDescription fcDescription = new FeatureClassDescription())
{
Field[] fields = fcDescription.RequiredFields;
GeometryDef geometryDef = fcDescription.DefaultGeometryDef;
using (FeatureClass featureClass = geodatabase.CreateFeatureClass("MyPointFC", fields, geometryDef))
{
// Create a point feature and add it to the feature class
using (RowBuffer rowBuffer = featureClass.CreateRowBuffer())
{
using (PointShapeBuffer shapeBuffer = new PointShapeBuffer(geometryDef))
{
shapeBuffer.SetPoint(0, 100.0, 50.0);
rowBuffer.SetGeometry(shapeBuffer);

// Set attribute values
rowBuffer["Name"] = "FirstPoint";

using (Row row = featureClass.CreateRow(rowBuffer))
{
Console.WriteLine("Point feature with attributes created.");
}
}
}
}
}
}
}
catch (GdbError e)
{
Console.WriteLine("Error: " + e.Message);
}
}
}
}
0 Kudos