IQueryFilter

1、IQueryFilter::SubFields
(1)默认值为“*”,即查询时返回整行数据,如果只需要某一个字段数据(比如“Country”字段),则可以指定SubFields “Country”,这样可以提高查询效率;查询多个字段时使用 SubFields “Country,Date”;
(2)需要编辑数据时,应该将SubFields字段设为“*”;

2、ISpatialFilter
(1)继承自IQueryFilter,扩展部分包括Geometry,GeometryField,SpatialRel,SpatialRelDescription
(2)使用举例   ISpatialFilter pSpatialFilter new SpatialFilterClass();
                pSpatialFilter.Geometry pEnvelope;
                pSpatialFilter.GeometryField pFeatureClass.ShapeFieldName;
                pSpatialFilter.SpatialRel esriSpatialRelEnum.esriSpatialRelIntersects;

3、获取SelectionSet中的Feature
(1)通过游标得到  pSelectionSet.Search(null,false,out pCursor);
(2)通过OID得到   IRow pRow pSelectionSet.Target.GetRow(oid);
                   IFeature pFeature row as IFeature;

using System; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.SystemUI; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.Display; namespace _2023012505 { public partial class Form1 : Form { private ICommand cmdPan; private ICommand cmdFullExtent; private ICommand cmdZoomIn; private ICommand cmdZoomOut; private bool isDrawing = false; private IPointCollection pointCollection = new MultipointClass(); private IGraphicsContainer graphicsContainer; private IActiveView activeView; private IElement pointElement; private IElement lineElement; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { axMapControl1.AddShapeFile("..\\222\\data", "hlj1.shp"); axMapControl1.AddShapeFile("..\\222\\data", "city.shp"); axTOCControl1.SetBuddyControl(axMapControl1); cmdPan = new ControlsMapPanToolClass(); cmdPan.OnCreate(axMapControl1.Object); cmdFullExtent = new ControlsMapFullExtentCommandClass(); cmdFullExtent.OnCreate(axMapControl1.Object); cmdZoomIn = new ControlsMapZoomInToolClass(); cmdZoomIn.OnCreate(axMapControl1.Object); cmdZoomOut = new ControlsMapZoomOutToolClass(); cmdZoomOut.OnCreate(axMapControl1.Object); graphicsContainer = axMapControl1.Map as IGraphicsContainer; activeView = axMapControl1.ActiveView; LoadPlaceNames(); } private void LoadPlaceNames() { comboBox1.Items.Clear(); IFeatureLayer featureLayer = GetFeatureLayerByName("hlj1"); if (featureLayer == null) return; IFeatureClass featureClass = featureLayer.FeatureClass; IFeatureCursor featureCursor = featureClass.Search(null, true); IFeature feature = featureCursor.NextFeature(); while (feature != null) { int nameFieldIndex = feature.Fields.FindField("name"); if (nameFieldIndex >= 0) { object fieldValue = feature.get_Value(nameFieldIndex); if (fieldValue != null) { comboBox1.Items.Add(fieldValue.ToString()); } } feature = featureCursor.NextFeature(); } } private IFeatureLayer GetFeatureLayerByName(string layerName) { for (int i = 0; i < axMapControl1.LayerCount; i++) { if (axMapControl1.get_Layer(i).Name == layerName) { return axMapControl1.get_Layer(i) as IFeatureLayer; } } return null; } private void axTOCControl1_OnMouseDown(object sender, AxESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e) { } private void button4_Click(object sender, EventArgs e) { axMapControl1.CurrentTool = cmdPan as ITool; } private void button2_Click(object sender, EventArgs e) { axMapControl1.Extent = axMapControl1.FullExtent; axMapControl1.CurrentTool = null; } private void button3_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(comboBox1.Text)) { MessageBox.Show("请先选择一个地名"); return; } IFeatureLayer featureLayer = GetFeatureLayerByName("hlj1"); if (featureLayer == null) { MessageBox.Show("未找到hlj1图层"); return; } IFeatureClass featureClass = featureLayer.FeatureClass; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = "name='" + comboBox1.Text + "'"; IFeatureCursor featureCursor = featureClass.Search(queryFilter, true); IFeature feature = featureCursor.NextFeature(); if (feature != null) { IFeatureSelection featureSelection = featureLayer as IFeatureSelection; if (featureSelection != null) { featureSelection.Clear(); } IQueryFilter selectFilter = new QueryFilterClass(); selectFilter.WhereClause = "name='" + comboBox1.Text + "'"; if (featureSelection != null) { featureSelection.SelectFeatures(selectFilter, esriSelectionResultEnum.esriSelectionResultNew, false); } axMapControl1.FlashShape(feature.Shape, 3, 500, null); IEnvelope featureExtent = feature.Extent; featureExtent.Expand(1.5, 1.5, true); axMapControl1.Extent = featureExtent; axMapControl1.ActiveView.Refresh(); } else { MessageBox.Show("未找到匹配的要素"); } } private void button5_Click(object sender, EventArgs e) { string searchText = ""; if (!string.IsNullOrEmpty(textBox1.Text)) { searchText = textBox1.Text; } else if (!string.IsNullOrEmpty(comboBox1.Text)) { searchText = comboBox1.Text; } else { MessageBox.Show("请在文本框中输入地名或在组合框中选择地名"); return; } IFeatureLayer featureLayer = GetFeatureLayerByName("hlj1"); if (featureLayer == null) { MessageBox.Show("未找到hlj1图层"); return; } IFeatureClass featureClass = featureLayer.FeatureClass; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = "name='" + searchText + "'"; IFeatureCursor featureCursor = featureClass.Search(queryFilter, true); IFeature feature = featureCursor.NextFeature(); if (feature != null) { IFeatureSelection featureSelection = featureLayer as IFeatureSelection; if (featureSelection != null) { featureSelection.Clear(); } IQueryFilter selectFilter = new QueryFilterClass(); selectFilter.WhereClause = "name='" + searchText + "'"; if (featureSelection != null) { featureSelection.SelectFeatures(selectFilter, esriSelectionResultEnum.esriSelectionResultNew, false); } axMapControl1.FlashShape(feature.Shape); axMapControl1.Extent = feature.Extent; axMapControl1.ActiveView.Refresh(); MessageBox.Show("已高亮显示"); } else { MessageBox.Show("未找到匹配的要素: {searchText}"); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "ArcGIS地图文档 (*.mxd)|*.mxd"; switch (openFile.ShowDialog()) { case DialogResult.OK: axMapControl1.LoadMxFile(openFile.FileName); MessageBox.Show("地图加载成功!"); break; case DialogResult.Cancel: default: break; } } private void 放大ToolStripMenuItem_Click(object sender, EventArgs e) { axMapControl1.CurrentTool = cmdZoomIn as ITool; } private void 缩小ToolStripMenuItem_Click(object sender, EventArgs e) { axMapControl1.CurrentTool = cmdZoomOut as ITool; } private void button1_Click(object sender, EventArgs e) { Form2 form = new Form2(this); form.Show(); } private void axMapControl1_OnMouseDown(object sender, AxESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e) { if (isDrawing) { IPoint point = axMapControl1.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y); pointCollection.AddPoint(point); ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass(); markerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle; markerSymbol.Size = 8; markerSymbol.Color = GetRGBColor(255, 0, 0); IMarkerElement markerElement = new MarkerElementClass(); markerElement.Symbol = markerSymbol; pointElement = markerElement as IElement; pointElement.Geometry = point; graphicsContainer.AddElement(pointElement, 0); if (!string.IsNullOrEmpty(textBox2.Text)) { ITextSymbol textSymbol = new TextSymbolClass(); textSymbol.Color = GetRGBColor(0, 0, 0); textSymbol.Size = 10; IPoint textPoint = new PointClass(); textPoint.X = point.X; textPoint.Y = point.Y + 1000; ITextElement textElement = new TextElementClass(); textElement.Symbol = textSymbol; textElement.Text = textBox2.Text; IElement element = textElement as IElement; element.Geometry = textPoint; graphicsContainer.AddElement(element, 0); } if (pointCollection.PointCount > 1) { IPolyline polyline = new PolylineClass(); polyline.FromPoint = pointCollection.get_Point(pointCollection.PointCount - 2); polyline.ToPoint = point; ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Style = esriSimpleLineStyle.esriSLSSolid; lineSymbol.Width = 2; lineSymbol.Color = GetRGBColor(0, 0, 255); ILineElement lineElementClass = new LineElementClass(); lineElementClass.Symbol = lineSymbol; lineElement = lineElementClass as IElement; lineElement.Geometry = polyline; graphicsContainer.AddElement(lineElement, 0); } activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } } private IColor GetRGBColor(int red, int green, int blue) { IRgbColor rgbColor = new RgbColorClass(); rgbColor.Red = red; rgbColor.Green = green; rgbColor.Blue = blue; return rgbColor as IColor; } private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e) { } private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e) { } private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e) { } private void button7_Click(object sender, EventArgs e) { isDrawing = true; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair; pointCollection = new MultipointClass(); } private void button9_Click(object sender, EventArgs e) { graphicsContainer.DeleteAllElements(); activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } private void button8_Click(object sender, EventArgs e) { graphicsContainer.Reset(); activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } private void textBox2_TextChanged(object sender, EventArgs e) { } private void button6_Click(object sender, EventArgs e) { } private void button10_Click(object sender, EventArgs e) { isDrawing = false; axMapControl1.MousePointer = esriControlsMousePointer.esriPointerArrow; } } }这是改后的代码using System; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.SystemUI; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geometry; namespace _2023012505 { public partial class Form1 : Form { private ICommand cmdPan; private ICommand cmdFullExtent; private ICommand cmdZoomIn; private ICommand cmdZoomOut; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { axMapControl1.AddShapeFile("..\\222\\data", "hlj1.shp"); axMapControl1.AddShapeFile("..\\222\\data", "city.shp"); axTOCControl1.SetBuddyControl(axMapControl1); cmdPan = new ControlsMapPanToolClass(); cmdPan.OnCreate(axMapControl1.Object); cmdFullExtent = new ControlsMapFullExtentCommandClass(); cmdFullExtent.OnCreate(axMapControl1.Object); cmdZoomIn = new ControlsMapZoomInToolClass(); cmdZoomIn.OnCreate(axMapControl1.Object); cmdZoomOut = new ControlsMapZoomOutToolClass(); cmdZoomOut.OnCreate(axMapControl1.Object); LoadPlaceNames(); } private void LoadPlaceNames() { comboBox1.Items.Clear(); IFeatureLayer featureLayer = GetFeatureLayerByName("hlj1"); if (featureLayer == null) return; IFeatureClass featureClass = featureLayer.FeatureClass; IFeatureCursor featureCursor = featureClass.Search(null, true); IFeature feature = featureCursor.NextFeature(); while (feature != null) { int nameFieldIndex = feature.Fields.FindField("name"); if (nameFieldIndex >= 0) { object fieldValue = feature.get_Value(nameFieldIndex); if (fieldValue != null) { comboBox1.Items.Add(fieldValue.ToString()); } } feature = featureCursor.NextFeature(); } } private IFeatureLayer GetFeatureLayerByName(string layerName) { for (int i = 0; i < axMapControl1.LayerCount; i++) { if (axMapControl1.get_Layer(i).Name == layerName) { return axMapControl1.get_Layer(i) as IFeatureLayer; } } return null; } private void axTOCControl1_OnMouseDown(object sender, AxESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e) { } private void button4_Click(object sender, EventArgs e) { axMapControl1.CurrentTool = cmdPan as ITool; } private void button2_Click(object sender, EventArgs e) { axMapControl1.Extent = axMapControl1.FullExtent; axMapControl1.CurrentTool = null; } private void button3_Click(object sender, EventArgs e) { // 检查combobox是否有选择内容 if (string.IsNullOrEmpty(comboBox1.Text)) { MessageBox.Show("请先选择一个地名"); return; } // 获取对应的要素并高亮显示 IFeatureLayer featureLayer = GetFeatureLayerByName("hlj1"); if (featureLayer == null) { MessageBox.Show("未找到hlj1图层"); return; } IFeatureClass featureClass = featureLayer.FeatureClass; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = "name='" + comboBox1.Text + "'"; IFeatureCursor featureCursor = featureClass.Search(queryFilter, true); IFeature feature = featureCursor.NextFeature(); if (feature != null) { IFeatureSelection featureSelection = featureLayer as IFeatureSelection; if (featureSelection != null) { featureSelection.Clear(); } IQueryFilter selectFilter = new QueryFilterClass(); selectFilter.WhereClause = "name='" + comboBox1.Text + "'"; if (featureSelection != null) { featureSelection.SelectFeatures(selectFilter, esriSelectionResultEnum.esriSelectionResultNew, false); } axMapControl1.FlashShape(feature.Shape, 3, 500, null); IEnvelope featureExtent = feature.Extent; featureExtent.Expand(1.5, 1.5, true); axMapControl1.Extent = featureExtent; axMapControl1.ActiveView.Refresh(); } else { MessageBox.Show("未找到匹配的要素"); } } private void button5_Click(object sender, EventArgs e) { string searchText = ""; if (!string.IsNullOrEmpty(textBox1.Text)) { searchText = textBox1.Text; } else if (!string.IsNullOrEmpty(comboBox1.Text)) { searchText = comboBox1.Text; } else { MessageBox.Show("请在文本框中输入地名或在组合框中选择地名"); return; } IFeatureLayer featureLayer = GetFeatureLayerByName("hlj1"); if (featureLayer == null) { MessageBox.Show("未找到hlj1图层"); return; } IFeatureClass featureClass = featureLayer.FeatureClass; IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = "name='" + searchText + "'"; IFeatureCursor featureCursor = featureClass.Search(queryFilter, true); IFeature feature = featureCursor.NextFeature(); if (feature != null) { IFeatureSelection featureSelection = featureLayer as IFeatureSelection; if (featureSelection != null) { featureSelection.Clear(); } IQueryFilter selectFilter = new QueryFilterClass(); selectFilter.WhereClause = "name='" + searchText + "'"; if (featureSelection != null) { featureSelection.SelectFeatures(selectFilter, esriSelectionResultEnum.esriSelectionResultNew, false); } axMapControl1.FlashShape(feature.Shape); axMapControl1.Extent = feature.Extent; axMapControl1.ActiveView.Refresh(); MessageBox.Show("已高亮显示"); } else { MessageBox.Show("未找到匹配的要素: {searchText}"); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "ArcGIS地图文档 (*.mxd)|*.mxd"; switch (openFile.ShowDialog()) { case DialogResult.OK: axMapControl1.LoadMxFile(openFile.FileName); MessageBox.Show("地图加载成功!"); break; case DialogResult.Cancel: default: break; } } private void 放大ToolStripMenuItem_Click(object sender, EventArgs e) { axMapControl1.CurrentTool = cmdZoomIn as ITool; } private void 缩小ToolStripMenuItem_Click(object sender, EventArgs e) { axMapControl1.CurrentTool = cmdZoomOut as ITool; } private void button1_Click(object sender, EventArgs e) { Form2 form = new Form2(this); form.Show(); } private void axMapControl1_OnMouseDown(object sender, AxESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e) { } private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e) { } private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e) { } } }这是改前的代码,为什么改后会报错未处理 System.Runtime.InteropServices.COMException HResult=-2146827235 Message=指定的路径无效 Source=esriControls.MapControl.1 ErrorCode=-2146827235 StackTrace: 在 System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) 在 ESRI.ArcGIS.Controls.IMapControlDefault.AddShapeFile(String Path, String fileName) 在 AxESRI.ArcGIS.Controls.AxMapControl.AddShapeFile(String path, String fileName) 在 _2023012505.Form1.Form1_Load(Object sender, EventArgs e) 位置 D:\改\地信一班李健华2023012505\地信一班李健华2023012505\2023012505\form1.cs:行号 38 在 System.Windows.Forms.Form.OnLoad(EventArgs e) 在 System.Windows.Forms.Form.OnCreateControl() 在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) 在 System.Windows.Forms.Control.CreateControl() 在 System.Windows.Forms.Control.WmShowWindow(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.Form.WmShowWindow(Message& m) 在 System.Windows.Forms.Form.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) InnerException: ,而改前不会
最新发布
11-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值