/// <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);
        }
Exemplo n.º 2
0
        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 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();
		}
Exemplo n.º 4
0
        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();
        }
Exemplo n.º 5
0
        /// <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;
        }