一、类库接口描述
1、IQueryFilter接口
过滤数据通过属性值或者属性之间的关系,一般为其赋WhereClause和SubFields属性。
2、IFeatureClass接口
(1)Search方法 IFeatureCursor返回值
返回查询结果集IFeatureCursor接口的游标,目的是遍历查询结果,在地图中不会显示查询结果
(2)Select方法 ISelectionSet返回值
返回结果为ISelectionSet接口的选择集,在地图中不会显示查询结果。
3、IFeatureLayer接口
Search方法 IFeatureCursor返回值
与IFeatureClass接口中的Search方法相同。
4、IFeatureSelection接口
SelectFeatures方法 void返回值
无返回值,目的是根据查询条件选择要素,将在地图上高亮显示查询结果。
5、IQueryDef接口
Evaluate方法 ICursor返回值
根据设定的查询条件执行查询操作,返回值为Icursor接口的游标,可进行行遍历,在地图上不会显示查询结果。
二、 实现思路
1、设计界面
2、理清逻辑思路,设计功能
通过主界面将MapControl传到属性查询的界面,用户可以通过“选择图层”的下拉框选择不同的图层,再选择“方法”:创建新选择内容、添加到当前选择内容、从当前选择内容移除、从当前选择内容选择,当选择不同的图层时,左边会显示出当前图层的字段,用户点击“字段”,点击“获取唯一属性值”,右侧会相应将字段显示出来。双击左侧和右侧的字段和单击中间的符号会在下方相应区域显示,只要写入正确的语句,则会在地图中进行相应的高亮显示。 当勾选“定位到查询结果”,查询后会直接跳转到查询结果处。
3、代码实现
4、测试
三、 属性查询流程图
四、 关键代码
1、获得图层名字
// 通过这个函数获得所有图层
public IEnumLayer GetLayers()
{
IEnumLayer layers;
if (m_map.LayerCount == 0)
return null;
layers = m_map.get_Layers(null, true);
layers.Reset();
return layers;
}
// 将得到的图层转为矢量要素
public IFeatureLayer GetFeatureLayer(string layername)
{
if (GetLayers() == null)
return null;
IEnumLayer layers = GetLayers();
layers.Reset();
ILayer layer = null;
while ((layer = layers.Next()) != null)
{
if (layer.Name == layername)
return layer as IFeatureLayer;
}
return null;
}
// 添加图层名字
public void cbxAddLayer()
{
if (GetLayers() == null) return;
IEnumLayer layers = GetLayers();
layers.Reset();
ILayer layer = null;
while ((layer = layers.Next()) != null)
{
if (layer is IFeatureLayer)
comboBoxLayer.Items.Add(layer.Name);
}
}
2、当图层进行选择时,下面框数据随着变化:利用comboBoxLayer_SelectedIndexChanged事件
//图层选择
private void comboBoxLayer_SelectedIndexChanged(object sender, EventArgs e)
{
if (GetLayers() == null) return;
IEnumLayer layers = GetLayers();
IField m_field;
int fieldtype;
layers.Reset();
listBoxFields.Items.Clear();
while ((m_layer = layers.Next()) != null)
{
if (m_layer.Name != comboBoxLayer.Text) continue;
this.m_layerfield = m_layer as ILayerFields;
for (int i = 0; i < m_layerfield.FieldCount; i++)
{
m_field = m_layerfield.get_Field(i);
fieldtype = (int)m_field.Type; if (fieldtype > 5) continue;
this.listBoxFields.Items.Add(m_field.Name);
}
}
this.m_featurelayer = GetFeatureLayer(comboBoxLayer.Text);
}
3、符号加载功能相关代码
private void listBoxFields_MouseDoubleClick(object sender, MouseEventArgs e)
{
Fieldstr = listBoxFields.Text;
textBoxSqlClause.Text = Fieldstr + " ";
}
private void listBoxValues_MouseDoubleClick(object sender, MouseEventArgs e)
{
valuesstr = listBoxValues.Text;
textBoxSqlClause.Text += " " + valuesstr;
}
private void buttonX1_MouseClick(object sender, MouseEventArgs e)
{
constr = "=";
textBoxSqlClause.Text += constr;
}
…………
4、属性查询功能相关代码
private void buttonX18_Click(object sender, EventArgs e)
{
if (textBoxSqlClause.Text == "" )
{
MessageBox.Show( "请生成语句。" );
return ;
}
try
{
IQueryFilter2 QueryFilter = (IQueryFilter2)new QueryFilter();
QueryFilter.WhereClause = textBoxSqlClause.Text;
IFeatureSelection FeatureSelection = m_featurelayer as IFeatureSelection ;
m_mapcontrol.ActiveView.Refresh();
int iSelectedFeaturesCount = FeatureSelection.SelectionSet.Count;
esriSelectionResultEnum SelectMothod;
switch (comboBoxMethod.SelectedIndex)
{
case 0:
m_map.ClearSelection();
SelectMothod = esriSelectionResultEnum .esriSelectionResultNew;
break ;
case 1:
SelectMothod = esriSelectionResultEnum .esriSelectionResultAdd;
break ;
case 2:
SelectMothod =esriSelectionResultEnum .esriSelectionResultSubtract;
break ;
case 3:
SelectMothod = esriSelectionResultEnum .esriSelectionResultAnd;
break ;
default :
SelectMothod =esriSelectionResultEnum .esriSelectionResultNew;
break ;
}
FeatureSelection.SelectFeatures(QueryFilter, SelectMothod, false );
VaryInt = VaryInt + 1;
if (FeatureSelection.SelectionSet.Count == 0)
{
MessageBox.Show( "没有符合本次查询的条件。" );
return ;
}
// 如果复选框被选中就定位到选择结果
if (checkBox2.Checked == true )
{
IEnumFeature EnumFeature = m_mapcontrol.Map.FeatureSelection as IEnumFeature ;
IFeature Feature = EnumFeature.Next();
IEnvelope Envelope = (IEnvelope)new Envelope();
while (Feature != null )
{
Envelope.Union(Feature.Extent);
Feature = EnumFeature.Next();
}
m_mapcontrol.ActiveView.Extent = Envelope;
m_mapcontrol.ActiveView.Refresh();
}
//m_mapcontrol.ActiveView.Refresh();
m_mapcontrol.ActiveView.PartialRefresh( esriViewDrawPhase .esriViewGeoSelection, null ,null );
}
catch ( Exception ex)
{
MessageBox.Show( "您的查询语句可能有误,请检查!| " + ex.Message);
return ;
}
}
五、注意内容
在实现AE属性数据的查询过程中,除了考虑属性查询的代码编写,还需考虑如何在不同窗体之间进行传参,如何在下拉框改变时,下方的listbox内的值随之变动,如何获得图层的唯一值等问题。