唯一值符号化
1.制作子窗体
2.编写代码,在确定按钮的click事件中
private void button1_Click(object sender, EventArgs e)
{
IFeatureLayer pfealayer = currentFeatureLayer;
string sFieldName = comboBox2.SelectedItem.ToString();
IGeoFeatureLayer pGeoFeaLayer = pfealayer as IGeoFeatureLayer;
ITable ptable = pfealayer as ITable;
IUniqueValueRenderer puniRender = new UniqueValueRendererClass();
int fieldNumber = ptable.FindField(sFieldName);
puniRender.FieldCount = 1;
puniRender.set_Field(0, sFieldName);
//设置显示颜色的范围
IRandomColorRamp pRandColorRamp = new RandomColorRampClass();
pRandColorRamp.StartHue = 0;
pRandColorRamp.MinValue = 85;
pRandColorRamp.MinSaturation = 15;
pRandColorRamp.EndHue = 360;
pRandColorRamp.MaxValue = 100;
pRandColorRamp.MaxSaturation = 30;
//创建随机颜色带
pRandColorRamp.Size = getUniqueValue(pfealayer.FeatureClass, sFieldName)+1;
bool bSucess = false;
pRandColorRamp.CreateRamp(out bSucess);
IEnumColors pEnumColors = pRandColorRamp.Colors;
IColor pNextUniqueColor = null;
//属性唯一值
IQueryFilter pQueryFilter = new QueryFilterClass();
pQueryFilter.AddField(sFieldName);
ICursor pCursor = pfealayer.FeatureClass.Search(pQueryFilter, false) as ICursor;
IRow pNextRow = pCursor.NextRow();
object codeValue = null;
IRowBuffer pNextRowBuffer = null;
IFillSymbol pFillSymbol = null;
ILineSymbol pLineSymbol = null;
IMarkerSymbol pMarkerSymbol = null;
while (pNextRow != null)
{
pNextRowBuffer = pNextRow as IRowBuffer;
codeValue = pNextRowBuffer.get_Value(fieldNumber);
pNextUniqueColor = pEnumColors.Next();
if (pNextUniqueColor == null)
{
pEnumColors.Reset();
pNextUniqueColor = pEnumColors.Next();
}
switch (pGeoFeaLayer.FeatureClass.ShapeType)
{
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
pMarkerSymbol = new SimpleMarkerSymbolClass();
pMarkerSymbol.Color = pNextUniqueColor;
puniRender.AddValue(codeValue.ToString(), "", pMarkerSymbol as ISymbol);
pNextRow = pCursor.NextRow();
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
pLineSymbol = new SimpleLineSymbolClass();
pLineSymbol.Color = pNextUniqueColor;
puniRender.AddValue(codeValue.ToString(), "", pLineSymbol as ISymbol);
pNextRow = pCursor.NextRow();
break;
case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
pFillSymbol = new SimpleFillSymbolClass();
pFillSymbol.Color = pNextUniqueColor;
puniRender.AddValue(codeValue.ToString(), "", pFillSymbol as ISymbol);
pNextRow = pCursor.NextRow();
int k = pFillSymbol.Color.CMYK;
break;
default:
break;
}
}
pGeoFeaLayer.Renderer = puniRender as IFeatureRenderer;
mapcontrol1.Refresh();
toccontrol1.Update();
this.Close();
必须手动清除COM对象,否则会造成内存溢出(尤其是IQueryFilter,ICursor)
//Marshal.ReleaseComObject(pQueryFilter);
//Marshal.ReleaseComObject(pCursor);
//Marshal.ReleaseComObject(pEnumColors);
}
3.定义函数,用来获取唯一值的个数
public static int getUniqueValue(IFeatureClass pFeatureClass, string strFld)
{
//得到IFeatureCursor游标
IFeatureCursor pCursor = pFeatureClass.Search(null, false);
//coClass对象实例生成
IDataStatistics pData = new DataStatisticsClass();
pData.Field=strFld;
pData.Cursor=pCursor as ICursor;
//枚举唯一值
IEnumerator pEnumVar = pData.UniqueValues;
//记录总数
int RecordCount = pData.UniqueValueCount;
//字符数组
string[] strValue = new string[RecordCount];
pEnumVar.Reset();
int i = 0;
while(pEnumVar.MoveNext())
{
strValue[i++]=pEnumVar.Current.ToString();
}
return i;
}
4.添加引用
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using System.Collections;
using ESRI.ArcGIS.Controls;