GIS二次开发之查询
最近做B/S的项目,实在是有点忙,虽然感觉没什么难度,可是复杂的流程和巨大的工作量还是很无奈。上班时间根本没有时间再学习GIS,所以只好下班回去简单的学习一下。本来上讲中提到这讲会介绍MapTOCControl的右击菜单,哈哈,这个暂时没整理好,还是先看看GIS的查询吧。哈哈。
老规矩,有图有真相,看看效果图。
在GIS中查询分为:属性查询和空间查询,分别对应的接口是IQueryFilter和ISpatialFilter,当然后者继承了前者。
在效果图中,用鼠标点击地图弹出BalloonCallOut的是利用空间查询实现的,在右边的查询条件中点击查询按钮实现的定位查询是属性查询。(哈哈,我想大家一看就知道了。)
①:属性查询代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#region 属性查询
private void btnSearchCity_Click(object sender, EventArgs e)
{
IActiveView pActiveView = axMapMain.Map as IActiveView;
IGraphicsContainer pGrahpicsContainer = pActiveView as IGraphicsContainer;
//先清除
pGrahpicsContainer.DeleteAllElements();
axMapMain.Extent = pActiveView.FullExtent;
axMapMain.ActiveView.ScreenDisplay.UpdateWindow();
IFeatureLayer pFeatureLayer = axMapMain.get_Layer(1) as IFeatureLayer;
if (null == pFeatureLayer) MessageBox.Show("选择图层不是Feature图层");
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = "Name='" + this.cbCity.Text + "'";
IFeatureCursor featureCursor = pFeatureLayer.Search(queryFilter, false);
IFeature pFeature = null;
while ((pFeature = featureCursor.NextFeature()) != null)
{
axMapMain.FlashShape(pFeature.Shape);
}
}
#endregion
private void btnSearchCity_Click(object sender, EventArgs e)
{
IActiveView pActiveView = axMapMain.Map as IActiveView;
IGraphicsContainer pGrahpicsContainer = pActiveView as IGraphicsContainer;
//先清除
pGrahpicsContainer.DeleteAllElements();
axMapMain.Extent = pActiveView.FullExtent;
axMapMain.ActiveView.ScreenDisplay.UpdateWindow();
IFeatureLayer pFeatureLayer = axMapMain.get_Layer(1) as IFeatureLayer;
if (null == pFeatureLayer) MessageBox.Show("选择图层不是Feature图层");
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = "Name='" + this.cbCity.Text + "'";
IFeatureCursor featureCursor = pFeatureLayer.Search(queryFilter, false);
IFeature pFeature = null;
while ((pFeature = featureCursor.NextFeature()) != null)
{
axMapMain.FlashShape(pFeature.Shape);
}
}
#endregion
②:空间查询
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#region 几何查询
private void axMapMain_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
IFeatureLayer pFeatureLayer = axMapMain.get_Layer(1) as IFeatureLayer;
if (null == pFeatureLayer) return;
IPoint point = new PointClass();
point.X = e.mapX;
point.Y = e.mapY;
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = point;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
IFeatureCursor featureCursor = pFeatureLayer.Search(spatialFilter, true);
IFeature pFeature = featureCursor.NextFeature();
while (pFeature != null)
{
int index = pFeature.Table.FindField("Name");
string name = pFeature.Table.GetRow(pFeature.OID).get_Value(index).ToString();
AddBalloonCallout(e.mapX, e.mapY, name);
axMapMain.FlashShape(pFeature.Shape);
pFeature = featureCursor.NextFeature();
}
System.Threading.Thread.Sleep(1000);
IActiveView pActiveView = axMapMain.Map as IActiveView;
IGraphicsContainer pGrahpicsContainer = pActiveView as IGraphicsContainer;
pGrahpicsContainer.DeleteAllElements();
axMapMain.ActiveView.ScreenDisplay.UpdateWindow();
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
#endregion
private void axMapMain_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
IFeatureLayer pFeatureLayer = axMapMain.get_Layer(1) as IFeatureLayer;
if (null == pFeatureLayer) return;
IPoint point = new PointClass();
point.X = e.mapX;
point.Y = e.mapY;
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.Geometry = point;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
IFeatureCursor featureCursor = pFeatureLayer.Search(spatialFilter, true);
IFeature pFeature = featureCursor.NextFeature();
while (pFeature != null)
{
int index = pFeature.Table.FindField("Name");
string name = pFeature.Table.GetRow(pFeature.OID).get_Value(index).ToString();
AddBalloonCallout(e.mapX, e.mapY, name);
axMapMain.FlashShape(pFeature.Shape);
pFeature = featureCursor.NextFeature();
}
System.Threading.Thread.Sleep(1000);
IActiveView pActiveView = axMapMain.Map as IActiveView;
IGraphicsContainer pGrahpicsContainer = pActiveView as IGraphicsContainer;
pGrahpicsContainer.DeleteAllElements();
axMapMain.ActiveView.ScreenDisplay.UpdateWindow();
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
#endregion