天道酬勤

博观而约取,厚积而薄发!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

MapXtreme 2008 最佳体验 GIS 地图实例【转】

Posted on 2010-04-30 13:18  Happy Coding  阅读(1289)  评论(0编辑  收藏  举报

MapXtreme 2008 最佳体验 GIS 地图实例


using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using MapInfo.Data;
using MapInfo.Mapping;
using MapInfo.Engine;
using MapInfo.Geometry;
using MapInfo.Styles;
using MapInfo.Tools;
using MapInfo.Windows.Controls;

 

 

using SLMApp.DBORM;

 

 

namespace SLMApp
{
    
public partial class MapForm : Form
    {
        
private MICommand _miCommand;
        
private MIConnection _miConnection;
        
private String strAppDir;//目录
        private static String testLayer = "设备";//图层名称
        private String filePath;//加载地图文件路径 兼容两种格式
        private static double zoomCk = 0.578;//初始化的倍率 
        private double range = 0.0002;//范围
        private double dblZoom = 0;//放大缩小倍率
        private List<FeatureGeometry> fgArray = new List<FeatureGeometry>();//坐标集合

 

 

//图元选择事件
        private void FeatureSelected(object sender, FeatureSelectedEventArgs e)
        {
            ISession session 
= MapInfo.Engine.Session.Current;
            Table table 
= session.Catalog.GetTable(testLayer);//获取层
            IResultSetFeatureCollection fc = session.Selections.DefaultSelection[table];//获取选择元素层
            int nCount = 0;
            
if (fc != null)
            {
                nCount 
= fc.Count;
                
if (nCount == 1)//判断在指定层上,是否只选中一个
                {
                    
this.findBySql("select * from " + testLayer, e.MapCoordinate.x, e.MapCoordinate.y);
                }
            }
        }

 

 

//左下脚显示放大缩小的比例
        void Map_ViewChangedEvent(object sender, ViewChangedEventArgs e)
        {
            
// 放大缩小
            this.dblZoom = System.Convert.ToDouble(String.Format("{0:E2}", mapControl1.Map.Zoom.Value));
            ultraStatusBar1.Text 
= "缩放比例: " + dblZoom.ToString() + " " + MapInfo.Geometry.CoordSys.DistanceUnitAbbreviation(mapControl1.Map.Zoom.Unit);
        }

 

 

public MapForm()
        {
            InitializeComponent();
        }

 

 

private void Form1_Load(object sender, EventArgs e)
        {
            
//链接物理层

            
this._miConnection = new MIConnection();
            _miConnection.Open();
            
this._miCommand = _miConnection.CreateCommand();

 

 

this.strAppDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            
this.strAppDir = this.strAppDir.Replace(@"file:\""");

 

 

this.filePath = strAppDir + @"\MapInfo\maps\SLM.gst";//加载地图

 

 

//this.filePath = @"D:\My Documents\Visual Studio 2008\Projects\MapXtremeMapFormApplication1\MapXtremeMapFormApplication1\bin\Debug\Map\xindou\xindou.gst";
            
//MessageBox.Show(this.filePath);
            
//Microsoft.Win32.RegistryKey keySamp = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MapInfo\MapXtreme\6.8.0");

 

 

//加载地图,兼容gst和nws两种格式
            if (filePath.ToLower().EndsWith(MapLoader.FileExtensionGST))
            {
                mapControl1.Map.Load(
new MapGeosetLoader(filePath));
            }
            
else if (filePath.ToLower().EndsWith(MapLoader.FileExtensionWOR))
            {
                mapControl1.Map.Load(
new MapWorkSpaceLoader(filePath));
                mapControl1.Map.Size 
= mapControl1.Size;
            }

 

 

//改变坐标
            mapControl1.Map.ViewChangedEvent += new ViewChangedEventHandler(Map_ViewChangedEvent);
            mapControl1.Tools.FeatureSelected 
+= new FeatureSelectedEventHandler(FeatureSelected);
            Map_ViewChangedEvent(
thisnull);

 

 

//将其他图层设为不可选
            for (int i = 0; i < mapControl1.Map.Layers.Count; i++)
            {
                MapInfo.Mapping.LayerHelper.SetSelectable(mapControl1.Map.Layers[i], 
false);
            }

 

 

//创建临时层********/

            var cat 
= Session.Current.Catalog;
            TableInfoMemTable tblInfoTemp 
= new TableInfoMemTable(testLayer);
            Table tblTemp 
= cat.GetTable(testLayer);
            
if (tblTemp != null)
            {
                cat.CloseTable(testLayer);
            }
            
//加载层元素
            tblInfoTemp.Columns.Add(ColumnFactory.CreateFeatureGeometryColumn(mapControl1.Map.GetDisplayCoordSys()));
            tblInfoTemp.Columns.Add(ColumnFactory.CreateStyleColumn());
            tblInfoTemp.Columns.Add(ColumnFactory.CreateStringColumn(
"名称"8));
            tblInfoTemp.Columns.Add(ColumnFactory.CreateStringColumn(
"设备编号"8));

 

 

//层上创建物理表
            tblTemp = cat.CreateTable(tblInfoTemp);

 

            FeatureLayer lyr 

= new FeatureLayer(tblTemp);

 

            mapControl1.Map.Layers.Add(lyr);

//加入层

            
/***********************/

 

 

//为点添加标注********/

            TextStyle _fontSymbol2 
= new TextStyle();
            _fontSymbol2.Font.ForeColor 
= Color.Black;
            _fontSymbol2.Font.Size 
= 6;
            LabelLayer layer 
= new LabelLayer();
            mapControl1.Map.Layers.Add(layer);
            LabelSource source 
= new LabelSource(cat.GetTable(testLayer));
            source.DefaultLabelProperties.Style 
= _fontSymbol2;
            layer.Sources.Append(source);

 

 

/***********************/

 

 

//在数据库分别读取每个地点的xy坐标 并且写进地图

            DataBaseDataContext db 
= new DataBaseDataContext();
            var result 
= from dt in db.DevType_Table select dt;
            
foreach (DevType_Table devtable in result)
            {
                showPointOnSearchTableMap((
double)devtable.x, (double)devtable.y, devtable.DeviceName.Trim(), devtable.DeviceID);
            }
            rtuDataGrid.DataSource 
= result;
            
//把除了名称的其他列都隐藏起来
            rtuDataGrid.Columns[0].HeaderText = "地点";
            
for (int i = 1; i < rtuDataGrid.ColumnCount; i++)
            {
                rtuDataGrid.Columns[i].Visible 
= false;
            }
        }

 

 

private void findBySql(string sql, double xx, double yy)
        {
            
try
            {
                
//计算倍率
                double rangeResult;
                
if (dblZoom > 0)
                {
                    rangeResult 
= range * (dblZoom / zoomCk);
                }
                
else
                {
                    rangeResult 
= range;
                }
                
//进行物理查询
                this._miCommand.CommandText = sql;
                MIDataReader miReader 
= this._miCommand.ExecuteReader();
                
while (miReader.Read())
                {

 

 

//获取图元坐标范围
                    FeatureGeometry fg = (FeatureGeometry)miReader.GetValue(0);

 

 

if (xx > fg.Bounds.x1 - rangeResult && xx < fg.Bounds.x2 + rangeResult && yy > fg.Bounds.y1 - rangeResult && yy < fg.Bounds.y2 + rangeResult)
                    {
                        
//如果点击范围在此图元的话 执行以下
                        /*
                        Form2 form2 = new Form2();
                        form2.graphTitle = dr[0] + "设备的运行情况";
                        form2.ShowDialog();
                         * 
*/
                        MessageBox.Show(
"显示设备信息");
                    }
                }
                miReader.Close();
            }
            
catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

 

 

//在指定层显示图元
        private void showPointOnSearchTableMap(double x, double y, string pointname, int id)
        {
            
//设置样式
            CoordSys coordSys = mapControl1.Map.GetDisplayCoordSys();
            MapInfo.Geometry.FeatureGeometry g 
= new MapInfo.Geometry.Point(coordSys, x, y);
            MapInfo.Styles.SimpleVectorPointStyle vs 
= new MapInfo.Styles.SimpleVectorPointStyle(37, System.Drawing.Color.Red, 14);
            MapInfo.Styles.CompositeStyle cs 
= new MapInfo.Styles.CompositeStyle(vs);
            
//添加图元
            MICommand cmd = _miConnection.CreateCommand();
            cmd.Parameters.Add(
"geometry", MIDbType.FeatureGeometry);
            cmd.Parameters.Add(
"style", MIDbType.Style);
            cmd.Parameters.Add(
"no", MIDbType.String);
            cmd.Parameters.Add(
"sbno", MIDbType.Int);
            cmd.CommandText 
= "Insert Into " + testLayer + " (obj,MI_Style,名称,设备编号) values (geometry,style,no,sbno)";
            cmd.Prepare();
            cmd.Parameters[
0].Value = g;
            cmd.Parameters[
1].Value = cs;
            cmd.Parameters[
2].Value = pointname;
            cmd.Parameters[
3].Value = id;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }

 

 

private void mapControl1_MouseMove(object sender, MouseEventArgs e)
        {
            
//将鼠标xy坐标转为mapX的地图经纬度坐标
            System.Drawing.PointF DisplayPoint = new PointF(e.X, e.Y);
            MapInfo.Geometry.DPoint MapPoint 
= new MapInfo.Geometry.DPoint();
            MapInfo.Geometry.DisplayTransform converter 
= this.mapControl1.Map.DisplayTransform;
            converter.FromDisplay(DisplayPoint, 
out MapPoint);
            
//如果状态条是有2个位置的话,在第二个位置显示经纬度信息
            ultraStatusBar1.Text = "经纬度信息:X=" + MapPoint.x.ToString() + ", Y=" + MapPoint.y.ToString();
        }

 

 

private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {

 

        }

 

private void rtuDataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
int rowIndex = e.RowIndex;//获取当前行   

            
string s = rtuDataGrid.Rows[rowIndex].Cells["x"].Value.ToString(); //获取当前行xh字段的值   

            MessageBox.Show(s);
        }

 

        
    }
}