public GdbWorkspaceIdentity([NotNull] Connector connector, string connectionString) { Assert.ArgumentNotNull(connector, nameof(connector)); _instance = null; _version = null; _user = null; _dbms = EnterpriseDatabaseType.Unknown; ConnectionString = string.Empty; switch (connector) { case DatabaseConnectionProperties connectionProperties: _instance = connectionProperties.Instance; _version = connectionProperties.Version; _user = connectionProperties.User; _dbms = connectionProperties.DBMS; ConnectionString = connectionString; WorkspaceFactory = WorkspaceFactory.SDE; break; case FileGeodatabaseConnectionPath fileGeodatabaseConnectionPath: // connectionString is "DATABASE=C:\\git\\KtLU.Dabank\\data\\Testdaten\\dabank_test_data\\Default.gdb" ConnectionString = fileGeodatabaseConnectionPath.Path.AbsolutePath; WorkspaceFactory = WorkspaceFactory.FileGDB; break; default: throw new NotImplementedException( $"connector {connector.GetType()} is not implemented"); } }
public GdbWorkspaceIdentity([NotNull] Connector connector) { Assert.ArgumentNotNull(connector, nameof(connector)); _instance = null; _version = null; _user = null; _path = null; _dbms = EnterpriseDatabaseType.Unknown; switch (connector) { case DatabaseConnectionProperties connectionProperties: _instance = connectionProperties.Instance; _version = connectionProperties.Version; _user = connectionProperties.User; _dbms = connectionProperties.DBMS; break; case FileGeodatabaseConnectionPath fileGeodatabaseConnectionPath: _path = fileGeodatabaseConnectionPath.Path.AbsolutePath; break; default: throw new NotImplementedException( $"connector {connector.GetType()} is not implemented"); } }
double GetLength(FeatureClass fc, EnterpriseDatabaseType enterpriseDbType) { try { using (FeatureClassDefinition fcd = fc.GetDefinition()) { // the name of the length field changes depending on what enterprise geodatabase is used var areaFieldName = "Shape_Length"; switch (enterpriseDbType) { case EnterpriseDatabaseType.SQLServer: areaFieldName = "STLength"; break; } Field lengthField = fcd.GetFields().FirstOrDefault(x => x.Name.Contains(areaFieldName)); if (lengthField == null) { return(0); } System.Diagnostics.Debug.WriteLine(lengthField.Name); StatisticsDescription SumDesc = new StatisticsDescription(lengthField, new List <StatisticsFunction>() { StatisticsFunction.Sum }); TableStatisticsDescription tsd = new TableStatisticsDescription(new List <StatisticsDescription>() { SumDesc }); double sum = 0; try { sum = fc.CalculateStatistics(tsd).FirstOrDefault().StatisticsResults.FirstOrDefault().Sum; // exception is thrown on this line } catch { sum = Utilities.GetSumWorkAround(fc, lengthField.Name); } return(sum); } } catch (Exception ex) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(ex.ToString(), "Error"); return(0); } }
/// <summary> /// returns the enterprise gdb type for a given feature layer /// </summary> /// <param name="lyr"></param> /// <returns>EnterpriseDatabaseType enum of database or .Unknown</returns> public static EnterpriseDatabaseType GetDatabaseType(FeatureLayer lyr) { EnterpriseDatabaseType enterpriseDatabaseType = EnterpriseDatabaseType.Unknown; using (Table table = lyr.GetTable()) { try { var geodatabase = table.GetDatastore() as Geodatabase; enterpriseDatabaseType = (geodatabase.GetConnector() as DatabaseConnectionProperties).DBMS; } catch (InvalidOperationException) { } } return(enterpriseDatabaseType); }
/// <summary> /// This method will /// 1. Make sure if a Feature Layer is selected. /// 2. The Workspace is not null /// 3. Make sure that the workspace is an Enterprise SQL Server Geodatabase Workspace /// /// and then create a new version (In a Queued Task) /// and Connect to the newly created version and delete all the features for the selected subtype (In a separate QueuedTask) /// </summary> /// <param name="item">The newly selected combo box item</param> protected override async void OnSelectionChange(ComboBoxItem item) { await QueuedTask.Run(async() => { if (item == null) { return; } if (string.IsNullOrEmpty(item.Text)) { return; } Layer layer = MapView.Active.GetSelectedLayers()[0]; if (layer is FeatureLayer featureLayer) { using (Geodatabase geodatabase = featureLayer.GetTable().GetDatastore() as Geodatabase) using (Table table = featureLayer.GetTable()) { if (geodatabase == null) { return; } EnterpriseDatabaseType enterpriseDatabaseType = ((DatabaseConnectionProperties)geodatabase.GetConnector()).DBMS; if (enterpriseDatabaseType != EnterpriseDatabaseType.SQLServer) { Enabled = false; return; } if (table.GetRegistrationType().Equals(RegistrationType.Nonversioned)) { return; } using (Version newVersion = await CreateVersionAsync(table)) using (Geodatabase newVersionGeodatabase = newVersion.Connect()) using (Table newVersionTable = newVersionGeodatabase.OpenDataset <Table>(table.GetName())) { string subtypeField = table.GetDefinition().GetSubtypeField(); int code = table.GetDefinition().GetSubtypes().First(subtype => subtype.GetName().Equals(item.Text)).GetCode(); QueryFilter queryFilter = new QueryFilter { WhereClause = string.Format("{0}={1}", subtypeField, code) }; using (var rowCursor = newVersionTable.Search(queryFilter, false)) { EditOperation editOperation = new EditOperation { EditOperationType = EditOperationType.Long, Name = "Delete Based On Subtype" }; editOperation.Callback(context => { while (rowCursor.MoveNext()) { using (Row row = rowCursor.Current) { context.Invalidate(row); row.Delete(); } } }, newVersionTable); bool result = await editOperation.ExecuteAsync(); if (!result) { MessageBox.Show(String.Format("Could not delete features for subtype {0} : {1}", item.Text, editOperation.ErrorMessage)); } await Project.Current.SaveEditsAsync(); } } } } }); }
/// <summary> /// This method will /// 1. Make sure if a Feature Layer is selected. /// 2. The Workspace is not null /// 3. Make sure that the workspace is an Enterprise SQL Server Geodatabase Workspace /// /// and then create a new version (In a Queued Task) /// and Connect to the newly created version and delete all the features for the selected subtype (In a separate QueuedTask) /// </summary> /// <param name="item">The newly selected combo box item</param> protected override async void OnSelectionChange(ComboBoxItem item) { if (item == null) { return; } if (string.IsNullOrEmpty(item.Text)) { return; } Layer layer = MapView.Active.GetSelectedLayers()[0]; FeatureLayer featureLayer = null; if (layer is FeatureLayer) { featureLayer = layer as FeatureLayer; Geodatabase geodatabase = null; await QueuedTask.Run(() => geodatabase = (featureLayer.GetTable().GetDatastore() as Geodatabase)); using (geodatabase) { if (geodatabase == null) { return; } } } else { return; } EnterpriseDatabaseType enterpriseDatabaseType = EnterpriseDatabaseType.Unknown; await QueuedTask.Run(() => { using (Table table = (MapView.Active.GetSelectedLayers()[0] as FeatureLayer).GetTable()) { try { enterpriseDatabaseType = (table.GetDatastore() as Geodatabase).GetEnterpriseDatabaseType(); } catch (InvalidOperationException e) { } } }); if (enterpriseDatabaseType != EnterpriseDatabaseType.SQLServer) { Enabled = false; return; } string versionName = String.Empty; await QueuedTask.Run(async() => { using (Table table = featureLayer.GetTable()) { versionName = await CreateVersion(table); } }); if (versionName == null) { return; } await QueuedTask.Run(async() => { using (Table table = featureLayer.GetTable()) { if (table.GetRegistrationType().Equals(RegistrationType.Nonversioned)) { return; } } }); QueuedTask.Run(async() => { using (Table table = featureLayer.GetTable()) { string subtypeField = table.GetDefinition().GetSubtypeField(); int code = table.GetDefinition().GetSubtypes().First(subtype => subtype.GetName().Equals(item.Text)).GetCode(); QueryFilter queryFilter = new QueryFilter { WhereClause = string.Format("{0} = {1}", subtypeField, code) }; try { VersionManager versionManager = (table.GetDatastore() as Geodatabase).GetVersionManager(); Version newVersion = versionManager.GetVersions().First(version => version.GetName().Contains(versionName)); Geodatabase newVersionGeodatabase = newVersion.Connect(); using (Table newVersionTable = newVersionGeodatabase.OpenDataset <Table>(table.GetName())) { using (var rowCursor = newVersionTable.Search(queryFilter, false)) { EditOperation editOperation = new EditOperation { EditOperationType = EditOperationType.Long, Name = "Delete Based On Subtype" }; editOperation.Callback(context => { while (rowCursor.MoveNext()) { using (Row row = rowCursor.Current) { context.Invalidate(row); row.Delete(); } } }, newVersionTable); bool result = await editOperation.ExecuteAsync(); if (!result) { MessageBox.Show(String.Format("Could not delete features for subtype {0} : {1}", item.Text, editOperation.ErrorMessage)); } await Project.Current.SaveEditsAsync(); } } } catch (Exception e) { Console.WriteLine(e); } } }); }
/// <summary> /// This method makes sure /// 1. The Mapview is Active /// 2. There is at least one Layer selected /// 3. The selected Layer is a FeatureLayer /// 4. The selected Layer is backed by an Enterprise Sql Server Geodatabase FeatureClass /// /// If all of these hold good, the DatabaseClient is used to execute a query which creates /// a Database Table containing the gdb_items records corresponding to all domains. The Table is /// then opened using the API and the domains combobox populated. Finally, the Table is deleted /// </summary> /// <param name="mapViewEventArgs"></param> private async void UpdateDomainList(MapViewEventArgs mapViewEventArgs) { if (MapView.Active == null || mapViewEventArgs.MapView.GetSelectedLayers().Count < 1 || !(mapViewEventArgs.MapView.GetSelectedLayers()[0] is FeatureLayer)) { Enabled = false; return; } EnterpriseDatabaseType enterpriseDatabaseType = EnterpriseDatabaseType.Unknown; await QueuedTask.Run(() => { using (Table table = (mapViewEventArgs.MapView.GetSelectedLayers()[0] as FeatureLayer).GetTable()) { if (!(table.GetDatastore() is Geodatabase)) { return; } try { enterpriseDatabaseType = (table.GetDatastore() as Geodatabase).GetEnterpriseDatabaseType(); } catch (InvalidOperationException e) { } } }); if (enterpriseDatabaseType != EnterpriseDatabaseType.SQLServer) { Enabled = false; return; } Enabled = true; Clear(); QueuedTask.Run(() => { using (Table table = (mapViewEventArgs.MapView.GetSelectedLayers()[0] as FeatureLayer).GetTable()) { var geodatabase = table.GetDatastore() as Geodatabase; Version defaultVersion = geodatabase.GetVersionManager().GetVersions().FirstOrDefault(version => { string name = version.GetName(); return(name.ToLowerInvariant().Equals("dbo.default") || name.ToLowerInvariant().Equals("sde.default")); }); if (defaultVersion == null) { return; } string tableName = String.Format("NewTable{0}{1}{2}{3}", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond); gdbItemsOwner = defaultVersion.GetName().Split('.')[0]; string statement = String.Format( @"select {1}.GDB_ITEMTYPES.Name as Type, {1}.GDB_ITEMS.Name into {0} from {1}.GDB_ITEMS JOIN {1}.GDB_ITEMTYPES ON {1}.GDB_ITEMS.Type = {1}.GDB_ITEMTYPES.UUID where {1}.GDB_ITEMTYPES.Name = 'Domain' OR {1}.GDB_ITEMTYPES.Name = 'Coded Value Domain' OR {1}.GDB_ITEMTYPES.Name = 'Range Domain'", tableName, gdbItemsOwner); try { DatabaseClient.ExecuteStatement(geodatabase, statement); } catch (GeodatabaseTableException exception) { MessageBox.Show(exception.Message); return; } var newTable = geodatabase.OpenDataset <Table>(tableName); using (RowCursor rowCursor = newTable.Search(null, false)) { while (rowCursor.MoveNext()) { using (Row row = rowCursor.Current) { Add(new ComboBoxItem(row["Name"].ToString())); } } } statement = String.Format(@"DROP TABLE {0}", tableName); DatabaseClient.ExecuteStatement(geodatabase, statement); } }); }