mapobject shape数据操作三

    创建新的图层 先要设置图层的TableDesc 也就是属性字段 ,让后利用AddGeoDataset 方法就可以产生新的Geodataset(数据集),如把他加载到新的DataConnection就产生一个新的shape图层文件

这写了简单的测试 例子 实际使用的时候 不能把字段写死

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapObjects2;
using System.Windows.Forms;
using System.Collections;

namespace roadEditTool
{
    /// <summary>
    /// 
    /// </summary>
    class MODataOperate
    {

        public  GeoDataset gobj_gds;
        public  string gs_shpName;
        public  TableDesc gobj_desc = new TableDesc();
        public  DataConnection gobj_dc = new DataConnection();
        public  MapLayer gobj_layer = new MapLayer();

        //新建一个shp文件
        public  void createShpFile()
        {
            SaveFileDialog lobj_saveShp = new SaveFileDialog();
            lobj_saveShp.Filter = "shape|*.shp";
            lobj_saveShp.RestoreDirectory = true;
            if (lobj_saveShp.ShowDialog() == DialogResult.OK)
            {
                // lobj_saveShp.OpenFile();
                if (lobj_saveShp.FileName == null || lobj_saveShp.FileName.Trim() == "")
                {
                    MessageBox.Show("文件名称不能为空");
                    return;
                }
                gs_shpName = lobj_saveShp.FileName.Substring(0, lobj_saveShp.FileName.Length - 4);
                gobj_dc.Database = gs_shpName.Substring(0, gs_shpName.LastIndexOf("\\"));
                gs_shpName = gs_shpName.Substring(gs_shpName.LastIndexOf("\\") + 1, gs_shpName.Length - gs_shpName.LastIndexOf("\\") - 1);
                createFileds();
                addDataset();
                mapLayer();
                gobj_dc.Disconnect();
                gobj_layer = null;
                gobj_desc = null;
                gobj_gds = null;
                gobj_dc = null;
            }

        }

        //打开shp数据
        public  void  openShpFile()
        {
                string ls_FileName = null;
                string ls_path = null;
                OpenFileDialog lobj_saveShp = new OpenFileDialog();
            lobj_saveShp.Filter = "shape|*.shp";
            lobj_saveShp.RestoreDirectory = true;
            if (lobj_saveShp.ShowDialog() == DialogResult.OK)
            {
                ls_FileName = lobj_saveShp.SafeFileName.Split('.')[0].ToString().Trim();
                System.IO.FileInfo file = new System.IO.FileInfo(lobj_saveShp.FileName);
                    ls_path = file.DirectoryName;
                // lobj_saveShp.OpenFile();
                if (lobj_saveShp.FileName == null || lobj_saveShp.FileName.Trim() == "")
                {
                    MessageBox.Show("文件名称不能为空");
                    return;
                }
                //gs_shpName = lobj_saveShp.FileName.Substring(0, lobj_saveShp.FileName.Length - 4);
                //gobj_dc.Database = gs_shpName.Substring(0, gs_shpName.LastIndexOf("\\"));
                // gs_shpName = gs_shpName.Substring(gs_shpName.LastIndexOf("\\") + 1, gs_shpName.Length - gs_shpName.LastIndexOf("\\") - 1);
                gobj_layer=  AddShape(ls_path, ls_FileName, true);
            }

        }
        //三维显示
        public  void openLayer()
        {
            /*Recordset rs = gobj_layer.GeoDataset;
           do
           {
           }while();*/
        }

       
        //添加一个图层 
        public  void mapLayer()
        {
            gobj_layer.GeoDataset = gobj_gds;
        }

        public  MapObjects2.MapLayer AddShape(string basepath, string fileName, bool mHasMeasure)
        {
            try
            {
                
               
                mHasMeasure = false;

                gobj_dc.Database = basepath;

                if (gobj_dc.Connect())
                {
                    gobj_gds = gobj_dc.FindGeoDataset(fileName);
                    if (gobj_gds == null)
                    {
                        MessageBox.Show("打开shape文件异常");
                        return null;
                    }
                    else
                    {
                        MapLayer newLayer = new MapLayer();
                        newLayer.GeoDataset = gobj_gds;
                        mHasMeasure = gobj_gds.HasMeasure;
                        gobj_dc = null;
                        gobj_gds = null;

                        return newLayer;
                    }
                }
                else
                {
                    gobj_dc = null;
                    gobj_gds = null;
                    return null;
                }
            }
            catch (Exception EX)
            {
                log.log.appLog.Error("Error:加载shp数据异常", EX);
                return null;
            }
        }
        //新建字段
        public  void createFileds()
        {
            gobj_desc.FieldCount = 10;
            //字段名称
            gobj_desc.set_FieldName(0, "路线名称");
            gobj_desc.set_FieldName(1, "路线代码");
            gobj_desc.set_FieldName(2, "起点桩号");
            gobj_desc.set_FieldName(3, "止点桩号");
            gobj_desc.set_FieldName(4, "L"); 
            gobj_desc.set_FieldName(5, "ROADNAME");
            gobj_desc.set_FieldName(6, "ROADCODE");
            gobj_desc.set_FieldName(7, "FMEAS");
            gobj_desc.set_FieldName(8, "TMEAS");
            gobj_desc.set_FieldName(9, "GUID");
            //字段类型 
            gobj_desc.set_FieldType(0, FieldTypeConstants.moString);
            gobj_desc.set_FieldType(1, FieldTypeConstants.moString);
            gobj_desc.set_FieldType(2, FieldTypeConstants.moDouble);
            gobj_desc.set_FieldType(3, FieldTypeConstants.moDouble);
            gobj_desc.set_FieldType(4, FieldTypeConstants.moDouble);
            gobj_desc.set_FieldType(5, FieldTypeConstants.moString);
            gobj_desc.set_FieldType(6, FieldTypeConstants.moString);
            gobj_desc.set_FieldType(7, FieldTypeConstants.moDouble);
            gobj_desc.set_FieldType(8, FieldTypeConstants.moDouble);
            gobj_desc.set_FieldType(9, FieldTypeConstants.moString);
            //字段长度
            gobj_desc.set_FieldLength(0, 50);
            gobj_desc.set_FieldLength(1, 50);
            gobj_desc.set_FieldPrecision(2, 15);       
            gobj_desc.set_FieldScale(2, 3);            
            gobj_desc.set_FieldPrecision(3, 15);
            gobj_desc.set_FieldScale(3, 3);
            gobj_desc.set_FieldPrecision(4, 16);
            gobj_desc.set_FieldScale(4, 11);
            gobj_desc.set_FieldLength(5, 50);
            gobj_desc.set_FieldLength(6, 50);
            gobj_desc.set_FieldPrecision(7, 15);
            gobj_desc.set_FieldScale(7, 3);
            gobj_desc.set_FieldPrecision(8, 15);
            gobj_desc.set_FieldScale(8, 3);
            gobj_desc.set_FieldLength(9, 50);
        }

        //添加记录集
        public  void addDataset()
        {
            gobj_gds = gobj_dc.AddGeoDataset(gs_shpName, ShapeTypeConstants.moShapeTypeLine, gobj_desc, true, true);
        }
        //添加矢量线及属性值
        public  void addLine(Line _line, Hashtable _table)
        {
            Recordset rs = gobj_layer.Records;
            rs.AddNew();
            rs.Fields.Item("shape").Value = _line;
           // rs.Fields.Item("路线名称").Value = _table["路线名称"].ToString().Trim();
            setStringValue(rs, "路线名称", _table);
            setStringValue(rs, "路线代码", _table);
            seDoubleValue(rs, "起点桩号", _table);
            seDoubleValue(rs, "止点桩号", _table);
            rs.Fields.Item("L").Value = _line.Length;
            setStringValue(rs, "ROADNAME", _table);
            setStringValue(rs, "ROADCODE", _table);
            seDoubleValue(rs, "FMEAS", _table);
            seDoubleValue(rs, "TMEAS", _table);
            rs.Fields.Item("GUID").Value = Guid.NewGuid().ToString();
            rs.Update();
            rs.StopEditing();
        }
        private  void setStringValue(Recordset rs, string name, Hashtable _table)
        {
            string value = _table[name].ToString().Trim();
            if (value == null || value == "") return;
            rs.Fields.Item(name).Value = value;
        }
        private  void seDoubleValue(Recordset rs, string name, Hashtable _table)
        {
            string value = _table[name].ToString().Trim();
            if (value == null || value == "") return;
            rs.Fields.Item(name).Value =Convert.ToDouble(value);
        }
    }
}


 

你可能感兴趣的:(mo)