超图举例单值专题图色带样式控制

此文是用.NET Iobject 7c 下利用vs2010开发的winform版 单值专题图的创建及色带的修改。在此记下来以便后期参考(可能很快就会被淹没,超图的更新换代很快)

  初期由于不知道样式的承载控件应该选择那个,测试过listview、listbox,但是效果都不满意,偶然间查到datagridview可以更好的充当这个角色,展示效果做好肯定就要配套的修改功能了,这个修改我咨询过客服,客服说在修改方面,只是给了一个样式修改的接口,我测试过对修改对象进行清空,赋值都不行,对象和地图展示的关联较强;

在测试中发现,若是对专题图对象删除再次添加,所添加的名称和删除图层的名称保持不变,最后只能曲线的进行对图层的删除和重新添加,

----》 第一个窗体为主窗体展示,最后一个窗体为增删值做准备

说明:1 下拉框根据单值的条件筛选填入字段名称

   2 承载数据的控件时datagridview,需要把该控件的列指定特定的值 如图 2 

     3  按钮--添加所有值就是重新根据条件进行读取一次数据并展示出来

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SuperMap.Mapping;
using SuperMap.Data;
using SuperMap.UI;
using soTest.ThematicMap;

namespace SuperMap.SampleCode.Mapping
{
    public partial class frmThemeUnique : Form
    {
        Workspace m_workspace;
        List<ColorGradientType> setColorTheml = null;
        Dataset objLayer = null;
        ThemeUnique objThemeUnique = null;
        MapControl objMapControl = null;
        public frmThemeUnique(Workspace m_workspace, Dataset layer, ThemeUnique objThemeUnique,MapControl objMapControl)
        {
            InitializeComponent();
            this.m_workspace = m_workspace;
            this.objLayer = layer;
            this.objThemeUnique = objThemeUnique;
            this.objMapControl = objMapControl;
            dataGridView2.CellMouseClick += new DataGridViewCellMouseEventHandler(dataGridView2_CellMouseClick);
            dataGridView2.CellMouseDoubleClick += new DataGridViewCellMouseEventHandler(dataGridView2_CellMouseDoubleClick);
        }
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmThemeUnique_Load(object sender, EventArgs e)
        {
            resources = m_workspace.Resources;
           // string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("CurExe"));
            //设置单值专题图数值        
            #region MyRegion            
            setColorTheml = new List<ColorGradientType>();
            setColorTheml.Add(ColorGradientType.Spectrum);
            setColorTheml.Add(ColorGradientType.Terrain);
            setColorTheml.Add(ColorGradientType.GreenOrangeViolet);
            setColorTheml.Add(ColorGradientType.Rainbow);
            setColorTheml.Add(ColorGradientType.CyanGreen);
            setColorTheml.Add(ColorGradientType.CyanBlue);
            //  setColorTheml.Add("青->蓝渐变色", ColorGradientType.PinkBlue);
            setColorTheml.Add(ColorGradientType.PinkRed);
            setColorTheml.Add(ColorGradientType.BlueRed);
            setColorTheml.Add(ColorGradientType.GreenRed);
            setColorTheml.Add(ColorGradientType.GreenBlue);
            setColorTheml.Add(ColorGradientType.YellowBlue);
            setColorTheml.Add(ColorGradientType.YellowGreen);
            setColorTheml.Add(ColorGradientType.YellowRed);
            setColorTheml.Add(ColorGradientType.CyanBlack);
            setColorTheml.Add(ColorGradientType.PinkBlack);
            setColorTheml.Add(ColorGradientType.YellowBlack);
            setColorTheml.Add(ColorGradientType.BlueBlack);
            setColorTheml.Add(ColorGradientType.GreenBlack);
            setColorTheml.Add(ColorGradientType.RedBlack);
            setColorTheml.Add(ColorGradientType.CyanWhite);
            setColorTheml.Add(ColorGradientType.PinkWhite);
            setColorTheml.Add(ColorGradientType.YellowWhite);
            setColorTheml.Add(ColorGradientType.BlueWhite);
            setColorTheml.Add(ColorGradientType.GreenWhite);
            setColorTheml.Add(ColorGradientType.RedWhite);
            setColorTheml.Add(ColorGradientType.BlackWhite);
            #endregion
            dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView2.AllowUserToAddRows = false;
            setDZcombox();
            if (objThemeUnique == null)
            {
                showDZinformation();
            }
            else
            {
                updateThemeUnique(objThemeUnique);
            }
           
        }

        public void updateThemeUnique(ThemeUnique objThemeUnique)
        {
          
            comboBox1.Text = objThemeUnique.UniqueExpression;         
            for (int i = 0; i < objThemeUnique.Count; i++)
            {
                objThemeUniqueItem.Add(objThemeUnique[i]);
                Image im = getImage(objLayer.Type, objThemeUnique[i].Style);
                dataGridView2.Rows.Add(true, im, objThemeUnique[i].Caption);             
            }
        }


        #region 单值专题图
        public void setDZcombox()
        {
            DatasetVector objDv = objLayer as DatasetVector;
            FieldInfos objFled = objDv.FieldInfos;
            FieldInfo objFd = null;
            for (int i = 0; i < objFled.Count; i++)
            {
                objFd = objFled[i];
                comboBox1.Items.Add(objFd.Name.Trim());
            }
            if (comboBox1.Items.Count > 0)
                comboBox1.SelectedIndex = 0;
            objFled = null;
            objDv = null;
        }
        // 设置单值专题图
        ThemeUnique themeunique = null;
        DatasetVector datasetVector = null;
        Geometry objg = null;
        Resources resources = null;
        //单只专题图子项集合
        List<ThemeUniqueItem> objThemeUniqueItem = new List<ThemeUniqueItem>();
        //删除的单值专题图集合
        List<ThemeUniqueItem> objDeleteThemeUniqueItem = new List<ThemeUniqueItem>();
        List<string> list = new List<string>();
        /// <summary>
        /// 单值绑定方法
        /// </summary>
        public void showDZinformation()
        {
            themeunique = new ThemeUnique();
            //清空数据
            objThemeUniqueItem.Clear();
            objDeleteThemeUniqueItem.Clear();
            list.Clear();
            //专题图清空
            themeunique.Clear();
            //列表清空
            dataGridView2.Rows.Clear();
            // 当只有普通图层时,添加专题图层
            datasetVector = objLayer as DatasetVector;
            //设置字段表达式
            themeunique.UniqueExpression = comboBox1.Text.Trim();
            Recordset objRecor = datasetVector.GetRecordset(false, CursorType.Static);
            FieldInfos objFled = datasetVector.FieldInfos;
            objRecor.MoveFirst();
            for (int j = 0; j < objRecor.RecordCount; j++)
            {
                //新建DataTable的行对象             
                if (objRecor.FieldCount == objFled.Count)
                {
                    object value = objRecor.GetFieldValue(comboBox1.SelectedIndex);
                    if (value != null)
                    {
                        if (!string.IsNullOrWhiteSpace(value.ToString().Trim()))
                        {
                            objg = objRecor.GetGeometry();
                            list.Add(value.ToString().Trim());
                        }
                    }
                }
                else
                {
                    // MessageBox.Show("字段数有问题!", "提示");
                }
                objRecor.MoveNext();//移到下个列
            }
            //释放对象
            objRecor.Dispose();
            //专题图子项
            ThemeUniqueItem objItem = null;
            GeoStyle geostyle1 = null;
            Random objRan = new Random();
            // 根据指定的渐变颜色类型,返回一个 Colors 类的对象
            //运行结果是根据指定的渐变颜色返回的4个颜色构成的颜色集合,颜色值为:
            //(R=0,G=255,B=0),(R=0,G=255,B=85),(R=0,G=255,B=170),(R=0,G=255,B=255)
            Colors colors = Colors.MakeGradient(list.Count, ColorGradientType.BlueBlack, false);
            // 得到点线面的RootGroup
            List<string> isTrueAndFalse = new List<string>();
            for (int i = 0; i < list.Count; i++)
            {
                string itemName = list[i];
                objItem = new ThemeUniqueItem();
                objItem.Caption = itemName;
                objItem.IsVisible = true;

                geostyle1 = GeoStyleSample(objLayer.Type, colors[isTrueAndFalse.Count]);// geostyle1;
                objItem.Style = geostyle1;
                objItem.Unique = itemName;
                if (!isTrueAndFalse.Contains(itemName))
                {
                    objThemeUniqueItem.Add(objItem);
                    Image im = getImage(objLayer.Type, geostyle1);
                    dataGridView2.Rows.Add(true, im, itemName);
                    isTrueAndFalse.Add(itemName);
                }
            }
        }
        /// <summary>
        /// 单元格单击事件 此事件主要针对专题图的显示和隐藏进行操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dataGridView2_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dataGridView2.Columns[e.ColumnIndex].Name.Equals("checkbox"))
            {
                bool value = (bool)dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                if (value)
                {
                    dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = false;
                    objThemeUniqueItem[e.RowIndex].IsVisible = false;
                }
                else
                {
                    dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true;
                    objThemeUniqueItem[e.RowIndex].IsVisible = true;
                }
            }
        }
       /// <summary>
       /// 单元格双击事件
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        void dataGridView2_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {          
            if (dataGridView2.Columns[e.ColumnIndex].Name.Equals("imgvalue"))
            {
                //样式符号
                GeoStyle objGeoStyle = SymbolDialog.ShowDialog(m_workspace.Resources, objThemeUniqueItem[e.RowIndex].Style, GetSymbolType(objLayer.Type));
                if (objGeoStyle != null)
                {
                    //更新专题图样式
                    objThemeUniqueItem[e.RowIndex].Style = objGeoStyle;
                  
                    //更换列表中的图像
                    if (objLayer.Type == DatasetType.Point)
                    {
                        //获取符号编号
                        int num = objGeoStyle.MarkerSymbolID;
                        //加载符号库
                        SymbolLibrary symbolMarkerLibrary = resources.MarkerLibrary;
                        //拟定一个点对象
                        GeoPoint point = new GeoPoint(5, 5);
                        //获取并赋值对应的点样式
                        point.Style = objGeoStyle;
                        //从库中查找对应的点符号编号
                        Symbol symbol = symbolMarkerLibrary.FindSymbol(num);
                        //objThemeUniqueItem[e.RowIndex].Style.SetSymbolMarker(symbol as SymbolMarker);
                        dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = DrawSymbol(symbol, point, 20, 20);
                    }
                    else if (objLayer.Type == DatasetType.Line)
                    {
                        //获取符号编号
                        int num = objGeoStyle.LineSymbolID;
                        //加载符号库
                        SymbolLibrary symbolMarkerLibrary = resources.LineLibrary;
                        //拟定一个线对象
                        Point2Ds point2Ds = new Point2Ds();
                        point2Ds.Add(new Point2D(0, 5));
                        point2Ds.Add(new Point2D(15, 5));
                        GeoLine line = new GeoLine(point2Ds);
                        //获取并赋值对应的点样式
                        line.Style = objGeoStyle;
                        //从库中查找对应的线符号编号
                        Symbol symbol = symbolMarkerLibrary.FindSymbol(num);
                       // objThemeUniqueItem[e.RowIndex].Style.SetSymbolLine(symbol as SymbolLine);
                        dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = DrawSymbol(symbol, line, 20, 20);
                    }
                    else if (objLayer.Type == DatasetType.Region)
                    {
                        //获取符号编号
                        int num = objGeoStyle.FillSymbolID;
                        //加载符号库
                        SymbolLibrary symbolFillLibrary = resources.FillLibrary;
                        //拟定一个面对象
                        Point2Ds point2Ds = new Point2Ds();
                        point2Ds.Add(new Point2D(0, 0));
                        point2Ds.Add(new Point2D(0, 15));
                        point2Ds.Add(new Point2D(15, 15));
                        point2Ds.Add(new Point2D(15, 0));
                        GeoRegion region = new GeoRegion(point2Ds);
                        //获取并赋值对应的面样式
                        region.Style = objGeoStyle;
                        //从库中查找对应的点符号编号
                        Symbol symbol = symbolFillLibrary.FindSymbol(num);
                        //objThemeUniqueItem[e.RowIndex].Style.SetSymbolFill(symbol as SymbolFill);
                        dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = DrawSymbol(symbol, region, 20, 20);
                    }
                  
                }
              
            }
        }

        /// <summary>
        /// 根据数据集类型获取符号库的类型
        /// </summary>
        /// <param name="datasetType">数据集类型</param>
        /// <returns>返回符号库类型</returns>
        private SymbolType GetSymbolType(DatasetType datasetType)
        {
            SymbolType result = SymbolType.Marker;

            switch (datasetType)
            {
                case DatasetType.Line:
                    {
                        result = SymbolType.Line;
                    }
                    break;
                case DatasetType.Point:
                    {
                        result = SymbolType.Marker;
                    }
                    break;
                case DatasetType.Region:
                    {
                        result = SymbolType.Fill;
                    }
                    break;
                default:
                    break;
            }
            return result;
        }
        /// <summary>
        ///获取列表中显示的图片 设置默认值并在列表中显示
        /// </summary>
        /// <param name="P"></param>
        /// <param name="geostyle1"></param>
        /// <returns></returns>
        public Image getImage(DatasetType P, GeoStyle geostyle1)
        {
            Image im = null;
            if (P == DatasetType.Point)
            {
                //符号库
                SymbolLibrary symbolMarkerLibrary = resources.MarkerLibrary;
                SymbolGroup m_symbolMarkerRootGroup = symbolMarkerLibrary.RootGroup;
                GeoPoint point = new GeoPoint(5, 5);
                point.Style = geostyle1;
                Symbol symbol = null;
                if (geostyle1.MarkerSymbolID == null)
                {
                    symbol = m_symbolMarkerRootGroup[0];//symbolMarkerLibrary.FindSymbol(0);//
                }
                else {
                    symbol = m_symbolMarkerRootGroup[geostyle1.MarkerSymbolID];
                    //symbol = symbolMarkerLibrary.FindSymbol(geostyle1.MarkerSymbolID);//symbolMarkerLibrary.FindSymbol(0);//
                }             
                im = DrawSymbol(symbol, point, 20, 20);
            }
            else if (P == DatasetType.Line)
            {
                SymbolLibrary symbolLineLibrary = resources.LineLibrary;
                SymbolGroup m_symbolLineRootGroup = symbolLineLibrary.RootGroup;
                Point2Ds point2Ds = new Point2Ds();
                point2Ds.Add(new Point2D(0, 5));
                point2Ds.Add(new Point2D(15, 5));
                GeoLine line = new GeoLine(point2Ds);
                line.Style = geostyle1;
                Symbol symbol = null;
                if (geostyle1.LineSymbolID == null)
                {
                    symbol = m_symbolLineRootGroup[0];//symbolMarkerLibrary.FindSymbol(0);//
                }
                else
                {
                    symbol = m_symbolLineRootGroup[geostyle1.LineSymbolID];
                    //symbol = symbolLineLibrary.FindSymbol(geostyle1.LineSymbolID);//symbolMarkerLibrary.FindSymbol(0);//
                }              
                im = DrawSymbol(symbol, line, 20, 20);
            }
            else if (P == DatasetType.Region)
            {
                SymbolLibrary symbolFillLibrary = resources.FillLibrary;
                SymbolGroup m_symbolFillRootGroup = symbolFillLibrary.RootGroup;
                //构造点对象,由点组成面对象
                Point2Ds point2Ds = new Point2Ds();
                point2Ds.Add(new Point2D(0, 0));
                point2Ds.Add(new Point2D(0, 15));
                point2Ds.Add(new Point2D(15, 15));
                point2Ds.Add(new Point2D(15, 0));
                GeoRegion region = new GeoRegion(point2Ds);
                region.Style = geostyle1;
                Symbol symbol = null;
                if (geostyle1.FillSymbolID == null)
                {
                    symbol = m_symbolFillRootGroup[0];//symbolMarkerLibrary.FindSymbol(0);//
                }
                else
                {
                    symbol = m_symbolFillRootGroup[geostyle1.FillSymbolID];
                   // symbol = symbolFillLibrary.FindSymbol(geostyle1.FillSymbolID);//symbolMarkerLibrary.FindSymbol(0);//
                }
               // Symbol symbol = m_symbolFillRootGroup[0];//symbolFillLibrary.FindSymbol(0);//
                im = DrawSymbol(symbol, region, 20, 20);

            }
            return im;

        }
        /// <summary>
        /// 设置符号样式
        /// </summary>
        /// <param name="P"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public GeoStyle GeoStyleSample(DatasetType P, Color c)
        {
            GeoStyle objGeoStyle = new GeoStyle();
            switch (P)
            {
                case DatasetType.Point:
                    // 实例化一个点几何对象,并对其进行风格设置
                   // GeoStyle geoStyle_P = new GeoStyle();
                    objGeoStyle.LineColor = c;
                    objGeoStyle.MarkerSize = new Size2D(5, 5);
                   // geoStyle_P.MarkerSymbolID = 0;  //系统图标
                    //objGeoStyle = geoStyle_P;
                    break;
                case DatasetType.Line:
                    // 实例化一个线几何对象,并对其进行风格设置
                    //GeoStyle geoStyle_L = new GeoStyle();
                    objGeoStyle.LineColor = c;
                    //geoStyle_L.LineSymbolID = 15;
                    objGeoStyle.LineWidth = 1.0;                 
                   // objGeoStyle = geoStyle_L;
                    break;
                case DatasetType.Region:
                    // 实例化一个面几何对象,并对其进行渐变风格设置
                    //GeoStyle geoStyle_R = new GeoStyle();
                     objGeoStyle.FillBackColor = Color.White;;
                     objGeoStyle.FillForeColor = c;
                     objGeoStyle.FillBackOpaque = true;
                    // geoStyle_R.FillOpaqueRate = 50;
                   // geoStyle_R.FillSymbolID = 0;
                    //geoStyle_R.FillGradientMode = FillGradientMode.Linear;
                    //objGeoStyle = geoStyle_R;
                    break;
            }
            return objGeoStyle;
        }
        #endregion
        #region 点、线、面 datagridview绘制
        /// <summary>
        /// 绘制符号转换为图片
        /// 我去。。。
        /// </summary>
        /// <param name="symbol"></param>
        public Image DrawSymbol(Symbol symbol, Geometry geometry, Int32 width, Int32 height)
        {
            try
            {
                Bitmap bitmap = new Bitmap(width, height);
                if (symbol.Type == SymbolType.Marker)
                {
                    //geometry.Style.MarkerSymbolID = symbol.ID;                 
                    Toolkit.Draw(geometry, m_workspace.Resources, Graphics.FromImage(bitmap));
                }
                else if (symbol.Type == SymbolType.Line)
                {
                    //此句必须加上,不然在点击切换样式时无效果
                   // geometry.Style.LineSymbolID = symbol.ID;      
                    symbol.Draw(bitmap, geometry);
                  //  Toolkit.Draw(geometry, mainfrm.m_workspace.Resources, Graphics.FromImage(bitmap));
                }
                else if (symbol.Type == SymbolType.Fill)
                {
                    //geometry.Style.FillSymbolID = symbol.ID;
                    symbol.Draw(bitmap, geometry);
                  // Toolkit.Draw(geometry, mainfrm.m_workspace.Resources, Graphics.FromImage(bitmap));
                }
                Image objImage = bitmap;
                return objImage;

            }
            catch (Exception ex)
            {
                // Trace.WriteLine(ex.Message);
            }
            return null;
        }
        #endregion
        
        //确定按钮
        private void btnSure_Click(object sender, EventArgs e)
        {
            //默认的方式 预备暂时不用
            // ThemeUnique themeunique = ThemeUnique.MakeDefault(datasetVector, comboBox1.Text.Trim(), setColorTheml[comboBoxEx1.SelectedIndex]);
            // 将制作好的专题图添加到地图中显示
            if (datasetVector != null && objThemeUniqueItem.Count > 0)
            {
                for (int i = 0; i < objThemeUniqueItem.Count; i++)
                {
                    themeunique.Add(objThemeUniqueItem[i]);
                }
               // Layer sd = mainfrm.m_LayersControl.Map.Layers.FindLayer("");             
                Layer layer = objMapControl.Map.Layers.Add(datasetVector, themeunique, true);
               // m_MapControl.Map.Refresh();
                themeunique.Dispose();
                this.Close();
            }
            else {               
                 objThemeUnique.Clear();
               // mainfrm.m_LayersControl.Map.Layers.Remove()
                for (int i = 0; i < objThemeUniqueItem.Count; i++)
                {
                    objThemeUnique.Add(objThemeUniqueItem[i]);
                }
                 //objThemeUnique.FromXML(themeunique.ToXML());                
                objMapControl.Map.Refresh();               
                this.Close();
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAddValue_Click(object sender, EventArgs e)
        {
            showDZinformation();
        }
        /// <summary>
        /// 添加子项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddChild_Click(object sender, EventArgs e)
        {          
            changeUniqueItem showChage = new changeUniqueItem(objDeleteThemeUniqueItem, objThemeUniqueItem,dataGridView2,this);       
            showChage.ShowDialog();
        }
        /// <summary>
        /// 添加值
        /// </summary>
        /// <param name="num"></param>
        public void addToDataGridView(int num)
        {
            if (objDeleteThemeUniqueItem.Count > 0)
            {
                Image im = getImage(objLayer.Type, objDeleteThemeUniqueItem[num].Style);
                dataGridView2.Rows.Add(true, im, objDeleteThemeUniqueItem[num].Caption);
            }
        }
        /// <summary>
        /// 删除子项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteChild_Click(object sender, EventArgs e)
        {
            int num = dataGridView2.CurrentRow.Index;
            if (num >= 0) {
                //移除专题图               
                dataGridView2.Rows.RemoveAt(num);
                objDeleteThemeUniqueItem.Add(objThemeUniqueItem[num]);
                objThemeUniqueItem.RemoveAt(num);              
            }
        }



    }
}
主窗体代码

 

posted @ 2017-08-03 16:27  天翊无影  阅读(782)  评论(0编辑  收藏  举报