MapObject shape数据操作
利用MO 的GeoDataset 、DataConnection Recordset 进行数据操作
在MO中。使用GeoDataset对象和DataConnection 对象来连接和读取地理数据。
GeoDataset: 是表达一个图层的地利数据的对象
DatasetConnection对象指定一个和地理数据文件的连接,用Database 属性表示数据文件的地址。
Recordset 是Geodataset的记录集合或者查询后的数据集合
下面给一段 他们的使用 主要用于加载shp数据集并通过遍历获取线的每个节点
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 MapObjects2; namespace mo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public MapObjects2.MapLayer AddShape(string basepath, string fileName, bool mHasMeasure) { DataConnection dCon = new DataConnection(); GeoDataset gSet; mHasMeasure = false; dCon.Database = basepath; if (dCon.Connect()) { gSet = dCon.FindGeoDataset(fileName); if (gSet == null) { MessageBox.Show("打开shape文件异常"); return null; } else { MapLayer newLayer = new MapLayer(); newLayer.GeoDataset = gSet; mHasMeasure = gSet.HasMeasure; dCon = null; gSet = null; MessageBox.Show(newLayer.Records.Count.ToString()); return newLayer; } } else { dCon = null; gSet = null; return null; } } private void Form1_Load(object sender, EventArgs e) { } // h获取 图层中的每个节点 public void getLine(string mPath, string Name,bool fea) { Application.DoEvents(); string txt = null; MapObjects2.MapLayer mLay; MapObjects2.Recordset mrec; Line mline = new Line(); MapObjects2.Point mpt = new MapObjects2.Point(); ; mLay = AddShape(mPath, Name, false); mrec = mLay.Records; do { //从第一条数据开始处理 //MessageBox.Show(mrec.Fields.Item("shape").); mline = mrec.Fields.Item("shape").Value ; MessageBox.Show(mline.Parts.Count.ToString()); String mlxdm = mrec.Fields.Item("roadcode").Value.ToString(); MessageBox.Show(mlxdm); if (mline.Parts.Count < 1) { MessageBox.Show("line is nothing"); break; } for (int i = 0; i < mline.Parts.Count; i++) { MapObjects2.Points pt= mline.Parts.Item(i) as MapObjects2.Points; for (int j = 0; j < pt.Count;j++ ) { richTextBox1.Text = "x=" + pt.Item(j).X.ToString() + " y=" + pt.Item(j).Y.ToString() + " z=" + pt.Item(j).Z.ToString() + "\n" +richTextBox1.Text ; //MessageBox.Show("x=" + pt.Item(i).X.ToString() + " y=" + pt.Item(i).Y.ToString() + " z=" + pt.Item(i).Z.ToString()); // MessageBox.Show( pt.ToString()); } } mrec.MoveNext(); } while (!mrec.EOF); } private void button1_Click(object sender, EventArgs e) { getLine(Application.StartupPath + "\\layerdata", "nmg_route", true); } } }