现将对MapXtreme中Table的理解进行总结,也许对其他像我一样的新手有所帮助。如有高手,欢迎指正。
问题由一段判断两条线交点的代码引出:
//创建临时表
MapInfo.Data.TableInfoMemTable ti=new MapInfo.Data.TableInfoMemTable("memT");
MapInfo.Data.Table table=MapInfo.Engine.Session.Current.Catalog.CreateTable(ti);
//创建直线
MapInfo.Geometry.MultiCurve line1=MapInfo.Geometry.MultiCurve.CreateLine(
this.mapControl1.Map.GetDisplayCoordSys(),
new MapInfo.Geometry.DPoint(710,360),new MapInfo.Geometry.DPoint(32,69));
MapInfo.Geometry.MultiCurve line2=MapInfo.Geometry.MultiCurve.CreateLine(
this.mapControl1.Map.GetDisplayCoordSys(),
new MapInfo.Geometry.DPoint(79,210),new MapInfo.Geometry.DPoint(510,120));
//显示直线
MapInfo.Data.Feature f=new MapInfo.Data.Feature(line1,new MapInfo.Styles.CompositeStyle());
table.InsertFeature(f);
f=new MapInfo.Data.Feature(line2,new MapInfo.Styles.CompositeStyle());
table.InsertFeature(f);
MapInfo.Mapping.IMapLayer layer=new MapInfo.Mapping.FeatureLayer(table);
this.mapControl1.Map.Layers.Add(layer);
//获取交点
MapInfo.Geometry.MultiPoint mpoint=line1.IntersectNodes(line2,MapInfo.Geometry.IntersectTypes.InclAll);
//显示交点
this.listBox1.Items.Add("x: "+mpoint[0].x.ToString());
this.listBox1.Items.Add("y: "+mpoint[0].y.ToString());
这段代码在编译时系统会出现如下错误:
Unable to parse statement: insert into "memT"(obj,MI_Style) values(@0,@1):Invalid identifier(obj).
原因何在?通过查看帮助文件发现原来是对Table表结构缺乏深入了解所致.
Table 类是MapXtreme中所有数据访问的基本单元。Table、Column 和所有 TAB 文件元数据信息可以从 MapInfo Table 访问。表可以是可制图(包含 FeatureGeometry 类型的列,大多数表类型的几何体列都被赋予了名称“Obj”。)或不可制图的表。表不在地图中显示时也可以打开和访问。
需要说明的是可制图的表不仅要包含FeatureGeometry 类型的列MI_Geometry,而且还要包含MI_Style列。
看来问题出自声明了TableInfoMemTable对象,却没有定义MI_Geometry列和MI_Style列. 修改部分代码如下:
//创建临时表
MapInfo.Data.TableInfoMemTable ti=new MapInfo.Data.TableInfoMemTable("memT");
ti.Columns.Add(MapInfo.Data.ColumnFactory.CreateFeatureGeometryColumn(
this.mapControl1.Map.GetDisplayCoordSys()));
ti.Columns.Add(MapInfo.Data.ColumnFactory.CreateStyleColumn());
MapInfo.Data.Table table=MapInfo.Engine.Session.Current.Catalog.CreateTable(ti);
再编译,通过.
其实,如果使用TableInfoFactory,可能会更简单些,因为TableInfoFactory的CreateTemp方法在创建临时表的时候,也创建了Geometry 列和 Style 列. 代码如下:
//创建临时表
MapInfo.Data.TableInfo ti=MapInfo.Data.TableInfoFactory.CreateTemp("t");
MapInfo.Data.Table table=MapInfo.Engine.Session.Current.Catalog.CreateTable(ti)
|