`

总结查找图元的三种方法

SQL 
阅读更多
在MapXtreme 2005中,查找图元提供了非常多的方法,也非常容易实现,这里总结了三种方法。

(1)Search方法是非常强大的,可以实现几乎所有的查找,这也是最常用的查找方式。示例代码如下:
复制内容到剪贴板
代码:
/**//// <summary>
/// 通过Search方法查找图元
/// Design by Glacier
/// 2008年8月6日
/// <param name="tableName">查找的表名</param>
/// <param name="columnName">查找的列名</param>
/// <param name="strKey">查找的关键字</param>
/// </summary>
public static void SearchWithSearch(string tableName, string columnName, string strKey)
{
MapInfo.Mapping.Map map = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];

SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchWhere(columnName + " like '%" + strKey + "%'");
IResultSetFeatureCollection ifs = MapInfo.Engine.Session.Current.Catalog.Search(tableName, si);
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear();
if (ifs.Count <= 0)
{
lbSearch.Text = "Cannot find the point";
}
else
{
//高亮显示
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Add(ifs);
lbSearch.Text = "";
if (ifs.Count == 1)
{
map.Center = new DPoint(ifs[0].Geometry.Centroid.x, ifs[0].Geometry.Centroid.y);
MapInfo.Geometry.Distance d = new MapInfo.Geometry.Distance(0.5, map.Zoom.Unit);
}
else
{
map.SetView(ifs.Envelope);
}
//设置高亮显示的样式
//((SimpleInterior)MapInfo.Engine.Session.Current.Selections.DefaultSelection.Style.AreaStyle.Interior).BackColor = System.Drawing.Color.Red;
//((SimpleInterior)MapInfo.Engine.Session.Current.Selections.DefaultSelection.Style.AreaStyle.Interior).ForeColor = System.Drawing.Color.Green;

//输出查询信息
ListBox1.Items.Clear();
foreach (Feature feature in ifs)
{
ListBox1.Items.Add(feature["name"].ToString());
}
}
}
(2)通过构造Find对象,进行查找。示例代码如下:
复制内容到剪贴板
代码:
/**//// <summary>
/// 通过Find查找图元
/// Design by Glacier
/// 2008年8月6日
/// <param name="layerName">查找的图层名</param>
/// <param name="columnName">查找的列名</param>
/// <param name="strKey">查找的关键字</param>
/// </summary>
public static void SearchWithFind(string layerName, string columnName, string strKey)
{
Find find = null;
MapInfo.Mapping.Map map = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];

// Do the find
MapInfo.Mapping.FeatureLayer findLayer = (MapInfo.Mapping.FeatureLayer)map.Layers[PointLayerName];
find = new Find(findLayer.Table, findLayer.Table.TableInfo.Columns[columnName]);
FindResult findResult = find.Search(strFind);
if (findResult.ExactMatch)
{
// Set the map's center and zoom
map.Center = new DPoint(findResult.FoundPoint.X, findResult.FoundPoint.Y);
MapInfo.Geometry.Distance d = new MapInfo.Geometry.Distance(2, map.Zoom.Unit);
map.Zoom = d;
lbSearch.Text = "";
}
else
{
lbSearch.Text = "Cannot find the Point";
}
find.Dispose();
}
(3)能过构造Sql语句进行查找,示例代码如下:
复制内容到剪贴板
代码:
/**//// <summary>
/// 通过Sql语句查找图元
/// Design by Glacier
/// 2008年8月6日
/// <param name="tableName">查找的表名</param>
/// <param name="columnName">查找的列名</param>
/// <param name="strKey">查找的关键字</param>
/// </summary>
public static void SearchWithSql(string tableName, string columnName, string strKey)
{
MapInfo.Data.MIConnection miConnection = new MIConnection();
miConnection.Open();
MapInfo.Data.MICommand miCommand = miConnection.CreateCommand();
miCommand.CommandText = "select * from " + tableName + " where " + columnName + " like '%'+@name+'%'";
miCommand.Parameters.Add("@name", strKey);
IResultSetFeatureCollection ifs = miCommand.ExecuteFeatureCollection();
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear();
MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[MapControl1.MapAlias];
if (ifs.Count <= 0)
{
lbSearch.Text = "Cannot find the point";
}
else
{
//高亮显示
lbSearch.Text = "";
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Add(ifs);
if (ifs.Count == 1)
{
myMap.Center = new DPoint(ifs[0].Geometry.Centroid.x, ifs[0].Geometry.Centroid.y);
MapInfo.Geometry.Distance d = new MapInfo.Geometry.Distance(0.5, myMap.Zoom.Unit);
myMap.Zoom = d;
}
else
{
myMap.SetView(ifs.Envelope);
}
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics