MapXtreme 四项基本功(增删改查)

/// <summary>

        /// 向图层中添加点
        /// <param name="tempLayerTableName">表名</param>
        /// <param name="tempLayerName">图层名</param>
        /// <param name="dPoint">点坐标</param>
        public void AddPointToLayer(string tempLayerTableName, string tempLayerName, List<string> list)
        {
            DPoint dPoint = new DPoint(double.Parse(list[list.Count - 2]), double.Parse(list[list.Count - 1]));
            MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[mapControl1.Map.Alias];
            //获取图层和表
            FeatureLayer workLayer = (MapInfo.Mapping.FeatureLayer)myMap.Layers[tempLayerName];
            MapInfo.Data.Table tblTemp = MapInfo.Engine.Session.Current.Catalog.GetTable(tempLayerTableName);
 
            //创建点图元及其样式
            FeatureGeometry pgPoint = new MapInfo.Geometry.Point(workLayer.CoordSys, dPoint);
            MapInfo.Styles.SimpleVectorPointStyle spsPoint = new MapInfo.Styles.SimpleVectorPointStyle(37, System.Drawing.Color.Blue, 40);
            MapInfo.Styles.CompositeStyle csPoint = new MapInfo.Styles.CompositeStyle(spsPoint);
            MapInfo.Data.Feature feature = new MapInfo.Data.Feature(tblTemp.TableInfo.Columns);
            feature.Geometry = pgPoint;
            feature.Style = csPoint;
 
            feature[feature.Columns[6].Alias] = "xxx";//图元属性赋值
            feature[feature.Columns[7].Alias] = "YYYY";//图元属性赋值
 
            workLayer.Table.InsertFeature(feature);
        }
//*******************************************************
//删除图元
Key key = new Key();
        key.Value = featureKey;//featureKey为图元键值
        mapTable.DeleteFeature(key);//mapTable表
 
//*******************************************************
/// <summary>
        /// 通过Search方法查找图元
        /// <param name="tableName">查找的表名</param>
        /// <param name="sbuilder">查找条件</param>
         public void SearchWithSearch(Table table, StringBuilder sbuilder)
        {
            #region
            MapInfo.Mapping.Map map = mapControl1.Map;
            IResultSetFeatureCollection ifs = null;
            SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchWhere(sbuilder.ToString());
            try
            {
                ifs = Session.Current.Catalog.Search(table, si);
 
                MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear();
                if (ifs.Count > 0)
                {
                    //高亮显示
                    MapInfo.Engine.Session.Current.Selections.DefaultSelection.Add(ifs);
                    if (ifs.Count == 1)
                    {
                        map.Center = new DPoint(ifs[0].Geometry.Centroid.x, ifs[0].Geometry.Centroid.y);
                        map.Zoom = new MapInfo.Geometry.Distance(0.5, map.Zoom.Unit);
                    }
                    //设置高亮显示的样式
                    ((SimpleInterior)MapInfo.Engine.Session.Current.Selections.DefaultSelection.Style.AreaStyle.Interior).BackColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception ex)
            {
                string eee = ex.Message;
            }
            finally
            {
                if (ifs != null)
                {
                    ifs.Close();
                }
            }
            #endregion
        }
 
使用方法:
                StringBuilder searchWhere = new StringBuilder();
                string keyStr = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                searchWhere.Append("MI_Key");
                searchWhere.Append("='");
                searchWhere.Append(keyStr);
                searchWhere.Append("'");
                SearchWithSearch(mapTable, searchWhere);
 
//*******************************************************
        /// <summary>
        /// 修改图元
        /// </summary>
        private void updatefea()
        {
            DPoint featureCoord = new DPoint();
            featureCoord = feature.Geometry.Centroid;
            double x=0.0;
            double y=0.0;
            for (int i = 0; i < panel1.Controls.Count; i++)
            {
                Control control = panel1.Controls[i];
                if (control is TextBox)
                {
                    TextBox txtbox = (TextBox)control;
                    if (txtbox.Enabled)
                    {
                        for (int j = 0; j < feature.Columns.Count; j++)
                        {
                            if ("txt" + feature.Columns[j].Alias == txtbox.Name)
                            {
                                //修改图元属性值
                                feature[feature.Columns[j].Alias] = txtbox.Text;
                            }
                        }
                        if (txtbox.Name == "txtLON1")
                        {
                            //修改图元属性值
                            x= double.Parse(txtbox.Text);
                        }
                        if (txtbox.Name == "txtLAT1")
                        {
                            //修改图元属性值
                            y = double.Parse(txtbox.Text);
                        }
                    }
                }
            }
            //更改坐标
            feature.Geometry.GetGeometryEditor().OffsetByXY((x - featureCoord.x), (y - featureCoord.y), DistanceUnit.Degree, DistanceType.Spherical);
            feature.Geometry.EditingComplete();
            DialogResult ret = MessageBox.Show("确定要修改记录么?", "修改图元提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (ret == DialogResult.OK)
            {
                feature.Update();
            }
        }
//********************************
posted on 2013-02-26 11:27  读懂洋字码  阅读(314)  评论(0编辑  收藏  举报