public static IFeatureClassName GetFeatureClassName(IFeatureClass pfc) { IDataset pDS = pfc as IDataset; IWorkspaceName workspaceName = new WorkspaceName() as IWorkspaceName; workspaceName = pDS.FullName as IWorkspaceName; IFeatureClassName featureClassName = new FeatureClassName() as IFeatureClassName; IDatasetName datasetName = (IDatasetName)featureClassName; datasetName.Name = pDS.Name; datasetName.WorkspaceName = workspaceName; return(featureClassName); }
/// <summary> /// 导出图层中选定要素到单独的shp文件 /// </summary> /// <param name="featureLayer">要素图层</param> /// <param name="selectionSet">要素选择集</param> /// <param name="outName">输出shp文件路径</param> public void exportSelectedFeatureToShp(IFeatureLayer featureLayer, ISelectionSet selectionSet, string outName) { if (featureLayer == null) { return; } if (!Directory.Exists(System.IO.Path.GetDirectoryName(outName))) { return; } // 裁剪要素 IDataset dataset = featureLayer as IDataset; IFeatureClassName infeatClassName = dataset.FullName as IFeatureClassName; IDatasetName datasetName = infeatClassName as IDatasetName; // 输出要素类 IFeatureClassName outFeatClassName = new FeatureClassName() as IFeatureClassName; outFeatClassName.FeatureType = esriFeatureType.esriFTSimple; outFeatClassName.ShapeType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryAny; outFeatClassName.ShapeFieldName = "Shape"; // 输出文件 IDatasetName outDatasetName = outFeatClassName as IDatasetName; outDatasetName.Name = System.IO.Path.GetFileNameWithoutExtension(outName); IWorkspaceName workspaceName = new WorkspaceName() as IWorkspaceName; workspaceName.PathName = System.IO.Path.GetDirectoryName(outName); workspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.ShapefileWorkspaceFactory"; outDatasetName.WorkspaceName = workspaceName; // 导出 IExportOperation exportOper = new ExportOperation(); exportOper.ExportFeatureClass(datasetName, null, selectionSet, null, outFeatClassName, 0); }
protected override void OnSave(Stream outStrm) { if (_setupOp == null) { return; } // NOTE: Do not close or dispose BinaryWriter, as this will close the Stream BinaryWriter writer = new BinaryWriter(outStrm); int version = 3; writer.Write(version); // ************** VERSION 3 ************** //// Item 1: FeatureClassName: The orphan junction feature class of the Geometric Network for the underground conveyance system. FeatureClassName networkDataset = null; if (_setupOp.GeometricNetwork != null) { networkDataset = ((IDataset)_setupOp.GeometricNetwork.OrphanJunctionFeatureClass).FullName as FeatureClassName; } PersistenceHelper.Save <FeatureClassName>(outStrm, networkDataset); //// Item 2: FeatureClassName: The inlet feature class FeatureClassName inletClass = null; if (_setupOp.InletClass != null) { inletClass = ((IDataset)_setupOp.InletClass).FullName as FeatureClassName; } PersistenceHelper.Save <FeatureClassName>(outStrm, inletClass); //// Item 3: RasterDatasetName: The DEM dataset RasterDatasetName demDataset = null; if (_setupOp.DEM != null) { IDataset dem = ((IRasterAnalysisProps)_setupOp.DEM).RasterDataset as IDataset; if (dem != null) { demDataset = dem.FullName as RasterDatasetName; } } PersistenceHelper.Save <RasterDatasetName>(outStrm, demDataset); //// Item 4: Boolean: Whether to smooth boundaries writer.Write(_setupOp.SmoothBoundaries); //// Item 5: Boolean: Whether to include upstream pipe ends writer.Write(_setupOp.IncludeUpstreamPipeEnds); //// Item 6: Boolean: Whether to exclude downstream pipe ends writer.Write(_setupOp.ExcludeDownstreamPipeEnds); //// Item 7: RasterDatasetName: The flow direction dataset RasterDatasetName flowDirDataset = null; if (_setupOp.FlowDirection != null) { IDataset flowDir = ((IRasterAnalysisProps)_setupOp.FlowDirection).RasterDataset as IDataset; if (flowDir != null) { flowDirDataset = flowDir.FullName as RasterDatasetName; } } PersistenceHelper.Save <RasterDatasetName>(outStrm, flowDirDataset); //// Item 8: FeatureClassName: The catchment feature class FeatureClassName catchmentClass = null; if (_setupOp.Catchments != null) { catchmentClass = ((IDataset)_setupOp.Catchments).FullName as FeatureClassName; } PersistenceHelper.Save <FeatureClassName>(outStrm, catchmentClass); //// Item 9: Boolean: Whether to exclude disabled nodes writer.Write(_setupOp.ExcludeDisabledNodes); }
//TODO: Implement load and save protected override void OnLoad(Stream inStrm) { SetupOp setupOp = null; try { // NOTE: Do not close or dispose BinaryReader, as this will close the Stream BinaryReader reader = new BinaryReader(inStrm); int version = reader.ReadInt32(); // NOTE: With change to Add-In architecture, this extension is NOT backwards compatible with versions 1 and 2 if (version == 3) { //// Version 3: //// Item 1: FeatureClassName: The orphan junction feature class of the Geometric Network for the underground conveyance system. //// Item 2: FeatureClassName: The inlet feature class //// Item 3: RasterDatasetName: The DEM dataset //// Item 4: Boolean: Whether to smooth boundaries //// Item 5: Boolean: Whether to include upstream pipe ends //// Item 6: Boolean: Whether to exclude downstream pipe ends //// Item 7: RasterDatasetName: The flow direction dataset //// Item 8: FeatureClassName: The catchment feature class //// Item 9: Boolean: Whether to exclude disabled nodes setupOp = new SetupOp(); setupOp.ResultsDirectory = this.MxDocDirectoryName; setupOp.ScratchDirectory = this.TemporaryDirectory; FeatureClassName networkJunctionName = null; PersistenceHelper.Load <FeatureClassName>(inStrm, ref networkJunctionName); INetworkClass junctionClass = _SafeOpen(networkJunctionName) as INetworkClass; if (junctionClass != null) { setupOp.GeometricNetwork = junctionClass.GeometricNetwork; } FeatureClassName inletClassName = null; PersistenceHelper.Load <FeatureClassName>(inStrm, ref inletClassName); setupOp.InletClass = _SafeOpen(inletClassName) as IFeatureClass; RasterDatasetName demName = null; PersistenceHelper.Load <RasterDatasetName>(inStrm, ref demName); IRasterDataset demDataset = _SafeOpen(demName) as IRasterDataset; if (demDataset != null) { setupOp.DEM = demDataset.CreateDefaultRaster(); } setupOp.SmoothBoundaries = reader.ReadBoolean(); setupOp.IncludeUpstreamPipeEnds = reader.ReadBoolean(); setupOp.ExcludeDownstreamPipeEnds = reader.ReadBoolean(); RasterDatasetName flowDirName = null; PersistenceHelper.Load <RasterDatasetName>(inStrm, ref flowDirName); IRasterDataset flowDirDataset = _SafeOpen(flowDirName) as IRasterDataset; if (flowDirDataset != null) { setupOp.FlowDirection = flowDirDataset.CreateDefaultRaster(); } FeatureClassName catchmentClassName = null; PersistenceHelper.Load <FeatureClassName>(inStrm, ref catchmentClassName); setupOp.Catchments = _SafeOpen(catchmentClassName) as IFeatureClass; setupOp.ExcludeDisabledNodes = reader.ReadBoolean(); } _setupOp = setupOp; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.GetType().FullName + ": " + ex.Message); } }
public static void Project(IFeatureClass ifeatureClass_0, ISpatialReference ispatialReference_0, IFeatureDatasetName ifeatureDatasetName_0, string string_0, double double_0) { IWorkspace workspace = ((IDataset)ifeatureClass_0).Workspace; IFeatureDataConverter featureDataConverter = new FeatureDataConverter(); IWorkspaceName workspaceName = new WorkspaceName() as IWorkspaceName; workspaceName.ConnectionProperties = workspace.ConnectionProperties; workspaceName.WorkspaceFactoryProgID = workspace.WorkspaceFactory.GetClassID().Value.ToString(); IWorkspaceName workspaceName2 = (ifeatureDatasetName_0 as IDatasetName).WorkspaceName; IDatasetName datasetName = new FeatureClassName() as IDatasetName; string text = ifeatureClass_0.AliasName; int num = text.LastIndexOf("."); if (num != -1) { text = text.Substring(num + 1); } datasetName.Name = text; datasetName.WorkspaceName = workspaceName; IWorkspace2 workspace2 = (workspaceName2 as IName).Open() as IWorkspace2; IDatasetName datasetName2 = new FeatureClassName() as IDatasetName; datasetName2.WorkspaceName = workspaceName2; (datasetName2 as IFeatureClassName).FeatureDatasetName = (ifeatureDatasetName_0 as IDatasetName); IFieldChecker fieldChecker = new FieldChecker(); fieldChecker.ValidateWorkspace = (workspace2 as IWorkspace); string[] array = string_0.Split(new char[] { '.' }); string_0 = array[array.Length - 1] + "_Project"; string text2; fieldChecker.ValidateTableName(string_0, out text2); string text3 = text2; int num2 = 1; if (workspaceName2.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace || workspaceName2.Type == esriWorkspaceType.esriLocalDatabaseWorkspace) { while (workspace2.get_NameExists(esriDatasetType.esriDTFeatureClass, text3)) { text3 = text2 + "_" + num2.ToString(); num2++; } } else if (workspaceName2.Type == esriWorkspaceType.esriFileSystemWorkspace) { while (System.IO.File.Exists(text3 + ".shp")) { text3 = text2 + "_" + num2.ToString(); num2++; } } datasetName2.Name = text3; IFields fields = new ESRI.ArcGIS.Geodatabase.Fields() as IFields; num = ifeatureClass_0.Fields.FindField(ifeatureClass_0.ShapeFieldName); IField field = ifeatureClass_0.Fields.get_Field(num); IGeometryDef geometryDef = field.GeometryDef; ISpatialReference arg_1D9_0 = geometryDef.SpatialReference; double num3; double num4; double num5; double num6; ispatialReference_0.GetDomain(out num3, out num4, out num5, out num6); ((IGeometryDefEdit)geometryDef).GridCount_2 = 1; ((IGeometryDefEdit)geometryDef).set_GridSize(0, double_0); ((IGeometryDefEdit)geometryDef).SpatialReference_2 = ispatialReference_0; ((IFieldEdit)field).GeometryDef_2 = geometryDef; for (int i = 0; i < ifeatureClass_0.Fields.FieldCount; i++) { if (i == num) { ((IFieldsEdit)fields).AddField(field); } else { IField field2 = ifeatureClass_0.Fields.get_Field(i); ((IFieldsEdit)fields).AddField(field2); } } IEnumFieldError enumFieldError; IFields outputFields; fieldChecker.Validate(fields, out enumFieldError, out outputFields); if (SRLibCommonFunc.m_pfrm != null) { SRLibCommonFunc.m_pfrm.FeatureProgress = featureDataConverter; } try { featureDataConverter.ConvertFeatureClass((IFeatureClassName)datasetName, null, ifeatureDatasetName_0, (IFeatureClassName)datasetName2, geometryDef, outputFields, "", 1000, 0); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } }
public static void Project(IFeatureClass ifeatureClass_0, ISpatialReference ispatialReference_0, IWorkspace iworkspace_0, string string_0, double double_0) { string str; double num; double num1; double num2; double num3; IEnumFieldError enumFieldError; IFields field; IWorkspace workspace = ((IDataset)ifeatureClass_0).Workspace; IFeatureDataConverter featureDataConverterClass = new FeatureDataConverter(); IWorkspaceName workspaceNameClass = new WorkspaceName() as IWorkspaceName; workspaceNameClass.ConnectionProperties = workspace.ConnectionProperties; workspaceNameClass.WorkspaceFactoryProgID = workspace.WorkspaceFactory.GetClassID().Value.ToString(); IWorkspaceName connectionProperties = new WorkspaceName() as IWorkspaceName; PropertySet propertySetClass = new PropertySet(); connectionProperties.ConnectionProperties = iworkspace_0.ConnectionProperties; connectionProperties.WorkspaceFactoryProgID = iworkspace_0.WorkspaceFactory.GetClassID().Value.ToString(); IDatasetName featureClassNameClass = new FeatureClassName() as IDatasetName; string aliasName = ifeatureClass_0.AliasName; int num4 = aliasName.LastIndexOf("."); if (num4 != -1) { aliasName = aliasName.Substring(num4 + 1); } featureClassNameClass.Name = aliasName; featureClassNameClass.WorkspaceName = workspaceNameClass; IDatasetName datasetName = new FeatureClassName() as IDatasetName; { datasetName.WorkspaceName = connectionProperties; } ; IFieldChecker fieldCheckerClass = new FieldChecker() { ValidateWorkspace = iworkspace_0 }; string[] strArrays = string_0.Split(new char[] { '.' }); string_0 = string.Concat(strArrays[(int)strArrays.Length - 1], "_Project"); fieldCheckerClass.ValidateTableName(string_0, out str); string str1 = str; int num5 = 1; if ( !(iworkspace_0.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace ? false : iworkspace_0.Type != esriWorkspaceType.esriLocalDatabaseWorkspace)) { while (((IWorkspace2)iworkspace_0).NameExists[esriDatasetType.esriDTFeatureClass, str1]) { str1 = string.Concat(str, "_", num5.ToString()); num5++; } } else if (iworkspace_0.Type == esriWorkspaceType.esriFileSystemWorkspace) { while (File.Exists(string.Concat(str1, ".shp"))) { str1 = string.Concat(str, "_", num5.ToString()); num5++; } } datasetName.Name = str1; IFields fieldsClass = new ESRI.ArcGIS.Geodatabase.Fields(); num4 = ifeatureClass_0.Fields.FindField(ifeatureClass_0.ShapeFieldName); IField field1 = ifeatureClass_0.Fields.Field[num4]; IGeometryDef geometryDef = field1.GeometryDef; ISpatialReference spatialReference = geometryDef.SpatialReference; ispatialReference_0.GetDomain(out num, out num1, out num2, out num3); ((IGeometryDefEdit)geometryDef).GridCount_2 = 1; ((IGeometryDefEdit)geometryDef).GridSize_2[0] = double_0; ((IGeometryDefEdit)geometryDef).SpatialReference_2 = ispatialReference_0; ((IFieldEdit)field1).GeometryDef_2 = geometryDef; for (int i = 0; i < ifeatureClass_0.Fields.FieldCount; i++) { if (i != num4) { IField field2 = ifeatureClass_0.Fields.Field[i]; ((IFieldsEdit)fieldsClass).AddField(field2); } else { ((IFieldsEdit)fieldsClass).AddField(field1); } } fieldCheckerClass.Validate(fieldsClass, out enumFieldError, out field); if (SRLibCommonFunc.m_pfrm != null) { SRLibCommonFunc.m_pfrm.FeatureProgress = featureDataConverterClass; } try { featureDataConverterClass.ConvertFeatureClass((IFeatureClassName)featureClassNameClass, null, null, (IFeatureClassName)datasetName, geometryDef, field, "", 1000, 0); } catch (Exception exception) { MessageBox.Show(exception.Message); } }
/// <summary> /// 将指定要素类复制到目标数据库中(在目标数据库中创建要素类) /// </summary> /// <param name="pSrcFC">源要素类</param> /// <param name="pQueryFilter">查询条件</param> /// <param name="pTargetWks">目标数据库</param> /// <param name="sMessage">转换消息</param> /// <param name="sTargetName">目标要素类名</param> /// <param name="sAlias">目标要素类别名</param> /// <param name="sFDatasetName">目标要素数据集</param> /// <returns></returns> public bool TranslateFeatureClass(IFeatureClass pSrcFC, IQueryFilter pQueryFilter, IWorkspace pTargetWks, string sTargetName = null, string sAlias = null, string sFDatasetName = null) { GeoDataUtils geoDataUtils = new GeoDataUtils(); IDataset pSrcDataset = pSrcFC as IDataset; IFeatureClassName pSrcName = pSrcDataset.FullName as IFeatureClassName; IFeatureDatasetName pFeatureDatasetName = null; if (pTargetWks.Type != esriWorkspaceType.esriFileSystemWorkspace) { string sFeatureDatasetName = sFDatasetName; if (string.IsNullOrEmpty(sFeatureDatasetName) && pSrcFC.FeatureDataset != null) { sFeatureDatasetName = geoDataUtils.ExtractDatasetName(pSrcFC.FeatureDataset.Name); } if (!string.IsNullOrEmpty(sFeatureDatasetName)) { if (geoDataUtils.IsNameExist(pTargetWks, sFeatureDatasetName, esriDatasetType.esriDTFeatureDataset)) { IFeatureDataset pTargetFeatureDataset = (pTargetWks as IFeatureWorkspace).OpenFeatureDataset(sFeatureDatasetName); pFeatureDatasetName = pTargetFeatureDataset.FullName as IFeatureDatasetName; Marshal.ReleaseComObject(pTargetFeatureDataset); } else { pFeatureDatasetName = new FeatureDatasetName() as IFeatureDatasetName; IDatasetName pDatasetName = pFeatureDatasetName as IDatasetName; pDatasetName.Name = sFeatureDatasetName; pDatasetName.WorkspaceName = (pTargetWks as IDataset).FullName as IWorkspaceName; } } } //目标要素类名 FeatureClassName pTargetName = new FeatureClassName(); IDatasetName pTargetDatasetName = pTargetName as IDatasetName; if (string.IsNullOrEmpty(sTargetName)) { sTargetName = geoDataUtils.ExtractDatasetName(pSrcDataset.Name); } pTargetDatasetName.Name = sTargetName; pTargetDatasetName.WorkspaceName = (pTargetWks as IDataset).FullName as IWorkspaceName; geoDataUtils.DeleteDataset(pTargetWks, pTargetDatasetName); // 验证字段 IFields pTargetFields; string sShapeFieldName; geoDataUtils.CheckFields(pSrcFC, pTargetWks, out pTargetFields, out sShapeFieldName); IEnumInvalidObject pEnumInvalidObject = null; IFeatureDataConverter pConverter = null; try { int iIndex = pTargetFields.FindField(sShapeFieldName); IGeometryDef pTargetGeometryDef = pTargetFields.get_Field(iIndex).GeometryDef; // 执行批量转换 pConverter = new FeatureDataConverter(); pEnumInvalidObject = pConverter.ConvertFeatureClass(pSrcName, pQueryFilter, pFeatureDatasetName as IFeatureDatasetName, pTargetName as IFeatureClassName, pTargetGeometryDef, pTargetFields, "", 1000, 0); // 检查转换过程中是否产生错误 if (pEnumInvalidObject != null) { IInvalidObjectInfo pInvalidInfo = null; pEnumInvalidObject.Reset(); while ((pInvalidInfo = pEnumInvalidObject.Next()) != null) { ErrorMessage += pSrcFC.AliasName + "图层要素:" + pInvalidInfo.InvalidObjectID + "执行失败!"; } } } finally //释放资源 { if (pEnumInvalidObject != null) { Marshal.ReleaseComObject(pEnumInvalidObject); } if (pConverter != null) { Marshal.ReleaseComObject(pConverter); } } //修改要素类别名 if (string.IsNullOrEmpty(sAlias)) { sAlias = pSrcFC.AliasName; } IFeatureClass pTargetFC = (pTargetWks as IFeatureWorkspace).OpenFeatureClass(sTargetName); geoDataUtils.AlterDatasetAlias(pTargetFC, sAlias); Marshal.ReleaseComObject(pTargetFC); return(true); }