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

GIS编程

开发平台:

Visual C++

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using ESRI.ArcGIS.Carto;
  10. using ESRI.ArcGIS.Display;
  11. using ESRI.ArcGIS.esriSystem;
  12. using ESRI.ArcGIS.Geodatabase;
  13. using ESRI.ArcGIS.Geometry;
  14. using ESRI.ArcGIS.SystemUI;
  15. namespace LinGIS
  16. {
  17.     public partial class FeatLyrFrm : DevComponents.DotNetBar.Office2007Form
  18.     {
  19.         private ILayer pLayer;
  20.         public ILayer Layer
  21.         {
  22.             set
  23.             {
  24.                 this.pLayer = value;
  25.             }
  26.         }
  27.         public FeatLyrFrm()
  28.         {
  29.             InitializeComponent();
  30.         }
  31.         private void FeatLyrFrm_Load(object sender, EventArgs e)
  32.         {
  33.         }
  34.         private void tabCtrlLayer_SelectedIndexChanged(object sender, EventArgs e)
  35.         {
  36.             //MessageBox.Show(this.tpgFields.SelectedTab.Name);
  37.         }
  38.         private void btnCancel_Click(object sender, EventArgs e)
  39.         {
  40.             this.Close();
  41.         }
  42.         private void btnOK_Click(object sender, EventArgs e)
  43.         {
  44.             try
  45.             {
  46.                 this.resultGeneralTab();
  47.                 this.resultSelectionTab();
  48.                 this.resultSymbologyTab();
  49.                 this.Close();
  50.             }
  51.             catch (Exception ex)
  52.             {
  53.                 MessageBox.Show(ex.Message);
  54.             }
  55.         }
  56.         private void FeatLyrFrm_Shown(object sender, EventArgs e)
  57.         {
  58.             this.loadGeneralTab();
  59.             this.loadSelectionTab();
  60.             this.loadSourceTab();
  61.             this.loadFieldsTab();
  62.             this.loadSymbologyTab();
  63.         }
  64.         #region GerneralTab
  65.         private void loadGeneralTab()
  66.         {
  67.             this.txtLayerName.Text = this.pLayer.Name;
  68.             this.txtLayerDescription.Text = ((ILayerGeneralProperties)pLayer).LayerDescription;
  69.             this.ckbVisible.Checked = pLayer.Visible;
  70.             if (pLayer.MaximumScale == 0 && pLayer.MinimumScale == 0)
  71.             {
  72.                 this.rbnNone.Checked = true;
  73.                 this.cbbMaxScale.Enabled = false;
  74.                 this.cbbMinScale.Enabled = false;
  75.             }
  76.             else
  77.             {
  78.                 this.rbnRange.Checked = true;
  79.                 this.cbbMaxScale.Enabled = true;
  80.                 this.cbbMaxScale.Text = pLayer.MaximumScale.ToString();
  81.                 this.cbbMinScale.Enabled = true;
  82.                 this.cbbMinScale.Text = pLayer.MinimumScale.ToString();
  83.             }
  84.         }
  85.         private void rbnNone_CheckedChanged(object sender, EventArgs e)
  86.         {
  87.             if (this.rbnNone.Checked == true)
  88.             {
  89.                 this.cbbMinScale.Enabled = false;
  90.                 this.cbbMaxScale.Enabled = false;
  91.             }
  92.             else
  93.             {
  94.                 this.cbbMinScale.Enabled = true;
  95.                 this.cbbMaxScale.Enabled = true;
  96.             }
  97.         }
  98.         private void resultGeneralTab()
  99.         {
  100.             pLayer.Name = this.txtLayerName.Text;
  101.             ((ILayerGeneralProperties)pLayer).LayerDescription = this.txtLayerDescription.Text;
  102.             if (this.rbnNone.Checked)
  103.             {
  104.                 pLayer.MinimumScale = 0;
  105.                 pLayer.MaximumScale = 0;
  106.             }
  107.             else if (this.rbnRange.Checked)
  108.             {
  109.                 pLayer.MaximumScale = double.Parse(this.cbbMaxScale.Text);
  110.                 pLayer.MinimumScale = double.Parse(this.cbbMinScale.Text);
  111.             }
  112.         }
  113.         #endregion
  114.         #region SourceTab
  115.         private void loadSourceTab()
  116.         {
  117.             //Extent
  118.             IEnvelope pEnvelope = ((IGeoDataset)((IFeatureLayer)pLayer).FeatureClass).Extent;
  119.             this.txtExtentTop.Text = pEnvelope.YMax.ToString();
  120.             this.txtExtentButtom.Text = pEnvelope.YMin.ToString();
  121.             this.txtExtentLeft.Text = pEnvelope.XMin.ToString();
  122.             this.txtExtentRight.Text = pEnvelope.XMax.ToString();
  123.             //DataSource
  124.             StringBuilder strDataSource = new StringBuilder();
  125.             strDataSource.Append("DataType:");
  126.             IFeatureLayer pFeatureLayer = (IFeatureLayer)this.pLayer;
  127.             strDataSource.Append(pFeatureLayer.DataSourceType);
  128.             IDataLayer2 pDataLayer2 = (IDataLayer2)this.pLayer;
  129.             strDataSource.Append(":n");
  130.             strDataSource.Append(pDataLayer2.DataSourceName.NameString);
  131.             IGeoDataset pGeoDataset = (IGeoDataset)this.pLayer;
  132.             ISpatialReference pSpatialReference = pGeoDataset.SpatialReference;
  133.             IProjectedCoordinateSystem5 pProCoorSys = pSpatialReference as IProjectedCoordinateSystem5;
  134.             if (pProCoorSys != null)
  135.             {
  136.                 strDataSource.Append("nProjected Corrdinate System:");
  137.                 strDataSource.Append(pProCoorSys.Name);
  138.                 strDataSource.Append("nProjection:");
  139.                 strDataSource.Append(pProCoorSys.Projection.Name);
  140.                 strDataSource.Append("nFalse_Easting:");
  141.                 strDataSource.Append(pProCoorSys.FalseEasting);
  142.                 //strDataSource.AppendFormat("F8", pProCoorSys.FalseEasting);
  143.                 strDataSource.Append("nFalse_Northing:");
  144.                 strDataSource.Append(pProCoorSys.FalseNorthing);
  145.                 strDataSource.Append("nCentral_Meridian:");
  146.                 strDataSource.Append(pProCoorSys.get_CentralMeridian(true));
  147.                 strDataSource.Append("nStandard)Parallel_1:");
  148.                 strDataSource.Append(pProCoorSys.StandardParallel1);
  149.                 strDataSource.Append("nStandard_Parallel_2:");
  150.                 strDataSource.Append(pProCoorSys.StandardParallel2);
  151.                 strDataSource.Append("nLatitude_Of_Origin:");
  152.                 strDataSource.Append(pProCoorSys.LatitudeOfOrigin);
  153.                 strDataSource.Append("nLinear_Unit:");
  154.                 strDataSource.Append(pProCoorSys.CoordinateUnit.Name);
  155.                 strDataSource.Append("n");
  156.             }
  157.             //IGeographicCoordinateSystem pGeoCoorSys = pProCoorSys.GeographicCoordinateSystem;
  158.             IGeographicCoordinateSystem pGeoCoorSys = pSpatialReference as IGeographicCoordinateSystem;
  159.             if (pGeoCoorSys != null)
  160.             {
  161.                 strDataSource.Append("nGeographic Coordinate System:");
  162.                 strDataSource.Append(pGeoCoorSys.Name);
  163.                 strDataSource.Append("nDatum:");
  164.                 strDataSource.Append(pGeoCoorSys.Datum.Name);
  165.                 strDataSource.Append("nPrime_Meridian:");
  166.                 strDataSource.Append(pGeoCoorSys.PrimeMeridian.Longitude);
  167.                 strDataSource.Append("nAangular_Unit:");
  168.                 strDataSource.Append(pGeoCoorSys.CoordinateUnit.Name);
  169.             }
  170.             this.txtDataSource.Text = strDataSource.ToString();
  171.         }
  172.         #endregion
  173.         #region SelectionTab
  174.         private void loadSelectionTab()
  175.         {
  176.             IFeatureSelection pFeatureSelection = this.pLayer as IFeatureSelection;
  177.             if (pFeatureSelection.SetSelectionSymbol == true)
  178.             {
  179.                 this.rbnSelectionSymbol.Checked = true;
  180.                 this.selectionSymbol = pFeatureSelection.SelectionSymbol;
  181.             }
  182.             else
  183.             {
  184.                 this.rbnSelectionDefault.Checked = true;
  185.             }
  186.         }
  187.         private ISymbol selectionSymbol;
  188.         private void btnSelectionSymbol_Click(object sender, EventArgs e)
  189.         {
  190.             SymbolSelectorFrm selectionSymbolSelectorFrm = new SymbolSelectorFrm(null, this.pLayer);
  191.             if (selectionSymbolSelectorFrm.ShowDialog() == DialogResult.OK)
  192.             {
  193.                 this.rbnSelectionSymbol.Checked = true;
  194.                 this.btnSelectionSymbol.BackgroundImage = selectionSymbolSelectorFrm.pSymbolImage;
  195.                 this.selectionSymbol = selectionSymbolSelectorFrm.pSymbol;
  196.             }
  197.         }
  198.         private void btnSelectionColor_Click(object sender, EventArgs e)
  199.         {
  200.             if (this.colorDialog.ShowDialog() == DialogResult.OK)
  201.             {
  202.                 this.rbnSelectionColor.Checked = true;
  203.                 this.btnSelectionColor.BackColor = this.colorDialog.Color;
  204.             }
  205.         }
  206.         /// <summary>
  207.         /// 将.NET中的Color结构转换至于ArcGIS Engine中的IColor接口
  208.         /// </summary>
  209.         /// <param name="color">.NET中的System.Drawing.Color结构表示ARGB颜色</param>
  210.         /// <returns>IColor</returns>
  211.         public IColor ConvertColorToIColor(Color color)
  212.         {
  213.             IColor pColor = new RgbColorClass();
  214.             pColor.RGB = color.B * 65536 + color.G * 256 + color.R;
  215.             return pColor;
  216.         }
  217.         /// <summary>
  218.         /// 将.NET中的Color结构转换至于ArcGIS Engine中的IRgbColor接口
  219.         /// </summary>
  220.         /// <param name="color">.NET中的System.Drawing.Color结构表示ARGB颜色</param>
  221.         /// <returns>IRgbColor</returns>
  222.         private void resultSelectionTab()
  223.         {
  224.             IFeatureSelection pFeatureSeletion = (IFeatureSelection)this.pLayer;
  225.             if (this.rbnSelectionDefault.Checked == true)
  226.             {
  227.                 pFeatureSeletion.SetSelectionSymbol = false;
  228.             }
  229.             else if (this.rbnSelectionSymbol.Checked == true)
  230.             {
  231.                 pFeatureSeletion.SetSelectionSymbol = true;
  232.                 pFeatureSeletion.SelectionSymbol = this.selectionSymbol;
  233.             }
  234.             else if (this.rbnSelectionColor.Checked == true)
  235.             {
  236.                 pFeatureSeletion.SetSelectionSymbol = true;
  237.                 pFeatureSeletion.SelectionColor = this.ConvertColorToIColor(this.btnSelectionColor.BackColor);
  238.             }
  239.         }
  240.         #endregion
  241.         #region FieldsTab
  242.         string[] strfieldType = {"Short Integer","Long Integer","Float","Double",
  243.                                  "Text","Date","Object ID","Geometry",
  244.                                  "Blob","Raster","GUID","GlobalID"};
  245.         private void loadFieldsTab()
  246.         {
  247.             this.lsvFields.Items.Clear();
  248.             ITable pTable = this.pLayer as ITable;
  249.             IFields pFields = pTable.Fields;
  250.             IField pField;
  251.             //string fieldType;
  252.             for (int i = 0; i < pFields.FieldCount; i++)
  253.             {
  254.                 pField = pFields.get_Field(i);
  255.                 this.lsvFields.Items.Add(pField.Name);
  256.                 this.lsvFields.Items[i].SubItems.Add(pField.AliasName);
  257.                 this.lsvFields.Items[i].SubItems.Add(this.strfieldType[(int)pField.Type]);
  258.                 this.lsvFields.Items[i].SubItems.Add(pField.Length.ToString());
  259.                 this.lsvFields.Items[i].SubItems.Add(pField.Precision.ToString());
  260.                 this.lsvFields.Items[i].SubItems.Add(pField.Scale.ToString());
  261.             }
  262.         }
  263.         #endregion
  264.         #region SymbologyTab
  265.         
  266.         private ISymbol singleSymbol;
  267.         private void loadSymbologyTab()
  268.         {
  269.             IFeatureRenderer pFeatureRender = ((IGeoFeatureLayer)this.pLayer).Renderer;
  270.             if (pFeatureRender is ISimpleRenderer)
  271.             {
  272.                 this.trvSymbologyShows.SelectedNode = this.trvSymbologyShows.Nodes[0].Nodes[0];
  273.                 this.singleSymbol = ((ISimpleRenderer)pFeatureRender).Symbol;
  274.             }
  275.             else if (pFeatureRender is IUniqueValueRenderer)
  276.             {
  277.                 this.trvSymbologyShows.SelectedNode = this.trvSymbologyShows.Nodes[1].Nodes[0];
  278.             }
  279.             else if (pFeatureRender is IClassBreaksRenderer)
  280.             {
  281.                 this.trvSymbologyShows.SelectedNode = this.trvSymbologyShows.Nodes[2].Nodes[0];
  282.                 this.cbbClassBreakField.SelectedIndex = this.cbbClassBreakField.FindString(((IClassBreaksRenderer)pFeatureRender).Field);
  283.             }
  284.         }
  285.         private void btnSingleSymbol_Click(object sender, EventArgs e)
  286.         {
  287.             SymbolSelectorFrm SingleSymbolSelctor = new SymbolSelectorFrm(null, this.pLayer);
  288.             if (SingleSymbolSelctor.ShowDialog() == DialogResult.OK)
  289.             {
  290.                 this.btnSingleSymbol.BackgroundImage = SingleSymbolSelctor.pSymbolImage;
  291.                 this.singleSymbol = SingleSymbolSelctor.pSymbol;
  292.             }
  293.         }
  294.         private void resultSymbologyTab()
  295.         {
  296.             IGeoFeatureLayer pGeoFeatureLayer = this.pLayer as IGeoFeatureLayer;
  297.             TreeNode currentNode = this.trvSymbologyShows.SelectedNode;
  298.             if (currentNode.Text == "单一符号")
  299.             {
  300.                 if (this.singleSymbol == null)
  301.                 {
  302.                     return;
  303.                 }
  304.                 ISimpleRenderer pSimpleRender = new SimpleRendererClass();
  305.                 pSimpleRender.Symbol = this.singleSymbol;
  306.                 pSimpleRender.Label = this.txtSingleSymbolLabel.Text;
  307.                 pSimpleRender.Description = this.txtSingleSymbolDescription.Text;
  308.                 pGeoFeatureLayer.Renderer = pSimpleRender as IFeatureRenderer;
  309.             }
  310.             else if (currentNode.Text == "唯一值")
  311.             {
  312.                 if (this.lsvUniqueValue.Items.Count == 0 || this.pUniValueColorRamp == null)
  313.                 {
  314.                     return;
  315.                 }
  316.                 this.pUniValueColorRamp.Size = this.lsvUniqueValue.Items.Count - 1;
  317.                 bool IsColorRampCreatedOK = false;
  318.                 this.pUniValueColorRamp.CreateRamp(out IsColorRampCreatedOK);
  319.                 if (IsColorRampCreatedOK)
  320.                 {
  321.                     IEnumColors pEnumColors = pUniValueColorRamp.Colors;
  322.                     pEnumColors.Reset();
  323.                     IUniqueValueRenderer pUniqueValueRender = new UniqueValueRendererClass();
  324.                     pUniqueValueRender.FieldCount = 1;
  325.                     pUniqueValueRender.set_Field(0, this.cbbUniValueField.Text);
  326.                     IColor pColor;
  327.                     if (((IFeatureLayer2)this.pLayer).ShapeType == esriGeometryType.esriGeometryPolygon)
  328.                     {
  329.                         ISimpleFillSymbol pSimpleFillSymbol;
  330.                         for (int i = 0; i < pUniValueColorRamp.Size; i++)
  331.                         {
  332.                             pColor = pEnumColors.Next();
  333.                             pSimpleFillSymbol = new SimpleFillSymbolClass();
  334.                             pSimpleFillSymbol.Color = pColor;
  335.                             pUniqueValueRender.AddValue(this.lsvUniqueValue.Items[i + 1].Text, "", (ISymbol)pSimpleFillSymbol);
  336.                         }
  337.                     }
  338.                     else if (((IFeatureLayer2)this.pLayer).ShapeType == esriGeometryType.esriGeometryPolyline)
  339.                     {
  340.                         ISimpleLineSymbol pSimpleLineSymbol;
  341.                         for (int i = 0; i < pUniValueColorRamp.Size; i++)
  342.                         {
  343.                             pColor = pEnumColors.Next();
  344.                             pSimpleLineSymbol = new SimpleLineSymbolClass();
  345.                             pSimpleLineSymbol.Color = pColor;
  346.                             pUniqueValueRender.AddValue(this.lsvUniqueValue.Items[i + 1].Text, "", (ISymbol)pSimpleLineSymbol);
  347.                         }
  348.                     }
  349.                     else if (((IFeatureLayer2)this.pLayer).ShapeType == esriGeometryType.esriGeometryPoint)
  350.                     {
  351.                         ISimpleMarkerSymbol pSimpleMarkerSymbol;
  352.                         for (int i = 0; i < pUniValueColorRamp.Size; i++)
  353.                         {
  354.                             pColor = pEnumColors.Next();
  355.                             pSimpleMarkerSymbol = new SimpleMarkerSymbolClass();
  356.                             pSimpleMarkerSymbol.Color = pColor;
  357.                             pUniqueValueRender.AddValue(this.lsvUniqueValue.Items[i + 1].Text, "", (ISymbol)pSimpleMarkerSymbol);
  358.                         }
  359.                     }
  360.                     pGeoFeatureLayer.Renderer = (IFeatureRenderer)pUniqueValueRender;
  361.                 }
  362.             }
  363.             else if (currentNode.Text == "分级颜色")
  364.             {
  365.                 if (this.lsvClassBreaksSymbol.Items.Count == 0 || this.pClassBreaksColorRamp == null)
  366.                 {
  367.                     return;
  368.                 }
  369.                 int classCount = int.Parse(this.cbbClassBreaksCount.Text);
  370.                 
  371.                 IClassBreaksRenderer pClassBreaksRenderer = new ClassBreaksRendererClass();
  372.                 pClassBreaksRenderer.BreakCount = classCount;
  373.                 pClassBreaksRenderer.Field = this.cbbClassBreakField.Text;
  374.                 pClassBreaksRenderer.SortClassesAscending = true;
  375.                 IColorRamp pColorRamp = this.pClassBreaksColorRamp;
  376.                 pColorRamp.Size = classCount;
  377.                 bool ok;
  378.                 pColorRamp.CreateRamp(out ok);
  379.                 if (!ok)
  380.                 {
  381.                     return;
  382.                 }
  383.                 IEnumColors pEnumColors = pColorRamp.Colors;
  384.                 pEnumColors.Reset();
  385.                 IColor pColor;
  386.                 if (((IFeatureLayer2)this.pLayer).ShapeType == esriGeometryType.esriGeometryPolygon)
  387.                 {
  388.                     for (int i = 0; i < classCount; i++)//为每个值范围设置符号(此处为SimpleFillSymbol)
  389.                     {
  390.                         pColor = pEnumColors.Next();
  391.                         ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass();
  392.                         pSimpleFillSymbol.Color = pColor;
  393.                         pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
  394.                         
  395.                         pClassBreaksRenderer.set_Break(i, this.classBreaks[i + 1]);//设置临界值,注意下标,关键!!!
  396.                         pClassBreaksRenderer.set_Symbol(i, (ISymbol)pSimpleFillSymbol);//设置Symbol,关键!!!
  397.                         pClassBreaksRenderer.set_Label(i, this.lsvClassBreaksSymbol.Items[i].Text);
  398.                         pClassBreaksRenderer.set_Description(i, this.lsvClassBreaksSymbol.Items[i].Text);
  399.                     }
  400.                 }
  401.                 else if (((IFeatureLayer2)this.pLayer).ShapeType == esriGeometryType.esriGeometryPolyline)
  402.                 {
  403.                     for (int i = 0; i < classCount; i++)//为每个值范围设置符号
  404.                     {
  405.                         pColor = pEnumColors.Next();
  406.                         ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbolClass();
  407.                         pSimpleLineSymbol.Color = pColor;
  408.                         pClassBreaksRenderer.set_Symbol(i, (ISymbol)pSimpleLineSymbol);//设置Symbol,关键!!!
  409.                         pClassBreaksRenderer.set_Label(i, this.lsvClassBreaksSymbol.Items[i].Text);
  410.                         pClassBreaksRenderer.set_Break(i, this.classBreaks[i + 1]);//设置临界值,注意下标,关键!!!
  411.                     }
  412.                 }
  413.                 else if (((IFeatureLayer2)this.pLayer).ShapeType == esriGeometryType.esriGeometryPoint)
  414.                 {
  415.                     for (int i = 0; i < classCount; i++)//为每个值范围设置符号
  416.                     {
  417.                         pColor = pEnumColors.Next();
  418.                         ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbolClass();
  419.                         pSimpleMarkerSymbol.Color = pColor;
  420.                         pClassBreaksRenderer.set_Symbol(i, (ISymbol)pSimpleMarkerSymbol);//设置Symbol,关键!!!
  421.                         pClassBreaksRenderer.set_Label(i, this.lsvClassBreaksSymbol.Items[i].Text);
  422.                         pClassBreaksRenderer.set_Break(i, this.classBreaks[i + 1]);//设置临界值,注意下标,关键!!!
  423.                     }
  424.                 }
  425.                 pGeoFeatureLayer.Renderer = pClassBreaksRenderer as IFeatureRenderer;
  426.             }
  427.         }
  428.         #endregion
  429.         private void trvSymbologyShows_AfterSelect(object sender, TreeViewEventArgs e)
  430.         {
  431.             if (e.Node.Level == 0)
  432.             {
  433.                 e.Node.ExpandAll();
  434.                 this.trvSymbologyShows.SelectedNode = e.Node.NextVisibleNode;
  435.             }
  436.             else
  437.             {
  438.                 string fieldType;
  439.                 if (e.Node.Text == "单一符号")
  440.                 {
  441.                     this.panelUniValueSymbol.Visible = false;
  442.                     this.panelClassBreaksSymbol.Visible = false;
  443.                     this.panelSingleSymbol.Location = new System.Drawing.Point(170, 4);
  444.                     this.panelSingleSymbol.Visible = true;
  445.                 }
  446.                 else if (e.Node.Text == "唯一值")
  447.                 {
  448.                     this.panelSingleSymbol.Visible = false;
  449.                     this.panelClassBreaksSymbol.Visible = false;
  450.                     this.panelUniValueSymbol.Location = new System.Drawing.Point(170, 4);
  451.                     this.panelUniValueSymbol.BringToFront();
  452.                     this.panelUniValueSymbol.Visible = true;
  453.                     //this.lsvUniqueValue.Items.Clear();
  454.                     this.cbbUniValueField.Items.Clear();
  455.                     this.cbbClassBreakField.Items.Add("无");
  456.                     for (int i = 0; i < this.lsvFields.Items.Count; i++)
  457.                     {
  458.                         fieldType = this.lsvFields.Items[i].SubItems[2].Text;
  459.                         if (fieldType != "Geometry" && fieldType != "Object ID" && fieldType != "Raster" && fieldType != "Blob")
  460.                         {
  461.                             this.cbbUniValueField.Items.Add(this.lsvFields.Items[i].Text);
  462.                         }
  463.                     }
  464.                     this.cbbUniValueField.SelectedIndex = 0;
  465.                 }
  466.                 else if (e.Node.Text == "分级颜色")
  467.                 {
  468.                     this.panelSingleSymbol.Visible = false;
  469.                     this.panelUniValueSymbol.Visible = false;
  470.                     this.panelClassBreaksSymbol.Location = new System.Drawing.Point(170, 4);
  471.                     this.panelClassBreaksSymbol.Visible = true;
  472.                     this.cbbClassBreakField.Items.Clear();
  473.                     //this.cbbClassBreakNomalization.Items.Clear();
  474.                     this.cbbClassBreakField.Items.Add("无");
  475.                     for (int i = 0; i < this.lsvFields.Items.Count; i++)
  476.                     {
  477.                         fieldType = this.lsvFields.Items[i].SubItems[2].Text;
  478.                         if (fieldType == "Short Integer" || fieldType == "Long Integer" || fieldType == "Float" || fieldType == "Double")
  479.                         {
  480.                             this.cbbClassBreakField.Items.Add(this.lsvFields.Items[i].Text);
  481.                             //this.cbbClassBreakNomalization.Items.Add(this.lsvFields.Items[i].Text);
  482.                         }
  483.                     }
  484.                     this.cbbClassBreakField.SelectedIndex = 0;
  485.                     this.cbbClassBreakNomalization.SelectedIndex = 0;
  486.                 }
  487.                 
  488.             }
  489.         }
  490.         /// <summary>
  491.         /// 按下"添加所有值"按钮后,让table按当前字段的值进行排序,并把信息添加到listView里面
  492.         /// </summary>
  493.         /// <param name="sender"></param>
  494.         /// <param name="e"></param>
  495.         private void btnUniValueAddAllValues_Click(object sender, EventArgs e)
  496.         {
  497.             try
  498.             {
  499.                 string currentFieldName = this.cbbUniValueField.Text;//当前字段名
  500.                 string currentFieldType = this.lsvFields.FindItemWithText(currentFieldName).SubItems[2].Text;//当前字段类型
  501.                 bool currentTypeIsNumeric = false;//判断当前字段类型是否为数字类型
  502.                 if (currentFieldType == "Short Integer" || currentFieldType == "Long Integer" || currentFieldType == "Float" || currentFieldType == "Double")
  503.                 {
  504.                     currentTypeIsNumeric = true;
  505.                 }
  506.                 this.lsvUniqueValue.Items.Clear();
  507.                 //对Table中当前字段进行排序,把结果赋给Cursor
  508.                 ITable pTable = this.pLayer as ITable;
  509.                 ITableSort pTableSort = new TableSortClass();
  510.                 pTableSort.Table = pTable;
  511.                 pTableSort.Fields = currentFieldName;
  512.                 pTableSort.set_Ascending(currentFieldName, true);
  513.                 pTableSort.set_CaseSensitive(currentFieldName, true);
  514.                 pTableSort.Sort(null);//排序
  515.                 ICursor pCursor = pTableSort.Rows;
  516.                 //字段统计
  517.                 IDataStatistics pDataStatistics = new DataStatisticsClass();
  518.                 pDataStatistics.Cursor = pCursor;
  519.                 pDataStatistics.Field = currentFieldName;
  520.                 System.Collections.IEnumerator pEnumeratorUniqueValues = pDataStatistics.UniqueValues;//唯一值枚举
  521.                 int uniqueValueCount = pDataStatistics.UniqueValueCount;//唯一值的个数
  522.                 //table中当前字段有值(不为null)的row的个数,并把信息添加到listView的第一行
  523.                  IQueryFilter pQueryFilter = new QueryFilterClass();
  524.                 pQueryFilter.AddField(currentFieldName);
  525.                 int valueSum = pTable.RowCount(pQueryFilter);
  526.                 this.lsvUniqueValue.Items.Add(currentFieldName);
  527.                 this.lsvUniqueValue.Items[0].SubItems.Add(currentFieldName);
  528.                 this.lsvUniqueValue.Items[0].SubItems.Add(valueSum.ToString());
  529.                 //循环把信息添加到listView里
  530.                 int i = 1;//注意!是从1开始,因为第一行已经被占用
  531.                 string currentValue = null;//指示当前的值
  532.                 //IDataStatistics pUniValueStatistics = new DataStatisticsClass();
  533.                 int currentValueCount;
  534.                 for (pEnumeratorUniqueValues.Reset(); pEnumeratorUniqueValues.MoveNext(); i++)
  535.                 {
  536.                     currentValue = pEnumeratorUniqueValues.Current.ToString();//当前值
  537.                     this.lsvUniqueValue.Items.Add(currentValue);
  538.                     this.lsvUniqueValue.Items[i].SubItems.Add(currentValue);
  539.                     //需要这个if的原因是SQL语句中数字和非数字的写法不一样
  540.                     if (currentTypeIsNumeric)
  541.                     {
  542.                         pQueryFilter.WhereClause = """ + currentFieldName + """ + " = " + currentValue;
  543.                     }
  544.                     else
  545.                     {
  546.                         pQueryFilter.WhereClause = """ + currentFieldName + """ + " = " + "'" + currentValue + "'";
  547.                     }
  548.                     currentValueCount = pTable.RowCount(pQueryFilter);//table中该字段是当前值的row的个数
  549.                     this.lsvUniqueValue.Items[i].SubItems.Add(currentValueCount.ToString());
  550.                 }
  551.             }
  552.             catch (Exception ex)
  553.             {
  554.                 MessageBox.Show(ex.Message);
  555.             }
  556.         }
  557.         /// <summary>
  558.         /// Cafe版主帮忙写的正则表达式,用于判断字符串是否可以转化为数字
  559.         /// </summary>
  560.         /// <param name="str"></param>
  561.         /// <returns></returns>
  562.         bool IsNumeric(string str)
  563.         {
  564.             System.Text.RegularExpressions.Regex reg1
  565.                 = new System.Text.RegularExpressions.Regex(@"^[-]?d+[.]?d*$");
  566.             return reg1.IsMatch(str);
  567.         }
  568.         private ColorRampSelectorFrm newColorRanmpSelectorFrm = null;
  569.         private IColorRamp pUniValueColorRamp = null;
  570.         private IColorRamp pClassBreaksColorRamp = null;
  571.         private void btnUniValueColorRamp_Click(object sender, EventArgs e)
  572.         {
  573.             if (this.newColorRanmpSelectorFrm == null)
  574.             {
  575.                 this.newColorRanmpSelectorFrm = new ColorRampSelectorFrm();
  576.             }
  577.             if (this.newColorRanmpSelectorFrm.ShowDialog() == DialogResult.OK)
  578.             {
  579.                 this.pUniValueColorRamp = this.newColorRanmpSelectorFrm.pColorRamp;
  580.                 this.btnUniValueColorRamp.BackgroundImage = this.newColorRanmpSelectorFrm.pColorRampImage;
  581.             }
  582.         }
  583.         private void cbbUniValueField_SelectedIndexChanged(object sender, EventArgs e)
  584.         {
  585.             this.lsvUniqueValue.Items.Clear();
  586.         }
  587.         private void btnUniValueUp_Click(object sender, EventArgs e)
  588.         {
  589.             try
  590.             {
  591.                 ListView.SelectedListViewItemCollection pItemCollection = this.lsvUniqueValue.SelectedItems;
  592.                 if (pItemCollection.Count == 0)
  593.                 {
  594.                     return;
  595.                 }
  596.                 ListViewItem item = (ListViewItem)pItemCollection[0].Clone();
  597.                 int itemIndex = pItemCollection[0].Index;
  598.                 if (itemIndex == 0 || itemIndex == 1 || itemIndex == -1)
  599.                 {
  600.                     return;
  601.                 }
  602.                 //this.lsvUniqueValue.Items.Remove(pItemCollection[0]);
  603.                 this.lsvUniqueValue.Items.Insert(itemIndex - 1, item);
  604.                 this.lsvUniqueValue.Items.Remove(pItemCollection[0]);
  605.             }
  606.             catch (Exception ex)
  607.             {
  608.                 MessageBox.Show(ex.Message);
  609.             }
  610.         }
  611.         private void btnUniValueDown_Click(object sender, EventArgs e)
  612.         {
  613.             try
  614.             {
  615.                 ListView.SelectedListViewItemCollection pItemCollection = this.lsvUniqueValue.SelectedItems;
  616.                 if (pItemCollection.Count == 0)
  617.                 {
  618.                     return;
  619.                 }
  620.                 ListViewItem item = (ListViewItem)pItemCollection[0].Clone();
  621.                 int itemIndex = pItemCollection[0].Index;
  622.                 if (itemIndex == this.lsvUniqueValue.Items.Count - 1)
  623.                 {
  624.                     return;
  625.                 }
  626.                 this.lsvUniqueValue.Items.Remove(pItemCollection[0]);
  627.                 this.lsvUniqueValue.Items.Insert(itemIndex + 1, item);
  628.                 //this.lsvUniqueValue.Items.Remove(pItemCollection[0]);
  629.             }
  630.             catch (Exception ex)
  631.             {
  632.                 MessageBox.Show(ex.Message);
  633.             }
  634.         }
  635.         private void lsvUniqueValue_SelectedIndexChanged(object sender, EventArgs e)
  636.         {
  637.             if (this.lsvUniqueValue.SelectedIndices.Count != 0)
  638.             {
  639.                 int selectedItemIndex = this.lsvUniqueValue.SelectedIndices[0];
  640.                 int lsvLastIndex = this.lsvUniqueValue.Items.Count - 1;
  641.                 //switch (selectedItemIndex)
  642.                 //{
  643.                 //    case 0||1:
  644.                 //        this.btnUniValueUp.Enabled = false;
  645.                 //        break;
  646.                 //    case this.lsvUniqueValue.Items.Count-1:
  647.                 //        this.btnUniValueDown.Enabled = false;
  648.                 //        break;
  649.                 //}
  650.                 if (selectedItemIndex != 0 && selectedItemIndex != 1 && selectedItemIndex != lsvLastIndex)
  651.                 {
  652.                     this.btnUniValueDown.Enabled = true;
  653.                     this.btnUniValueUp.Enabled = true;
  654.                 }
  655.                 else if (selectedItemIndex == 0)
  656.                 {
  657.                     this.btnUniValueUp.Enabled = false;
  658.                     this.btnUniValueDown.Enabled = false;
  659.                 }
  660.                 else if (selectedItemIndex == 1)
  661.                 {
  662.                     this.btnUniValueUp.Enabled = false;
  663.                     this.btnUniValueDown.Enabled = true;
  664.                 }
  665.                 else if (selectedItemIndex == lsvLastIndex)
  666.                 {
  667.                     this.btnUniValueUp.Enabled = true;
  668.                     this.btnUniValueDown.Enabled = false;
  669.                 }
  670.             }
  671.         }
  672.         //分级字段ComboBox选择变化
  673.         private void cbbClassBreakField_SelectedIndexChanged(object sender, EventArgs e)
  674.         {
  675.             if (this.cbbClassBreakField.SelectedIndex == 0)
  676.             {
  677.                 this.lblClassBreaksCount.Enabled = false;
  678.                 this.cbbClassBreaksCount.Enabled = false;
  679.                 this.lblClassBreaksMethod.Enabled = false;
  680.                 this.lblClassBreaksMethod.Enabled = false;
  681.                 this.groupBox8.Enabled = false;
  682.             }
  683.             else
  684.             {
  685.                 this.lblClassBreaksCount.Enabled = true;
  686.                 this.cbbClassBreaksCount.Enabled = true;
  687.                 this.lblClassBreaksMethod.Enabled = true;
  688.                 this.cbbClassBreaksMethod.Enabled = true;
  689.                 this.groupBox8.Enabled = true;
  690.                 string currentFieldName = this.cbbClassBreakField.Text;//当前字段名
  691.                 
  692.                 ITable pTable = this.pLayer as ITable;
  693.                 ICursor pCursor = pTable.Search(null, true);
  694.                 //字段统计
  695.                 IDataStatistics pDataStatistics = new DataStatisticsClass();
  696.                 pDataStatistics.Cursor = pCursor;
  697.                 pDataStatistics.Field = currentFieldName;
  698.                 System.Collections.IEnumerator pEnumeratorUniqueValues = pDataStatistics.UniqueValues;//唯一值枚举
  699.                 int uniqueValueCount = pDataStatistics.UniqueValueCount;//唯一值的个数
  700.                 this.cbbClassBreaksCount.Items.Clear();
  701.                 for (int i = 1; i <= uniqueValueCount; i++)
  702.                 {
  703.                     this.cbbClassBreaksCount.Items.Add(i.ToString());
  704.                 }
  705.                 this.cbbClassBreaksCount.SelectedIndex = 0;
  706.                 this.cbbClassBreaksMethod.SelectedIndex = 0;
  707.             }
  708.         }
  709.         private double[] classBreaks;
  710.         private void btnClassiFy_Click(object sender, EventArgs e)
  711.         {
  712.             ITable pTable = this.pLayer as ITable;
  713.             object dataValues;
  714.             object dataFrequency;
  715.             
  716.             //以下代码用TableHistogram和BasicHistogram统计出Table某一字段的值和值的频率
  717.             ITableHistogram pTableHistogram;
  718.             pTableHistogram = new BasicTableHistogramClass();
  719.             pTableHistogram.Table = pTable;//需传入一个ITable
  720.             pTableHistogram.Field = this.cbbClassBreakField.Text;//统计的字段
  721.             IBasicHistogram pBasicHistogram;
  722.             pBasicHistogram = pTableHistogram as IBasicHistogram;
  723.             pBasicHistogram.GetHistogram(out dataValues, out dataFrequency);//关键
  724.             //以下代码用IClassifyGEN和EqualInterval对象,基于分段数目,生成各段的临界值,并放在一个数组当中
  725.             IClassifyGEN pClassifyGEN;
  726.             switch (this.cbbClassBreaksMethod.SelectedIndex)
  727.             {
  728.                 case 0:
  729.                     pClassifyGEN = new EqualIntervalClass();
  730.                     break;
  731.                 case 1:
  732.                     pClassifyGEN = new GeometricalIntervalClass();
  733.                     break;
  734.                 case 2:
  735.                     pClassifyGEN = new NaturalBreaksClass();
  736.                     break;
  737.                 case 3:
  738.                     pClassifyGEN = new QuantileClass();
  739.                     break;
  740.                 default:
  741.                     pClassifyGEN = new EqualIntervalClass();
  742.                     break;
  743.             }
  744.             //double[] classes;
  745.             int classCount = int.Parse(this.cbbClassBreaksCount.Text);
  746.             pClassifyGEN.Classify(dataValues, dataFrequency, ref classCount);//用到了上面ITableHistogram生成的值和值频率数组,关键!!!!!!
  747.             this.classBreaks = pClassifyGEN.ClassBreaks as double[];//注意,此对象下标从1开始(我觉得0为最开头,所以临界值从1开始有意义),关键!!!!!!
  748.             this.lsvClassBreaksSymbol.Items.Clear();
  749.             string currentRange;
  750.             for (int i = 0; i < classCount; i++)
  751.             {
  752.                 currentRange = this.classBreaks[i].ToString() + " ~ " + this.classBreaks[i + 1].ToString();
  753.                 this.lsvClassBreaksSymbol.Items.Add(currentRange);
  754.                 this.lsvClassBreaksSymbol.Items[i].SubItems.Add(currentRange);
  755.             }
  756.         }
  757.         private void btnClassBreaksColorRamp_Click(object sender, EventArgs e)
  758.         {
  759.             if (this.newColorRanmpSelectorFrm == null)
  760.             {
  761.                 this.newColorRanmpSelectorFrm = new ColorRampSelectorFrm();
  762.             }
  763.             if (this.newColorRanmpSelectorFrm.ShowDialog() == DialogResult.OK)
  764.             {
  765.                 this.pClassBreaksColorRamp = this.newColorRanmpSelectorFrm.pColorRamp;
  766.                 this.btnClassBreaksColorRamp.BackgroundImage = this.newColorRanmpSelectorFrm.pColorRampImage;
  767.             }
  768.         }
  769.     }
  770. }