Esempio n. 1
0
        // ArcGIS Snippet Title:
        // Create Table
        //
        // Long Description:
        // Creates a dataset in a workspace.
        //
        // Add the following references to the project:
        // ESRI.ArcGIS.Geodatabase
        // ESRI.ArcGIS.Geometry
        // ESRI.ArcGIS.System
        //
        // Intended ArcGIS Products for this snippet:
        // ArcGIS Desktop (ArcEditor, ArcInfo, ArcView)
        // ArcGIS Engine
        // ArcGIS Server
        //
        // Applicable ArcGIS Product Versions:
        // 9.2
        // 9.3
        // 9.3.1
        // 10.0
        //
        // Required ArcGIS Extensions:
        // (NONE)
        //
        // Notes:
        // This snippet is intended to be inserted at the base level of a Class.
        // It is not intended to be nested within an existing Uitvoeren.
        //

        ///<summary>Creates a table with some default fields.</summary>
        ///
        ///<param name="workspace">An IWorkspace2 interface</param>
        ///<param name="tableName">A System.String of the table name in the workspace. Example: "owners"</param>
        ///<param name="fields">An IFields interface or Nothing</param>
        ///
        ///<returns>An ITable interface or Nothing</returns>
        ///
        ///<remarks>
        ///Notes:
        ///(1) If an IFields interface is supplied for the 'fields' collection it will be used to create the
        ///    table. If a Nothing value is supplied for the 'fields' collection, a table will be created using
        ///    default values in the method.
        ///(2) If a table with the supplied 'tableName' exists in the workspace an ITable will be returned.
        ///    if table does not exit a new one will be created.
        ///</remarks>
        public ESRI.ArcGIS.Geodatabase.ITable CreateOrOpenTableLog(ESRI.ArcGIS.Geodatabase.IWorkspace2 workspace, System.String tableName, ESRI.ArcGIS.Geodatabase.IFields fields)
        {
            // create the behavior clasid for the featureclass
            ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass();

            if (workspace == null)
            {
                return(null);                                                                                                  // valid feature workspace not passed in as an argument to the method
            }
            ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explicit Cast
            ESRI.ArcGIS.Geodatabase.ITable            table;

            if (workspace.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTTable, tableName))
            {
                // table with that name already exists return that table
                table = featureWorkspace.OpenTable(tableName);
                return(table);
            }

            uid.Value = "esriGeoDatabase.Object";

            ESRI.ArcGIS.Geodatabase.IObjectClassDescription objectClassDescription = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass();

            // if a fields collection is not passed in then supply our own
            if (fields == null)
            {
                // create the fields using the required fields method
                fields = objectClassDescription.RequiredFields;
                ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; // Explicit Cast

                fieldsEdit.AddField(this.CreateFieldInt("ID"));
                fieldsEdit.AddField(this.CreateFieldInt("index"));
                fieldsEdit.AddField(this.CreateFieldDouble("X_From", 8, 2));
                fieldsEdit.AddField(this.CreateFieldDouble("Y_From", 8, 2));
                fieldsEdit.AddField(this.CreateFieldDouble("M_From", 8, 2));
                fieldsEdit.AddField(this.CreateFieldDouble("X_To", 8, 2));
                fieldsEdit.AddField(this.CreateFieldDouble("Y_To", 8, 2));
                fieldsEdit.AddField(this.CreateFieldDouble("M_To", 8, 2));


                // add field to field collection
                fields = (ESRI.ArcGIS.Geodatabase.IFields)fieldsEdit; // Explicit Cast
            }

            // Use IFieldChecker to create a validated fields collection.
            ESRI.ArcGIS.Geodatabase.IFieldChecker   fieldChecker    = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass();
            ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError  = null;
            ESRI.ArcGIS.Geodatabase.IFields         validatedFields = null;
            fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)workspace;
            fieldChecker.Validate(fields, out enumFieldError, out validatedFields);

            // The enumFieldError enumerator can be inspected at this point to determine
            // which fields were modified during validation.


            // create and return the table
            table = featureWorkspace.CreateTable(tableName, validatedFields, uid, null, "");

            return(table);
        }
Esempio n. 2
0
        /// <summary>
        /// CreateFeatureClass
        /// </summary>
        /// <param name="FeatureWorkspace"></param>
        /// <param name="LayerName"></param>
        /// <param name="featureType"></param>
        /// <param name="GeometryType"></param>
        /// <returns></returns>
        public IFeatureClass CreateFeatureClass(IFeatureWorkspace FeatureWorkspace, string LayerName, esriFeatureType featureType, esriGeometryType GeometryType)
        {
            //----------------
            ESRI.ArcGIS.Geodatabase.FeatureClassDescriptionClass objectClassDescription = new ESRI.ArcGIS.Geodatabase.FeatureClassDescriptionClass();

            // create the fields using the required fields method
            ESRI.ArcGIS.Geodatabase.IFields     fields     = objectClassDescription.RequiredFields;
            ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; // Explicit Cast
            IField fd              = null;
            bool   IsOIDField      = false;
            bool   IsGeometryField = false;

            for (int i = 0; i < fields.FieldCount; i++)
            {
                fd = fields.get_Field(i);
                if (fd.Type == esriFieldType.esriFieldTypeOID)
                {
                    (fd as IFieldEdit).Name_2 = this.OIDFieldName;
                    IsOIDField = true;
                }
                if (fd.Type == esriFieldType.esriFieldTypeGeometry)
                {
                    (fd as IFieldEdit).Name_2 = this.GeometryFieldName;
                    (fd.GeometryDef as IGeometryDefEdit).GeometryType_2 = GeometryType;
                    IsGeometryField = true;
                }
            }
            //----------------

            if (IsOIDField == false)
            {
                //创建OBJECTID字段
                IField     field3 = new FieldClass();
                IFieldEdit edit2  = field3 as IFieldEdit;
                edit2.Name_2      = this.OIDFieldName;
                edit2.AliasName_2 = this.OIDFieldName;
                edit2.Type_2      = esriFieldType.esriFieldTypeOID;
                fieldsEdit.AddField(field3);
            }
            string ShapeFiledName = this.GeometryFieldName;

            if (IsGeometryField == false)
            {
                //创建几何字段
                IGeometryDef     def   = new GeometryDefClass();
                IGeometryDefEdit edit4 = def as IGeometryDefEdit;
                edit4.GeometryType_2 = this.GeometryType;
                edit4.GridCount_2    = 1;
                edit4.set_GridSize(0, 1000);
                edit4.AvgNumPoints_2     = 2;
                edit4.HasM_2             = false;
                edit4.HasZ_2             = false;
                edit4.SpatialReference_2 = this.ShapeSpatialReference;
                //
                IField     field4 = new FieldClass();
                IFieldEdit edit3  = field4 as IFieldEdit;
                edit3.Name_2        = this.GeometryFieldName;
                edit3.AliasName_2   = this.GeometryFieldName;
                edit3.Type_2        = esriFieldType.esriFieldTypeGeometry;
                edit3.GeometryDef_2 = def;
                fieldsEdit.AddField(field4);
                //
                ShapeFiledName = field4.Name;
            }

            UID uid  = null;
            UID uid2 = null;

            switch (featureType)
            {
            case esriFeatureType.esriFTSimple:       //FeatureClass
                uid  = objectClassDescription.InstanceCLSID;
                uid2 = objectClassDescription.ClassExtensionCLSID;
                break;

            default:
                break;
            }
            //创建要素对象
            IFeatureClass fc = FeatureWorkspace.CreateFeatureClass(LayerName, fields, uid, uid2, featureType, ShapeFiledName, null);

            //
            return(fc);
        }
Esempio n. 3
0
        public static IFeatureClass CreateFeatureClassInPGDB(IWorkspace2 workspace, IFeatureDataset featureDataset, string featureClassName, IFields fields, UID CLSID, UID CLSEXT, string strConfigKeyword, esriGeometryType esriGeometryType)
        {
            if (featureClassName == "")
            {
                return(null);                        // name was not passed in
            }
            ESRI.ArcGIS.Geodatabase.IFeatureClass     featureClass;
            ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explicit Cast
            if (workspace.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass, featureClassName))        //feature class with that name already exists
            {
                featureClass = featureWorkspace.OpenFeatureClass(featureClassName);
                return(featureClass);
            }


            // assign the class id value if not assigned

            if (CLSID == null)
            {
                CLSID       = new ESRI.ArcGIS.esriSystem.UIDClass();
                CLSID.Value = "esriGeoDatabase.Feature";
            }

            ESRI.ArcGIS.Geodatabase.IObjectClassDescription objectClassDescription = new ESRI.ArcGIS.Geodatabase.FeatureClassDescriptionClass();


            // if a fields collection is not passed in then supply our own

            if (fields == null)
            {
                // create the fields using the required fields method

                fields = objectClassDescription.RequiredFields;

                ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; // Explicit Cast
                ESRI.ArcGIS.Geodatabase.IField      field      = new ESRI.ArcGIS.Geodatabase.FieldClass();

                // create a user defined text field
                ESRI.ArcGIS.Geodatabase.IFieldEdit fieldEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit)field; // Explicit Cast

                // setup field properties
                fieldEdit.Name_2         = "SampleField";
                fieldEdit.Type_2         = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString;
                fieldEdit.IsNullable_2   = true;
                fieldEdit.AliasName_2    = "Sample Field Column";
                fieldEdit.DefaultValue_2 = "test";
                fieldEdit.Editable_2     = true;
                fieldEdit.Length_2       = 100;

                // add field to field collection
                fieldsEdit.AddField(field);
                fields = (ESRI.ArcGIS.Geodatabase.IFields)fieldsEdit; // Explicit Cast
            }

            System.String strShapeField = "";

            // locate the shape field
            for (int j = 0; j < fields.FieldCount; j++)
            {
                if (fields.get_Field(j).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    strShapeField = fields.get_Field(j).Name;
                    ((IGeometryDefEdit)fields.get_Field(j).GeometryDef).GeometryType_2 = esriGeometryType;
                }
            }

            // Use IFieldChecker to create a validated fields collection.
            ESRI.ArcGIS.Geodatabase.IFieldChecker   fieldChecker    = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass();
            ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError  = null;
            ESRI.ArcGIS.Geodatabase.IFields         validatedFields = null;
            fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)workspace;
            fieldChecker.Validate(fields, out enumFieldError, out validatedFields);
            // The enumFieldError enumerator can be inspected at this point to determine
            // which fields were modified during validation.

            // finally create and return the feature class

            if (featureDataset == null)// if no feature dataset passed in, create at the workspace level
            {
                featureClass = featureWorkspace.CreateFeatureClass(featureClassName, validatedFields, CLSID, CLSEXT, ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword);
            }
            else
            {
                featureClass = featureDataset.CreateFeatureClass(featureClassName, validatedFields, CLSID, CLSEXT, ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword);
            }
            return(featureClass);
        }