/// <summary> /// Create the feature workspace /// </summary> /// <param name="workspaceNameString">Workspace name</param> /// <returns></returns> public static IFeatureWorkspace CreateFeatureWorkspace(string workspaceNameString) { IScratchWorkspaceFactory2 ipScWsFactory = new FileGDBScratchWorkspaceFactoryClass(); IWorkspace ipScWorkspace = ipScWsFactory.CurrentScratchWorkspace; if (null == ipScWorkspace) { ipScWorkspace = ipScWsFactory.CreateNewScratchWorkspace(); } IFeatureWorkspace featWork = (IFeatureWorkspace)ipScWorkspace; return(featWork); }
public List <string> CheckDataSchema(string template, string dbpath) { List <string> msgs = new List <string>(); try { Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance (factoryType); IFeatureWorkspace source_fws = workspaceFactory.OpenFromFile(dbpath, 0) as IFeatureWorkspace; IScratchWorkspaceFactory target_wsf = new FileGDBScratchWorkspaceFactoryClass(); IWorkspace target_ws = target_wsf.CreateNewScratchWorkspace(); IGdbXmlImport importer = new GdbImporterClass(); IEnumNameMapping mapping = null; bool isConflict = false; isConflict = importer.GenerateNameMapping(template, target_ws, out mapping); importer.ImportWorkspace(template, mapping, target_ws, true); IDataset ds = target_ws as IDataset; List <String> fcNames = new List <string>(); Util.GetAllFeatureClassNames(ds, ref fcNames); IFeatureWorkspace target_fws = target_ws as IFeatureWorkspace; IWorkspace2 source_ws = source_fws as IWorkspace2; foreach (string fcName in fcNames) { if (!source_ws.get_NameExists(esriDatasetType.esriDTFeatureClass, fcName)) { msgs.Add(string.Format("数据源中丢失图层:{0}", fcName)); continue; } IFeatureClass source_fc = source_fws.OpenFeatureClass(fcName); IFeatureClass target_fc = target_fws.OpenFeatureClass(fcName); for (int i = 0; i < target_fc.Fields.FieldCount; i++) { IField fld = target_fc.Fields.get_Field(i); if (fcName == target_fc.OIDFieldName) { continue; } int source_fld_idx = source_fc.FindField(fld.Name); if (source_fld_idx == -1) { msgs.Add(string.Format("数据源中丢失字段{0}", fld.Name)); } else { IField source_fld = source_fc.Fields.get_Field(source_fld_idx); if (source_fld.Type != fld.Type) { msgs.Add(string.Format("数据源中字段{0}类型不正确", fld.Name)); } if (source_fld.Length != fld.Length) { msgs.Add(string.Format("数据源中字段{0}长度不正确", fld.Name)); } if (source_fld.Precision != fld.Precision) { msgs.Add(string.Format("数据源中字段{0}精度不正确", fld.Name)); } } } if (source_fc.FindField("SyncID") == -1) { msgs.Add(@"数据源中丢失系统字段SyncID"); } if (source_fc.FindField("SyncTimeStamp") == -1) { msgs.Add(@"数据源中丢失系统字段SyncTimeStamp"); } if (source_fc.FindField("SyncStatus") == -1) { msgs.Add(@"数据源中丢失系统字段SyncStatus"); } if (source_fc.FindField("SyncEditable") == -1) { msgs.Add(@"数据源中丢失系统字段SyncEditable"); } } } catch { } return(msgs); }
public void MakeScratchPoints() { try { IMxDocument pmxdoc = ArcMap.Document as IMxDocument; IMap pmap = pmxdoc.FocusMap; ILayer pShorePoints = FindLayer(pmap, "ShorePoints"); if (pShorePoints != null) { pmap.DeleteLayer(pShorePoints); } IScratchWorkspaceFactory workspaceFactory = new FileGDBScratchWorkspaceFactoryClass(); IScratchWorkspaceFactory2 workspaceFactory2 = workspaceFactory as IScratchWorkspaceFactory2; IWorkspace scratchWorkspace = workspaceFactory2.CreateNewScratchWorkspace(); IFeatureWorkspace pFeatWorkspace = scratchWorkspace as IFeatureWorkspace; // This function creates a new feature class in a supplied feature dataset by building all of the // fields from scratch. IFeatureClassDescription (or IObjectClassDescription if the table is // created at the workspace level) can be used to get the required fields and are used to // get the InstanceClassID and ExtensionClassID. // Create new fields collection with the number of fields you plan to add. Must add at least two fields // for a feature class: Object ID and Shape field. IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; fieldsEdit.FieldCount_2 = 3; // Create Object ID field. IField fieldUserDefined = new Field(); IFieldEdit fieldEdit = (IFieldEdit)fieldUserDefined; fieldEdit.Name_2 = "OBJECTID"; fieldEdit.AliasName_2 = "OBJECT ID"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; fieldsEdit.set_Field(0, fieldUserDefined); // Create Shape field. fieldUserDefined = new Field(); fieldEdit = (IFieldEdit)fieldUserDefined; // Set up geometry definition for the Shape field. // You do not have to set the spatial reference, as it is inherited from the feature dataset. IGeometryDef geometryDef = new GeometryDefClass(); IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; geometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint; geometryDefEdit.SpatialReference_2 = pmap.SpatialReference; // By setting the grid size to 0, you are allowing ArcGIS to determine the appropriate grid sizes for the feature class. // If in a personal geodatabase, the grid size is 1,000. If in a file or ArcSDE geodatabase, the grid size // is based on the initial loading or inserting of features. geometryDefEdit.GridCount_2 = 1; geometryDefEdit.set_GridSize(0, 0); geometryDefEdit.HasM_2 = false; geometryDefEdit.HasZ_2 = false; // Set standard field properties. fieldEdit.Name_2 = "SHAPE"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; fieldEdit.GeometryDef_2 = geometryDef; fieldEdit.IsNullable_2 = true; fieldEdit.Required_2 = true; fieldsEdit.set_Field(1, fieldUserDefined); // Create a field of type double to hold some information for the features. fieldUserDefined = new Field(); fieldEdit = (IFieldEdit)fieldUserDefined; fieldEdit.Name_2 = "ID"; fieldEdit.AliasName_2 = "ID"; fieldEdit.Editable_2 = true; fieldEdit.IsNullable_2 = false; fieldEdit.Precision_2 = 2; fieldEdit.Scale_2 = 5; fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; fieldsEdit.set_Field(2, fieldUserDefined); // Create a feature class description object to use for specifying the CLSID and EXTCLSID. IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass(); IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc; IFeatureClass pLineFClass = pFeatWorkspace.CreateFeatureClass("ShorePoints", fields, ocDesc.InstanceCLSID, ocDesc.ClassExtensionCLSID, esriFeatureType.esriFTSimple, "SHAPE", ""); IFeatureLayer pFeatureLayer = new FeatureLayerClass(); pFeatureLayer.FeatureClass = pLineFClass; pFeatureLayer.Name = "ShorePoints"; pmxdoc.AddLayer(pFeatureLayer); } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ex.StackTrace); } }
public static void Main(string[] args) { #region Licensing ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop); IAoInitialize aoInitialize = new AoInitializeClass(); esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced); if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut) { MessageBox.Show("An Advanced License could not be checked out."); return; } #endregion try { // Open the workspace. IWorkspaceFactory workspaceFactory = null; switch (geodatabaseType) { case GeodatabaseType.ArcSDE: workspaceFactory = new SdeWorkspaceFactoryClass(); break; case GeodatabaseType.FileGDB: workspaceFactory = new FileGDBWorkspaceFactoryClass(); break; case GeodatabaseType.PersonalGDB: workspaceFactory = new AccessWorkspaceFactoryClass(); break; } IWorkspace workspace = workspaceFactory.OpenFromFile(workspacePath, 0); IWorkspaceExtensionManager workspaceExtensionManager = (IWorkspaceExtensionManager)workspace; // Create a UID for the workspace extension. UID uid = new UIDClass(); uid.Value = extensionGuid; // Determine whether there are any existing geodatabase-register extensions. // To disambiguate between GDB-register extensions and component category extensions, // check the extension count of a new scratch workspace. IScratchWorkspaceFactory scratchWorkspaceFactory = new FileGDBScratchWorkspaceFactoryClass(); IWorkspace scratchWorkspace = scratchWorkspaceFactory.CreateNewScratchWorkspace(); IWorkspaceExtensionManager scratchExtensionManager = (IWorkspaceExtensionManager)scratchWorkspace; Boolean workspaceExtensionApplied = false; UID gdbRegisteredUID = null; try { workspaceExtensionApplied = (workspaceExtensionManager.ExtensionCount > scratchExtensionManager.ExtensionCount); } catch (COMException comExc) { // This is necessary in case the existing extension could not be initiated. if (comExc.ErrorCode == (int)fdoError.FDO_E_WORKSPACE_EXTENSION_CREATE_FAILED) { // Parse the error message for the current extension's GUID. Regex regex = new Regex("(?<guid>{[^}]+})"); MatchCollection matchCollection = regex.Matches(comExc.Message); if (matchCollection.Count > 0) { Match match = matchCollection[0]; gdbRegisteredUID = new UIDClass(); gdbRegisteredUID.Value = match.Groups["guid"].Value; workspaceExtensionApplied = true; } else { throw comExc; } } else { throw comExc; } } if (workspaceExtensionApplied) { if (gdbRegisteredUID == null) { // There is GDB-registered extension on the SDE workspace. Find the SDE extension that is not // applied to the scratch workspace. for (int i = 0; i < workspaceExtensionManager.ExtensionCount; i++) { IWorkspaceExtension workspaceExtension = workspaceExtensionManager.get_Extension(i); IWorkspaceExtension scratchExtension = scratchExtensionManager.FindExtension(workspaceExtension.GUID); if (scratchExtension == null) { gdbRegisteredUID = workspaceExtension.GUID; } } } } // If the extension could be located, remove it. if (gdbRegisteredUID != null) { workspaceExtensionManager.UnRegisterExtension(gdbRegisteredUID); } // Register the extension. workspaceExtensionManager.RegisterExtension("RasterSyncExtension.RasterSyncWorkspaceExtension", uid); } catch (COMException comExc) { switch (comExc.ErrorCode) { case (int)fdoError.FDO_E_WORKSPACE_EXTENSION_NO_REG_PRIV: MessageBox.Show("The connection file's privileges are insufficient to perform this operation.", "Register Workspace Extension", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case (int)fdoError.FDO_E_WORKSPACE_EXTENSION_CREATE_FAILED: String createErrorMessage = String.Concat("The workspace extension could not be created.", Environment.NewLine, "Ensure that it has been registered for COM Interop."); MessageBox.Show(createErrorMessage, "Register Workspace Extension", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case (int)fdoError.FDO_E_WORKSPACE_EXTENSION_DUP_GUID: case (int)fdoError.FDO_E_WORKSPACE_EXTENSION_DUP_NAME: String dupErrorMessage = String.Concat("A duplicate name or GUID was detected. Make sure any existing GDB-registered", "workspaces are not component category-registered as well."); MessageBox.Show(dupErrorMessage, "Register Workspace Extension", MessageBoxButtons.OK, MessageBoxIcon.Error); break; default: String otherErrorMessage = String.Format("An unexpected error has occurred:{0}{1}{2}", Environment.NewLine, comExc.Message, comExc.ErrorCode); MessageBox.Show(otherErrorMessage); break; } } catch (Exception exc) { String errorMessage = String.Format("An unexpected error has occurred:{0}{1}", Environment.NewLine, exc.Message); MessageBox.Show(errorMessage); } // Shutdown the AO initializer. aoInitialize.Shutdown(); }
/// <summary> /// Create the feature workspace /// </summary> /// <param name="workspaceNameString">Workspace name</param> /// <returns></returns> public static IFeatureWorkspace CreateFeatureWorkspace(string workspaceNameString) { IScratchWorkspaceFactory2 ipScWsFactory = new FileGDBScratchWorkspaceFactoryClass(); IWorkspace ipScWorkspace = ipScWsFactory.CurrentScratchWorkspace; if (null == ipScWorkspace) ipScWorkspace = ipScWsFactory.CreateNewScratchWorkspace(); IFeatureWorkspace featWork = (IFeatureWorkspace)ipScWorkspace; return featWork; }