/// <summary> /// 删除图层 /// </summary> /// <param name="name"></param> /// <returns></returns> public static bool DeleteFeatureClass(this IWorkspace workspace, string name) { try { IEnumDatasetName pEnumDsName = workspace.get_DatasetNames(esriDatasetType.esriDTFeatureClass); IDatasetName datasetName = pEnumDsName.Next(); while (datasetName != null) { string[] name_arr = datasetName.Name.Split(new char[] { '.', '/', '\\' }); if (name_arr[name_arr.Length - 1].ToUpper() == (name.ToUpper())) { IFeatureWorkspaceManage pFWSM = workspace as IFeatureWorkspaceManage; if (pFWSM.CanDelete((IName)datasetName)) { pFWSM.DeleteByName(datasetName); break; } } datasetName = pEnumDsName.Next(); } return(true); } catch { return(false); } }
public static void Import2(string savemdbFile, string sourcemdbFile, List <StockTable> tables, string XZSDM, string XZSMC) { IWorkspace workspaceA = savemdbFile.OpenAccessFileWorkSpace(); IFeatureWorkspace featureWorkspace = workspaceA as IFeatureWorkspace; IFeatureWorkspaceManage featureWorkspaceManagerA = featureWorkspace as IFeatureWorkspaceManage; IWorkspace workspaceB = sourcemdbFile.OpenAccessFileWorkSpace(); foreach (var table in tables) { if (table.IsSpace == false) { continue; } var outfeatureClassName = table.Name + "_Merge"; if (!MergeFeatureClass(string.Format("{0}\\{1};{2}\\{1}", savemdbFile, table.Name, sourcemdbFile), null, string.Format("{0}\\{1}", savemdbFile, outfeatureClassName))) { Console.WriteLine("Merge失败"); continue; } IFeatureClass featureClassA = workspaceA.GetFeatureClass(table.Name); IDataset datasetA = featureClassA as IDataset; datasetA.Delete(); IFeatureClass featureClassAC = workspaceA.GetFeatureClass(outfeatureClassName); if (featureClassAC != null) { IDataset datasetAc = featureClassAC as IDataset; try { if (datasetAc.CanRename()) { datasetAc.Rename(table.Name); } } catch { Console.WriteLine("重命名失败"); } } Marshal.ReleaseComObject(featureClassA); } Marshal.ReleaseComObject(workspaceA); Marshal.ReleaseComObject(workspaceB); }
/// <summary> /// 删除特定数据集下的表 /// </summary> /// <param name="gdbPath">workspace</param> /// <param name="tableName">表名称</param> public static void DeleteTable(IWorkspace workspace, string tableName) { IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; IFeatureWorkspaceManage featureWorkspaceMange = (IFeatureWorkspaceManage)featureWorkspace; //得到的数据集是FeatureClass //IEnumDatasetName enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTFeatureClass); IEnumDatasetName enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTTable); IDatasetName datasetName = null; while ((datasetName = enumDatasetName.Next()) != null) { if (datasetName.Name.Equals(tableName)) { //删除指定要素类(表) featureWorkspaceMange.DeleteByName(datasetName); break; } } }
/// <summary> /// 删除名称包含指定关键字的要素类 /// </summary> /// <param name="workspace">工作空间</param> /// <param name="keyWord">关键字,不区分大小写</param> public static void DeleteFeatureClassesByKeyWord(this IWorkspace workspace, string keyWord) { if (!(workspace is IFeatureWorkspace featureWorkspace)) { throw new Exception("工作空间不是要素类工作空间!"); } keyWord = keyWord.ToLower(); IFeatureWorkspaceManage featureWorkspaceMange = (IFeatureWorkspaceManage)featureWorkspace; IEnumDatasetName enumDatasetName = workspace.DatasetNames[esriDatasetType.esriDTFeatureClass]; IDatasetName datasetName; while ((datasetName = enumDatasetName.Next()) != null) { if (datasetName.Name.ToLower().Contains(keyWord)) { featureWorkspaceMange.DeleteByName(datasetName);//删除指定要素类 } } }
/// <summary> /// 删除一个或多个要素类 /// </summary> /// <param name="workspace">工作空间</param> /// <param name="featueClassNames">要删除的要素类的名称,名称不区分大小写</param> public static void DeleteFeatureClasses(this IWorkspace workspace, params string[] featueClassNames) { if (!(workspace is IFeatureWorkspace featureWorkspace)) { throw new Exception("工作空间不是要素类工作空间!"); } featueClassNames = featueClassNames.Select(v => v.ToLower()).ToArray(); IFeatureWorkspaceManage featureWorkspaceMange = (IFeatureWorkspaceManage)featureWorkspace; IEnumDatasetName enumDatasetName = workspace.DatasetNames[esriDatasetType.esriDTFeatureClass]; IDatasetName datasetName; while ((datasetName = enumDatasetName.Next()) != null) { if (featueClassNames.Contains(datasetName.Name.ToLower())) { featureWorkspaceMange.DeleteByName(datasetName);//删除指定要素类 } } }
/// <summary> /// 删除特定数据集下的表 /// </summary> /// <param name="gdbPath">gdb路径</param> /// <param name="tableName">表名称</param> public static void DeleteFeatureClass(string gdbPath, string tableName) { IWorkspaceFactory worFact = new FileGDBWorkspaceFactory(); IWorkspace workspace = worFact.OpenFromFile(gdbPath, 0); IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; IFeatureWorkspaceManage featureWorkspaceMange = (IFeatureWorkspaceManage)featureWorkspace; //得到的数据集是FeatureClass //IEnumDatasetName enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTFeatureClass); IEnumDatasetName enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTTable); IDatasetName datasetName = null; while ((datasetName = enumDatasetName.Next()) != null) { if (datasetName.Name.Equals(tableName)) { //删除指定要素类(表) featureWorkspaceMange.DeleteByName(datasetName); break; } } }
public void doWork() { String sdeconnfname = mSdeConnFileName; // "c:\\t\\test.sde"; String tabname = mTabName; // "TEST.TABLE1"; Log.p("doWork started..."); IWorkspaceFactory wspf = new SdeWorkspaceFactoryClass(); Log.p("open worksp. from sde conn.file [" + sdeconnfname + "]"); IWorkspace wsp = wspf.OpenFromFile(sdeconnfname, 0); IFeatureWorkspaceManage fwspm = (IFeatureWorkspaceManage)wsp; Boolean isreg = fwspm.IsRegisteredAsObjectClass(tabname); if (isreg != false) { Log.p("registered already, tab.name [" + tabname + "]", "both"); // throw (new Exception("registered already, tab.name [" + tabname + "]")); return; } IFeatureWorkspace fwsp = (IFeatureWorkspace)wsp; Log.p("open tab. by name [" + tabname + "]", "both"); ITable tbl = fwsp.OpenTable(tabname); // IObjectClass from ITable // IClassSchemaEdit from objectClass // ISchemaLock from objectClass // schemaLock.ChangeSchemaLock() // schemaEdit.RegisterAsObjectClass() // schemaLock.ChangeSchemaLock() IObjectClass oc = (IObjectClass)tbl; Log.p("do register..."); RegisterWithGeodatabase(oc, mOidFldName); Log.p("OK, registered.", "both"); } // doWork method
public void Execute(ESRI.ArcGIS.esriSystem.IArray paramvalues, ESRI.ArcGIS.esriSystem.ITrackCancel TrackCancel, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager envMgr, ESRI.ArcGIS.Geodatabase.IGPMessages message) { try { IGPUtilities3 execute_Utilities = new GPUtilitiesClass(); if (TrackCancel == null) { TrackCancel = new CancelTrackerClass(); } IGPParameter inputOSMParameter = paramvalues.get_Element(in_osmFeatureClass) as IGPParameter; IGPValue inputOSMGPValue = execute_Utilities.UnpackGPValue(inputOSMParameter); IGPParameter tagCollectionParameter = paramvalues.get_Element(in_attributeSelector) as IGPParameter; IGPMultiValue tagCollectionGPValue = execute_Utilities.UnpackGPValue(tagCollectionParameter) as IGPMultiValue; if (tagCollectionGPValue == null) { message.AddError(120048, string.Format(resourceManager.GetString("GPTools_NullPointerParameterType"), tagCollectionParameter.Name)); return; } bool useUpdateCursor = false; IFeatureClass osmFeatureClass = null; ITable osmInputTable = null; IQueryFilter osmQueryFilter = null; try { execute_Utilities.DecodeFeatureLayer(inputOSMGPValue, out osmFeatureClass, out osmQueryFilter); if (osmFeatureClass != null) { if (osmFeatureClass.Extension is IOSMClassExtension) { useUpdateCursor = false; } else { useUpdateCursor = true; } } osmInputTable = osmFeatureClass as ITable; } catch { } try { if (osmInputTable == null) { execute_Utilities.DecodeTableView(inputOSMGPValue, out osmInputTable, out osmQueryFilter); } } catch { } if (osmInputTable == null) { string errorMessage = String.Format(resourceManager.GetString("GPTools_OSMGPAttributeSelecto_unableopentable"), inputOSMGPValue.GetAsText()); message.AddError(120053, errorMessage); return; } // find the field that holds tag binary/xml field int osmTagCollectionFieldIndex = osmInputTable.FindField("osmTags"); // if the Field doesn't exist - wasn't found (index = -1) get out if (osmTagCollectionFieldIndex == -1) { message.AddError(120005, resourceManager.GetString("GPTools_OSMGPAttributeSelector_notagfieldfound")); return; } // check if the tag collection includes the keyword "ALL", if does then we'll need to extract all tags bool extractAll = false; for (int valueIndex = 0; valueIndex < tagCollectionGPValue.Count; valueIndex++) { if (tagCollectionGPValue.get_Value(valueIndex).GetAsText().Equals("ALL")) { extractAll = true; break; } } //if (extractAll) //{ // if (osmTagKeyCodedValues == null) // extractAllTags(ref osmTagKeyCodedValues, osmInputTable, osmQueryFilter, osmTagCollectionFieldIndex, false); // if (osmTagKeyCodedValues == null) // { // message.AddAbort(resourceManager.GetString("GPTools_OSMGPAttributeSelector_Unable2RetrieveTags")); // return; // } // // empty the existing gp multivalue object // tagCollectionGPValue = new GPMultiValueClass(); // // fill the coded domain in gp multivalue object // for (int valueIndex = 0; valueIndex < osmTagKeyCodedValues.CodeCount; valueIndex++) // { // tagCollectionGPValue.AddValue(osmTagKeyCodedValues.get_Value(valueIndex)); // } //} // get an overall feature count as that determines the progress indicator int featureCount = osmInputTable.RowCount(osmQueryFilter); // set up the progress indicator IStepProgressor stepProgressor = TrackCancel as IStepProgressor; if (stepProgressor != null) { stepProgressor.MinRange = 0; stepProgressor.MaxRange = featureCount; stepProgressor.Position = 0; stepProgressor.Message = resourceManager.GetString("GPTools_OSMGPAttributeSelector_progressMessage"); stepProgressor.StepValue = 1; stepProgressor.Show(); } // let's get all the indices of the desired fields // if the field already exists get the index and if it doesn't exist create it Dictionary <string, int> tagsAttributesIndices = new Dictionary <string, int>(); Dictionary <int, int> attributeFieldLength = new Dictionary <int, int>(); IFeatureWorkspaceManage featureWorkspaceManage = ((IDataset)osmInputTable).Workspace as IFeatureWorkspaceManage; String illegalCharacters = String.Empty; ISQLSyntax sqlSyntax = ((IDataset)osmInputTable).Workspace as ISQLSyntax; if (sqlSyntax != null) { illegalCharacters = sqlSyntax.GetInvalidCharacters(); } IFieldsEdit fieldsEdit = osmInputTable.Fields as IFieldsEdit; using (SchemaLockManager lockMgr = new SchemaLockManager(osmInputTable)) { try { string tagKey = String.Empty; ESRI.ArcGIS.Geoprocessing.IGeoProcessor2 gp = new ESRI.ArcGIS.Geoprocessing.GeoProcessorClass(); // if we have explicitly defined tags to extract then go through the list of values now if (extractAll == false) { for (int valueIndex = 0; valueIndex < tagCollectionGPValue.Count; valueIndex++) { if (TrackCancel.Continue() == false) { return; } try { // Check if the input field already exists. string nameofTag = tagCollectionGPValue.get_Value(valueIndex).GetAsText(); tagKey = convert2AttributeFieldName(nameofTag, illegalCharacters); int fieldIndex = osmInputTable.FindField(tagKey); if (fieldIndex < 0) { // generate a new attribute field IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = tagKey; fieldEdit.AliasName_2 = nameofTag + resourceManager.GetString("GPTools_OSMGPAttributeSelector_aliasaddition"); fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 100; osmInputTable.AddField(fieldEdit); message.AddMessage(string.Format(resourceManager.GetString("GPTools_OSMGPAttributeSelector_addField"), tagKey, nameofTag)); // re-generate the attribute index fieldIndex = osmInputTable.FindField(tagKey); } if (fieldIndex > 0) { tagsAttributesIndices.Add(nameofTag, fieldIndex); attributeFieldLength.Add(fieldIndex, osmInputTable.Fields.get_Field(fieldIndex).Length); } } catch (Exception ex) { // the key is already there, this might result because from multiple upper and lower-case combinations of the same key message.AddWarning(ex.Message + " (" + convert2OSMKey(tagKey, illegalCharacters) + ")"); } } } else { List <string> listofAllTags = extractAllTags(osmInputTable, osmQueryFilter, osmTagCollectionFieldIndex); foreach (string nameOfTag in listofAllTags) { if (TrackCancel.Continue() == false) { return; } try { // Check if the input field already exists. tagKey = convert2AttributeFieldName(nameOfTag, illegalCharacters); int fieldIndex = osmInputTable.FindField(tagKey); if (fieldIndex < 0) { // generate a new attribute field IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = tagKey; fieldEdit.AliasName_2 = nameOfTag + resourceManager.GetString("GPTools_OSMGPAttributeSelector_aliasaddition"); fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 100; osmInputTable.AddField(fieldEdit); message.AddMessage(string.Format(resourceManager.GetString("GPTools_OSMGPAttributeSelector_addField"), tagKey, nameOfTag)); // re-generate the attribute index fieldIndex = osmInputTable.FindField(tagKey); } if (fieldIndex > 0) { tagsAttributesIndices.Add(nameOfTag, fieldIndex); attributeFieldLength.Add(fieldIndex, osmInputTable.Fields.get_Field(fieldIndex).Length); } } catch (Exception ex) { // the key is already there, this might result because from multiple upper and lower-case combinations of the same key message.AddWarning(ex.Message + " (" + convert2OSMKey(tagKey, illegalCharacters) + ")"); } } } } catch (Exception ex) { message.AddWarning(ex.Message); } } try { execute_Utilities.DecodeFeatureLayer(inputOSMGPValue, out osmFeatureClass, out osmQueryFilter); if (osmFeatureClass != null) { if (osmFeatureClass.Extension is IOSMClassExtension) { useUpdateCursor = false; } else { useUpdateCursor = true; } } osmInputTable = osmFeatureClass as ITable; } catch { } try { if (osmInputTable == null) { execute_Utilities.DecodeTableView(inputOSMGPValue, out osmInputTable, out osmQueryFilter); } } catch { } if (osmInputTable == null) { string errorMessage = String.Format(resourceManager.GetString("GPTools_OSMGPAttributeSelecto_unableopentable"), inputOSMGPValue.GetAsText()); message.AddError(120053, errorMessage); return; } using (ComReleaser comReleaser = new ComReleaser()) { using (SchemaLockManager lockMgr = new SchemaLockManager(osmInputTable)) { // get an update cursor for all the features to process ICursor rowCursor = null; if (useUpdateCursor) { rowCursor = osmInputTable.Update(osmQueryFilter, false); } else { rowCursor = osmInputTable.Search(osmQueryFilter, false); } comReleaser.ManageLifetime(rowCursor); IRow osmRow = null; Dictionary <string, string> tagKeys = new Dictionary <string, string>(); int progessIndex = 0; #if DEBUG message.AddMessage("useUpdateCursor: " + useUpdateCursor.ToString()); #endif // as long as there are features.... while ((osmRow = rowCursor.NextRow()) != null) { // retrieve the tags of the current feature tag[] storedTags = _osmUtility.retrieveOSMTags(osmRow, osmTagCollectionFieldIndex, ((IDataset)osmInputTable).Workspace); bool rowChanged = false; if (storedTags != null) { foreach (tag tagItem in storedTags) { // Check for matching values so we only change a minimum number of rows if (tagsAttributesIndices.ContainsKey(tagItem.k)) { int fieldIndex = tagsAttributesIndices[tagItem.k]; //...then stored the value in the attribute field // ensure that the content of the tag actually does fit into the field length...otherwise do truncate it string tagValue = tagItem.v; int fieldLength = attributeFieldLength[fieldIndex]; if (tagValue.Length > fieldLength) { tagValue = tagValue.Substring(0, fieldLength); } osmRow.set_Value(fieldIndex, tagValue); rowChanged = true; } else { #if DEBUG //message.AddWarning(tagItem.k); #endif } } } storedTags = null; try { if (rowChanged) { if (useUpdateCursor) { rowCursor.UpdateRow(osmRow); } else { // update the feature through the cursor osmRow.Store(); } } progessIndex++; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); message.AddWarning(ex.Message); } if (osmRow != null) { Marshal.ReleaseComObject(osmRow); } if (stepProgressor != null) { // update the progress UI stepProgressor.Position = progessIndex; } // check for user cancellation (every 100 rows) if ((progessIndex % 100 == 0) && (TrackCancel.Continue() == false)) { return; } } if (stepProgressor != null) { stepProgressor.Hide(); } } } execute_Utilities.ReleaseInternals(); Marshal.ReleaseComObject(execute_Utilities); } catch (Exception ex) { message.AddError(120054, ex.Message); } }
//删除名称对象 public static void DeleteByName(IFeatureWorkspace pFeaWorkspace, IDatasetName pDatasetName) { IFeatureWorkspaceManage pWorkspaceManager = pFeaWorkspace as IFeatureWorkspaceManage; pWorkspaceManager?.DeleteByName(pDatasetName); }
/// <summary> /// 移除拓扑 /// </summary> /// <param name="pFeaDataet">数据集</param> /// <param name="pTopoName">拓扑名</param> /// <param name="outError">异常</param> public void RemoveTopo(IFeatureDataset pFeaDataet, string topoName, out Exception outError) { outError = null; //topoName = topoName + "_Topology"; IWorkspace pWorkspace = pFeaDataet.Workspace; IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace; IFeatureWorkspaceManage pFeatureWorkspaceManage = pFeatureWorkspace as IFeatureWorkspaceManage; //if (!((IWorkspaceEdit)pWorkspace).IsBeingEdited()) //{ // //((IWorkspaceEdit)pWorkspace).StartEditing(true); // //((IWorkspaceEdit)pWorkspace).StartEditOperation(); //} // int iCount = 0; // try // { try { ITopologyContainer pTopoCon = pFeaDataet as ITopologyContainer; ITopology pTopo = pTopoCon.get_TopologyByName(topoName); //ISchemaLock pLock = pTopo as ISchemaLock; //pLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); ////删除该拓扑下所有的拓扑规则 //RemoveTopoRule(pTopo, out outError); //if (outError != null) return; ////删除该拓扑下左右的拓扑图层 //RemoveTopoClass(pTopo, out outError); //if (outError != null) return; //删除拓扑 IDataset pTopoDt = pTopo as IDataset; IName pName = pTopoDt.FullName; IDatasetName ptopodatasetname = pName as IDatasetName; pFeatureWorkspaceManage.DeleteByName(ptopodatasetname); //(pTopo as IDataset).Delete(); //pTopo.FeatureDataset.Delete(); //Marshal.ReleaseComObject(pTopo); pTopo = null; //++iCount; //pLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } catch (Exception ex) { ////********************************************* ////guozheng 2010-12-24 平安夜 added 系统异常日志 //if (ModData.SysLog == null) ModData.SysLog = new clsWriteSystemFunctionLog(); //ModData.SysLog.Write(ex); ////********************************************** outError = ex; } //} //finally //{ // if (((IWorkspaceEdit)pWorkspace).IsBeingEdited()) // { // if (iCount > 0) // { // ((IWorkspaceEdit)pWorkspace).StopEditOperation(); // ((IWorkspaceEdit)pWorkspace).StopEditing(true); // } // { // ((IWorkspaceEdit)pWorkspace).AbortEditOperation(); // ((IWorkspaceEdit)pWorkspace).StopEditing(true); // } // } //} }