C#進行MapX二次開發之圖層操作

來源:互聯網
上載者:User
C#進行MapX二次開發之圖層操作

特別說明,本文整理自一篇網路的文章《MapX從資料庫讀取資料形成新圖層(C#)》

  在C#中實現MapX從資料庫讀取資料形成新圖層分為兩個問題:

  1. MapX從資料庫讀取資料形成新圖層;

  2. 將DataTable轉換為ADO的Recordset。這裡的第二個問題是由第一個問題引起的,因為MapX是一個COM控制項,而且它只支援ADO的資料訪問方式,而C#編程時一般會使用ADO.NET方式,為此需要在兩種方式之間做一下轉換。(當然也可以在C#中使用ADO方式)

  DataTable轉換為ADO的Recordset的作業碼如下所示。

    /// <summary>
    /// 在.net中用ADO.NET取代了ADO實現對資料的訪問,但一些COM控制項只支援ADO並不支援ADO.NET。
    /// 為了使用這類控制項,只能將ADO.NET中的資料對象,比如轉換DataTable為ADO中的Recordset
    /// (DataSet對象本質上是DataTable的集合,因此本文只講述DataTable對象的轉換)。
    /// </summary>
    public sealed class ADONETtoADO
    {
        /// <summary>
        /// 將DataTable對象轉換為Recordeset對象
        /// </summary>
        /// <param name="table">DataTable對象</param>
        /// <returns>轉換後得到的Recordeset對象</returns>
        public static Recordset ConvertDataTableToRecordset(DataTable table)
        {
            //思路:
            // 1. 建立Recordset對象後,在其中對應DataTable的Column建立Field,為此需要將ADO.NET的資料類型轉換為ADO的資料類型;
            // 2. 開啟Recordset對象,對應DataTable對象中的每一行,在Recordset對象中建立一條記錄,並對每個欄位賦值。

            Recordset rs = new RecordsetClass();
            foreach (DataColumn dc in table.Columns)
            {
                rs.Fields.Append(dc.ColumnName, GetDataType(dc.DataType), -1, FieldAttributeEnum.adFldIsNullable, Missing.Value);
            }

            rs.Open(Missing.Value, Missing.Value, CursorTypeEnum.adOpenUnspecified, LockTypeEnum.adLockUnspecified, -1);
            foreach (DataRow dr in table.Rows)
            {
                rs.AddNew(Missing.Value, Missing.Value); object o;
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    rs.Fields[i].Value = dr[i];
                    o = rs.Fields[i].Value;
                }
            }

            return rs;
        }

        /// <summary>
        /// 將ADO.NET的資料類型轉換為ADO的資料類型
        /// </summary>
        /// <param name="dataType">ADO.NET的資料類型</param>
        /// <returns>ADO的資料類型</returns>
        private static DataTypeEnum GetDataType(Type dataType)
        {
            switch (dataType.ToString())
            {
                case "System.Boolean": return DataTypeEnum.adBoolean;
                case "System.Byte": return DataTypeEnum.adUnsignedTinyInt;
                case "System.Char": return DataTypeEnum.adChar;
                case "System.DateTime": return DataTypeEnum.adDate;
                case "System.Decimal": return DataTypeEnum.adDecimal;
                case "System.Double": return DataTypeEnum.adDouble;
                case "System.Int16": return DataTypeEnum.adSmallInt;
                case "System.Int32": return DataTypeEnum.adInteger;
                case "System.Int64": return DataTypeEnum.adBigInt;
                case "System.SByte": return DataTypeEnum.adTinyInt;
                case "System.Single": return DataTypeEnum.adSingle;
                case "System.String": return DataTypeEnum.adVarChar;
                //case "TimeSpan":return DataTypeEnum.
                case "System.UInt16": return DataTypeEnum.adUnsignedSmallInt;
                case "System.UInt32": return DataTypeEnum.adUnsignedInt;
                case "System.UInt64": return DataTypeEnum.adUnsignedBigInt;
                default: throw (new Exception("沒有對應的資料類型"));
            }
        }
    }

 

在得到了Recordset對象後,如何解決第一個問題。步驟如下:

  1. 建立CMapXFields對象,並對應資料庫中欄位添加欄位;

  2. 建立CMapXBindLayer對象,指定其座標值欄位的序號;

  3. 向map.DataSets中添加資料集,從而產生新的圖層;

  4. 指定新圖層中要素的顯示風格,本文採用顯示位元影像的方式,為此需要將要顯示的位元影像放入MapX安裝目錄的CUSTSYMB檔案夾下。

  具體的作業碼如下所示:

        /// <summary>
        /// 刪除所有的圖層資料
        /// </summary>
        /// <param name="layerName"></param>
        private void DeleteLayerByName(string layerName)
        {
            //Layer的序號是從1開始
            int count = axMap1.Layers.Count;  
            for (int i = 1; i < count; i++) 
            {
                if (axMap1.Layers[i].Name == layerName)
                {
                    axMap1.Layers.Remove(i);
                }
            }
        }

        /// <summary>
        /// 建立新的圖層資訊
        /// </summary>
        /// <param name="layerName"></param>
        /// <param name="rsNoPass"></param>
        private void CreatNewLayerfromDB(string layerName, ADODB.Recordset rsNoPass)
        {
            DeleteLayerByName(layerName); //將原有層刪除
            CMapXFields flds = new FieldsClass();

            // Describe the structure of the Unbound dataset
            flds.Add("stationid", "theid", AggregationFunctionConstants.miAggregationIndividual,
                   FieldTypeConstants.miTypeString);
            flds.Add("address", "address", AggregationFunctionConstants.miAggregationIndividual,
                   FieldTypeConstants.miTypeString);
            flds.Add("longitude", "longitude", AggregationFunctionConstants.miAggregationSum,
                   FieldTypeConstants.miTypeNumeric);  //經度
            flds.Add("latitude", "latitude", AggregationFunctionConstants.miAggregationSum,
                   FieldTypeConstants.miTypeNumeric);  //緯度

            CMapXBindLayer bindLayerObject = new BindLayerClass();
            bindLayerObject.LayerName = layerName;
            bindLayerObject.RefColumn1 = 3;
            bindLayerObject.RefColumn2 = 4;
            bindLayerObject.LayerType = BindLayerTypeConstants.miBindLayerTypeXY;

            CMapXDataset dataSet = axMap1.DataSets.Add(DatasetTypeConstants.miDataSetADO, rsNoPass, layerName, "stationid", "address", bindLayerObject, flds, false);
            CMapXLayer layer = axMap1.Layers._Item(layerName);

            layer.OverrideStyle = true;
            string picName = "icon.BMP";
            if (layer.Style.SupportsBitmapSymbols == true)
            {
                layer.Style.SymbolType = SymbolTypeConstants.miSymbolTypeBitmap;
                layer.Style.SymbolBitmapSize = 60;
                layer.Style.SymbolBitmapTransparent = true;
                layer.Style.SymbolBitmapName = picName;
            }
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.