GIS生涯

导航

MapXtreme 2005代码段(2)数据查询部分

1、遍历所有要素
foreach(MapInfo.Data.Feature f in _FeatureLayer.Table)
{
// do something with the Feature
}
2、点查询
MapInfo.Geometry.DPoint point = new MapInfo.Geometry.DPoint(-100, 40);

MapInfo.Data.MIConnection connection = new MapInfo.Data.MIConnection();
connection.Open();
MapInfo.Data.MICommand command = connection.CreateCommand();

command.CommandText = "Select * from USA where MI_Point(@x, @y, @cs) within obj";
command.Parameters.Add("@x", point.x);
command.Parameters.Add("@y", point.y);
command.Parameters.Add("@cs", this.mapControl1.Map.GetDisplayCoordSys());

command.Prepare();
MapInfo.Data.IResultSetFeatureCollection irfc = command.ExecuteFeatureCollection();

command.Dispose();
connection.Close();
3.返回所有列
MapInfo.Data.Table USATable= MapInfo.Engine.Session.Current.Catalog.GetTable("usa");

string[] s = new string[1];
s[0] = "*";

MapInfo.Data.SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchWhere("State = 'NY'");
si.QueryDefinition.Columns = s;

MapInfo.Data.IResultSetFeatureCollection irfc = MapInfo.Engine.Session.Current.Catalog.Search(USATable.Alias, si);

int count = 0;
int ColCount = USATable.TableInfo.Columns.Count;

foreach (MapInfo.Data.Feature f in irfc)
{
count = 0;
while (count < ColCount)
{
MapInfo.Data.Column c = irfc.Columns[count];
this.listBox1.Items.Add(f[c.ToString()]).ToString();
count++;
}
}
4、返回查询结果
MapInfo.Data.Table USATable= MapInfo.Engine.Session.Current.Catalog.GetTable("usa");

string[] s = new string[1];
s[0] = "*";

MapInfo.Data.SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchWhere("State = 'NY'");
si.QueryDefinition.Columns = s;

MapInfo.Data.IResultSetFeatureCollection irfc = MapInfo.Engine.Session.Current.Catalog.Search(USATable.Alias, si);

int count = 0;
int ColCount = USATable.TableInfo.Columns.Count;

foreach (MapInfo.Data.Feature f in irfc)
{
count = 0;
while (count < ColCount)
{
MapInfo.Data.Column c = irfc.Columns[count];
this.listBox1.Items.Add(f[c.ToString()]).ToString();
count++;
}
}

5、距离内查询
MapInfo.Data.MIConnection connection = new MapInfo.Data.MIConnection();
connection.Open();

//This is the point of origin...
MapInfo.Geometry.DPoint dpt1 = new MapInfo.Geometry.DPoint(pos1.X,pos1.Y);

//'tablename' is the string alias of the table to perform the search on...'DoesTableExist' is a
//function that can be written to check to be sure the table was open. A 'try...catch' could also be
//used here.

if(this.DoesTableExist(tablename))
{
//get the table from the current catalog and assign to tab1
MapInfo.Data.Table tab1 = connection.Catalog.GetTable(tablename);
//create a feature layer from tab1
MapInfo.Mapping.FeatureLayer f1 = new MapInfo.Mapping.FeatureLayer(tab1);
//create a Distance object from the search radius and unit type -
MapInfo.Geometry.Distance dist4 = new MapInfo.Geometry.Distance(25,MapInfo.Geometry.DistanceUnit.Kilometer);
//Use the SearchInfoFactory class to return a search info based on search type. In this case -
//SearchWithinDistance
MapInfo.Data.SearchInfo si= MapInfo.Data.SearchInfoFactory.SearchWithinDistance(dpt1,f1.CoordSys,dist,MapInfo.Data.ContainsType.Centroid);
//The QueryDefinition (what to return), in this case - all columns '*'
si.QueryDefinition.Columns = new string[] {"*"};
//Perform the search on the table, return an IResultSetFeatureCollection object...
MapInfo.Data.IResultSetFeatureCollection irfc = MapInfo.Engine.Session.Current.Catalog.Search(tablename, si);

int i = 0;
//This IRFC can now be stepped through with a foreach loop...
foreach (MapInfo.Data.Feature f in irfc)
{
//And each cell in each returned feature can be accessed with a simple for loop...
for(int j = 0; j < irfc.Columns.Count; j++)
{
//print the contents of cell j in feature f to the console screen...
Console.WriteLine(f[j].ToString());
}
i++;

}


}
6、缓冲结果查询
'Select features in buffer
Dim con As New MapInfo.Data.MIConnection
Dim cmd As MapInfo.Data.MICommand
con.Open()

Dim Dist As Double = 1
Dim Units As String = "mi"
Dim Geo As MapInfo.Geometry.MultiPolygon = CType(g, MapInfo.Geometry.MultiPolygon)

cmd = con.CreateCommand
cmd.CommandText = "Select * from PropertyInformation where Obj within MI_Buffer(@Geo, @Dist, @Units, 'Spherical', 99)"
cmd.Parameters.Add("@Geo", Geo)
cmd.Parameters.Add("@Dist", Dist)
cmd.Parameters.Add("@Units", Units)

Dim rfc As MapInfo.Data.IResultSetFeatureCollection = cmd.ExecuteFeatureCollection()

MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear()
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Add(rfc)

Dim ftr1 As MapInfo.Data.Feature
Dim col As MapInfo.Data.Column

Dim dt As New System.Data.DataTable

dt.Columns.Add(New DataColumn("AccountID", GetType(String)))
dt.Columns.Add(New DataColumn("OwnerName1", GetType(String)))
dt.Columns.Add(New DataColumn("OwnerName2", GetType(String)))
dt.Columns.Add(New DataColumn("LegalDescript1", GetType(String)))
dt.Columns.Add(New DataColumn("LegalDescript2", GetType(String)))
dt.Columns.Add(New DataColumn("X", GetType(Double)))
dt.Columns.Add(New DataColumn("Y", GetType(Double)))

For Each ftr1 In rfc
Dim dr As System.Data.DataRow = dt.NewRow()
dr(0) = ftr1(1)
dr(1) = ftr1(3)
dr(2) = ftr1(4)
dr(3) = ftr1(7)
dr(4) = ftr1(8)
dr(5) = ftr1(35)
dr(6) = ftr1(36)
dt.Rows.Add(dr)
Next

Me.Grid2.Visible = True
Me.Grid2.DataSource = dt
Me.Grid2.DataBind()

cmd.Dispose()
con.Close()
con.Dispose()
7、要素内查询
MapInfo.Data.SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchWithinFeature(f, MapInfo.Data.ContainsType.Centroid);
MapInfo.Data.IResultSetFeatureCollection irfc = MapInfo.Engine.Session.Current.Catalog.Search("USCty_8k", si);

MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear();
MapInfo.Engine.Session.Current.Selections.DefaultSelection.Add(irfc);
irfc.Close();

posted on 2006-11-29 13:58  Fantasia  阅读(1258)  评论(0)    收藏  举报