zoukankan      html  css  js  c++  java
  • 整理的MapXtreme2004应用问答

    1、问:如何在桌面程序中更改地图的坐标系?(TOP)
    答:代码如下:
       using MapInfo.Geometry;
       //要使用到MapInfo.Geometry命名空间
       Map map = mapControl1.Map;
       MapInfo.Geometry.CoordSys coordSys = Session.Current.CoordSysFactory.CreateLongLat(DatumID.NAD83);
       //DatumID为枚举类型,其中列出了经纬度坐标系统的大量枚举类型,参阅帮助可获取更多信息。
       map.SetDisplayCoordSys(coordSys);
    2、问:在C#应用中如何读取存在ORACLE(或SQL Server)中的MapInfo表?(TOP)
    答:读取ORACLE中表的方法如下代码:
      using MapInfo.Data;           //这里要添加对MapInfo数据的引用
      MIConnection Connection=new MIConnection();
      Connection.Open();
      MapInfo.Data.Table [] tables=new MapInfo.Data.Table[4];
      TableInfoServer tis1=new TableInfoServer("WORLD","SVR=MYORACLE;UID=system;PWD=manager","select * from world",MapInfo.Data.ServerToolkit.Oci);
      tables[0]=Connection.Catalog.OpenTable(tis1);
      TableInfoServer tis2=new TableInfoServer("WORLDCAP","SVR=MYORACLE;UID=system;PWD=manager","select * from worldcap",MapInfo.Data.ServerToolkit.Oci);
      tables[1]=Connection.Catalog.OpenTable(tis2);
      TableInfoServer tis3=new TableInfoServer("wldcty25","SVR=MYORACLE;UID=system;PWD=manager","select * from wldcty25",MapInfo.Data.ServerToolkit.Oci);
      tables[2]=Connection.Catalog.OpenTable(tis3);
      TableInfoServer tis4=new TableInfoServer("OCEAN","SVR=MYORACLE;UID=system;PWD=manager","select * from OCEAN",MapInfo.Data.ServerToolkit.Oci);
      tables[3]=Connection.Catalog.OpenTable(tis4);
      MapControl1.Map.Load(new MapInfo.Mapping.MapTableLoader(tables));
      Connection.Close();
    而读取存放在SQL Server2000中的表时,应当使用如下修改过的代码:
      /* SQL Server数据库连接*/
      MIConnection Connection=new MIConnection();
      Connection.Open();
      MapInfo.Data.Table [] tables=new MapInfo.Data.Table[2];
      TableInfoServer tis1=new TableInfoServer("CH_SHENGHUI","DRIVER={SQL Server};SERVER=YC31;DATABASE=MYWEBGIS;Trusted_Connection=Yes","select * from CH_SHENGHUI",MapInfo.Data.ServerToolkit.Odbc);//注意这里使用的是Odbc,且区分大小写。
      tables[0]=Connection.Catalog.OpenTable(tis1);
      TableInfoServer tis2=new TableInfoServer("CH_SHENGJIE_P","DRIVER={SQL Server};SERVER=YC31;DATABASE=MYWEBGIS;Trusted_Connection=Yes","select * from CH_SHENGJIE_P",MapInfo.Data.ServerToolkit.Odbc);//注意这里使用的是Odbc,且区分大小写。
      tables[1]=Connection.Catalog.OpenTable(tis2);
      mapControl1.Map.Load(new MapInfo.Mapping.MapTableLoader(tables));
      Connection.Close();
      /*上面的TableInfoServer语句分开来写可以表达成如下方法。*/
      /*
      TableInfoServer tiServer = new TableInfoServer("SHENGHUI");
      tiServer.ConnectString = "DRIVER={SQL Server};SERVER=YC31;DATABASE=MYWEBGIS;Trusted_Connection=Yes";
      tiServer.Query = "Select * from CH_SHENGHUI";
      tiServer.Toolkit = ServerToolkit.Odbc;
      MapTableLoader tl = new MapTableLoader(tiServer);
      mapControl1.Map.Load(tl);
      */
    3、问:在C#桌面与Web应用中读取硬盘上MapInfo表一法(TOP)
    答:二者皆可使用如下代码:
    using MapInfo.Data;
    MIConnection Connection=new MIConnection();
    Connection.Open();
    tables[0]=Connection.Catalog.OpenTable(@"G:\Ch_shenghui.TAB");
    tables[1]=Connection.Catalog.OpenTable(@"G:\Shengjie_p.TAB");
    mapControl1.Map.Load(new MapInfo.Mapping.MapTableLoader(tables));
    4、问:如何在MapXtreme2004桌面应用程序中的任务栏上显示出鼠标的坐标?
    答:代码如下:
    1、添加mapControl1的MouseMove事件;
    2、该事件及其中代码如下:
    public void MapControl1_MouseMove(object sender, MouseEventArgs e)
    {
       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);
       this.statusBar1.Text = "Cursor Location: " + MapPoint.x.ToString() + ", " + MapPoint.y.ToString();
    }
    5、问:如何使用代码修改桌面应用程序的坐标系统?(TOP)
    答:代码如下:
    Map map = mapControl1.Map;
    MapInfo.Geometry.CoordSys coordSys = Session.Current.CoordSysFactory.CreateLongLat(DatumID.WGS84);
    map.SetDisplayCoordSys(coordSys);
    6、问:在C#中如何使用SpatialWare提供的用户自定义函数(UDFs)?
    答:代码如下:以测量数据库中的两个点之间的距离为例。
      string connectionstring = "Integrated Security=SSPI;database=MYWEBGIS;server=(local);Connect Timeout=30";
      SqlConnection connection = new SqlConnection(connectionstring);
      connection.Open();
      SqlCommand command = connection.CreateCommand();
      command.CommandText = "select dbo.HG_SphericalDistance(a.SW_GEOMETRY, l.SW_GEOMETRY,'km') AS Expr1 from CH_SHENGHUI a, CH_SHENGHUI l where a.NAME = '北京' and l.NAME = '合肥'";
      SqlDataReader reader = command.ExecuteReader();
      while(reader.Read()){
       textBox1.Text= reader["Expr1"].ToString();
      }
      reader.Close();
      connection.Close();
    这里需要注意的就是,在SQL语句中使用SpatialWare的函数时,一定要在前面加上dbo.,否则运行时就会出错。具体语句可以在SQL Server 2000企业管理器中调试后再拷贝到程序中即可。
    7、问:如何在一个Web应用中将打开的SpatialWare数据库地图存储成为一个文件?
    答:代码如下:
    MIConnection Connection =new MIConnection ();
    Connection.Open() ;
    TableInfoServer tis = new TableInfoServer("usa",
    "dsn=sqlserver;UID=sa;PWD=zackary","Select * from usa",MapInfo.Data.ServerToolkit.Odbc );
    MapInfo.Data.Table StateCapXY = Connection.Catalog.OpenTable(tis);
    mapControl1.Map.Load(new MapTableLoader(StateCapXY ));
    MapInfo.Mapping.FeatureLayer lyr=mapControl1.Map.Layers["USA"] as FeatureLayer ;
    MapInfo.Data.Table ti = MapInfo.Engine.Session.Current.Catalog.GetTable("usa");
    MapInfo.Data.SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchAll();
    si.QueryDefinition.SetColumns("*");
    MapInfo.Data.IResultSetFeatureCollection irfc = MapInfo.Engine.Session.Current.Catalog.Search(ti.Alias, si);
    MapInfo.Data.TableInfoNative tf = (MapInfo.Data.TableInfoNative)MapInfo.Data.TableInfoFactory.CreateFromFeatureCollection("newtab",MapInfo.Data.TableType.Native ,irfc);
    foreach(MapInfo.Data.Column cl in tf.Columns)
    {
    cl.ReadOnly =false;
    }
    tf.TablePath = "c:\\temp\\newtab.tab";
    tf.WriteTabFile ();
    MapInfo.Data.Table t = MapInfo.Engine.Session.Current.Catalog.CreateTable (tf);
    MapInfo.Engine.Session.Current.Catalog.CloseTable("newtab");
    t=MapInfo.Engine.Session.Current.Catalog.OpenTable("c:\\temp\\newtab.tab");
    mapControl1.Map.Load(new MapTableLoader(t));
    MapInfo.Data.MICommand micomm=new MapInfo.Data.MICommand ("insert into " + t.Alias + " (obj,state,state_name,sw_member) select obj,state,state_name,sw_member from usa", Connection);
    micomm.ExecuteNonQuery();
    8、问:如何在地图上查找指定位置的代码
    答:代码如下:
      Table _searchTable;
      //定义被查找的表
      Column _searchColumn;
      //定义被查找的列,必须是被索引的
      FindResult _result;
      //定义查询结果
      Find find = null;
      //定义查找对象,并实例化为null
      _searchTable = Session.Current.Catalog.OpenTable(@"C:\Program Files\MapInfo\MapXtreme\6.0\Samples\Data\worldcap.TAB");
      //打开被查找的表
      Columns columns = _searchTable.TableInfo.Columns;
      //定义出表的列
      _searchColumn = columns["Capital"];
      //注意此处的Capital是要区分大小写的。指定查找的列
      find = new Find(_searchTable,_searchColumn);
      //实例化查找对象
      find.UseCloseMatches = true;
      //指定如果找不到完全匹配是否返回 "N" 个接近的匹配。
      find.CloseMatchesMax = 6;
      //即上面的 "N"
      FindResult _findResult = find.Search(textBox2.Text.ToString());
      //给出所要查找的目标,返回查找的结果
      MapInfo.Geometry.DPoint dpoint;
      dpoint.x = (double)_findResult.FoundPoint.X;
      dpoint.y = (double)_findResult.FoundPoint.Y;
      mapControl1.Map.Center = dpoint;
      find.Dispose();
      //释放find对象
    9、问:如何获取地图上一个表中所有图元的信息?
    答:代码如下:
      MapInfo.Mapping.FeatureLayer fl = (MapInfo.Mapping.FeatureLayer)mapControl1.Map.Layers["worldcap"];
      MapInfo.Data.Table  t = fl.Table;
      //MIDataReader提供了从 MapInfo 数据提供方处读取行的前向数据流的手段
      MIDataReader tr;
      //MIConnection 表示与 Catalog 的连接
      MIConnection con = new MIConnection();
      //MICommand 提供了必要的接口来根据 MapInfo Data Provider 执行 SQL 命令
      //MICommand 创建 MIDataReader 和 MIScrollableReader 实例
      //分别通过 ExecuteReader 和 ExecuteScrollableReader 方法来获取数据
      MICommand tc = con.CreateCommand();
      tc.CommandText = "select * from " + t.Alias ;
      con.Open();
      //ExecuteReader执行对 Connection 的 CommandText,并使用其中一个 CommandBehavior 值生成 MIDataReader
      tr = tc.ExecuteReader() ;
      //MIDataReader.Read 如果有多个行则返回 true,否则返回 false
      int i=0;
      while (tr.Read())
      {
       //获得指定字段的字符串值
       listView1.Items.Add(tr.GetString(1));
      }
      //Cancel 尝试取消当前命令的执行
      tc.Cancel();
      //Dispose 处置 MICommand 对象
      tc.Dispose();
      //tr.Close关闭数据读取器并释放资源,con.Close关闭连接
      tr.Close() ;
      con.Close();
      //t.Close();
    10、问:如何在桌面应用中对地图赋予指定的工具?
    答:代码如下:
      mapControl1.Tools.LeftButtonTool = "pan";
    MapToolButtonType 枚举:指定 MapToolBarButton 的按钮类型。
    成员名 说明
    OpenTable 调用 LoadMapWizard。该按钮是推入型按钮。
    LayerControl 调用 LayerControlDlg。该按钮是推入型按钮。
    Arrow 设置 Arrow 工具。
    ZoomIn 设置 ZoomIn 工具。
    ZoomOut 设置 ZoomOut 工具。
    Center 设置 Center 工具。
    Pan 设置 Pan 工具。
    Select 设置 Select 工具。
    SelectRectangle 设置 SelectRectangle 工具。
    SelectRadius 设置 SelectRadius 工具。
    SelectPolygon 设置 SelectPolygon 工具。
    SelectRegion 设置 SelectRegion 工具。
    Label 设置 Label 工具。
    AddText 设置 AddText 工具。
    AddPoint 设置 AddPoint 工具。
    AddLine 设置 AddLine 工具。
    AddPolyline 设置 AddPolyline 工具。
    AddPolygon 设置 AddPolygon 工具。
    AddRectangle 设置 AddRectangle 工具。
    AddCircle 设置 AddCircle 工具。
    AddEllipse 设置 AddEllipse 工具。
    11、问:如何获取地图上指定的一个表中所选择到的图元的信息(包括经纬度值)?
    答:代码如下:
      FeatureLayer lyr=mapControl1.Map.Layers["DIJISHI"] as FeatureLayer ;
      IResultSetFeatureCollection irfc = Session.Current.Selections.DefaultSelection[lyr.Table ];
      foreach(MapInfo.Data.Feature l in irfc )
      {
       foreach(MapInfo.Data.Column column in l.Columns)
       {
        //print out the column name and contents with the following:
        MessageBox.Show (string.Format("{0}:{1}",column.ToString().ToUpper(),l[column.ToString()].ToString()));
       }
       MessageBox.Show("经度:" + l.Geometry.Centroid.x.ToString() + ",纬度:" + l.Geometry.Centroid.y.ToString());
      }

  • 相关阅读:
    [TC_SRM_466]DrawingBlackCrosses
    [POJ3254]Corn Fields
    [openjudge6043]哆啦A梦的时光机
    网络流 24 题汇总(LOJ 上只有 22 题???)
    Xadmin组件的url设计
    《今日简史》一、旧故事已然崩坏,新故事尚未构建
    《人类简史》二、认知革命——上帝之手的秘密
    ORM复习
    无拘无束的爱情
    使用代理导致IE host被修改问题的解决方案
  • 原文地址:https://www.cnblogs.com/googlegis/p/2978970.html
Copyright © 2011-2022 走看看