码迷,mamicode.com
首页 > 数据库 > 详细

根据SQL Server中的数据向矢量图层中添加点对象

时间:2016-08-05 00:29:24      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

  1. SQL Server中的数据已有坐标信息(LAT,LNG)
  2. 在FormLoad事件中从SQL里获取数据并存储在DataSet。
 //在顶部定义SqlConnection对象
 // public static string conStr = "server=(local);database=DbName;uid=sa;pwd=123";
 //public SqlConnection conn = new SqlConnection(conStr);
            conn.Open();//打开数据库连接
            if (conn.State != ConnectionState.Open)
            {
                MessageBox.Show("数据库连接失败!");
            }
            SqlCommand cmd = new SqlCommand("此处撰写select语句(对无效数据进行过滤)", conn);
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds, "AQI");
            DataTable tblAQI = ds.Tables["AQI"];//AQI表
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "AQI";
            conn.Close();            

3.打开工作空间,调用PointEditor方法

//图层编辑
            IMap pMap2 = axMapControl1.Map;
            pLayer = null;
            pLayer = pMap2.get_Layer(0);//获取目标图层
            IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
            IFeatureClass pFeatureclass = pFeatureLayer.FeatureClass;
            //开启编辑空间
            IWorkspace IW = ((IDataset)pFeatureclass).Workspace;
            IWorkspaceEdit IWE = IW as IWorkspaceEdit;
            bool startEdit = IWE.IsBeingEdited();
            if (!startEdit)
            {
                IWE.StartEditing(false);
            }
            IWE.StartEditOperation();
            //添加站点要素
            for (int j = 0; j < tblAQI.Rows.Count; j++)
            {
                levelinfo leinfo = new levelinfo();//结构体levelinfo,用于暂时存储数据
                leinfo.Name2 = tblAQI.Rows[j][19].ToString();
                leinfo.Loacx = float.Parse(tblAQI.Rows[j][10].ToString());
                leinfo.Loacy = float.Parse(tblAQI.Rows[j][9].ToString());
                leinfo.AQI = float.Parse(tblAQI.Rows[j][0].ToString());
                leinfo.code = tblAQI.Rows[j][18].ToString();
                leinfo.Level = fenji(leinfo.AQI);
                pointEditor(leinfo.code, leinfo.Level, pFeatureclass, leinfo.Loacx, leinfo.Loacy, leinfo .Name2 );
                pointEditor2(leinfo.code, leinfo.AQI, pFeatureclass);
                IActiveView pActiveView = pMap2 as IActiveView;
                pActiveView.Refresh();
            }
            fenjiZS();//分级渲染
            IWE.StopEditOperation();
            startEdit = IWE.IsBeingEdited();
            if (!startEdit)
            {
                IWE.StopEditing(true);
            }
            axMapControl1.Refresh();

PointEditor方法

 public void pointEditor(string nameTo, int ValueToSet, IFeatureClass pFeatureclass, float x, float y, string name2)//修改AQIType属性的方法
        {
            try
            {
                IFields pFields1 = pFeatureclass.Fields;
                IQueryFilter queryFilter = new QueryFilterClass();
                queryFilter.WhereClause = "code = ‘" + nameTo + "";//查询的字段属性 
                IFeatureCursor featurecursor = pFeatureclass.Search(queryFilter, false);
                IFeature feature = featurecursor.NextFeature();
                feature.set_Value(pFields1.FindField("AQIType"), ValueToSet);
                feature.Store();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(featurecursor);
            }
            catch
            {
                IFeature pFeature;
                IFeatureClassWrite pFeatureClassW = pFeatureclass as IFeatureClassWrite;
                IPoint pPoint = new PointClass();
                pPoint.SpatialReference = this.axMapControl1.SpatialReference;
                pPoint.PutCoords(x, y);
                IGeometry pGeo;
                pGeo = pPoint;
                pFeature = pFeatureclass.CreateFeature();
                pFeature.Shape = pGeo;
                pFeature.set_Value(5, nameTo );
                pFeature.set_Value(7, ValueToSet);
                pFeature.set_Value(6, name2);
                pFeature.Store();
            }
        }

PointEditor2方法

public void pointEditor2(string nameTo, float ValueToSet, IFeatureClass pFeatureclass)//修改AQI属性的方法
        {
            if (ValueToSet == -999)
            {
                ValueToSet = 1;
            }
            IFields pFields1 = pFeatureclass.Fields;
            IQueryFilter queryFilter = new QueryFilterClass();
            queryFilter.WhereClause = "code = ‘" + nameTo + "";//查询的字段属性
            IFeatureCursor featurecursor = pFeatureclass.Search(queryFilter, false);
            IFeature feature = featurecursor.NextFeature();
            feature.set_Value(pFields1.FindField("AQI"), ValueToSet);
            feature.Store();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(featurecursor);
        }

 

根据SQL Server中的数据向矢量图层中添加点对象

标签:

原文地址:http://www.cnblogs.com/MansonsBlog/p/5738854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!