// 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); }
// Create 3 feature classes to hold the point, polyline, and polygon transactions private Dictionary <esriGeometryType, IFeatureClass> CreateFCS(string path) { // Connect to the output file GDB IWorkspaceFactory wsFact = new FileGDBWorkspaceFactory(); IWorkspace ws = wsFact.OpenFromFile(path, this.Handle.ToInt32()); IFeatureWorkspace featws = ws as IFeatureWorkspace; // Get the coded value domain for the transaction type, creating if necessary IDomain domain = GetOrCreateDomain(ws); Dictionary <esriGeometryType, IFeatureClass> dictionary = new Dictionary <esriGeometryType, IFeatureClass>(); foreach (var geomType in new esriGeometryType[] { esriGeometryType.esriGeometryPoint, esriGeometryType.esriGeometryPolyline, esriGeometryType.esriGeometryPolygon }) { // Create the feature classes string fcName = Enum.GetName(typeof(esriGeometryType), geomType) + "_TransactionsExportedAt" + DateTime.Now.ToString("yyyyMMdd_HHmm"); UID tableUID = new UIDClass(); tableUID.Value = "esriGeoDatabase.Feature"; // Get the fields for this geometry type IFields fields = GetFields(geomType, domain); // Validate the fields ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass(); ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null; ESRI.ArcGIS.Geodatabase.IFields validatedFields = null; fieldChecker.ValidateWorkspace = ws; fieldChecker.Validate(fields, out enumFieldError, out validatedFields); IFieldError err; while (enumFieldError != null && (err = enumFieldError.Next()) != null) { // If the field validation failed, display an error (since fields are mostly hardcoded, this shouldn't really happen) MessageBox.Show(Enum.GetName(typeof(esriFieldNameErrorType), err.FieldError), "Field Error"); } dictionary.Add(geomType, featws.CreateFeatureClass(fcName, validatedFields, tableUID, null, esriFeatureType.esriFTSimple, "SHAPE", "DEFAULT")); } return(dictionary); }
public IFeatureClass createFeatureClassInMemory(string strName, IFields FeatureFields, IWorkspace pWS, esriFeatureType featType) { ESRI.ArcGIS.esriSystem.UID CLSID = null; //ESRI.ArcGIS.esriSystem.UID CLSEXT = null; IFeatureWorkspace pFWS = null; ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = null; ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null; ESRI.ArcGIS.Geodatabase.IFields validatedFields = null; try { //CLSEXT = null; pFWS = (IFeatureWorkspace)pWS; if (CLSID == null) { CLSID = new ESRI.ArcGIS.esriSystem.UIDClass(); CLSID.Value = "esriGeoDatabase.Feature"; } fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass(); enumFieldError = null; validatedFields = null; fieldChecker.ValidateWorkspace = pWS; fieldChecker.Validate(FeatureFields, out enumFieldError, out validatedFields); bool FCCreated = false; IFeatureClass newFeat = null; int loopCnt = 0; while (FCCreated == false) { try { if (loopCnt == 0) { loopCnt = loopCnt + 1; newFeat = pFWS.CreateFeatureClass(strName, validatedFields, null, null, featType, "SHAPE", ""); } else { loopCnt = loopCnt + 1; newFeat = pFWS.CreateFeatureClass(strName + (loopCnt - 1).ToString(), validatedFields, null, null, featType, "SHAPE", ""); } FCCreated = true; } catch { FCCreated = false; } if (loopCnt == 100) { FCCreated = true; } } return(newFeat); } catch { return(null); } finally { CLSID = null; pFWS = null; fieldChecker = null; enumFieldError = null; validatedFields = null; } }
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); }