SelectByAttrFrm.cs
上传用户:songlsx
上传日期:2022-06-19
资源大小:2227k
文件大小:13k
源码类别:

GIS编程

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using ESRI.ArcGIS.Carto;
  9. using ESRI.ArcGIS.Geodatabase;
  10. namespace LinGIS
  11. {
  12.     public partial class SelectByAttrFrm : DevComponents.DotNetBar.Office2007Form
  13.     {
  14.         public SelectByAttrFrm(MainFrm mainFrm)
  15.         {
  16.             InitializeComponent();
  17.             this.tempMainFrm = mainFrm;
  18.         }
  19.         MainFrm tempMainFrm;
  20.         IMap pMap;
  21.         IFeatureLayer pFeatureLayer;
  22.         ILayer pLayer;
  23.         ILayerFields pLayerFields;
  24.         IEnumLayer pEnumLayer;
  25.         #region 鼠标单击/双击生成Whereclause的部分
  26.         private void listBoxValues_DoubleClick(object sender, EventArgs e)
  27.         {
  28.             textBoxWhereClause.SelectedText = " " + listBoxValues.SelectedItem.ToString();
  29.         }
  30.         private void buttonEqual_Click(object sender, EventArgs e)
  31.         {
  32.             textBoxWhereClause.SelectedText = " = ";
  33.         }
  34.         private void buttonNotEqual_Click(object sender, EventArgs e)
  35.         {
  36.             textBoxWhereClause.SelectedText = " <> ";
  37.         }
  38.         private void buttonBig_Click(object sender, EventArgs e)
  39.         {
  40.             textBoxWhereClause.SelectedText = " > ";
  41.         }
  42.         private void buttonBigEqual_Click(object sender, EventArgs e)
  43.         {
  44.             textBoxWhereClause.SelectedText = " >= ";
  45.         }
  46.         private void buttonSmall_Click(object sender, EventArgs e)
  47.         {
  48.             textBoxWhereClause.SelectedText = " < ";
  49.         }
  50.         private void buttonSmallEqual_Click(object sender, EventArgs e)
  51.         {
  52.             textBoxWhereClause.SelectedText = " <= ";
  53.         }
  54.         private void buttonChars_Click(object sender, EventArgs e)
  55.         {
  56.             textBoxWhereClause.SelectedText = "%";
  57.         }
  58.         private void buttonChar_Click(object sender, EventArgs e)
  59.         {
  60.             textBoxWhereClause.SelectedText = "_";
  61.         }
  62.         private void buttonLike_Click(object sender, EventArgs e)
  63.         {
  64.             textBoxWhereClause.SelectedText = " Like ";
  65.         }
  66.         private void buttonAnd_Click(object sender, EventArgs e)
  67.         {
  68.             textBoxWhereClause.SelectedText = " And ";
  69.         }
  70.         private void buttonOr_Click(object sender, EventArgs e)
  71.         {
  72.             textBoxWhereClause.SelectedText = " Or ";
  73.         }
  74.         private void buttonNot_Click(object sender, EventArgs e)
  75.         {
  76.             textBoxWhereClause.SelectedText = " Not ";
  77.         }
  78.         private void buttonIs_Click(object sender, EventArgs e)
  79.         {
  80.             textBoxWhereClause.SelectedText = " Is ";
  81.         }
  82.         private void buttonBrace_Click(object sender, EventArgs e)
  83.         {
  84.             textBoxWhereClause.SelectedText = "(  )";
  85.             //让输入的位置恰好处在()里面,就同arcmap的效果一样
  86.             textBoxWhereClause.SelectionStart = textBoxWhereClause.Text.Length - 2;
  87.         }
  88.         #endregion
  89.         /// <summary>
  90.         /// 窗体初始化
  91.         /// </summary>
  92.         /// <param name="sender"></param>
  93.         /// <param name="e"></param>
  94.         private void SelectByAttrFrm_Load(object sender, EventArgs e)
  95.         {
  96.             
  97.             this.pMap = this.tempMainFrm.mainMapControl.Map;
  98.             if (this.pMap.LayerCount == 0) return;
  99.             this.pEnumLayer = this.pMap.get_Layers(null, true);
  100.             if (pEnumLayer == null)
  101.             {
  102.                 return;
  103.             }
  104.             pEnumLayer.Reset();
  105.             for (this.pLayer = pEnumLayer.Next(); this.pLayer != null; this.pLayer = pEnumLayer.Next())
  106.             {
  107.                 if (this.pLayer is IFeatureLayer)
  108.                 {
  109.                     this.comboBoxLayers.Items.Add(this.pLayer.Name);
  110.                 }
  111.             }
  112.             if (this.comboBoxLayers.Items.Count == 0)
  113.             {
  114.                 MessageBox.Show("没有可供选择的图层!");
  115.                 this.Close();
  116.                 return;
  117.             }
  118.             this.comboBoxLayers.SelectedIndex = 0;
  119.             this.comboBoxMethod.SelectedIndex = 0;
  120.         }
  121.         /// <summary>
  122.         /// 选择了别的图层
  123.         /// </summary>
  124.         /// <param name="sender"></param>
  125.         /// <param name="e"></param>
  126.         private void comboBoxLayers_SelectedIndexChanged(object sender, EventArgs e)
  127.         {
  128.             if (this.pEnumLayer == null) return;
  129.             
  130.             IField pField;
  131.             int currentFieldType;
  132.             this.pEnumLayer.Reset();
  133.             this.listBoxFields.Items.Clear();
  134.             for (this.pLayer = this.pEnumLayer.Next(); this.pLayer != null; this.pLayer = this.pEnumLayer.Next())
  135.             {
  136.                 if (this.pLayer.Name != this.comboBoxLayers.Text) continue;
  137.                 this.pLayerFields = this.pLayer as ILayerFields;
  138.                 for (int i = 0; i < this.pLayerFields.FieldCount; i++)
  139.                 {
  140.                     pField = this.pLayerFields.get_Field(i);
  141.                     currentFieldType = (int)pField.Type;
  142.                     if (currentFieldType > 5) continue;//不是可以查询的字段类型
  143.                     this.listBoxFields.Items.Add(pField.Name);
  144.                 }
  145.                 break;
  146.             }
  147.             this.pFeatureLayer = this.pLayer as IFeatureLayer;
  148.             this.labelLayer.Text = this.comboBoxLayers.Text;
  149.         }
  150.         /// <summary>
  151.         /// 双击字段名称
  152.         /// </summary>
  153.         /// <param name="sender"></param>
  154.         /// <param name="e"></param>
  155.         private void listBoxFields_DoubleClick(object sender, EventArgs e)
  156.         {
  157.             textBoxWhereClause.SelectedText = listBoxFields.SelectedItem.ToString() + " ";
  158.         }
  159.         /// <summary>
  160.         /// 获得当前字段的唯一值
  161.         /// </summary>
  162.         /// <param name="sender"></param>
  163.         /// <param name="e"></param>
  164.         private void buttonGetValue_Click(object sender, EventArgs e)
  165.         {
  166.             try
  167.             {
  168.                 if (this.listBoxFields.SelectedIndex == -1) return;
  169.                 string currentFieldName = this.listBoxFields.Text;//当前字段名
  170.                 string currentLayerName=this.comboBoxLayers.Text;
  171.                 this.pEnumLayer.Reset();
  172.                 for (this.pLayer = this.pEnumLayer.Next(); this.pLayer != null; this.pLayer = this.pEnumLayer.Next())
  173.                 {
  174.                     if (this.pLayer.Name == currentLayerName) break;
  175.                 }
  176.                 this.pLayerFields = this.pLayer as ILayerFields;
  177.                 IField pField = this.pLayerFields.get_Field(this.pLayerFields.FindField(currentFieldName));
  178.                 esriFieldType pFieldType = pField.Type;
  179.                 //对Table中当前字段进行排序,把结果赋给Cursor
  180.                 ITable pTable = this.pLayer as ITable;
  181.                 ITableSort pTableSort = new TableSortClass();
  182.                 pTableSort.Table = pTable;
  183.                 pTableSort.Fields = currentFieldName;
  184.                 pTableSort.set_Ascending(currentFieldName, true);
  185.                 pTableSort.set_CaseSensitive(currentFieldName, true);
  186.                 pTableSort.Sort(null);//排序
  187.                 ICursor pCursor = pTableSort.Rows;
  188.                 //字段统计
  189.                 IDataStatistics pDataStatistics = new DataStatisticsClass();
  190.                 pDataStatistics.Cursor = pCursor;
  191.                 pDataStatistics.Field = currentFieldName;
  192.                 System.Collections.IEnumerator pEnumeratorUniqueValues = pDataStatistics.UniqueValues;//唯一值枚举
  193.                 int uniqueValueCount = pDataStatistics.UniqueValueCount;//唯一值的个数
  194.                 this.listBoxValues.Items.Clear();
  195.                 string currentValue = null;
  196.                 pEnumeratorUniqueValues.Reset();
  197.                 if (pFieldType == esriFieldType.esriFieldTypeString)
  198.                 {
  199.                     while (pEnumeratorUniqueValues.MoveNext())
  200.                     {
  201.                         currentValue = pEnumeratorUniqueValues.Current.ToString();
  202.                         this.listBoxValues.Items.Add("'" + currentValue + "'");
  203.                     }
  204.                 }
  205.                 else
  206.                 {
  207.                     while (pEnumeratorUniqueValues.MoveNext())
  208.                     {
  209.                         currentValue = pEnumeratorUniqueValues.Current.ToString();
  210.                         this.listBoxValues.Items.Add(currentValue);
  211.                     }
  212.                 }
  213.             }
  214.             catch (Exception ex)
  215.             {
  216.                 MessageBox.Show(ex.Message);
  217.             }
  218.         }
  219.         /// <summary>
  220.         /// 清空
  221.         /// </summary>
  222.         /// <param name="sender"></param>
  223.         /// <param name="e"></param>
  224.         private void buttonClear_Click(object sender, EventArgs e)
  225.         {
  226.             textBoxWhereClause.Clear();
  227.         }
  228.         private void buttonApply_Click(object sender, EventArgs e)
  229.         {
  230.             if (textBoxWhereClause.Text == "")
  231.             {
  232.                 MessageBox.Show("请生成查询语句!");
  233.                 return;
  234.             }
  235.             try
  236.             {
  237.                 IQueryFilter pQueryFilter = new QueryFilterClass();
  238.                 pQueryFilter.WhereClause = textBoxWhereClause.Text;
  239.                 
  240.                 IFeatureSelection pFeatureSelection = this.pFeatureLayer as IFeatureSelection;
  241.                 int iSelectedFeaturesCount = pFeatureSelection.SelectionSet.Count;
  242.                 esriSelectionResultEnum selectMethod;
  243.                 switch (comboBoxMethod.SelectedIndex)
  244.                 {
  245.                     case 0: selectMethod = esriSelectionResultEnum.esriSelectionResultNew; break;
  246.                     case 1: selectMethod = esriSelectionResultEnum.esriSelectionResultAdd; break;
  247.                     case 2: selectMethod = esriSelectionResultEnum.esriSelectionResultSubtract; break;
  248.                     case 3: selectMethod = esriSelectionResultEnum.esriSelectionResultAnd; break;
  249.                     default: selectMethod = esriSelectionResultEnum.esriSelectionResultNew; break;
  250.                 }
  251.                 pFeatureSelection.SelectFeatures(pQueryFilter, selectMethod, false);//执行查询
  252.                 //如果本次查询后,查询的结果数目没有改变,则认为本次查询没有产生新的结果
  253.                 if (pFeatureSelection.SelectionSet.Count == iSelectedFeaturesCount || pFeatureSelection.SelectionSet.Count == 0)
  254.                 {
  255.                     MessageBox.Show("没有符合本次查询条件的结果!");
  256.                     return;
  257.                 }
  258.                 ////如果复选框被选中,则定位到选择结果
  259.                 //if (checkBoxZoomtoSelected.Checked == true)
  260.                 //{
  261.                 //    IEnumFeature pEnumFeature = MainAxMapControl.Map.FeatureSelection as IEnumFeature;
  262.                 //    IFeature pFeature = pEnumFeature.Next();
  263.                 //    IEnvelope pEnvelope = new EnvelopeClass();
  264.                 //    while (pFeature != null)
  265.                 //    {
  266.                 //        pEnvelope.Union(pFeature.Extent);
  267.                 //        pFeature = pEnumFeature.Next();
  268.                 //    }
  269.                 //    MainAxMapControl.ActiveView.Extent = pEnvelope;
  270.                 //    MainAxMapControl.ActiveView.Refresh();//如果不这样刷新,只要查询前地图已经被放大所效果的话,定位后
  271.                 //    //底图没有刷新,选择集倒是定位和刷新了
  272.                 //}
  273.                 //else MainAxMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
  274.                 this.tempMainFrm.mainMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
  275.                 //double i = MainAxMapControl.Map.SelectionCount;
  276.                 //i = Math.Round(i, 0);//小数点后指定为0位数字
  277.                 //pMainform.toolStripStatusLabel1.Text = "当前共有" + i.ToString() + "个查询结果";
  278.             }
  279.             catch (Exception ex)
  280.             {
  281.                 MessageBox.Show("您的查询语句可能有误,请检查 | " + ex.Message);
  282.                 return;
  283.             }
  284.         }
  285.         private void buttonOk_Click(object sender, EventArgs e)
  286.         {
  287.             this.buttonAnd_Click(sender, e);
  288.             this.Dispose();
  289.         }
  290.         private void buttonCancel_Click(object sender, EventArgs e)
  291.         {
  292.             this.Dispose();
  293.         }
  294.         private void listBoxFields_SelectedIndexChanged(object sender, EventArgs e)
  295.         {
  296.             this.listBoxValues.Items.Clear();
  297.         }
  298.         
  299.     }
  300. }