ArcGis 中空间数据的插入与更新
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using ESRI.ArcGIS.Geodatabase; 8 using ESRI.ArcGIS.esriSystem; 9 using ESRI.ArcGIS.Carto; 10 using ESRI.ArcGIS.Geometry; 11 12 public partial class _Default : System.Web.UI.Page 13 { 14 protected void Page_Load(object sender, EventArgs e) 15 { 16 17 } 18 19 20 public static void ArcGisData() 21 { 22 //ESRI.ArcGIS 设置属性【嵌入互操作类型:False】 23 24 //注册 License 25 IAoInitialize m_aoinitialize = new AoInitializeClass();// new AoInitialize(); 26 // m_aoinitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo); // 10.0以下的ArcGis版本使用该方法 27 m_aoinitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced); // 10.0及以上的ArcGis版本使用该方法 28 29 Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory"); 30 IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); 31 32 //通过IPropertySet设置通过SDE连接数据库的各种参数 33 IPropertySet propertySet = new PropertySetClass(); 34 string arc_server = "192.168.111.111"; 35 string arc_instance = "5151/tcp"; 36 string arc_database = "orcl"; 37 string arc_user = "GIS"; 38 string arc_pwd = "GIS"; 39 string arc_version = "sde.DEFAULT"; 40 propertySet.SetProperty("SERVER", arc_server); 41 propertySet.SetProperty("Instance", arc_instance); 42 propertySet.SetProperty("Database", arc_database); 43 propertySet.SetProperty("User", arc_user); 44 propertySet.SetProperty("Password", arc_pwd); 45 propertySet.SetProperty("Version", arc_version); 46 47 //通过以上设置的参数将数据库的数据通过SDE读入工作空间 48 IWorkspace workspace = workspaceFactory.Open(propertySet, 0); 49 IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; 50 IFeatureLayer layer = new FeatureLayerClass(); 51 layer.Name = "JCSS_aaaaa_PT"; // 操作的表名 52 IFeatureClass feature = featureWorkspace.OpenFeatureClass(layer.Name);//dataSet.Name 53 layer.FeatureClass = feature; 54 55 IPoint pp = new PointClass(); 56 pp.X = 113.1846; 57 pp.Y = 22.8472; 58 59 #region 插入 60 // 插入方法1 61 try 62 { 63 IDataset pDataset = (IDataset)layer.FeatureClass; 64 IWorkspaceEdit pWorkSpaceEdit = (IWorkspaceEdit)pDataset.Workspace; 65 if (!pWorkSpaceEdit.IsBeingEdited()) 66 { 67 pWorkSpaceEdit.StartEditing(true); 68 pWorkSpaceEdit.EnableUndoRedo(); 69 } 70 pWorkSpaceEdit.StartEditOperation(); 71 IFeature pFeature = layer.FeatureClass.CreateFeature(); 72 pFeature.Shape = pp as IGeometry; 73 pFeature.Store(); 74 pWorkSpaceEdit.StopEditOperation(); 75 } 76 catch (Exception ex) 77 { 78 } 79 80 // 插入方法2 81 try 82 { 83 IFeatureCursor feaCursor = feature.Insert(true); 84 for (int i = 0; i < 1; i++) 85 { 86 IFeatureBuffer feaBuffer = feature.CreateFeatureBuffer(); 87 feaBuffer.Shape = pp; 88 feaBuffer.set_Value(feaBuffer.Fields.FindField("MC"), "老张"); 89 feaCursor.InsertFeature(feaBuffer); 90 } 91 feaCursor.Flush(); 92 } 93 catch (Exception) 94 { 95 96 throw; 97 } 98 #endregion 99 100 101 #region 更新 102 IPoint Upp = new PointClass(); 103 Upp.X = 113.1846; 104 Upp.Y = 22.8472; 105 int objectid = 0; 106 // 更新方法1 107 try 108 { 109 IDataset pDataset = (IDataset)layer.FeatureClass; 110 IWorkspaceEdit pWorkSpaceEdit = (IWorkspaceEdit)pDataset.Workspace; 111 if (!pWorkSpaceEdit.IsBeingEdited()) 112 { 113 pWorkSpaceEdit.StartEditing(true); 114 pWorkSpaceEdit.EnableUndoRedo(); 115 } 116 pWorkSpaceEdit.StartEditOperation(); 117 118 IFeature pFeature = layer.FeatureClass.GetFeature(objectid); 119 pFeature.Shape = Upp as IGeometry; 120 pFeature.Store(); 121 pWorkSpaceEdit.StopEditOperation(); 122 } 123 catch (Exception ex) 124 { 125 126 } 127 // 更新方法2 128 try 129 { 130 IDataset pDataset = (IDataset)layer.FeatureClass; 131 IWorkspaceEdit pWorkSpaceEdit = (IWorkspaceEdit)pDataset.Workspace; 132 if (!pWorkSpaceEdit.IsBeingEdited()) 133 { 134 pWorkSpaceEdit.StartEditing(true); 135 pWorkSpaceEdit.EnableUndoRedo(); 136 } 137 pWorkSpaceEdit.StartEditOperation(); 138 IFeature pFeature = layer.FeatureClass.GetFeature(objectid); 139 pFeature.Shape = Upp as IGeometry; 140 pFeature.Store(); 141 pWorkSpaceEdit.StopEditOperation(); 142 } 143 catch (Exception) 144 { 145 146 throw; 147 } 148 #endregion 149 150 151 } 152 153 }