ArcGIS Engine 使用制图表达(Representation)保存图层渲染信息(转载)
转自:http://www.lugang.info/2012/11/arcgis-engine-representation.html
ArcGIS Engine 使用制图表达(Representation)保存图层渲染信息
在对图层进行符号化渲染后,可将图层符号对象转换成制图表达以保存图层渲染信息,在ArcMap中对应着图层树右键”Convert Symbology to Representation”功能。在上一篇博文《ArcGIS图层渲染信息持久化方法》中对采用制图表达存储图层渲染信息的方法作过简单总结,本文将讨论如何使用ArcGIS Object对此功能进行实现。
基本思路
在ArcGIS中,图层的渲染方法由图层的Renderer属性控制,该属性是一个IFeatureRenderer接口对象,实现此接口的渲染类如UiquerValueRendererClass、ClassBreaksRendererClass等都可以赋值给Renderer属性,改变图层的渲染方式。在这些渲染类对象中存储了图层的渲染符号、渲染的属性值等,但是它们都只是存储在内存中,图层关闭后随之消亡。
制图表达渲染类RepresentationRendererClass也实现IFeatureRenderer接口,它将图层的渲染规则存储在图层的属性字段中,渲染规则不会随图层的消亡而消失且一个图层可以定义多种制图表达。
所以只需将图层的渲染方式生成制图表达规则,并将规则写入属性表,转换成制图表达渲染类赋值给图层,即可实现图层的制图表达渲染。为了方便管理存储在图层中的制图表达信息,只考虑在一个图层中储存一个制图表达流程图如下。
关键代码
转换方法总体思路
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#region 转换图层符号渲染到Representation渲染
///
/// 转换图层符号渲染到Representation渲染
///
///图层
///Representation渲染对象
public IRepresentationRenderer ConvertSymbolsToRepresentationRenderer(IFeatureLayer pFeatureLayer)
{
IGeoFeatureLayer pGeoFeatureLayer = pFeatureLayer as IGeoFeatureLayer;
//如不能转换返回
IDataset pDataset = pFeatureLayer as IDataset;
IWorkspace pWorkspace = pDataset.Workspace;
if (pWorkspace.Type == esriWorkspaceType.esriFileSystemWorkspace)
{
return null; ;
}
//创建表达类时应停止编辑进程
IWorkspaceEdit pWksEdit = pWorkspace as IWorkspaceEdit;
if (pWksEdit.IsBeingEdited())
{
pWksEdit.StopEditOperation();
pWksEdit.StopEditing(true);
}
//制图表达扩展
IRepresentationWorkspaceExtension pRepWksExt = GetRepresentationWrokspaceExtFromFeatureClass(pFeatureLayer.FeatureClass);
//存在Representation则删除
bool isContainPepresentationClass = pRepWksExt.get_FeatureClassHasRepresentations(pFeatureLayer.FeatureClass);
if (isContainPepresentationClass)
{
//删除制图表达
DropRepresentation(pRepWksExt, pFeatureLayer.FeatureClass);
}
//删除了后重新转换
if (!pRepWksExt.get_FeatureClassHasRepresentations(pFeatureLayer.FeatureClass))
{
IRepresentationRenderer pRepresentationRenderer = Rerder2RepresentationRenderer(pFeatureLayer, pRepWksExt);
return pRepresentationRenderer;
}
return null ;
}
#endregion
|
转换到渲染规则,生成制表表达类,属性赋值,生成制图表达渲染对象
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#region 将图层渲染转换到Representation渲染
///
/// 将图层渲染转换到Representation渲染
///
///图层
///Representation扩展
///Representation渲染对象
private IRepresentationRenderer Rerder2RepresentationRenderer(IFeatureLayer pFeatureLayer, IRepresentationWorkspaceExtension pRepWksExt)
{
IRepresentationRenderer pRepresentationRenderer = new RepresentationRendererClass(); ;
IRepresentationRules pRepresentationRules = new RepresentationRulesClass();
IRepresentationRule pRepresentationRule=null;
IRepresentationRuleInit pRepresentationRuleInit=null;
IRepresentationClass pRepresentationClass = null;
IGeoFeatureLayer pGeoFeatureLayer = pFeatureLayer as IGeoFeatureLayer;
if (pGeoFeatureLayer.Renderer is ISimpleRenderer)
{
//设置简单符号渲染rules
ISimpleRenderer pSimpleRender = pGeoFeatureLayer.Renderer as ISimpleRenderer;
pRepresentationRule = new RepresentationRuleClass();
pRepresentationRuleInit = pRepresentationRule as IRepresentationRuleInit;
pRepresentationRuleInit.InitWithSymbol(pSimpleRender.Symbol);
int id = pRepresentationRules.Add(pRepresentationRule);
pRepresentationRules.set_Name(id, pSimpleRender.Label);
//生成representataionclass
pRepresentationClass =
pRepWksExt.CreateRepresentationClass(pFeatureLayer.FeatureClass, pFeatureLayer.Name +
"_Rep", "RuleId_SingleRenderer", "OverrideId_SingleRenderer", false, pRepresentationRules, null);
///更新rules到属性表
SetSimpleRendererRuleIDValue(pRepresentationClass, pRepresentationRules);
//赋值representataionclass到representataion渲染器
pRepresentationRenderer.RepresentationClass = pRepresentationClass;
}
else if (pGeoFeatureLayer.Renderer is IUniqueValueRenderer)
{
IUniqueValueRenderer pUniqueRenderer = pGeoFeatureLayer.Renderer as IUniqueValueRenderer;
string uniqueRendererFieldName = pUniqueRenderer.get_Field(0);
//设置唯一值渲染rules
for (int i = 0; i < pUniqueRenderer.ValueCount; i++)
{
//设置各唯一值符号和值
pRepresentationRule = new RepresentationRuleClass();
pRepresentationRuleInit = pRepresentationRule as IRepresentationRuleInit;
pRepresentationRuleInit.InitWithSymbol(pUniqueRenderer.get_Symbol(pUniqueRenderer.get_Value(i)));
int ruleid = pRepresentationRules.Add(pRepresentationRule);
pRepresentationRules.set_Name(ruleid, pUniqueRenderer.get_Value(i));
}
//生成representataionclass
pRepresentationClass =
pRepWksExt.CreateRepresentationClass(pFeatureLayer.FeatureClass, pFeatureLayer.Name +
"_"+uniqueRendererFieldName, "RuleId_UniqueRender", "OverrideId_UniqueRender", false, pRepresentationRules, null);
///更新rules到属性表
SetUniqueRendererRuleIDValue(pRepresentationClass, pRepresentationRules, uniqueRendererFieldName);
//赋值representataionclass到representataion渲染器
pRepresentationRenderer.RepresentationClass = pRepresentationClass;
}
//分级渲染
else if (pGeoFeatureLayer.Renderer is IClassBreaksRenderer)
{
IClassBreaksRenderer pClassBreaksRenderer = pGeoFeatureLayer.Renderer as IClassBreaksRenderer;
string classBreaksFieldName = pClassBreaksRenderer.Field;
//设置渲染rules
for (int i = 0; i < pClassBreaksRenderer.BreakCount; i++)
{
//设置各唯一值符号和值
pRepresentationRule = new RepresentationRuleClass();
pRepresentationRuleInit = pRepresentationRule as IRepresentationRuleInit;
pRepresentationRuleInit.InitWithSymbol(pClassBreaksRenderer.get_Symbol(i));
int ruleid = pRepresentationRules.Add(pRepresentationRule);
pRepresentationRules.set_Name(ruleid, pClassBreaksRenderer.get_Break(i).ToString());
}
//生成representataionclass
pRepresentationClass =
pRepWksExt.CreateRepresentationClass(pFeatureLayer.FeatureClass, pFeatureLayer.Name +
"_" + classBreaksFieldName, "RuleId_ClassBreaks", "OverrideId_ClassBreaks", false, pRepresentationRules, null);
///更新rules到属性表
SetClassBreakRendererRuleIDValue(pRepresentationClass, pRepresentationRules, classBreaksFieldName);
//赋值representataionclass到representataion渲染器
pRepresentationRenderer.RepresentationClass = pRepresentationClass;
}
return pRepresentationRenderer;
}
#endregion
|
写图层制图表达图层属性
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#region 设置唯一值渲染ruleID值
///
/// 设置唯一值渲染ruleID值
///
///制图表达类
///制图表达规则集合
///唯一值字段
private void SetUniqueRendererRuleIDValue(IRepresentationClass pRepClass,IRepresentationRules pRepresentationRules, string uniqueFieldName)
{
IQueryFilter pQueryFilter = new QueryFilterClass();
IFeatureCursor pFCursor = pRepClass.FeatureClass.Update(pQueryFilter, false);
IFeature pFeature = pFCursor.NextFeature();
//ruleid字段索引
int ruleIDFieldIndex = pRepClass.RuleIDFieldIndex;
while (pFeature != null)
{
int index = 0;
IRepresentationRule rule = null;
pRepresentationRules.Reset();
pRepresentationRules.Next(out index, out rule);
while (rule != null)
{
//唯一值与规则中名称相同则赋值规则id
string tempName = pRepresentationRules.get_Name(index);
if (tempName == pFeature.get_Value(pFeature.Fields.FindField(uniqueFieldName)).ToString())
{
pFeature.set_Value(ruleIDFieldIndex, index);
break;
}
pRepresentationRules.Next(out index, out rule);
}
pFCursor.UpdateFeature(pFeature);
pFeature = pFCursor.NextFeature();
}
}
#
#region 设置简单符号渲染ruleID值
///
/// 设置简单符号渲染ruleID值
///
///制图表达类
///制图表达规则集合
private void SetSimpleRendererRuleIDValue(IRepresentationClass pRepClass, IRepresentationRules pRepresentationRules)
{
IQueryFilter pQueryFilter = new QueryFilterClass();
IFeatureCursor pFCursor = pRepClass.FeatureClass.Update(pQueryFilter, false);
IFeature pFeature = pFCursor.NextFeature();
//ruleid字段索引
int ruleIDFieldIndex = pRepClass.RuleIDFieldIndex;
while (pFeature != null)
{
int index = 0;
IRepresentationRule rule = null;
pRepresentationRules.Reset();
pRepresentationRules.Next(out index, out rule);
pFeature.set_Value(ruleIDFieldIndex, index);
pFCursor.UpdateFeature(pFeature);
pFeature = pFCursor.NextFeature();
}
}
#
#region 设置分级渲染ruleID值
///
/// 设置分级渲染ruleID值
///
///制图表达类
///制图表达规则集合
///分级字段
private void SetClassBreakRendererRuleIDValue(IRepresentationClass pRepClass, IRepresentationRules pRepresentationRules, string classBreakFieldName)
{
IQueryFilter pQueryFilter = new QueryFilterClass();
IFeatureCursor pFCursor = pRepClass.FeatureClass.Update(pQueryFilter, false);
IFeature pFeature = pFCursor.NextFeature();
//ruleid字段索引
int ruleIDFieldIndex = pRepClass.RuleIDFieldIndex;
while (pFeature != null)
{
int index = 0;
IRepresentationRule rule = null;
pRepresentationRules.Reset();
pRepresentationRules.Next(out index, out rule);
while (rule != null)
{
//等级值如小于等于第i规则中等级值,则视为应用i规则
double breakValue = Convert.ToDouble(pRepresentationRules.get_Name(index));
if (breakValue >= Convert.ToDouble(pFeature.get_Value(pFeature.Fields.FindField(classBreakFieldName)).ToString()))
{
pFeature.set_Value(ruleIDFieldIndex, index);
break;
}
pRepresentationRules.Next(out index, out rule);
}
pFCursor.UpdateFeature(pFeature);
pFeature = pFCursor.NextFeature();
}
}
#endregion
|
删除制图表达方法尝试了很多,最后使用AO中的GP方法实现
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#region 删除要素类中Representation
///
/// 删除要素类中Representation
///
///要素类
///是否删除成功
public bool RemoveRepresentation( IFeatureClass pFeatureClass)
{
IRepresentationWorkspaceExtension pRepWksExt = GetRepresentationWrokspaceExtFromFeatureClass(pFeatureClass);
IEnumDatasetName enumDatasetName = pRepWksExt.get_FeatureClassRepresentationNames(pFeatureClass);
enumDatasetName.Reset();
IDatasetName pDatasetName = enumDatasetName.Next();
ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
while (pDatasetName != null)
{
try
{
DropRepresentation dropRep = new DropRepresentation();
dropRep.representation = pDatasetName.Name;
dropRep.in_features = pFeatureClass;
gp.Execute(dropRep, null);
}
catch (Exception ex)
{
GISRenderUtil.PopupMsg(pDatasetName.Name + "删除失败");
return false;
}
pDatasetName = enumDatasetName.Next();
}
return true;
}
#endregion
|
效果图
唯一值渲染转制图表达效果
问题
1.制图表达不支持图表类符号渲染,所以不能对饼图、直方图符号渲染的图层进行制图表达转换。
2.在创建RepresentationClass时经常报“The required XML element was not found.”,在ArcMap中也有此种情况,原因未知,估计可能数据有关。
3.只能对GeoDatabase数据与SDE数据创建制图表达
浙公网安备 33010602011771号