private void 添加点型图层ToolStripMenuItem_Click(object sender, EventArgs e) { //创建要素类 #region 创建新的内存工作空间 IWorkspaceFactory pWSF = new InMemoryWorkspaceFactoryClass(); IWorkspaceName pWSName = pWSF.Create("", "Temp", null, 0); IName pName = (IName)pWSName; IWorkspace pMemoryWS = (IWorkspace)pName.Open(); #endregion IField oField = new FieldClass(); IFields oFields = new FieldsClass(); IFieldsEdit oFieldsEdit = null; IFieldEdit oFieldEdit = null; IFeatureClass oFeatureClass = null; IFeatureLayer oFeatureLayer = null; oFieldsEdit = oFields as IFieldsEdit; oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "OBJECTID"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; oFieldEdit.IsNullable_2 = false; oFieldEdit.Required_2 = false; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef; pGeoDefEdit.AvgNumPoints_2 = 5; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeoDefEdit.GridCount_2 = 1; pGeoDefEdit.HasM_2 = false; pGeoDefEdit.HasZ_2 = false; pGeoDefEdit.SpatialReference_2 = axMapControl1.SpatialReference; oFieldEdit.Name_2 = "SHAPE"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; oFieldEdit.GeometryDef_2 = pGeoDef; oFieldEdit.IsNullable_2 = true; oFieldEdit.Required_2 = true; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "Code"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger; //oFieldEdit.Length = 10; oFieldEdit.IsNullable_2 = true; oFieldsEdit.AddField(oField); //创建要素类 oFeatureClass = (pMemoryWS as IFeatureWorkspace).CreateFeatureClass("Temp", oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); oFeatureLayer = new FeatureLayerClass(); oFeatureLayer.Name = "PointLayer"; oFeatureLayer.FeatureClass = oFeatureClass; //创建唯一值符号化对象 IUniqueValueRenderer pURender = new UniqueValueRendererClass(); pURender.FieldCount = 1; pURender.set_Field(0, "Code"); pURender.UseDefaultSymbol = false; //创建SimpleMarkerSymbolClass对象 ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbolClass(); //创建RgbColorClass对象为pSimpleMarkerSymbol设置颜色 IRgbColor pRgbColor = new RgbColorClass(); pRgbColor.Red = 255; pSimpleMarkerSymbol.Color = pRgbColor as IColor; //设置pSimpleMarkerSymbol对象的符号类型,选择钻石 pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond; //设置pSimpleMarkerSymbol对象大小,设置为5 pSimpleMarkerSymbol.Size = 5; //显示外框线 pSimpleMarkerSymbol.Outline = true; //为外框线设置颜色 IRgbColor pLineRgbColor = new RgbColorClass(); pLineRgbColor.Green = 255; pSimpleMarkerSymbol.OutlineColor = pLineRgbColor as IColor; //设置外框线的宽度 pSimpleMarkerSymbol.OutlineSize = 1; //半透明颜色 pURender.AddValue("1", "", pSimpleMarkerSymbol as ISymbol); //唯一值符号化内存图层 (oFeatureLayer as IGeoFeatureLayer).Renderer = pURender as IFeatureRenderer; ILayerEffects pLyrEffect = oFeatureLayer as ILayerEffects; //透明度 pLyrEffect.Transparency = 0; oFeatureLayer.Visible = true; this.axMapControl1.AddLayer(oFeatureLayer,axMapControl1.LayerCount); insertpoint = true; }
public IWorkspace CreateInMemoryWorkspace() { IWorkspaceFactory workspaceFactory = null; IWorkspaceName workspaceName = null; IName name = null; IWorkspace workspace = null; try { // Create an InMemory workspace factory. workspaceFactory = new InMemoryWorkspaceFactoryClass(); // Create an InMemory geodatabase. workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); // Cast for IName. name = (IName)workspaceName; //Open a reference to the InMemory workspace through the name object. workspace = (IWorkspace)name.Open(); return(workspace); } catch { return(null); } finally { workspaceFactory = null; workspaceName = null; name = null; } }
//通过intersect关系进行裁切 public void ClipByExtent(int groupNm, int extNum) { IGeometry extent = GetClipExtent(extNum); string findName = "grp" + groupNm.ToString() + "ext0"; IGroupLayer grpLy = avlbLayers.Find(item => item.Name == findName); ICompositeLayer comLy = grpLy as ICompositeLayer; IGroupLayer newGroupLayer = new GroupLayerClass(); newGroupLayer.Name = "grp" + groupNm.ToString() + "ext" + extNum.ToString(); for (int i = 0; i < comLy.Count; i++) { IFeatureLayer feaLy = comLy.get_Layer(i) as IFeatureLayer; IFeatureClass clipFC = feaLy.FeatureClass; IFields flds = clipFC.Fields; ISpatialFilter spatialFilter = new SpatialFilterClass(); spatialFilter.Geometry = extent; spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; IFeatureCursor cursor = clipFC.Search(spatialFilter, false); IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create(null, "MyWorkspace", null, 0); IName name = (IName)workspaceName; IWorkspace workspace = (IWorkspace)name.Open(); IFeatureWorkspace inmemFeaWksp = workspace as IFeatureWorkspace; if (clipFC.ShapeType == esriGeometryType.esriGeometryPoint) { IFeatureClass inmemPTFC = CreateWithoutDescription("inmemPTFC_" + groupNumber.ToString(), null, inmemFeaWksp, clipFC.ShapeType, flds); InsertFeaturesBoun(inmemPTFC, cursor, extent); IFeatureLayer inmemPTFCLayer = new FeatureLayerClass(); inmemPTFCLayer.FeatureClass = inmemPTFC; newGroupLayer.Add(inmemPTFCLayer); } else if (clipFC.ShapeType == esriGeometryType.esriGeometryPolyline) { IFeatureClass inmemLNFC = CreateWithoutDescription("inmemLNFC" + groupNumber.ToString(), null, inmemFeaWksp, clipFC.ShapeType, flds); InsertFeaturesBoun(inmemLNFC, cursor, extent); IFeatureLayer inmemLNFCLayer = new FeatureLayerClass(); inmemLNFCLayer.FeatureClass = inmemLNFC; newGroupLayer.Add(inmemLNFCLayer); } else if (clipFC.ShapeType == esriGeometryType.esriGeometryPolygon) { IFeatureClass inmemPLGFC = CreateWithoutDescription("inmemPLGFC" + groupNumber.ToString(), null, inmemFeaWksp, clipFC.ShapeType, flds); InsertFeaturesBoun(inmemPLGFC, cursor, extent); IFeatureLayer inmemPLGFCLayer = new FeatureLayerClass(); inmemPLGFCLayer.FeatureClass = inmemPLGFC; newGroupLayer.Add(inmemPLGFCLayer); } } avlbLayers.Add(newGroupLayer); }
public ITable DataTableToAETable(DataTable table, string name) { IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); IName names = (IName)workspaceName; IWorkspace inmemWor = (IWorkspace)names.Open(); ITable pTable = DataTableToAETable(table, inmemWor as IFeatureWorkspace, name); if (names != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(names); } if (workspaceFactory != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFactory); } if (workspaceName != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceName); } if (inmemWor != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(inmemWor); } return(pTable); }
/// <summary> /// 创建内存IFeatureLayer,该方法为创建内存临时图层的便捷方法,创建的图层带有id,和name两个属性字段。 /// </summary> /// <param name="myAxMapControl">AxMapControl扩展对象</param> /// <param name="strIFeatureClassName">IFeatureClass名称</param> /// <param name="myEsriGeometryType">数据类型</param> /// <returns>IFeatureLayer对象</returns> public static IFeatureLayer CreateMemoryFeatureLayer(this AxMapControl myAxMapControl, String strIFeatureClassName, esriGeometryType myEsriGeometryType) { IWorkspaceFactory myIWorkspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName myIWorkspaceName = myIWorkspaceFactory.Create("", strIFeatureClassName, null, 0); IName myIName = (IName)myIWorkspaceName; IWorkspace myIWorkspace = (IWorkspace)myIName.Open(); IField myIField = null; IFields myIFields = new FieldsClass(); IFieldsEdit myIFieldsEdit = myIFields as IFieldsEdit; IFieldEdit myIFieldEdit = null; IFeatureClass myIFeatureClass = null; IFeatureLayer myIFeatureLayer = null; try { //主键id myIField = new FieldClass(); myIFieldEdit = myIField as IFieldEdit; myIFieldEdit.Name_2 = "id"; myIFieldEdit.IsNullable_2 = true; myIFieldEdit.Length_2 = 50; myIFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; myIFieldsEdit.AddField(myIField); //名称name myIField = new FieldClass(); myIFieldEdit = myIField as IFieldEdit; myIFieldEdit.Name_2 = "name"; myIFieldEdit.IsNullable_2 = true; myIFieldEdit.Length_2 = 50; myIFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; myIFieldsEdit.AddField(myIField); //IGeometryI字段 IGeometryDef myIGeometryDef = new GeometryDefClass(); IGeometryDefEdit myIGeometryDefEdit = (IGeometryDefEdit)myIGeometryDef; myIGeometryDefEdit.AvgNumPoints_2 = 5; myIGeometryDefEdit.GeometryType_2 = myEsriGeometryType; myIGeometryDefEdit.GridCount_2 = 1; myIGeometryDefEdit.HasM_2 = false; myIGeometryDefEdit.HasZ_2 = false; myIGeometryDefEdit.SpatialReference_2 = myAxMapControl.SpatialReference; myIField = new FieldClass(); myIFieldEdit = myIField as IFieldEdit; myIFieldEdit.Name_2 = "SHAPE"; myIFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; myIFieldEdit.GeometryDef_2 = myIGeometryDef; myIFieldEdit.IsNullable_2 = true; myIFieldEdit.Required_2 = true; myIFieldsEdit.AddField(myIField); // myIFeatureClass = (myIWorkspace as IFeatureWorkspace).CreateFeatureClass(strIFeatureClassName, myIFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); (myIFeatureClass as IDataset).BrowseName = strIFeatureClassName; myIFeatureLayer = new FeatureLayerClass(); myIFeatureLayer.Name = strIFeatureClassName; myIFeatureLayer.FeatureClass = myIFeatureClass; } catch { } return(myIFeatureLayer); }
/// <summary> /// Join Urban-rural Devide Table /// </summary> /// <param name="layer"></param> /// <param name="year"></param> /// <author>Shen Yongyuan</author> /// <date>20091111</date> public static void JoinDevideTable(ILayer layer, int year, string city) { //Add Fields IFieldsEdit allFields = new FieldsClass(); IFieldEdit field1 = new FieldClass(); field1.Name_2 = "ID"; field1.Type_2 = esriFieldType.esriFieldTypeString; allFields.AddField(field1); IFieldEdit field2 = new FieldClass(); field2.Name_2 = Town.Constant.Constant.TmpFieldName; field2.Type_2 = esriFieldType.esriFieldTypeString; allFields.AddField(field2); //Create Table IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("", "Temporary WorkSpace In Memory", null, 0); IFeatureWorkspace workspace = ((IName)workspaceName).Open() as IFeatureWorkspace; ITable table = workspace.CreateTable(Town.Constant.Constant.TmpTableDevide, allFields, null, null, ""); //Import Data IWorkspaceEdit workspaceEdit = workspace as IWorkspaceEdit; workspaceEdit.StartEditing(false); workspaceEdit.StartEditOperation(); ICursor cursor = table.Insert(true); int fldField1 = cursor.Fields.FindField("ID"); int fldField2 = cursor.Fields.FindField(Town.Constant.Constant.TmpFieldName); //Query and Import Dictionary <int, string> devideResult = DataLib.DA_Devide.GetDevideResult(year, city); foreach (KeyValuePair <int, string> d in devideResult) { IRowBuffer buffer = table.CreateRowBuffer(); buffer.set_Value(fldField1, d.Key.ToString()); buffer.set_Value(fldField2, d.Value); cursor.InsertRow(buffer); } cursor.Flush(); workspaceEdit.StartEditOperation(); workspaceEdit.StopEditing(true); System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor); //Join IMemoryRelationshipClassFactory pMemRelFact = new MemoryRelationshipClassFactory(); IFeatureLayer feaLayer = layer as IFeatureLayer; ITable originTable = feaLayer.FeatureClass as ITable; IRelationshipClass pRelClass = pMemRelFact.Open("Join", originTable as IObjectClass, "ID", table as IObjectClass, "ID", "forward", "backward", esriRelCardinality.esriRelCardinalityOneToOne); IDisplayRelationshipClass pDispRC = feaLayer as IDisplayRelationshipClass; pDispRC.DisplayRelationshipClass(null, esriJoinType.esriLeftOuterJoin); pDispRC.DisplayRelationshipClass(pRelClass, esriJoinType.esriLeftOuterJoin); }
public ITable DataTableToAETable(DataTable table, string[] fieldName, string name) { IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); IName names = (IName)workspaceName; IWorkspace inmemWor = (IWorkspace)names.Open(); return(DataTableToAETable(table, inmemWor as IFeatureWorkspace, fieldName, name)); }
private IWorkspace CreateInMemoryWorkspace() { var newWorkspaceFactory = new InMemoryWorkspaceFactoryClass(); var workspaceName = newWorkspaceFactory.Create("", "MCDAWorkspace", null, 0); var name = (IName)workspaceName; var inMemoryWorkspace = (IWorkspace)name.Open(); return(inMemoryWorkspace); }
/// <summary> /// 创建内存工作空间 /// </summary> /// <param name="strWorkspaceName">内存工作空间名称</param> /// <returns></returns> public static IWorkspace NewInMemoryWorkspace(string strWorkspaceName) { IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("", strWorkspaceName, null, 0); IName name = (IName)workspaceName; IWorkspace workspace = (IWorkspace)name.Open(); Marshal.ReleaseComObject(workspaceFactory); return(workspace); }
/// <summary> /// 获得内存中的要素工作空间 /// </summary> /// <returns></returns> public IFeatureWorkspace FeatureWorkspace() { IWorkspaceFactory pWorkspacFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName pWorkspaceName = pWorkspacFactory.Create("", "pWorkspace", null, 0); IName pName = (IName)pWorkspaceName; IWorkspace pWorkspace = (IWorkspace)pName.Open(); IFeatureWorkspace pFeatuerwok = (IFeatureWorkspace)pWorkspace; return(pFeatuerwok); }
/// <summary> /// 根据字段集合创建一个内存要素类 /// </summary> /// <param name="fields"></param> /// <param name="shapeFiledName"></param> /// <param name="className"></param> /// <returns></returns> public static IFeatureClass CreateMemoryFeatureClass(IFields fields, string shapeFiledName, string className) { // 创建内存工作空间 IWorkspaceFactory pWSF = new InMemoryWorkspaceFactoryClass(); IWorkspaceName pWSName = pWSF.Create("", "Temp", null, 0); IName pName = (IName)pWSName; IWorkspace memoryWS = (IWorkspace)pName.Open(); //创建要素类 IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)memoryWS; IFeatureClass featureClass = featureWorkspace.CreateFeatureClass( className, fields, null, null, esriFeatureType.esriFTSimple, shapeFiledName, ""); return(featureClass); }
/// <summary> /// 建立記憶體中的workspace /// </summary> public void CreateInstance() { // Create an in-memory workspace factory. IWorkspaceFactory2 pWorkspaceFactory; pWorkspaceFactory = new InMemoryWorkspaceFactoryClass() as IWorkspaceFactory2; // Create a new in-memory workspace. This returns a name object. IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create(null, workspaceName, null, 0); IName pName = (IName)pWorkspaceName; // Open the workspace through the name object. //IWorkspace workspace = (IWorkspace)name.Open(); pWorkspace = (IWorkspace)pName.Open(); }
/// <summary> /// This function creates a copy of a feature class in memory. /// The fields are added - including an x/y field for the shape.x and shape.y /// for use in the copy features function. /// </summary> /// <param name="pFC">a Feature class -- any fc will do (ie: FC, shapefile)</param> /// <returns>IFeatureClass - an in memory fc with 2 additional fields</returns> /// <notes> /// Modified from Kirk Kuykendall's code from this post /// http://forums.esri.com/Thread.asp?c=93&f=993&t=210767&mc=12#msgid652601 /// </notes> /// <changelog> /// djr /// comments created. /// /// </changelog> public IFeatureClass cacheToMemory(IFeatureClass pFC, string xValue, string yValue) { IWorkspaceFactory pWSF = new InMemoryWorkspaceFactoryClass(); IName pName = (IName)pWSF.Create("", "inmemory", null, 0); IFeatureWorkspace pFWS = (IFeatureWorkspace)pName.Open(); IFields pFlds = this.copyFields(pFC.Fields, xValue, yValue); char[] period = { '.' }; string[] vShpFld = pFC.ShapeFieldName.Split(period); IFeatureClass pInMemFC = (IFeatureClass)pFWS.CreateFeatureClass(pFC.AliasName, pFlds, pFC.CLSID, null, pFC.FeatureType, vShpFld[vShpFld.GetUpperBound(0)].ToString(), ""); return(pInMemFC); }
public static ESRI.ArcGIS.Geodatabase.IWorkspace GetWorkspace(string inXMLFile) { IWorkspaceFactory inMemoryWorkspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName workspaceName = inMemoryWorkspaceFactory.Create("", "InMemory", null, 0); IName name = workspaceName as IName; IWorkspace workspace = (IWorkspace)name.Open(); //Create a GDB importer IGdbXmlImport xmlImport = new GdbImporterClass(); // Read schema section from the XML file. // Note: enumNameMapping is initialized to nothing and its value is set 'by ref' with a call to the GenerateNameMapping method. IEnumNameMapping eNameMapping = null; xmlImport.GenerateNameMapping(inXMLFile, workspace, out eNameMapping); //Create the workspace schema and load the data xmlImport.ImportWorkspace(inXMLFile, eNameMapping, workspace, false); return(workspace); }
/// <summary> /// 创建一个只包含几何字段的简单内存要素类 /// </summary> /// <param name="geometryType"></param> /// <param name="spatialReference"></param> /// <param name="className"></param> /// <returns></returns> public static IFeatureClass CreateMemorySimpleFeatureClass(esriGeometryType geometryType, ISpatialReference spatialReference, string className) { // 创建内存工作空间 IWorkspaceFactory pWSF = new InMemoryWorkspaceFactoryClass(); IWorkspaceName pWSName = pWSF.Create("", "Temp", null, 0); IName pName = (IName)pWSName; IWorkspace memoryWS = (IWorkspace)pName.Open(); //创建字段集 IField field = new FieldClass(); IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = fields as IFieldsEdit; IFieldEdit fieldEdit = field as IFieldEdit; //创建图形字段 field = new FieldClass(); fieldEdit = field as IFieldEdit; IGeometryDef geoDef = new GeometryDefClass(); IGeometryDefEdit geoDefEdit = (IGeometryDefEdit)geoDef; geoDefEdit.AvgNumPoints_2 = 5; geoDefEdit.GeometryType_2 = geometryType; geoDefEdit.GridCount_2 = 1; geoDefEdit.HasM_2 = false; geoDefEdit.HasZ_2 = false; geoDefEdit.SpatialReference_2 = spatialReference; fieldEdit.Name_2 = "SHAPE"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; fieldEdit.GeometryDef_2 = geoDef; fieldEdit.IsNullable_2 = true; fieldEdit.Required_2 = true; fieldsEdit.AddField(field); //创建要素类 IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)memoryWS; IFeatureClass featureClass = featureWorkspace.CreateFeatureClass( className, fields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); return(featureClass); }
private void 添加面形地物ToolStripMenuItem_Click(object sender, EventArgs e) { //创建要素类 #region 创建新的内存工作空间 IWorkspaceFactory pWSF = new InMemoryWorkspaceFactoryClass(); IWorkspaceName pWSName = pWSF.Create("", "Temp", null, 0); IName pName = (IName)pWSName; IWorkspace pMemoryWS = (IWorkspace)pName.Open(); #endregion IField oField = new FieldClass(); IFields oFields = new FieldsClass(); IFieldsEdit oFieldsEdit = null; IFieldEdit oFieldEdit = null; IFeatureClass oFeatureClass = null; IFeatureLayer oFeatureLayer = null; oFieldsEdit = oFields as IFieldsEdit; oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "OBJECTID"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; oFieldEdit.IsNullable_2 = false; oFieldEdit.Required_2 = false; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef; pGeoDefEdit.AvgNumPoints_2 = 5; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeoDefEdit.GridCount_2 = 1; pGeoDefEdit.HasM_2 = false; pGeoDefEdit.HasZ_2 = false; pGeoDefEdit.SpatialReference_2 = axMapControl1.SpatialReference; oFieldEdit.Name_2 = "SHAPE"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; oFieldEdit.GeometryDef_2 = pGeoDef; oFieldEdit.IsNullable_2 = true; oFieldEdit.Required_2 = true; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "Code"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger; oFieldEdit.IsNullable_2 = true; oFieldsEdit.AddField(oField); //创建要素类 oFeatureClass = (pMemoryWS as IFeatureWorkspace).CreateFeatureClass("Temp", oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); oFeatureLayer = new FeatureLayerClass(); oFeatureLayer.Name = "PolygonLayer"; oFeatureLayer.FeatureClass = oFeatureClass; //创建唯一值符号化对象 IUniqueValueRenderer pURender = new UniqueValueRendererClass(); pURender.FieldCount = 1; pURender.set_Field(0, "Code"); pURender.UseDefaultSymbol = false; ISimpleFillSymbol pFillSym = new SimpleFillSymbolClass(); pFillSym.Style = esriSimpleFillStyle.esriSFSSolid; //半透明颜色 IRgbColor pColor = new RgbColorClass(); pColor.Red = 255; pColor.Green = 255; pFillSym.Color = pColor; pURender.AddValue("1", "", pFillSym as ISymbol); pFillSym = new SimpleFillSymbolClass(); pFillSym.Style = esriSimpleFillStyle.esriSFSSolid; //唯一值符号化内存图层 (oFeatureLayer as IGeoFeatureLayer).Renderer = pURender as IFeatureRenderer; ILayerEffects pLyrEffect = oFeatureLayer as ILayerEffects; //透明度 pLyrEffect.Transparency = 0; this.axMapControl1.AddLayer(oFeatureLayer, axMapControl1.LayerCount); insertPolygon = true; }
/// <summary> /// 根据离散点数据动态生成featureclass点图层 /// </summary> /// <params name="vectors"></params> /// <returns></returns> private static IFeatureClass GetFeatureCLass(LibGeometry.Vector3_DW[] vectors,ESRI.ArcGIS.Controls.AxMapControl map) { IWorkspaceFactory pWorkspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create("", "pWorkspace", null, 0); IName pName = (IName)pWorkspaceName; IWorkspace pWorkspace = (IWorkspace)pName.Open(); IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace; IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "SHAPE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeometryDef = new GeometryDefClass(); IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit; pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; //为FeatureClass赋参考系,不写会出错*************************************** pGeometryDefEdit.SpatialReference_2 = map.SpatialReference; //************************************************************************ pFieldEdit.GeometryDef_2 = pGeometryDef; pFieldsEdit.AddField(pField); pField = new FieldClass();//不要省略写!容易出问题 pFieldEdit = pField as IFieldEdit; pFieldEdit.AliasName_2 = "高程"; pFieldEdit.Name_2 = "CoordinateZ"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldsEdit.AddField(pField); pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(Application.StartupPath+"\\tempRaster", 0) as IFeatureWorkspace; IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass("points", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); //IWorkspaceFactory pWorkSpaceFac = new ShapefileWorkspaceFactoryClass(); //IFeatureWorkspace pFeatureWorkSpace = pWorkSpaceFac.OpenFromFile(shpfolder, 0) as IFeatureWorkspace; ////创建字段集2 //IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass(); //IObjectClassDescription ocDescription = (IObjectClassDescription)fcDescription;//创建必要字段 //IFields fields = ocDescription.RequiredFields; //int shapeFieldIndex = fields.FindField(fcDescription.ShapeFieldName); //IField field = fields.get_Field(shapeFieldIndex); //IGeometryDef geometryDef = field.GeometryDef; //IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; ////geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; ////geometryDefEdit.SpatialReference_2 = spatialReference; //geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; //ISpatialReferenceFactory pSpatialRefFac = new SpatialReferenceEnvironmentClass(); //IProjectedCoordinateSystem pcsSys = pSpatialRefFac.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_Zone_39); //geometryDefEdit.SpatialReference_2 = pcsSys; //IFieldChecker fieldChecker = new FieldCheckerClass(); //IEnumFieldError enumFieldError = null; //IFields validatedFields = null; //将传入字段 转成 validatedFields //fieldChecker.ValidateWorkspace = (IWorkspace)pFeatureWorkSpace; //fieldChecker.Validate(fields, out enumFieldError, out validatedFields); //pFeatureWorkSpace.CreateFeatureClass(shpname, validatedFields, ocDescription.InstanceCLSID, ocDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDescription.ShapeFieldName, ""); //从vectors中获取散点值 Dictionary<int, IPoint> pointDictionary = new Dictionary<int, IPoint>(); for (int i = 0; i < vectors.Length; i++) { IPoint pPoint=new PointClass(); pPoint.X = vectors[i].X; pPoint.Y = vectors[i].Y; pPoint.Z =vectors[i].Z; if (pPoint.Z.ToString() != "非数字") { pointDictionary.Add(i, pPoint); } } //插入到新建的FeatureClass中 IWorkspaceEdit pWorkspaceEdit = pWorkspace as IWorkspaceEdit; pWorkspaceEdit.StartEditing(true); pWorkspaceEdit.StartEditOperation(); IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer(); IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true); for (int featureNum = 4; featureNum < pointDictionary.Count;featureNum++ ) { pFeatureBuffer.Shape = pointDictionary[featureNum] as IPoint;//出错点,在于新建字段的错误 pFeatureBuffer.set_Value(pFeatureClass.Fields.FindField("CoordinateZ"), pointDictionary[featureNum].Z); pFeatureCursor.InsertFeature(pFeatureBuffer); } pFeatureCursor.Flush(); pWorkspaceEdit.StopEditOperation(); pWorkspaceEdit.StopEditing(true); return pFeatureClass; }
/// <summary> /// Join Index /// </summary> /// <param name="layer"></param> /// <param name="year"></param> /// <author>Shen Yongyuan</author> /// <date>20091111</date> public static void JoinIndexTable(ILayer layer, string indexName, int year, string city, bool normal) { //Add Fields IFieldsEdit allFields = new FieldsClass(); IFieldEdit field1 = new FieldClass(); field1.Name_2 = "ID"; field1.Type_2 = esriFieldType.esriFieldTypeString; allFields.AddField(field1); IFieldEdit field2 = new FieldClass(); field2.Name_2 = Town.Constant.Constant.TmpFieldName; field2.Type_2 = esriFieldType.esriFieldTypeDouble; allFields.AddField(field2); //Create Table IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("", "Temporary WorkSpace In Memory", null, 0); IFeatureWorkspace workspace = ((IName)workspaceName).Open() as IFeatureWorkspace; ITable table = workspace.CreateTable(Town.Constant.Constant.TmpTableIndex, allFields, null, null, ""); //Import Data IWorkspaceEdit workspaceEdit = workspace as IWorkspaceEdit; workspaceEdit.StartEditing(false); workspaceEdit.StartEditOperation(); ICursor cursor = table.Insert(true); int fldField1 = cursor.Fields.FindField("ID"); int fldField2 = cursor.Fields.FindField(Town.Constant.Constant.TmpFieldName); IQueryable <DataLib.I_INDEX> indexCollect = DataLib.DA_Index.QueryIndex(indexName, city).Where(c => c.FromDate.Year == year); foreach (DataLib.I_INDEX index in indexCollect) { IRowBuffer buffer = table.CreateRowBuffer(); buffer.set_Value(fldField1, index.I_ZONE.OID); if (normal == false) { buffer.set_Value(fldField2, index.Value); } else if (normal == true && DataLib.DA_Index.QueryIndex(indexName, city).Min(c => c.Value) > 0) { buffer.set_Value(fldField2, index.Value); } else { buffer.set_Value(fldField2, index.Value + Math.Abs(DataLib.DA_Index.QueryIndex(indexName, city).Min(c => c.Value)) + 1); } cursor.InsertRow(buffer); } cursor.Flush(); workspaceEdit.StartEditOperation(); workspaceEdit.StopEditing(true); System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor); //Join IMemoryRelationshipClassFactory pMemRelFact = new MemoryRelationshipClassFactory(); IFeatureLayer feaLayer = layer as IFeatureLayer; ITable originTable = feaLayer.FeatureClass as ITable; IRelationshipClass pRelClass = pMemRelFact.Open("Join", originTable as IObjectClass, "ID", table as IObjectClass, "ID", "forward", "backward", esriRelCardinality.esriRelCardinalityOneToOne); IDisplayRelationshipClass pDispRC = feaLayer as IDisplayRelationshipClass; pDispRC.DisplayRelationshipClass(null, esriJoinType.esriLeftOuterJoin); pDispRC.DisplayRelationshipClass(pRelClass, esriJoinType.esriLeftOuterJoin); }
private IWorkspace CreateInMemoryWorkspace() { IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); // Create an InMemory geodatabase. IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); // Cast for IName. IName name = (IName)workspaceName; //Open a reference to the InMemory workspace through the name object. IWorkspace workspace = (IWorkspace)name.Open(); return workspace; }
/// <summary> /// 创建人员图层 /// </summary> /// <param name="DataSetName"></param> /// <param name="AliaseName"></param> /// <param name="SpatialRef"></param> /// <param name="GeometryType"></param> /// <param name="PropertyFields"></param> /// <returns></returns> private void CreateDrawnLayer() { try { if (axMapControl1.LayerCount >= 2) { axMapControl1.DeleteLayer(0); } } catch { } IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); ESRI.ArcGIS.Geodatabase.IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); ESRI.ArcGIS.esriSystem.IName name = (IName)workspaceName; ESRI.ArcGIS.Geodatabase.IWorkspace inmemWor = (IWorkspace)name.Open(); IField oField = new FieldClass(); IFields oFields = new FieldsClass(); IFieldsEdit oFieldsEdit = null; IFieldEdit oFieldEdit = null; IFeatureClass oFeatureClass = null; IFeatureLayer oFeatureLayer = null; try { oFieldsEdit = oFields as IFieldsEdit; oFieldEdit = oField as IFieldEdit; //创建图元属性 FieldClass feadName = new FieldClass(); IFieldEdit edit = feadName; edit.Name_2 = "Name"; edit.Type_2 = esriFieldType.esriFieldTypeString; oFieldsEdit.AddField(feadName); FieldClass feadID = new FieldClass(); IFieldEdit editID = feadID; editID.Name_2 = "ID"; editID.Type_2 = esriFieldType.esriFieldTypeString; oFieldsEdit.AddField(editID); IGeometryDef geometryDef = new GeometryDefClass(); IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; //geometryDefEdit.AvgNumPoints_2 = 5; geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline; geometryDefEdit.GridCount_2 = 1; geometryDefEdit.HasM_2 = false; geometryDefEdit.HasZ_2 = false; geometryDefEdit.SpatialReference_2 = axMapControl1.SpatialReference; oFieldEdit.Name_2 = "SHAPE"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; oFieldEdit.GeometryDef_2 = geometryDef; oFieldEdit.IsNullable_2 = true; oFieldEdit.Required_2 = true; oFieldsEdit.AddField(oField); oFeatureClass = (inmemWor as IFeatureWorkspace).CreateFeatureClass("DrawnDS", oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); (oFeatureClass as IDataset).BrowseName = "DrawnDS"; oFeatureLayer = new FeatureLayerClass(); oFeatureLayer.Name = "Drawn"; oFeatureLayer.FeatureClass = oFeatureClass; } catch { } finally { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(oField); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFields); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldsEdit); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldEdit); System.Runtime.InteropServices.Marshal.ReleaseComObject(name); System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFactory); System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceName); System.Runtime.InteropServices.Marshal.ReleaseComObject(inmemWor); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFeatureClass); } catch { } GC.Collect(); } DrawnLayer = oFeatureLayer; axMapControl1.Map.AddLayer(oFeatureLayer); UniqueValueRenderFlyr(oFeatureLayer); axMapControl1.Refresh(); }
/// <summary> /// 在内存中创建图层 /// </summary> /// <param name="DataSetName">数据集名称</param> /// <param name="AliaseName">别名</param> /// <param name="SpatialRef">空间参考</param> /// <param name="GeometryType">几何类型</param> /// <param name="PropertyFields">属性字段集合</param> /// <returns>IfeatureLayer</returns> public static IFeatureLayer CreateFeatureLayerInmemeory(string DataSetName, string AliaseName, ISpatialReference SpatialRef, esriGeometryType GeometryType, IFields PropertyFields) { IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); ESRI.ArcGIS.Geodatabase.IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); ESRI.ArcGIS.esriSystem.IName name = (IName)workspaceName; ESRI.ArcGIS.Geodatabase.IWorkspace inmemWor = (IWorkspace)name.Open(); IField oField = new FieldClass(); IFields oFields = new FieldsClass(); IFieldsEdit oFieldsEdit = null; IFieldEdit oFieldEdit = null; IFeatureClass oFeatureClass = null; IFeatureLayer oFeatureLayer = null; try { oFieldsEdit = oFields as IFieldsEdit; oFieldEdit = oField as IFieldEdit; if (PropertyFields != null) { for (int i = 0; i < PropertyFields.FieldCount; i++) { oFieldsEdit.AddField(PropertyFields.get_Field(i)); } } IGeometryDef geometryDef = new GeometryDefClass(); IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; geometryDefEdit.AvgNumPoints_2 = 5; geometryDefEdit.GeometryType_2 = GeometryType; geometryDefEdit.GridCount_2 = 1; geometryDefEdit.HasM_2 = false; geometryDefEdit.HasZ_2 = false; geometryDefEdit.SpatialReference_2 = SpatialRef; geometryDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass();//没有这一句就报错,说尝试读取或写入受保护的内存。 geometryDefEdit.SpatialReference.SetDomain(-450359962737.05, 450359962737.05, -450359962737.05, 450359962737.05);//没有这句就抛异常来自HRESULT:0x8004120E。 oFieldEdit.Name_2 = "SHAPE"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; oFieldEdit.GeometryDef_2 = geometryDef; oFieldEdit.IsNullable_2 = true; oFieldEdit.Required_2 = true; oFieldsEdit.AddField(oField); oFeatureClass = (inmemWor as IFeatureWorkspace).CreateFeatureClass(DataSetName, oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); (oFeatureClass as IDataset).BrowseName = DataSetName; oFeatureLayer = new FeatureLayerClass(); oFeatureLayer.Name = AliaseName; oFeatureLayer.FeatureClass = oFeatureClass; } catch (Exception ex) { } finally { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(oField); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFields); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldsEdit); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldEdit); System.Runtime.InteropServices.Marshal.ReleaseComObject(name); System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFactory); System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceName); System.Runtime.InteropServices.Marshal.ReleaseComObject(inmemWor); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFeatureClass); } catch { } GC.Collect(); } return oFeatureLayer; }
/// <summary> /// 根据离散点数据动态生成featureclass点图层 /// </summary> /// <params name="vectors"></params> /// <returns></returns> private static IFeatureClass GetFeatureCLass(LibGeometry.Vector3_DW[] vectors, ESRI.ArcGIS.Controls.AxMapControl map) { IWorkspaceFactory pWorkspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create("", "pWorkspace", null, 0); IName pName = (IName)pWorkspaceName; IWorkspace pWorkspace = (IWorkspace)pName.Open(); IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace; IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "SHAPE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeometryDef = new GeometryDefClass(); IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit; pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; //为FeatureClass赋参考系,不写会出错*************************************** pGeometryDefEdit.SpatialReference_2 = map.SpatialReference; //************************************************************************ pFieldEdit.GeometryDef_2 = pGeometryDef; pFieldsEdit.AddField(pField); pField = new FieldClass();//不要省略写!容易出问题 pFieldEdit = pField as IFieldEdit; pFieldEdit.AliasName_2 = "高程"; pFieldEdit.Name_2 = "CoordinateZ"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldsEdit.AddField(pField); pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(Application.StartupPath + "\\tempRaster", 0) as IFeatureWorkspace; IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass("points", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); //IWorkspaceFactory pWorkSpaceFac = new ShapefileWorkspaceFactoryClass(); //IFeatureWorkspace pFeatureWorkSpace = pWorkSpaceFac.OpenFromFile(shpfolder, 0) as IFeatureWorkspace; ////创建字段集2 //IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass(); //IObjectClassDescription ocDescription = (IObjectClassDescription)fcDescription;//创建必要字段 //IFields fields = ocDescription.RequiredFields; //int shapeFieldIndex = fields.FindField(fcDescription.ShapeFieldName); //IField field = fields.get_Field(shapeFieldIndex); //IGeometryDef geometryDef = field.GeometryDef; //IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; ////geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; ////geometryDefEdit.SpatialReference_2 = spatialReference; //geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; //ISpatialReferenceFactory pSpatialRefFac = new SpatialReferenceEnvironmentClass(); //IProjectedCoordinateSystem pcsSys = pSpatialRefFac.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_Zone_39); //geometryDefEdit.SpatialReference_2 = pcsSys; //IFieldChecker fieldChecker = new FieldCheckerClass(); //IEnumFieldError enumFieldError = null; //IFields validatedFields = null; //将传入字段 转成 validatedFields //fieldChecker.ValidateWorkspace = (IWorkspace)pFeatureWorkSpace; //fieldChecker.Validate(fields, out enumFieldError, out validatedFields); //pFeatureWorkSpace.CreateFeatureClass(shpname, validatedFields, ocDescription.InstanceCLSID, ocDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDescription.ShapeFieldName, ""); //从vectors中获取散点值 Dictionary <int, IPoint> pointDictionary = new Dictionary <int, IPoint>(); for (int i = 0; i < vectors.Length; i++) { IPoint pPoint = new PointClass(); pPoint.X = vectors[i].X; pPoint.Y = vectors[i].Y; pPoint.Z = vectors[i].Z; if (pPoint.Z.ToString() != "非数字") { pointDictionary.Add(i, pPoint); } } //插入到新建的FeatureClass中 IWorkspaceEdit pWorkspaceEdit = pWorkspace as IWorkspaceEdit; pWorkspaceEdit.StartEditing(true); pWorkspaceEdit.StartEditOperation(); IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer(); IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true); for (int featureNum = 4; featureNum < pointDictionary.Count; featureNum++) { pFeatureBuffer.Shape = pointDictionary[featureNum] as IPoint; //出错点,在于新建字段的错误 pFeatureBuffer.set_Value(pFeatureClass.Fields.FindField("CoordinateZ"), pointDictionary[featureNum].Z); pFeatureCursor.InsertFeature(pFeatureBuffer); } pFeatureCursor.Flush(); pWorkspaceEdit.StopEditOperation(); pWorkspaceEdit.StopEditing(true); return(pFeatureClass); }
///<summary> ///�ڳ�������ʱ���ڴ��д���ʸ��Ҫ�ز㣬���ӵ���ͼ�ؼ���� ///</summary> ///<param name="pMapCtrl">��ͼ�ؼ�</param> ///<returns>IFeatureLayer �¼ӵ�Ҫ�ز�</returns> public IFeatureLayer AddFeatureLayerByMemoryWS(AxMapControl pMapCtrl, ISpatialReference pSReference) { try { if (pMapCtrl == null) return null; #region �����µ��ڴ湤���ռ� IWorkspaceFactory pWSF = new InMemoryWorkspaceFactoryClass(); IWorkspaceName pWSName = pWSF.Create("", "Temp", null, 0); IName pName = (IName)pWSName; IWorkspace pMemoryWS = (IWorkspace)pName.Open(); #endregion IField oField = new FieldClass(); IFields oFields = new FieldsClass(); IFieldsEdit oFieldsEdit = null; IFieldEdit oFieldEdit = null; IFeatureClass oFeatureClass = null; IFeatureLayer oFeatureLayer = null; try { oFieldsEdit = oFields as IFieldsEdit; oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "OBJECTID"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; oFieldEdit.IsNullable_2 = false; oFieldEdit.Required_2 = false; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef; pGeoDefEdit.AvgNumPoints_2 = 5; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeoDefEdit.GridCount_2 = 1; pGeoDefEdit.HasM_2 = false; pGeoDefEdit.HasZ_2 = false; pGeoDefEdit.SpatialReference_2 = pSReference; oFieldEdit.Name_2 = "SHAPE"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; oFieldEdit.GeometryDef_2 = pGeoDef; oFieldEdit.IsNullable_2 = true; oFieldEdit.Required_2 = true; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "Code"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger; oFieldEdit.IsNullable_2 = true; oFieldsEdit.AddField(oField); //����Ҫ���� oFeatureClass = (pMemoryWS as IFeatureWorkspace).CreateFeatureClass("Temp", oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); oFeatureLayer = new FeatureLayerClass(); oFeatureLayer.Name = "TransTemp"; oFeatureLayer.FeatureClass = oFeatureClass; //����Ψһֵ���Ż����� IUniqueValueRenderer pURender = new UniqueValueRendererClass(); pURender.FieldCount = 1; pURender.set_Field(0, "Code"); pURender.UseDefaultSymbol = false; ISimpleFillSymbol pFillSym = new SimpleFillSymbolClass(); pFillSym.Style = esriSimpleFillStyle.esriSFSSolid; //������ɫ IRgbColor pColor = new RgbColorClass(); pColor.Red = 255; pColor.Green = 255; pFillSym.Color = pColor; pURender.AddValue("1", "", pFillSym as ISymbol); pFillSym = new SimpleFillSymbolClass(); pFillSym.Style = esriSimpleFillStyle.esriSFSSolid; //Ψһֵ���Ż��ڴ�ͼ�� (oFeatureLayer as IGeoFeatureLayer).Renderer = pURender as IFeatureRenderer; ILayerEffects pLyrEffect = oFeatureLayer as ILayerEffects; //���� pLyrEffect.Transparency = 80; } catch (Exception Err) { MessageBox.Show(Err.Message); } finally { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(oField); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFields); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldsEdit); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldEdit); System.Runtime.InteropServices.Marshal.ReleaseComObject(pName); System.Runtime.InteropServices.Marshal.ReleaseComObject(pWSF); System.Runtime.InteropServices.Marshal.ReleaseComObject(pWSName); System.Runtime.InteropServices.Marshal.ReleaseComObject(pMemoryWS); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFeatureClass); } catch { } GC.Collect(); } return oFeatureLayer; } catch (Exception Err) { MessageBox.Show(Err.Message, "��ʾ", MessageBoxButtons.OK, MessageBoxIcon.Information); return null; } }
private void 添加点型图层ToolStripMenuItem_Click(object sender, EventArgs e) { //创建要素类 #region 创建新的内存工作空间 IWorkspaceFactory pWSF = new InMemoryWorkspaceFactoryClass(); IWorkspaceName pWSName = pWSF.Create("", "Temp", null, 0); IName pName = (IName)pWSName; IWorkspace pMemoryWS = (IWorkspace)pName.Open(); #endregion IField oField = new FieldClass(); IFields oFields = new FieldsClass(); IFieldsEdit oFieldsEdit = null; IFieldEdit oFieldEdit = null; IFeatureClass oFeatureClass = null; IFeatureLayer oFeatureLayer = null; oFieldsEdit = oFields as IFieldsEdit; oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "OBJECTID"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; oFieldEdit.IsNullable_2 = false; oFieldEdit.Required_2 = false; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef; pGeoDefEdit.AvgNumPoints_2 = 5; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeoDefEdit.GridCount_2 = 1; pGeoDefEdit.HasM_2 = false; pGeoDefEdit.HasZ_2 = false; pGeoDefEdit.SpatialReference_2 = axMapControl1.SpatialReference; oFieldEdit.Name_2 = "SHAPE"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; oFieldEdit.GeometryDef_2 = pGeoDef; oFieldEdit.IsNullable_2 = true; oFieldEdit.Required_2 = true; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "Code"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger; //oFieldEdit.Length = 10; oFieldEdit.IsNullable_2 = true; oFieldsEdit.AddField(oField); //创建要素类 oFeatureClass = (pMemoryWS as IFeatureWorkspace).CreateFeatureClass("Temp", oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); oFeatureLayer = new FeatureLayerClass(); oFeatureLayer.Name = "PointLayer"; oFeatureLayer.FeatureClass = oFeatureClass; //创建唯一值符号化对象 IUniqueValueRenderer pURender = new UniqueValueRendererClass(); pURender.FieldCount = 1; pURender.set_Field(0, "Code"); pURender.UseDefaultSymbol = false; //创建SimpleMarkerSymbolClass对象 ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbolClass(); //创建RgbColorClass对象为pSimpleMarkerSymbol设置颜色 IRgbColor pRgbColor = new RgbColorClass(); pRgbColor.Red = 255; pSimpleMarkerSymbol.Color = pRgbColor as IColor; //设置pSimpleMarkerSymbol对象的符号类型,选择钻石 pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond; //设置pSimpleMarkerSymbol对象大小,设置为5 pSimpleMarkerSymbol.Size = 5; //显示外框线 pSimpleMarkerSymbol.Outline = true; //为外框线设置颜色 IRgbColor pLineRgbColor = new RgbColorClass(); pLineRgbColor.Green = 255; pSimpleMarkerSymbol.OutlineColor = pLineRgbColor as IColor; //设置外框线的宽度 pSimpleMarkerSymbol.OutlineSize = 1; //半透明颜色 pURender.AddValue("1", "", pSimpleMarkerSymbol as ISymbol); //唯一值符号化内存图层 (oFeatureLayer as IGeoFeatureLayer).Renderer = pURender as IFeatureRenderer; ILayerEffects pLyrEffect = oFeatureLayer as ILayerEffects; //透明度 pLyrEffect.Transparency = 0; oFeatureLayer.Visible = true; this.axMapControl1.AddLayer(oFeatureLayer, axMapControl1.LayerCount); insertpoint = true; }
private ESRI.ArcGIS.Geodatabase.IFeatureClass CreatePointFeatureClass(System.String featureClassName, ESRI.ArcGIS.Geodatabase.IFields fields, ESRI.ArcGIS.esriSystem.UID CLSID) { if (featureClassName == "") return null; // name was not passed in IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); // Create a new in-memory workspace. This returns a name object. IWorkspaceName workspaceName = workspaceFactory.Create(null, "OSMPointsWorkspace", null, 0); IName name = (IName)workspaceName; // Open the workspace through the name object. IWorkspace workspace = (IWorkspace)name.Open(); ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = null; ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explicit Cast // assign the class id value if not assigned if (CLSID == null) { CLSID = new ESRI.ArcGIS.esriSystem.UIDClass(); CLSID.Value = "esriGeoDatabase.Feature"; } ESRI.ArcGIS.Geodatabase.IObjectClassDescription objectClassDescription = new ESRI.ArcGIS.Geodatabase.FeatureClassDescriptionClass(); // if a fields collection is not passed in then supply our own if (fields == null) { // create the fields using the required fields method fields = objectClassDescription.RequiredFields; ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; // Explicit Cast // add the OSM ID field IFieldEdit osmIDField = new FieldClass() as IFieldEdit; osmIDField.Name_2 = "OSMID"; osmIDField.Type_2 = esriFieldType.esriFieldTypeString; osmIDField.Length_2 = 20; fieldsEdit.AddField((IField)osmIDField); // add the field for the tag cloud for all other tag/value pairs IFieldEdit osmXmlTagsField = new FieldClass() as IFieldEdit; osmXmlTagsField.Name_2 = "osmTags"; osmXmlTagsField.Type_2 = esriFieldType.esriFieldTypeBlob; fieldsEdit.AddField((IField)osmXmlTagsField); // user, uid, visible, version, changeset, timestamp IFieldEdit osmuserField = new FieldClass() as IFieldEdit; osmuserField.Name_2 = "osmuser"; osmuserField.Type_2 = esriFieldType.esriFieldTypeString; osmuserField.Length_2 = 100; fieldsEdit.AddField((IField) osmuserField); IFieldEdit osmuidField = new FieldClass() as IFieldEdit; osmuidField.Name_2 = "osmuid"; osmuidField.Type_2 = esriFieldType.esriFieldTypeInteger; fieldsEdit.AddField((IField)osmuidField); IFieldEdit osmvisibleField = new FieldClass() as IFieldEdit; osmvisibleField.Name_2 = "osmvisible"; osmvisibleField.Type_2 = esriFieldType.esriFieldTypeString; osmvisibleField.Length_2 = 20; fieldsEdit.AddField((IField)osmvisibleField); IFieldEdit osmversionField = new FieldClass() as IFieldEdit; osmversionField.Name_2 = "osmversion"; osmversionField.Type_2 = esriFieldType.esriFieldTypeSmallInteger; fieldsEdit.AddField((IField)osmversionField); IFieldEdit osmchangesetField = new FieldClass() as IFieldEdit; osmchangesetField.Name_2 = "osmchangeset"; osmchangesetField.Type_2 = esriFieldType.esriFieldTypeInteger; fieldsEdit.AddField((IField)osmchangesetField); IFieldEdit osmtimestampField = new FieldClass() as IFieldEdit; osmtimestampField.Name_2 = "osmtimestamp"; osmtimestampField.Type_2 = esriFieldType.esriFieldTypeDate; fieldsEdit.AddField((IField)osmtimestampField); IFieldEdit osmrelationIDField = new FieldClass() as IFieldEdit; osmrelationIDField.Name_2 = "osmMemberOf"; osmrelationIDField.Type_2 = esriFieldType.esriFieldTypeBlob; fieldsEdit.AddField((IField)osmrelationIDField); IFieldEdit hasOSMTagsField = new FieldClass() as IFieldEdit; hasOSMTagsField.Name_2 = "hasOSMTags"; IFieldEdit osmSupportingElementField = new FieldClass() as IFieldEdit; osmSupportingElementField.Name_2 = "osmSupportingElement"; osmSupportingElementField.Type_2 = esriFieldType.esriFieldTypeString; osmSupportingElementField.Length_2 = 5; fieldsEdit.AddField((IField) osmSupportingElementField); IFieldEdit wayRefCountField = new FieldClass() as IFieldEdit; wayRefCountField.Name_2 = "wayRefCount"; wayRefCountField.Type_2 = esriFieldType.esriFieldTypeInteger; wayRefCountField.DefaultValue_2 = 0; fieldsEdit.AddField((IField)wayRefCountField); fields = (ESRI.ArcGIS.Geodatabase.IFields)fieldsEdit; // Explicit Cast } System.String strShapeField = ""; // locate the shape field for (int j = 0; j < fields.FieldCount; j++) { if (fields.get_Field(j).Type == ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry) { strShapeField = fields.get_Field(j).Name; // redefine geometry type IFieldEdit shapeField = fields.get_Field(j) as IFieldEdit; IGeometryDefEdit geometryDef = new GeometryDefClass() as IGeometryDefEdit; geometryDef.GeometryType_2 = esriGeometryType.esriGeometryPoint; geometryDef.HasZ_2 = false; geometryDef.HasM_2 = false; geometryDef.GridCount_2 = 1; geometryDef.set_GridSize(0, 1); ISpatialReferenceFactory spatialRefFactory = new SpatialReferenceEnvironmentClass(); ISpatialReference wgs84 = spatialRefFactory.CreateGeographicCoordinateSystem((int) esriSRGeoCSType.esriSRGeoCS_WGS1984) as ISpatialReference; geometryDef.SpatialReference_2 = wgs84; shapeField.GeometryDef_2 = (IGeometryDef)geometryDef; break; } } // Use IFieldChecker to create a validated fields collection. ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass(); ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null; ESRI.ArcGIS.Geodatabase.IFields validatedFields = null; fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)workspace; fieldChecker.Validate(fields, out enumFieldError, out validatedFields); // The enumFieldError enumerator can be inspected at this point to determine // which fields were modified during validation. // finally create and return the feature class try { featureClass = featureWorkspace.CreateFeatureClass(featureClassName, validatedFields, CLSID, null, ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple, strShapeField, ""); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } return featureClass; }
public IWorkspace CreateInMemoryWorkspace() { IWorkspaceFactory workspaceFactory = null; IWorkspaceName workspaceName = null; IName name = null; IWorkspace workspace = null; try { // Create an InMemory workspace factory. workspaceFactory = new InMemoryWorkspaceFactoryClass(); // Create an InMemory geodatabase. workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); // Cast for IName. name = (IName)workspaceName; //Open a reference to the InMemory workspace through the name object. workspace = (IWorkspace)name.Open(); return workspace; } catch { return null; } finally { workspaceFactory = null; workspaceName = null; name = null; } }
/// <summary> /// 在内存中创建图层 /// </summary> /// <param name="DataSetName">数据集名称</param> /// <param name="AliaseName">别名</param> /// <param name="SpatialRef">空间参考</param> /// <param name="GeometryType">几何类型</param> /// <param name="PropertyFields">属性字段集合</param> /// <returns>IfeatureLayer</returns> public static IFeatureLayer CreateFeatureLayerInmemeory(string DataSetName, string AliaseName, ISpatialReference SpatialRef, esriGeometryType GeometryType, IFields PropertyFields) { IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); ESRI.ArcGIS.Geodatabase.IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); ESRI.ArcGIS.esriSystem.IName name = (IName)workspaceName; ESRI.ArcGIS.Geodatabase.IWorkspace inmemWor = (IWorkspace)name.Open(); IField oField = new FieldClass(); IFields oFields = new FieldsClass(); IFieldsEdit oFieldsEdit = null; IFieldEdit oFieldEdit = null; IFeatureClass oFeatureClass = null; IFeatureLayer oFeatureLayer = null; try { oFieldsEdit = oFields as IFieldsEdit; oFieldEdit = oField as IFieldEdit; for (int i = 0; i < PropertyFields.FieldCount; i++) { oFieldsEdit.AddField(PropertyFields.get_Field(i)); } IGeometryDef geometryDef = new GeometryDefClass(); IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; geometryDefEdit.AvgNumPoints_2 = 5; geometryDefEdit.GeometryType_2 = GeometryType; geometryDefEdit.GridCount_2 = 1; geometryDefEdit.HasM_2 = false; geometryDefEdit.HasZ_2 = false; geometryDefEdit.SpatialReference_2 = SpatialRef; oFieldEdit.Name_2 = "SHAPE"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; oFieldEdit.GeometryDef_2 = geometryDef; oFieldEdit.IsNullable_2 = true; oFieldEdit.Required_2 = true; oFieldsEdit.AddField(oField); oFeatureClass = (inmemWor as IFeatureWorkspace).CreateFeatureClass(DataSetName, oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); (oFeatureClass as IDataset).BrowseName = DataSetName; oFeatureLayer = new FeatureLayerClass(); oFeatureLayer.Name = AliaseName; oFeatureLayer.FeatureClass = oFeatureClass; } catch { } finally { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(oField); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFields); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldsEdit); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldEdit); System.Runtime.InteropServices.Marshal.ReleaseComObject(name); System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceFactory); System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceName); System.Runtime.InteropServices.Marshal.ReleaseComObject(inmemWor); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFeatureClass); } catch { } GC.Collect(); } return(oFeatureLayer); }
///<summary> ///在程序运行时的内存中创建矢量要素层,并加到地图控件最顶端 ///</summary> ///<param name="pMapCtrl">地图控件</param> ///<returns>IFeatureLayer 新加的要素层</returns> public IFeatureLayer AddFeatureLayerByMemoryWS(AxMapControl pMapCtrl, ISpatialReference pSReference) { try { if (pMapCtrl == null) { return(null); } #region 创建新的内存工作空间 IWorkspaceFactory pWSF = new InMemoryWorkspaceFactoryClass(); IWorkspaceName pWSName = pWSF.Create("", "Temp", null, 0); IName pName = (IName)pWSName; IWorkspace pMemoryWS = (IWorkspace)pName.Open(); #endregion IField oField = new FieldClass(); IFields oFields = new FieldsClass(); IFieldsEdit oFieldsEdit = null; IFieldEdit oFieldEdit = null; IFeatureClass oFeatureClass = null; IFeatureLayer oFeatureLayer = null; try { oFieldsEdit = oFields as IFieldsEdit; oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "OBJECTID"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; oFieldEdit.IsNullable_2 = false; oFieldEdit.Required_2 = false; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef; pGeoDefEdit.AvgNumPoints_2 = 5; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeoDefEdit.GridCount_2 = 1; pGeoDefEdit.HasM_2 = false; pGeoDefEdit.HasZ_2 = false; pGeoDefEdit.SpatialReference_2 = pSReference; oFieldEdit.Name_2 = "SHAPE"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; oFieldEdit.GeometryDef_2 = pGeoDef; oFieldEdit.IsNullable_2 = true; oFieldEdit.Required_2 = true; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "Code"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger; oFieldEdit.IsNullable_2 = true; oFieldsEdit.AddField(oField); //创建要素类 oFeatureClass = (pMemoryWS as IFeatureWorkspace).CreateFeatureClass("Temp", oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); oFeatureLayer = new FeatureLayerClass(); oFeatureLayer.Name = "TransTemp"; oFeatureLayer.FeatureClass = oFeatureClass; //创建唯一值符号化对象 IUniqueValueRenderer pURender = new UniqueValueRendererClass(); pURender.FieldCount = 1; pURender.set_Field(0, "Code"); pURender.UseDefaultSymbol = false; ISimpleFillSymbol pFillSym = new SimpleFillSymbolClass(); pFillSym.Style = esriSimpleFillStyle.esriSFSSolid; //半透明颜色 IRgbColor pColor = new RgbColorClass(); pColor.Red = 255; pColor.Green = 255; pFillSym.Color = pColor; pURender.AddValue("1", "", pFillSym as ISymbol); pFillSym = new SimpleFillSymbolClass(); pFillSym.Style = esriSimpleFillStyle.esriSFSSolid; //唯一值符号化内存图层 (oFeatureLayer as IGeoFeatureLayer).Renderer = pURender as IFeatureRenderer; ILayerEffects pLyrEffect = oFeatureLayer as ILayerEffects; //透明度 pLyrEffect.Transparency = 80; } catch (Exception Err) { MessageBox.Show(Err.Message); } finally { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(oField); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFields); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldsEdit); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldEdit); System.Runtime.InteropServices.Marshal.ReleaseComObject(pName); System.Runtime.InteropServices.Marshal.ReleaseComObject(pWSF); System.Runtime.InteropServices.Marshal.ReleaseComObject(pWSName); System.Runtime.InteropServices.Marshal.ReleaseComObject(pMemoryWS); System.Runtime.InteropServices.Marshal.ReleaseComObject(oFeatureClass); } catch { } GC.Collect(); } return(oFeatureLayer); } catch (Exception Err) { MessageBox.Show(Err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return(null); } }
private ITable CreateArcTable(DataTable dt, bool regionIsInt) { IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); IName name = (IName)workspaceName; IFeatureWorkspace workspace = (IFeatureWorkspace)name.Open(); IFieldsEdit fe; fe = new FieldsClass(); IField oid, key, measure; oid = createField("ObjectId", esriFieldType.esriFieldTypeOID); if (regionIsInt) { key = createField("Region", esriFieldType.esriFieldTypeInteger); } else { key = createField("Region", esriFieldType.esriFieldTypeString); } measure = createField("Measure", esriFieldType.esriFieldTypeDouble); fe.AddField(oid); fe.AddField(key); fe.AddField(measure); UID clsid = new UIDClass(); clsid.Value = "esriGeoDatabase.Object"; ITable table = workspace.CreateTable("joinedTable", (IFields)fe, clsid, null, null); IRowBuffer rowBuffer = table.CreateRowBuffer(); ICursor cursor = table.Insert(true); foreach (DataRow dr in dt.Rows) { /*Set the Region and the Measure fields The dataTable has 2 columns and the ArcTable has 3 columns. The first column in the OID field which is generated internally by the ArcGIS engine.*/ for (int x = 1; x <= dt.Columns.Count; x++) { rowBuffer.set_Value(x, dr[x-1]); } cursor.InsertRow(rowBuffer); } cursor.Flush(); return table; }
protected void DoClip(IActiveView activeView, IFeatureLayer ifl_active, IGeometry geometry) { try { ESRI.ArcGIS.Carto.IMap map = activeView.FocusMap; ESRI.ArcGIS.Carto.ILayerFile layerFile = new ESRI.ArcGIS.Carto.LayerFileClass(); ISpatialFilter isf = new SpatialFilterClass(); isf.Geometry = geometry; isf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; gd.Title = "Save clipped feature class"; gd.ObjectFilter = new GxFilterFeatureClassesClass(); //new GxFilterFeatureClassesClass(); if (gd.DoModalSave(ArcMap.Application.hWnd) == false) { return; } while (System.IO.File.Exists(gd.FinalLocation.FullName + "\\" + gd.Name) || gd.ReplacingObject) { if (System.Windows.Forms.MessageBox.Show("You've selected a feature class that already exists. Select a different feature class name.", "Overwrite Feature Class", System.Windows.Forms.MessageBoxButtons.RetryCancel) == System.Windows.Forms.DialogResult.Cancel) { return; } if (gd.DoModalSave(ArcMap.Application.hWnd) == false) { return; } } // Create a new in-memory workspace. This returns a name object. InMemoryWorkspaceFactory wsf = new InMemoryWorkspaceFactoryClass(); IWorkspaceName workspaceName = wsf.Create(null, "MyWorkspace", null, 0); IName name = (IName)workspaceName; IFeatureWorkspace workspace; IWorkspaceEdit iwe; IFields flds; setFeatureSpatialReference(ifl_active, name, out workspace, out iwe, out flds); IFeatureClass ifc_new = workspace.CreateFeatureClass("AAA", flds, null, null, esriFeatureType.esriFTSimple, ifl_active.FeatureClass.ShapeFieldName, ""); IFeatureLayer fl = new FeatureLayerClass(); IGeoFeatureLayer gfl = (IGeoFeatureLayer)fl; IRgbColor rgbColor = new RgbColorClass(); rgbColor.Red = 255; rgbColor.Green = 0; rgbColor.Blue = 0; IColor color = rgbColor; // Implicit Cast fl.FeatureClass = ifc_new; fl.Name = "IntersectingShape"; ISimpleFillSymbol sfs = new SimpleFillSymbolClass(); ISimpleLineSymbol sls = new SimpleLineSymbolClass(); sls.Color = color; sls.Width = 4.0; sls.Style = esriSimpleLineStyle.esriSLSSolid; color.NullColor = true; sfs.Color = color; sfs.Outline = sls; ISimpleRenderer isr = new SimpleRendererClass(); isr.Symbol = (ISymbol)sfs; gfl.Renderer = (IFeatureRenderer)isr; IObjectCopy cpy = new ObjectCopyClass(); iwe.StartEditing(true); iwe.StartEditOperation(); IFeatureBuffer fb = ifc_new.CreateFeatureBuffer(); IFeatureCursor csri = ifc_new.Insert(false); fb.Shape = geometry; csri.InsertFeature(fb); csri.Flush(); iwe.StopEditOperation(); iwe.StopEditing(true); map.AddLayer(fl); ESRI.ArcGIS.AnalysisTools.Clip tool = new ESRI.ArcGIS.AnalysisTools.Clip(); tool.clip_features = fl; tool.in_features = ifl_active; tool.out_feature_class = gd.FinalLocation.FullName + "\\" + gd.Name; /*ws.PathName*/ //"In_memory" + "\\NWI_Clip_Result"; gp.AddOutputsToMap = true; gp.OverwriteOutput = true; gp.ToolExecuted += new EventHandler<ToolExecutedEventArgs>(gp_ToolExecuted); gp.ProgressChanged += new EventHandler<ProgressChangedEventArgs>(gp_ProgressChanged); gp.ExecuteAsync(tool); } catch (Exception err) { } finally { SelectArrowToolOnToolbar(); } }
/// <summary> /// Join Complex Index Table /// </summary> /// <param name="layer"></param> /// <param name="year"></param> /// <author>Shen Yongyuan</author> /// <date>20091111</date> public static void JoinComplexTable(ILayer layer, int year, string city, bool normal) { //Add Fields IFieldsEdit allFields = new FieldsClass(); IFieldEdit field1 = new FieldClass(); field1.Name_2 = "ID"; field1.Type_2 = esriFieldType.esriFieldTypeString; allFields.AddField(field1); IFieldEdit field2 = new FieldClass(); field2.Name_2 = "Ecology"; field2.Type_2 = esriFieldType.esriFieldTypeDouble; allFields.AddField(field2); IFieldEdit field3 = new FieldClass(); field3.Name_2 = "Social"; field3.Type_2 = esriFieldType.esriFieldTypeDouble; allFields.AddField(field3); IFieldEdit field4 = new FieldClass(); field4.Name_2 = "Economic"; field4.Type_2 = esriFieldType.esriFieldTypeDouble; allFields.AddField(field4); //Create Table IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("", "Temporary WorkSpace In Memory", null, 0); IFeatureWorkspace workspace = ((IName)workspaceName).Open() as IFeatureWorkspace; ITable table = workspace.CreateTable(Town.Constant.Constant.TmpTableIndex, allFields, null, null, ""); //Import Data IWorkspaceEdit workspaceEdit = workspace as IWorkspaceEdit; workspaceEdit.StartEditing(false); workspaceEdit.StartEditOperation(); ICursor cursor = table.Insert(true); int fldField1 = cursor.Fields.FindField("ID"); int fldField2 = cursor.Fields.FindField("Ecology"); int fldField3 = cursor.Fields.FindField("Social"); int fldField4 = cursor.Fields.FindField("Economic"); IQueryable <DataLib.I_INDEX> indexCollect = DataLib.DA_Index.QueryIndex(year, city); List <int> OIDList = new List <int>(); foreach (DataLib.I_INDEX i in indexCollect) { if (OIDList.Contains(i.I_ZONE.OID) == false) { double EcologyValue = indexCollect.Where(c => c.Name.Equals("生态子系统综合指标") && c.Zone == i.Zone).Single().Value; double SocialValue = indexCollect.Where(c => c.Name.Equals("社会子系统综合指标") && c.Zone == i.Zone).Single().Value; double EconomicValue = indexCollect.Where(c => c.Name.Equals("经济子系统综合指标") && c.Zone == i.Zone).Single().Value; //if (normal == true) //{ // EcologyValue += Math.Abs(DataLib.DA_Index.QueryIndex("生态子系统综合指标", city).Min(c => c.Value)) + 1; // SocialValue += Math.Abs(DataLib.DA_Index.QueryIndex("社会子系统综合指标", city).Min(c => c.Value)) + 1; // EconomicValue += Math.Abs(DataLib.DA_Index.QueryIndex("经济子系统综合指标", city).Min(c => c.Value)) + 1; //} EcologyValue = Math.Abs(EcologyValue); SocialValue = Math.Abs(SocialValue); EconomicValue = Math.Abs(EconomicValue); IRowBuffer buffer = table.CreateRowBuffer(); buffer.set_Value(fldField1, i.I_ZONE.OID); buffer.set_Value(fldField2, EcologyValue); buffer.set_Value(fldField3, SocialValue); buffer.set_Value(fldField4, EconomicValue); cursor.InsertRow(buffer); OIDList.Add(i.I_ZONE.OID); } } cursor.Flush(); workspaceEdit.StartEditOperation(); workspaceEdit.StopEditing(true); System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor); //Join IMemoryRelationshipClassFactory pMemRelFact = new MemoryRelationshipClassFactory(); IFeatureLayer feaLayer = layer as IFeatureLayer; ITable originTable = feaLayer.FeatureClass as ITable; IRelationshipClass pRelClass = pMemRelFact.Open("Join", originTable as IObjectClass, "ID", table as IObjectClass, "ID", "forward", "backward", esriRelCardinality.esriRelCardinalityOneToOne); IDisplayRelationshipClass pDispRC = feaLayer as IDisplayRelationshipClass; pDispRC.DisplayRelationshipClass(null, esriJoinType.esriLeftOuterJoin); pDispRC.DisplayRelationshipClass(pRelClass, esriJoinType.esriLeftOuterJoin); }