AE常用功能

1.加载数据库

try
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.Title = "打开地图文档";
openFileDialog.Filter = "Personal Geodatabase文件(*.mdb)|*.mdb";
openFileDialog.Multiselect = false; ////是否允许多选
openFileDialog.RestoreDirectory = true; ////存储打开的文件路径
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string sFileName = openFileDialog.FileName;
if (string.IsNullOrEmpty(sFileName))
{
return;
}

ClearAllData();
AddMdbFile(sFileName);
}

}
catch (Exception ex)
{
MessageBox.Show("加载MXD文件失败:" + ex.Message);
}

/// <summary>
/// 添加MDB文件
/// </summary>
/// <param name="sShpFilePath">文件路径</param>
private void AddMdbFile(string sAccessFilePath)
{
if (string.IsNullOrEmpty(sAccessFilePath))
{
return;
}

try
{
IWorkspaceFactory pWksFac = new AccessWorkspaceFactory();
IWorkspace pWks = pWksFac.OpenFromFile(sAccessFilePath, 0);

IEnumDataset pEnumDs = pWks.get_Datasets(esriDatasetType.esriDTAny);
pEnumDs.Reset();

IDataset pDs = pEnumDs.Next();
while (pDs != null)
{
if (pDs is IFeatureDataset)
{
IEnumDataset pEnumDs2 = pDs.Subsets;
pEnumDs2.Reset();
IDataset pDs2 = pEnumDs2.Next();
while (pDs2 != null)
{
IFeatureClass pFeaCls = pDs2 as IFeatureClass;
IFeatureLayer pFeaLyr = new FeatureLayerClass();
pFeaLyr.FeatureClass = pFeaCls;
pFeaLyr.Name = pFeaCls.AliasName;

axMapControl.Map.AddLayer(pFeaLyr);
pDs2 = pEnumDs2.Next();
}
}
else if (pDs is IFeatureClass)
{
IFeatureClass pFeaCls = pDs as IFeatureClass;
IFeatureLayer pFeaLyr = new FeatureLayerClass();
pFeaLyr.FeatureClass = pFeaCls;
pFeaLyr.Name = pFeaCls.AliasName;

axMapControl.Map.AddLayer(pFeaLyr);
}

pDs = pEnumDs.Next();
}

axMapControl.ActiveView.Refresh();

}
catch (Exception ex)
{
MessageBox.Show("加载SHP文件失败:" + ex.Message);
}
}

/// <summary>
/// 清除所有的数据
/// </summary>
private void ClearAllData()
{
if (axMapControl.Map != null && axMapControl.Map.LayerCount > 0)
{
axMapControl.ClearLayers();
axMapControl2.ClearLayers();
}
}

2. 保存

try
{
string sFilePath = axMapControl.DocumentFilename;
if (string.IsNullOrEmpty(sFilePath))
{
return;
}

IMapDocument pMapDoc = new MapDocumentClass();
if (axMapControl.CheckMxFile(sFilePath))
{
if (pMapDoc.get_IsReadOnly(sFilePath))
{
MessageBox.Show("本文档是只读的,不能保存。");
return;
}
}

pMapDoc.New(sFilePath);
pMapDoc.ReplaceContents(axMapControl.Map as IMxdContents);
pMapDoc.Save(pMapDoc.UsesRelativePaths, true);
pMapDoc.Close();
ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(pMapDoc);
}
catch (Exception ex)
{
MessageBox.Show("保存文档失败:" + ex.Message);
}

3. 另存为

try
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "另存为";
saveFileDialog.OverwritePrompt = true;
saveFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt";
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string sFilePath = saveFileDialog.FileName;

IMapDocument pMapDoc = new MapDocumentClass();
pMapDoc.New(sFilePath);
pMapDoc.ReplaceContents(axMapControl.Map as IMxdContents);
pMapDoc.Save(true, true);
pMapDoc.Close();
ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(pMapDoc);
}
}
catch (Exception ex)
{
MessageBox.Show("另存为失败:" + ex.Message);
}

4.鹰眼

private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (axMapControl2.LayerCount > 0)
{
if (e.button == 1)
{
IEnvelope pEnvelope = axMapControl.Extent;
if (pEnvelope == null)
{
return;
}

if (e.mapX > pEnvelope.XMin && e.mapY > pEnvelope.YMin & e.mapX < pEnvelope.XMax && e.mapY < pEnvelope.YMax)
{
m_CanDrag = true;
}

m_MoveRectPoint = new PointClass();
m_MoveRectPoint.PutCoords(e.mapX, e.mapY);
}
}
}

private void axMapControl2_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
if (axMapControl2.LayerCount > 0)
{
IEnvelope pEnvelope = axMapControl.Extent;
if (pEnvelope == null)
{
return;
}

if (e.mapX > pEnvelope.XMin && e.mapY > pEnvelope.YMin & e.mapX < pEnvelope.XMax && e.mapY < pEnvelope.YMax)
{
axMapControl2.MousePointer = esriControlsMousePointer.esriPointerHand;
if (e.button == 2)
{
axMapControl2.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
}
else
{
axMapControl2.MousePointer = esriControlsMousePointer.esriPointerDefault;
}

if (m_CanDrag)
{
double dX, dY;
dX = e.mapX - m_MoveRectPoint.X;
dY = e.mapY - m_MoveRectPoint.Y;
pEnvelope.Offset(dX, dY);
m_MoveRectPoint.PutCoords(e.mapX, e.mapY);
DrawRectabgle(pEnvelope);

axMapControl.Extent = pEnvelope;
}
}
}

private void axMapControl2_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e)
{
if (e.button == 1 && m_MoveRectPoint != null)
{
if (e.mapX == m_MoveRectPoint.X && e.mapY == m_MoveRectPoint.Y)
{
axMapControl.CenterAt(m_MoveRectPoint);
}

m_CanDrag = false;
}
}

private IEnvelope m_Envelope = null;

/// <summary>
/// 鹰眼外接矩形是否可移动
/// </summary>
private bool m_CanDrag = false;

/// <summary>
/// 移动时的第一个点
/// </summary>
private IPoint m_MoveRectPoint = null;

/// <summary>
/// 添加外接矩形
/// </summary>
/// <param name="pEnvelope"></param>
private void DrawRectabgle(IEnvelope pEnvelope)
{
////清空容器中所有已画的Element
IGraphicsContainer pGraphicsContainer = axMapControl2.Map as IGraphicsContainer;
IActiveView pActiveView = pGraphicsContainer as IActiveView;
pGraphicsContainer.DeleteAllElements();

////新建范围
IRectangleElement pRectangleElement = new RectangleElementClass();
IElement pElement = pRectangleElement as IElement;
pElement.Geometry = pEnvelope;

////设置矩形框(颜色、外边框颜色及透明度)
IRgbColor pRgbColor = new RgbColorClass();
pRgbColor = GetRgbColor(255, 0, 0);
pRgbColor.Transparency = 255;
ILineSymbol pLineSymbol = new SimpleLineSymbolClass();
pLineSymbol.Color = pRgbColor;
pLineSymbol.Width = 2;

IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
pRgbColor = new RgbColorClass();
pRgbColor.Transparency = 0;
pFillSymbol.Color = pRgbColor;
pFillSymbol.Outline = pLineSymbol;

////容器中添加矩形
IFillShapeElement pFillShapeElement = pElement as IFillShapeElement;
pFillShapeElement.Symbol = pFillSymbol;
pGraphicsContainer.AddElement(pFillShapeElement as IElement, 0);

////刷新
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}

/// <summary>
/// 创建颜色
/// </summary>
/// <param name="iRed"></param>
/// <param name="iGreen"></param>
/// <param name="iBlue"></param>
/// <returns></returns>
private IRgbColor GetRgbColor(int iRed, int iGreen, int iBlue)
{
IRgbColor pRgbColor = new RgbColorClass();
pRgbColor.Red = iRed;
pRgbColor.Green = iGreen;
pRgbColor.Blue = iBlue;
return pRgbColor;
}

private void axMapControl_OnAfterScreenDraw(object sender, IMapControlEvents2_OnAfterScreenDrawEvent e)
{
IActiveView pActiveView = (IActiveView)axPageLayoutControl.ActiveView.FocusMap;
IDisplayTransformation pDisTran = pActiveView.ScreenDisplay.DisplayTransformation;
pDisTran.VisibleBounds = axMapControl.Extent;
axPageLayoutControl.ActiveView.Refresh();
CopyToPageLayout();
}

private void axMapControl_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{
IEnvelope pEnvelope = axMapControl.Extent;
DrawRectabgle(pEnvelope);
}

private void axMapControl_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
SynchronizeEagleEye();
}

private void axMapControl_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
{
string sMapUnit = string.Empty;
switch (axMapControl.MapUnits.ToString().Substring(4).ToUpper())
{
case "INCHES":
sMapUnit = "英寸";
break;
case "POINTS":
sMapUnit = "点";
break;
case "FEET":
sMapUnit = "英尺";
break;
case "YARDS":
sMapUnit = "码";
break;
case "MILES":
sMapUnit = "英里";
break;
case "NAUTICALMILES":
sMapUnit = "海里";
break;
case "MILLIMETERS":
sMapUnit = "毫米";
break;
case "CENTIMETERS":
sMapUnit = "厘米";
break;
case "METERS":
sMapUnit = "米";
break;
case "KILOMETERS":
sMapUnit = "千米";
break;
case "DECIMALDEGREES":
sMapUnit = "度";
break;
case "DECIMETERS":
sMapUnit = "分米";
break;
default:
sMapUnit = axMapControl.MapUnits.ToString().Substring(4);
break;
}

bsiCoordinate.Caption = string.Format("{0} {1} {2}", e.mapX.ToString("#######.###"), e.mapY.ToString("#######.###"), sMapUnit);
}

private void axMapControl_OnSelectionChanged(object sender, EventArgs e)
{
this.bsiSelection.Caption = "当前选择了 " + this.axMapControl.Map.SelectionCount.ToString() + " 个图形";
}

4.统计

public partial class frmStatistics : DevExpress.XtraEditors.XtraForm
{
public frmStatistics()
{
InitializeComponent();
}

public void FormLoad(IMap pMap)
{
if (pMap == null || pMap.LayerCount == 0)
{
return;
}

InitializeLayerCombox(pMap);
}

/// <summary>
/// 初始化图层选择
/// </summary>
/// <param name="bIsSelect">是否只显示可选图层</param>
private void InitializeLayerCombox(IMap pMap)
{
if (pMap == null || pMap.LayerCount == 0)
{
return;
}

this.cboLayer.Properties.Items.Clear();
this.cboLayer.Text = "";

int iSelectCnt = 0;
int iLyrCnt = 0;
for (int i = 0; i < pMap.LayerCount; i++)
{
IFeatureLayer pFeaLyr = pMap.get_Layer(i) as IFeatureLayer;
if (pFeaLyr == null || pFeaLyr.FeatureClass == null)
{
continue;
}

if (((IFeatureSelection)pFeaLyr).SelectionSet.Count > 0)
{
iLyrCnt++;
iSelectCnt += ((IFeatureSelection)pFeaLyr).SelectionSet.Count;

LayerFun pLyrFun = new LayerFun();
pLyrFun.FeaClsName = (pFeaLyr.FeatureClass as IDataset).Name;
pLyrFun.AliasName = pFeaLyr.FeatureClass.AliasName;
pLyrFun.FeatureLayer = pFeaLyr;

this.cboLayer.Properties.Items.Add(pLyrFun);
}
}

this.lblSelect.Text = "当前地图选择集共有 " + iLyrCnt + " 个图层的 " + iSelectCnt + " 个要素被选中。";

if (this.cboLayer.Properties.Items.Count > 0)
{
this.cboLayer.SelectedIndex = 0;
}
}

private void cboLayer_SelectedIndexChanged(object sender, EventArgs e)
{
object obj = this.cboLayer.SelectedItem;
if (obj == null || !(obj is LayerFun))
{
return;
}

LayerFun pLyrFun = obj as LayerFun;
InitializeFieldNameCombox(pLyrFun.FeatureLayer);
}

private void InitializeFieldNameCombox(IFeatureLayer pFeaLyr)
{
if (pFeaLyr == null || pFeaLyr.FeatureClass == null)
{
return;
}

this.cboFieldName.Properties.Items.Clear();

IFields pFields = pFeaLyr.FeatureClass.Fields;
if (pFields != null && pFields.FieldCount > 0)
{
for (int i = 0; i < pFields.FieldCount; i++)
{
IField pField = pFields.get_Field(i);
//如果字段名称不为"OBJECTID"或 "SHAPE"
if (pField.Name.ToUpper() != "OBJECTID" && pField.Name.ToUpper() != "SHAPE")
{
//如果字段类型为可以进行统计的数值类型,则将该字段添加到comboBoxFields中
if (pField.Type == esriFieldType.esriFieldTypeInteger || pField.Type == esriFieldType.esriFieldTypeDouble
|| pField.Type == esriFieldType.esriFieldTypeSingle || pField.Type == esriFieldType.esriFieldTypeSmallInteger)
{
this.cboFieldName.Properties.Items.Add(pField.Name);
}
}

}
}
}

private void btnOK_Click(object sender, EventArgs e)
{
object obj = this.cboLayer.SelectedItem;
if (obj == null || !(obj is LayerFun))
{
MessageBox.Show("请选择图层。");
return;
}


if (this.cboFieldName.SelectedItem == null || string.IsNullOrEmpty(this.cboFieldName.SelectedItem.ToString()))
{
MessageBox.Show("请选择统计字段。");
return;
}

this.meStatistics.Text = GetStatisticsResult((obj as LayerFun).FeatureLayer, this.cboFieldName.Text.Trim()).ToString();
}

private StringBuilder GetStatisticsResult(IFeatureLayer pFeaLyr, string sFieldName)
{
ICursor pCursor = null;

try
{
IDataStatistics pDataStatistics = new DataStatisticsClass();
pDataStatistics.Field = sFieldName.Trim();

IFeatureSelection pFeaSelection = pFeaLyr as IFeatureSelection;
pFeaSelection.SelectionSet.Search(null, false, out pCursor);

pDataStatistics.Cursor = pFeaLyr as ICursor;

IStatisticsResults pStatisticsResult = pDataStatistics.Statistics;

StringBuilder stringBuilder = new StringBuilder();
////以下语句依次增加各类统计结果
stringBuilder.AppendLine("统计总数: " + pStatisticsResult.Count.ToString() + " ");
stringBuilder.AppendLine("最小值:" + pStatisticsResult.Minimum.ToString() + " ");
stringBuilder.AppendLine("最大值:" + pStatisticsResult.Maximum.ToString() + " ");
stringBuilder.AppendLine("总计: " + pStatisticsResult.Sum.ToString() + " ");
stringBuilder.AppendLine("平均值: " + pStatisticsResult.Mean.ToString() + " ");
stringBuilder.AppendLine("标准差: " + pStatisticsResult.StandardDeviation.ToString());

return stringBuilder;
}
catch (Exception ex)
{
MessageBox.Show("数据统计失败:" + ex.Message);
return null;
}
finally
{
if (pCursor != null)
{
ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(pCursor);
pCursor = null;
}
}
}


frmStatistics frm = new frmStatistics();
frm.FormLoad(axMapControl.Map);
frm.ShowDialog();

6.符号化

制作选择窗体

public partial class frmFeatureLayerSelect : DevExpress.XtraEditors.XtraForm
{
/// <summary>
/// 选择的图形
/// </summary>
private IFeatureLayer m_FeatureLayer = null;
public IFeatureLayer SelectFeatureLayer
{
get
{
return m_FeatureLayer;
}
}

public frmFeatureLayerSelect()
{
InitializeComponent();
}

/// <summary>
/// 窗体初始化
/// </summary>
/// <param name="Map">地图窗口</param>
/// <param name="featureType">要素类型</param>
/// <param name="geometryType">图形类型</param>
public void FormLoad(IMap Map, esriFeatureType featureType, esriGeometryType geometryType)
{
if (Map == null || Map.LayerCount == 0)
{
return;
}

this.cboLayer.Properties.Items.Clear();

for (int i = 0; i < Map.LayerCount; i++)
{
IFeatureLayer pFeaLyr = Map.get_Layer(i) as IFeatureLayer;
if (pFeaLyr == null || pFeaLyr.FeatureClass == null)
{
continue;
}

if (featureType == esriFeatureType.esriFTAnnotation && pFeaLyr.FeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)
{
LayerFun pLyrFun = new LayerFun();
pLyrFun.FeaClsName = (pFeaLyr.FeatureClass as IDataset).Name;
pLyrFun.AliasName = pFeaLyr.FeatureClass.AliasName;
pLyrFun.FeatureLayer = pFeaLyr;

this.cboLayer.Properties.Items.Add(pLyrFun);
}
else if(featureType == esriFeatureType.esriFTSimple)
{
if (pFeaLyr.FeatureClass.ShapeType == geometryType)
{
LayerFun pLyrFun = new LayerFun();
pLyrFun.FeaClsName = (pFeaLyr.FeatureClass as IDataset).Name;
pLyrFun.AliasName = pFeaLyr.FeatureClass.AliasName;
pLyrFun.FeatureLayer = pFeaLyr;

this.cboLayer.Properties.Items.Add(pLyrFun);
}
}
}

if (this.cboLayer.Properties.Items.Count > 0)
{
this.cboLayer.SelectedIndex = 0;
}
}

private void frmFeatureLayerSelect_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.DialogResult == DialogResult.OK)
{
object obj = this.cboLayer.SelectedItem;
if (obj == null)
{
MessageBox.Show("请选择需要符号化的图层。");
e.Cancel = true;
return;
}

m_FeatureLayer = (obj as LayerFun).FeatureLayer;
}
}

LayerFun是自定义的类

class LayerFun
{
/// <summary>
/// 图层名称
/// </summary>
public string FeaClsName
{
get;
set;
}

/// <summary>
/// 图层别名
/// </summary>
public string AliasName
{
get;
set;
}

/// <summary>
/// 图层
/// </summary>
public IFeatureLayer FeatureLayer
{
get;
set;
}

/// <summary>
/// 重写ToString方法
/// </summary>
/// <returns></returns>
public override string ToString()
{
string sValue = string.IsNullOrEmpty(AliasName) ? FeaClsName : AliasName + "(" + FeaClsName + ")";
return sValue;
}
}

简单点图层符号化

frmFeatureLayerSelect frm = new frmFeatureLayerSelect();
frm.FormLoad(axMapControl.Map, esriFeatureType.esriFTSimple, esriGeometryType.esriGeometryPoint);
if (frm.ShowDialog() == DialogResult.OK)
{
IFeatureLayer pFeaLyr = frm.SelectFeatureLayer;
SymbolSimplePoint(pFeaLyr);
}

private void SymbolSimplePoint(IFeatureLayer pFeaLyr)
{
if (pFeaLyr == null || pFeaLyr.FeatureClass == null)
{
return;
}

try
{
IGeoFeatureLayer pGeoFeaLyr = pFeaLyr as IGeoFeatureLayer;
ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbolClass();
////符号样式为方形
pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSSquare;

IRgbColor pRgbColor = GetRgbColor(255, 100, 100);
pSimpleMarkerSymbol.Color = pRgbColor;

ISymbol pSymbol = (ISymbol)pSimpleMarkerSymbol;

////更改符号样式
ISimpleRenderer pSimpleRender = new SimpleRendererClass();
pSimpleRender.Symbol = pSymbol;
pGeoFeaLyr.Renderer = pSimpleRender as IFeatureRenderer;

////刷新
axMapControl.Refresh();
axTOCControl.Update();
}
catch(Exception ex)
{
MessageBox.Show("简单符号化点失败:" + ex.Message);
}
}

8,点多图层符号化

frmFeatureLayerSelect frm = new frmFeatureLayerSelect();
frm.FormLoad(axMapControl.Map, esriFeatureType.esriFTSimple, esriGeometryType.esriGeometryPoint);
if (frm.ShowDialog() == DialogResult.OK)
{
IFeatureLayer pFeaLyr = frm.SelectFeatureLayer;
SymbolMultiLayerPoint(pFeaLyr);
}

private void SymbolMultiLayerPoint(IFeatureLayer pFeaLyr)
{
if (pFeaLyr == null || pFeaLyr.FeatureClass == null)
{
return;
}

try
{
IGeoFeatureLayer pGeoFeaLyr = pFeaLyr as IGeoFeatureLayer;

////创建字符符号
ICharacterMarkerSymbol pCharacterMarkerSymbol = new CharacterMarkerSymbolClass();
IRgbColor pRgbColor = GetRgbColor(0, 0, 0);
IFontDisp pFontDisp = (IFontDisp)(new StdFontClass());
pFontDisp.Name = "arial";
pFontDisp.Italic = true;
pFontDisp.Size = 12;
pCharacterMarkerSymbol.Angle = 0;
pCharacterMarkerSymbol.CharacterIndex = 97;
pCharacterMarkerSymbol.Color = pRgbColor;
pCharacterMarkerSymbol.Font = pFontDisp;
pCharacterMarkerSymbol.Size = 24;

////创建图片符号
IPictureMarkerSymbol pPictureMarkerSymbol = new PictureMarkerSymbolClass();
string sFilePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
sFilePath = sFilePath + @".... esmpcity.bmp";
if (!File.Exists(sFilePath))
{
return;
}

pPictureMarkerSymbol.CreateMarkerSymbolFromFile(esriIPictureType.esriIPictureBitmap, sFilePath);
pPictureMarkerSymbol.Angle = 0;
pPictureMarkerSymbol.BitmapTransparencyColor = pRgbColor;
pPictureMarkerSymbol.Size = 10;

////创建叠加符号
IMultiLayerMarkerSymbol pMultiLayerMarkerSymbol = new MultiLayerMarkerSymbolClass();
pMultiLayerMarkerSymbol.AddLayer(pCharacterMarkerSymbol);
pMultiLayerMarkerSymbol.AddLayer(pPictureMarkerSymbol);
pMultiLayerMarkerSymbol.Angle = 0;
pMultiLayerMarkerSymbol.Size = 20;
pMultiLayerMarkerSymbol.XOffset = 5;
pMultiLayerMarkerSymbol.YOffset = 5;

////更改符号样式
ISymbol pSymbol = (ISymbol)pMultiLayerMarkerSymbol;
ISimpleRenderer pSimpleRender = new SimpleRendererClass();
pSimpleRender.Symbol = pSymbol;
pGeoFeaLyr.Renderer = pSimpleRender as IFeatureRenderer;

////刷新
axMapControl.Refresh();
axTOCControl.Update();
}
catch (Exception ex)
{
MessageBox.Show("图片符号化点失败:" + ex.Message);
}
}

10.多图层符号化线

frmFeatureLayerSelect frm = new frmFeatureLayerSelect();
frm.FormLoad(axMapControl.Map, esriFeatureType.esriFTSimple, esriGeometryType.esriGeometryPolyline);
if (frm.ShowDialog() == DialogResult.OK)
{
IFeatureLayer pFeaLyr = frm.SelectFeatureLayer;
SymbolMultiLayerLine(pFeaLyr);
}

private void SymbolMultiLayerLine(IFeatureLayer pFeaLyr)
{
if (pFeaLyr == null || pFeaLyr.FeatureClass == null)
{
return;
}

try
{
IGeoFeatureLayer pGeoFeaLyr = pFeaLyr as IGeoFeatureLayer;

////创建简单符号
ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbolClass();
IRgbColor pRgbColor = GetRgbColor(255, 0, 0);
pSimpleLineSymbol.Width = 2;
pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSInsideFrame;
pSimpleLineSymbol.Color = pRgbColor;

////创建制图符号
ICartographicLineSymbol pCartographicLineSymbol = new CartographicLineSymbolClass();
pCartographicLineSymbol.Cap = esriLineCapStyle.esriLCSRound;
pCartographicLineSymbol.Join = esriLineJoinStyle.esriLJSRound;
pCartographicLineSymbol.Width = 2;
pRgbColor = GetRgbColor(0, 255, 0);
pCartographicLineSymbol.Color = pRgbColor;

////设置线要素符号模板
ILineProperties pLineProperties = pCartographicLineSymbol as ILineProperties;
pLineProperties.Offset = 0;

double[] dob = new double[6];
dob[0] = 0;
dob[1] = 1;
dob[2] = 2;
dob[3] = 3;
dob[4] = 4;
dob[5] = 5;

ITemplate pTemplate = new TemplateClass();
pTemplate.Interval = 1;
for (int i = 0; i < dob.Length; i = i + 2)
{
pTemplate.AddPatternElement(dob[i], dob[i + 1]);
}

pLineProperties.Template = pTemplate;

////创建多图层符号
IMultiLayerLineSymbol pMultiLayerLineSymbol = new MultiLayerLineSymbolClass();
pMultiLayerLineSymbol.AddLayer(pSimpleLineSymbol);
pMultiLayerLineSymbol.AddLayer(pCartographicLineSymbol);

////更改符号样式
ISymbol pSymbol = (ISymbol)pMultiLayerLineSymbol;
ISimpleRenderer pSimpleRender = new SimpleRendererClass();
pSimpleRender.Symbol = pSymbol;
pGeoFeaLyr.Renderer = pSimpleRender as IFeatureRenderer;

////刷新
axMapControl.Refresh();
axTOCControl.Update();
}
catch (Exception ex)
{
MessageBox.Show("多图层符号化线失败:" + ex.Message);
}
}

11.图片符号化线

frmFeatureLayerSelect frm = new frmFeatureLayerSelect();
frm.FormLoad(axMapControl.Map, esriFeatureType.esriFTSimple, esriGeometryType.esriGeometryPolyline);
if (frm.ShowDialog() == DialogResult.OK)
{
IFeatureLayer pFeaLyr = frm.SelectFeatureLayer;
SymbolPictureLine(pFeaLyr);
}

private void SymbolPictureLine(IFeatureLayer pFeaLyr)
{
if (pFeaLyr == null || pFeaLyr.FeatureClass == null)
{
return;
}

try
{
IGeoFeatureLayer pGeoFeaLyr = pFeaLyr as IGeoFeatureLayer;

IPictureLineSymbol pPictureLineSymbol = new PictureLineSymbolClass();
string sFilePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
sFilePath = sFilePath + @".... esmporder.bmp";
if (!File.Exists(sFilePath))
{
return;
}

pPictureLineSymbol.CreateLineSymbolFromFile(esriIPictureType.esriIPictureBitmap, sFilePath);

IRgbColor pRgbColor = GetRgbColor(255, 0, 0);
pPictureLineSymbol.Color = pRgbColor;
pPictureLineSymbol.Offset = 0;
pPictureLineSymbol.Width = 2;
pPictureLineSymbol.Rotate = false;

ISymbol pSymbol = (ISymbol)pPictureLineSymbol;
////更改符号样式
ISimpleRenderer pSimpleRender = new SimpleRendererClass();
pSimpleRender.Symbol = pSymbol;
pGeoFeaLyr.Renderer = pSimpleRender as IFeatureRenderer;

////刷新
axMapControl.Refresh();
axTOCControl.Update();
}
catch (Exception ex)
{
MessageBox.Show("离散型符号化线失败:" + ex.Message);
}
}


private void btnHashLine_ItemClick(object sender, ItemClickEventArgs e)
{
frmFeatureLayerSelect frm = new frmFeatureLayerSelect();
frm.FormLoad(axMapControl.Map, esriFeatureType.esriFTSimple, esriGeometryType.esriGeometryPolyline);
if (frm.ShowDialog() == DialogResult.OK)
{
IFeatureLayer pFeaLyr = frm.SelectFeatureLayer;
SymbolHashLine(pFeaLyr);
}
}

private void SymbolHashLine(IFeatureLayer pFeaLyr)
{
if (pFeaLyr == null || pFeaLyr.FeatureClass == null)
{
return;
}

try
{
IGeoFeatureLayer pGeoFeaLyr = pFeaLyr as IGeoFeatureLayer;

IHashLineSymbol pHashLineSymbol = new HashLineSymbolClass();
////设置线要素符号模板
ILineProperties pLineProperties = pHashLineSymbol as ILineProperties;
pLineProperties.Offset = 0;
double[] dob = new double[6];
dob[0] = 0;
dob[1] = 1;
dob[2] = 2;
dob[3] = 3;
dob[4] = 4;
dob[5] = 5;
ITemplate pTemplate = new TemplateClass();
pTemplate.Interval = 1;
for (int i = 0; i < dob.Length; i = i + 2)
{
pTemplate.AddPatternElement(dob[i], dob[i + 1]);
}
pLineProperties.Template = pTemplate;

pHashLineSymbol.Width = 2;
pHashLineSymbol.Angle = 45;
IRgbColor pRgbColor = GetRgbColor(0, 0, 255);
pHashLineSymbol.Color = pRgbColor;

////更改符号样式
ISymbol pSymbol = (ISymbol)pHashLineSymbol;
ISimpleRenderer pSimpleRender = new SimpleRendererClass();
pSimpleRender.Symbol = pSymbol;
pGeoFeaLyr.Renderer = pSimpleRender as IFeatureRenderer;

////刷新
axMapControl.Refresh();
axTOCControl.Update();
}
catch (Exception ex)
{
MessageBox.Show("图片符号化线失败:" + ex.Message);
}
}

13,面渐变色

frmFeatureLayerSelect frm = new frmFeatureLayerSelect();
frm.FormLoad(axMapControl.Map, esriFeatureType.esriFTSimple, esriGeometryType.esriGeometryPolygon);
if (frm.ShowDialog() == DialogResult.OK)
{
IFeatureLayer pFeaLyr = frm.SelectFeatureLayer;
SymbolGradientPolygon(pFeaLyr);
}

private void SymbolGradientPolygon(IFeatureLayer pFeaLyr)
{
if (pFeaLyr == null || pFeaLyr.FeatureClass == null)
{
return;
}

try
{
IGeoFeatureLayer pGeoFeaLyr = pFeaLyr as IGeoFeatureLayer;

IGradientFillSymbol pGradientFillSymbol = new GradientFillSymbolClass();

////设置颜色带
IAlgorithmicColorRamp pAlgorithmicColorRamp = new AlgorithmicColorRampClass();
IRgbColor pFromColor = GetRgbColor(255, 0, 0);
IRgbColor pToColor = GetRgbColor(0, 255, 0);
pAlgorithmicColorRamp.FromColor = pFromColor;
pAlgorithmicColorRamp.ToColor = pToColor;
pAlgorithmicColorRamp.Algorithm = esriColorRampAlgorithm.esriHSVAlgorithm;

pGradientFillSymbol.ColorRamp = pAlgorithmicColorRamp;
pGradientFillSymbol.GradientAngle = 90; ////填充方向
pGradientFillSymbol.GradientPercentage = 1; ////饱和度
pGradientFillSymbol.IntervalCount = 5; ////色带数目
pGradientFillSymbol.Style = esriGradientFillStyle.esriGFSLinear; ////线性填充

////更改符号样式
ISymbol pSymbol = (ISymbol)pGradientFillSymbol;
ISimpleRenderer pSimpleRender = new SimpleRendererClass();
pSimpleRender.Symbol = pSymbol;
pGeoFeaLyr.Renderer = pSimpleRender as IFeatureRenderer;

////刷新
axMapControl.Refresh();
axTOCControl.Update();
}
catch (Exception ex)
{
MessageBox.Show("渐变色填充符号化面失败:" + ex.Message);
}
}

14.点填充面

frmFeatureLayerSelect frm = new frmFeatureLayerSelect();
frm.FormLoad(axMapControl.Map, esriFeatureType.esriFTSimple, esriGeometryType.esriGeometryPolygon);
if (frm.ShowDialog() == DialogResult.OK)
{
IFeatureLayer pFeaLyr = frm.SelectFeatureLayer;
SymbolPointPolygon(pFeaLyr);
}

private void SymbolPointPolygon(IFeatureLayer pFeaLyr)
{
if (pFeaLyr == null || pFeaLyr.FeatureClass == null)
{
return;
}

try
{
IGeoFeatureLayer pGeoFeaLyr = pFeaLyr as IGeoFeatureLayer;

IArrowMarkerSymbol pArrowMarkerSymbol = new ArrowMarkerSymbolClass();
IRgbColor pRgbColor = GetRgbColor(255, 0, 0);
pArrowMarkerSymbol.Color = pRgbColor;
pArrowMarkerSymbol.Length = 2;
pArrowMarkerSymbol.Width = 2;
pArrowMarkerSymbol.Style = esriArrowMarkerStyle.esriAMSPlain;

IMarkerFillSymbol pMarkerFillSymbol = new MarkerFillSymbolClass();
pMarkerFillSymbol.MarkerSymbol = pArrowMarkerSymbol;
pRgbColor = GetRgbColor(255, 0, 0);
pMarkerFillSymbol.Color = pRgbColor;
pMarkerFillSymbol.Style = esriMarkerFillStyle.esriMFSGrid;

ILineSymbol pLineSymbol = new SimpleLineSymbolClass();
pLineSymbol.Width = 1;
pRgbColor = GetRgbColor(0, 255, 0);
pLineSymbol.Color = pRgbColor;
pMarkerFillSymbol.Outline = pLineSymbol;

IFillProperties pFillProperties = pMarkerFillSymbol as IFillProperties;
pFillProperties.XOffset = 2;
pFillProperties.YOffset = 2;
pFillProperties.XSeparation = 5;
pFillProperties.YSeparation = 5;

////更改符号样式
ISymbol pSymbol = (ISymbol)pMarkerFillSymbol;
ISimpleRenderer pSimpleRender = new SimpleRendererClass();
pSimpleRender.Symbol = pSymbol;
pGeoFeaLyr.Renderer = pSimpleRender as IFeatureRenderer;

////刷新
axMapControl.Refresh();
axTOCControl.Update();
}
catch (Exception ex)
{
MessageBox.Show("线填充符号化面失败:" + ex.Message);
}
}

15, 简单唯一值渲染

frmFeatureLayerSelect frm = new frmFeatureLayerSelect();
frm.FormLoad(axMapControl.Map, esriFeatureType.esriFTSimple, esriGeometryType.esriGeometryPolygon);
if (frm.ShowDialog() == DialogResult.OK)
{
IFeatureLayer pFeaLyr = frm.SelectFeatureLayer;
UniqueValueRender(pFeaLyr, pFeaLyr.FeatureClass.FeatureCount(null), pFeaLyr.FeatureClass.OIDFieldName);
}

private void UniqueValueRender(IFeatureLayer pFtLayer, int pCount, string pFieldName)
{
IGeoFeatureLayer pGeoFeaturelayer = pFtLayer as IGeoFeatureLayer;
IUniqueValueRenderer pUnique = new UniqueValueRendererClass();
pUnique.FieldCount = 1;
pUnique.set_Field(0, pFieldName);
ISimpleFillSymbol pSimFill = new SimpleFillSymbolClass();
//给颜色内部文档,请勿外传
IFeatureCursor pFtCursor = pFtLayer.FeatureClass.Search(null, false);
IFeature pFt = pFtCursor.NextFeature();
IFillSymbol pFillSymbol1;
IRandomColorRamp pColorRamp = new RandomColorRampClass();
pColorRamp.StartHue = 0;
pColorRamp.MinValue = 20;
pColorRamp.MinSaturation = 15;
pColorRamp.EndHue = 360;
pColorRamp.MaxValue = 100;
pColorRamp.MaxSaturation = 30;
pColorRamp.Size = pCount;
bool ok = true;
pColorRamp.CreateRamp(out ok);
IEnumColors pEnumRamp = pColorRamp.Colors;
int pIndex = pFt.Fields.FindField(pFieldName);
while (pFt != null)
{
IColor pColor = pEnumRamp.Next();
if (pColor == null)
{
pEnumRamp.Reset();
pColor = pEnumRamp.Next();
}

pFillSymbol1 = new SimpleFillSymbolClass();
pFillSymbol1.Color = pColor;
pUnique.AddValue(Convert.ToString(pFt.get_Value(pIndex)), pFieldName, pFillSymbol1 as ISymbol);
pFt = pFtCursor.NextFeature();
}

pGeoFeaturelayer.Renderer = pUnique as IFeatureRenderer;
axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
axTOCControl.Update();
}

原文地址:https://www.cnblogs.com/bby2014210552/p/6269380.html