📄 formselectfield.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
namespace WindowsApplication1
{
public partial class formselectfield : Form
{
//图层
public IMap pMap;
//选择图层的索引
public int layerIndex;
//选择图层的名字
public string layerName;
//图标渲染时选择的第二个图层的索引
public int fieldIndex;
//图标渲染时选择的第二个图层的名字
public int fieldIndex1;
//选择字段的名字
public string fieldName;
//图标渲染时选择的第二个字段的名字
public string fieldName1;
//分类渲染时分类的数目
public int breakCount;
public IFeatureLayer pLayer;
public ITable pTable;
//点密度渲染时每个点代表数值的默认值为400
public double dotValue = 400;
//存储不同渲染类型的名称,用于判断
public string pFlag;
//存储SQL语句
public string querySentence = "";
//所选字段的属性值
public double[] fieldValue;
//分类渲染时用到的,各类型之间的界限
public double[] breakValue;
//选中字段的属性值个数,即行数
int rowCount;
ITable pPrimaryTable;
IGeoFeatureLayer pGeoFeatureL;
//构造函数
public formselectfield(string inflag, ITable connectedTable, IGeoFeatureLayer pGeoFL)
{
InitializeComponent();
//因为很多渲染都不用到“分类数目一栏”,所以先隐藏起来
groupBox1.Visible = false;
pFlag = inflag;
pPrimaryTable = connectedTable;
pGeoFeatureL = pGeoFL;
}
//选择土层
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear();
//填充图层列表框
int i, layerCount;
layerCount = pMap.LayerCount;
for (i = 0; i < layerCount; i++)
comboBox1.Items.Add(pMap.get_Layer(i).Name);
}
//Load时根据不同渲染类型,对窗体的text和一些对话框进行屏蔽或者改动
private void formselectfield_Load(object sender, EventArgs e)
{
listBox1.Enabled = false;
comboBox1.Items.Clear();
if (pFlag == "Unique")
{
this.Text = "唯一值渲染";
}
else if (pFlag == "DotValue")
{
this.Text = "点密度渲染";
lbDotValue.Visible = true;
tbDotValue.Visible = true;//有问题但在运行之后再看是什么错误
}
else if (pFlag == "Proportional")
{
this.Text = "比例渲染";
}
else if (pFlag == "Chart")
{
this.Text = "图表渲染";
comboBox3.Visible = true;
comboBox3.Enabled = true;
}
else if (pFlag == "pie")
{
this.Text = "饼状渲染";
comboBox3.Visible = true;
comboBox3.Enabled = true;
}
//else if (pFlag == "FeatureToGraph")
//{
// this.Text = "属性查询图形";
//}
else if (pFlag == "ClassBreak")
{
groupBox1.Visible = true;
}
}
//作用是把图层放上去
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
layerIndex = comboBox1.SelectedIndex;
string text = comboBox1.SelectedText;
if (layerIndex <0)
{
MessageBox.Show("必须选择一个图层");
return;
}
comboBox2.Enabled = true;
pLayer = (IFeatureLayer)pMap.get_Layer(layerIndex);
pTable = (ITable)pLayer;
int fieldCount, i;
fieldCount = pTable.Fields.FieldCount;
comboBox2.Items.Clear();
comboBox3.Items.Clear();
for (i = 0; i < fieldCount; i++)
comboBox2.Items.Add(pTable.Fields.get_Field(i).Name);
if (pFlag == "Chart")
{
for (i = 0; i < fieldCount; i++)
comboBox3.Items.Add(pTable.Fields.get_Field(i).Name);
}
}
//作用是真的选择字段
private void comboBox2_SelectionChangeCommitted(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox1.Enabled = true;
fieldIndex = comboBox2.SelectedIndex;
fieldName = pTable.Fields.get_Field(fieldIndex).Name;
//给右侧的属性值列表框添加字段
IQueryFilter pQueryFilter;
pQueryFilter = new QueryFilterClass();
pQueryFilter.AddField(fieldName);
rowCount = pTable.RowCount(pQueryFilter);
fieldValue = new double[rowCount];
for (int i = 0; i < rowCount; i++)
{
listBox1.Items.Add(pTable.GetRow(i).get_Value(fieldIndex).ToString());
if (pTable.GetRow(i).get_Value(fieldIndex) is int)
fieldValue[i] = (int)pTable.GetRow(i).get_Value(fieldIndex);
if (pTable.GetRow(i).get_Value(fieldIndex) is double)
fieldValue[i] = (double)pTable.GetRow(i).get_Value(fieldIndex);
}
}
//fenleixuanran
private void ClassBreak()
{
////获取输入的分类数目
//try
//{
// breakCount = Int32.Parse(textBox1.Text);
//}
//catch (FormatException)
//{
// MessageBox.Show("分类数目栏中应填写数值型字符,请正确填写", "提示");
//}
////由分类数目确定分类的界限
//breakValue = new double[breakCount];
//for (int j = 0; j < breakCount - 1; j++)
//{
// breakValue[j] = fieldValue[(int)((j + 1) * rowCount / breakCount)];
//}
////开始渲染
//IClassBreaksRenderer pClasBreakRender = new ClassBreaksRendererClass();
//pClasBreakRender.Field = "AREA";
//pClasBreakRender.BreakCount = breakCount;
//pClasBreakRender.SortClassesAscending = true;
//IHsvColor pFromColor = new HsvColorClass();
//pFromColor.Hue = 60;
//pFromColor.Saturation = 100;
//pFromColor.Value = 96;
//IHsvColor pToColor = new HsvColorClass();
//pToColor.Hue = 0;
//pToColor.Saturation = 100;
//pToColor.Value = 96;
//IAlgorithmicColorRamp pRamp = new AlgorithmicColorRampClass();
//pRamp.Algorithm = esriColorRampAlgorithm.esriHSVAlgorithm;
//pRamp.FromColor = pFromColor;
//pRamp.ToColor = pToColor;
//pRamp.Size = breakCount;
//bool ok;
//pRamp.CreateRamp(out ok);
//IEnumColors pEnumColors = pRamp.Colors;
//IColor pColor;
//ISimpleFillSymbol pFillSymbol;
//for (int breakIndex = 0; breakIndex < breakCount; breakIndex++)
//{
// pColor = pEnumColors.Next();
// pFillSymbol = new SimpleFillSymbolClass();
// pFillSymbol.Color = pColor;
// pFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
// pClasBreakRender.set_Symbol(breakIndex, (ISymbol)pFillSymbol);
// pClasBreakRender.set_Break(breakIndex, breakValue[breakIndex]);
//}
////IGeoFeatureLayer pGeoFeatureLayer = (IGeoFeatureLayer)pMap.get_Layer(0);
//pGeoFeatureL.Renderer = (IFeatureRenderer)pClasBreakRender;
//获取输入的分类数目
try
{
breakCount = Int32.Parse(textBox1.Text);
}
catch (FormatException)
{
MessageBox.Show("分类数目栏中应填整数,请正确填写", "提示");
return;
}
breakValue = new double[breakCount];
ITable table = new FeatureLayerClass();
//table = connectedTable as ITable;
table = pGeoFeatureL as ITable;
ITableHistogram tableHistogram = new BasicTableHistogramClass();
IBasicHistogram basicHistogram = new BasicTableHistogramClass();
object dataFrequency;
object dataValues;
tableHistogram.Field = fieldName;
tableHistogram.Table = table;
basicHistogram = tableHistogram as IBasicHistogram;
basicHistogram.GetHistogram(out dataValues, out dataFrequency);
//下面时分级方法,用于根据获得的值计算得出符合要求的数据
IClassifyGEN classifyGEN = new EqualIntervalClass();
double[] Classes = new double[] { };
//根据条件计算出Classes和ClassesCount,5为分级数目
breakValue = new double[breakCount];
classifyGEN.Classify(dataValues, dataFrequency, ref breakCount);
//返回一个数组,注意这个数组的下标时从1开始的
breakValue = classifyGEN.ClassBreaks as double[];
//开始渲染
IClassBreaksRenderer pClasBreakRender = new ClassBreaksRendererClass();
pClasBreakRender.Field = fieldName;
pClasBreakRender.BreakCount = breakCount;
pClasBreakRender.SortClassesAscending = true;
IHsvColor pFromColor = new HsvColorClass();
pFromColor.Hue = 60;
pFromColor.Saturation = 100;
pFromColor.Value = 96;
IHsvColor pToColor = new HsvColorClass();
pToColor.Hue = 0;
pToColor.Saturation = 100;
pToColor.Value = 96;
IAlgorithmicColorRamp pRamp = new AlgorithmicColorRampClass();
pRamp.Algorithm = esriColorRampAlgorithm.esriHSVAlgorithm;
pRamp.FromColor = pFromColor;
pRamp.ToColor = pToColor;
pRamp.Size = breakCount;
bool ok;
pRamp.CreateRamp(out ok);
IEnumColors pEnumColors = pRamp.Colors;
IColor pColor;
ISimpleFillSymbol pFillSymbol;
for (int breakIndex = 0; breakIndex < breakCount; breakIndex++)
{
pColor = pEnumColors.Next();
pColor.Transparency = 100;
pFillSymbol = new SimpleFillSymbolClass();
pFillSymbol.Color = pColor;
pFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
pClasBreakRender.set_Symbol(breakIndex, (ISymbol)pFillSymbol);
pClasBreakRender.set_Break(breakIndex, breakValue[breakIndex]);
}
pGeoFeatureL.Renderer = (IFeatureRenderer)pClasBreakRender;
}
//weiyizhixuanran
private void Unique()
{
IFeatureLayer pFeatureLayer = pMap.get_Layer(layerIndex) as IFeatureLayer;
IGeoFeatureLayer pGeoFeatureLayer = pFeatureLayer as IGeoFeatureLayer;
IFeatureClass pFeatureClass = pPrimaryTable as IFeatureClass;//pFeatureLayer.FeatureClass;
IQueryFilter pQueryFilter = new QueryFilterClass();
IFeatureCursor pFeatureCursor = pFeatureClass.Search(pQueryFilter, false);
string field = fieldName;
//定义渐变色
IRandomColorRamp pRandomColorRamp = new RandomColorRampClass();
pRandomColorRamp.MinSaturation = 20;
pRandomColorRamp.MaxSaturation = 40;
pRandomColorRamp.MinValue = 80;
pRandomColorRamp.MaxValue = 100;
pRandomColorRamp.StartHue = 60;
pRandomColorRamp.EndHue = 180;
pRandomColorRamp.UseSeed = true;
pRandomColorRamp.Seed = 40;
//设置填充符号
ISimpleFillSymbol pFillSym = new SimpleFillSymbolClass();
pFillSym.Style = esriSimpleFillStyle.esriSFSSolid;
pFillSym.Outline.Width = 2;
//
IUniqueValueRenderer pRender = new UniqueValueRendererClass();
pRender.FieldCount = 1;
pRender.set_Field(0, field);
pRender.DefaultSymbol = pFillSym as ISymbol;
pRender.UseDefaultSymbol = true;
//得到要素类中的要素个数n
long n = pFeatureClass.FeatureCount(pQueryFilter);
bool ValFound;
IFields pFields = pFeatureCursor.Fields;
int iField = pFields.FindField(field);
IFeature pFeature;
for (int i = 0; i < n; i++)
{
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass();
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
pSimpleFillSymbol.Outline.Width = 5;
pFeature = pFeatureCursor.NextFeature();
string str = pFeature.get_Value(iField).ToString();
//
ValFound = false;
for (int j = 0; j < pRender.ValueCount; j++)
{
if (pRender.get_Value(j) == str)
{
ValFound = true;
break;
}
}
if (ValFound == false)
{
pRender.AddValue(str, field, pSimpleFillSymbol as ISymbol);
pRender.set_Label(str, str);
pRender.set_Symbol(str, pSimpleFillSymbol as ISymbol);
}
}
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -