所有习题材料:
链接:https://pan.baidu.com/s/1oiylGi6IEgndYO2j9_oQnQ
提取码:jp3r
复制这段内容后打开百度网盘手机应用程序,操作更方便哦
EX04 :编写程序,位于访问型文件数据库地理的要素类,并实现两种渲染器。
有了前面工作的基础,往后的习题应该都可以自己摸索了,本人的代码仅供参考,
此次实现主要落在了渲染上,所以没有采用IWorkSpace的方式打开要素,直接加载了MXD文件进行渲染。另外控件名称按照同学我的个人习惯作了修改,还是那句话,理解过程和看懂代码为主。
下面为我的窗体样式:
关于窗体和工具条等等的的搭建就不赘述了,不懂的自行看笔者之前的文章,全部代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.SystemUI;
//可能有多余的添加
private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
for (int i = 0; i < axMapControl1.LayerCount; i++)
{
ILayer pLayer = axMapControl1.get_Layer(i);
layer_cBox.Items.Add(pLayer.Name);
}
} //事件:当添加地图之后,自动将图层name读进layer_cBox下拉框内
private void layer_cBox_SelectedIndexChanged(object sender, EventArgs e)
{
red_cBox.Items.Clear();
Green_cBox.Items.Clear();
Blue_cBox.Items.Clear();
uniquevalue_cBox.Items.Clear();
int index = layer_cBox.SelectedIndex;
ILayer pLayer = axMapControl1.get_Layer(index); //设置图层可见性
pLayer.Visible = true;
axMapControl1.Refresh(); //刷新地图,不然显示不出来
IFeatureLayer featureLayer = pLayer as IFeatureLayer;
IFeatureClass featureClass = featureLayer.FeatureClass;
int num = featureClass.Fields.FieldCount; //读取所选要素的字段
for (int i = 0; i < num; i++)
{
string name = featureClass.Fields.get_Field(i).Name;
uniquevalue_cBox.Items.Add(name);
}
for (int j = 1; j < 256; j++)
{
red_cBox.Items.Add(j);
Green_cBox.Items.Add(j);
Blue_cBox.Items.Add(j);
}
}
第二种多值渲染直接采用了table的方式访问,因为没有涉及空间信息,也可以采用ifeatureCursur,完全可以
private void button1_Click(object sender, EventArgs e)
{
ILayer pLayer = null;
if (radioButton1.Checked == true && (layer_cBox.SelectedIndex == 0))
{
int RValue = Convert.ToInt32(red_cBox.Text);
int GValue = Convert.ToInt32(Green_cBox.Text);
int BValue = Convert.ToInt32(Blue_cBox.Text);
if ((RValue > 255 || RValue < 0) || (GValue > 255 || GValue < 0) || (BValue > 255 || BValue < 0))
{
MessageBox.Show("Please Enter Right RGB Value!");
}
pLayer = axMapControl1.get_Layer(0);
IGeoFeatureLayer geoLayer = pLayer as IGeoFeatureLayer;
ISimpleFillSymbol pSym = new SimpleFillSymbolClass(); // 构造SimpleRenderer,填充要素
IRgbColor pColor = new RgbColorClass();
pColor.Red = RValue;
pColor.Green =GValue ;
pColor.Blue = BValue;
pSym.Color = pColor;//赋颜色,
ISimpleRenderer pRenderer = new SimpleRendererClass();
pRenderer.Symbol = (ISymbol)pSym;
geoLayer.Renderer = (IFeatureRenderer)pRenderer;
axMapControl1.Refresh();//或axMapControl1.ActiveView.Refresh();
axTOCControl1.Update();//或axMapControl1.ActiveView.ContentsChanged();刷新
}
//以上为针对lake的唯一值渲染
if (radioButton2.Checked == true && (layer_cBox.SelectedIndex == 1))
{
pLayer = axMapControl1.get_Layer(1);
IGeoFeatureLayer geoLayer = pLayer as IGeoFeatureLayer;
IUniqueValueRenderer pUniqueValueRenderer = new UniqueValueRendererClass();
//构造一个UniqueValueRenderer
pUniqueValueRenderer.FieldCount = 1; //设置唯一值的字段为1
string FieldName = uniquevalue_cBox.SelectedItem.ToString(); //获取选取字段
pUniqueValueRenderer.set_Field(0, FieldName); //设置需要唯一值符号化、渲染的第一个关键字段
//下面创建并设置随机色谱并给其参数赋值(颜色更加相近),我们要给每一个值定义一种颜色
IRandomColorRamp pColorRamp = new RandomColorRampClass();
pColorRamp.StartHue = 0;
pColorRamp.MinValue = 20;
pColorRamp.MinSaturation = 15;
pColorRamp.EndHue = 360;
pColorRamp.MaxValue = 100;
pColorRamp.MaxSaturation = 30; //这部分代码可选,如果选择了就是地图颜色渐变差异小
IQueryFilter pQueryFilter = new QueryFilterClass();
pColorRamp.Size = geoLayer.FeatureClass.FeatureCount(pQueryFilter);//根据渲染字段的值的个数,设施一组随机颜色
bool out_parameter = false;
pColorRamp.CreateRamp(out out_parameter); //创建随机颜色→需要输出参数,参数类型为bool类型
IEnumColors pEnumRamp = pColorRamp.Colors; //创建颜色枚举
IColor pNextUniqueColor = null; //视为用来移动的指示颜色的指针
pQueryFilter = new QueryFilterClass(); //刷新值
pQueryFilter.AddField(FieldName); //添加待查询字段
ITable pTable = pLayer as ITable;
int FieldNo = pTable.FindField(FieldName); //字段在表中的索引(即第几列)
ICursor pCursor = pTable.Search(pQueryFilter, true);
IRow pNextRow = pCursor.NextRow();
IRowBuffer pNextRowBuffer = null;
object codeValue = null; //创建移动指针
while (pNextRow != null)
{
pNextRowBuffer = pNextRow as IRowBuffer;
codeValue = pNextRowBuffer.get_Value(FieldNo); //获取渲染字段的每一个值
pNextUniqueColor = pEnumRamp.Next();
if (pNextUniqueColor == null)
{
pEnumRamp.Reset();
pNextUniqueColor = pEnumRamp.Next();
} //若为空值则后移,重新赋值
IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
pFillSymbol.Color = pNextUniqueColor;
pUniqueValueRenderer.AddValue(codeValue.ToString(),"",pFillSymbol as ISymbol);
pNextRow = pCursor.NextRow();
}
geoLayer.Renderer = pUniqueValueRenderer as IFeatureRenderer;
axMapControl1.Refresh();
axTOCControl1.Update();
}
}
//点击渲染按钮,如果已经选择好字段并且选中图层Countries,随机渲染。
以上为本次作业,错误之处及不足请指出,不胜感激。
看更新加关注啊,嘻嘻