【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(二)
Graphics layer用于显示用户自定义绘制的点、线、面图形。使用时确保xaml文件中Graphics layer定义在其它图层的下面,以确保它能显示在其它图层的上面。
<esri:Map x:Name="MyMap" Extent=", , , " >
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayerID="."Url="http://../rest/./MapServer"/>
<esri:GraphicsLayer ID=”.” />
</esri:Map.Layers>
</esri:Map>
在Graphics layer上创建Graphics的步骤一般如下:
(1)获取Graphics layer
(2)创建或获取Graphic
(3)设置Graphic的Geometry
(4)应用Graphic的Symbol
(5)将Graphic添加到Graphics layers
代码如下:
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
foreach (Graphic graphic in graphicsList)
{
graphic.Symbol = MySymbol;
graphicsLayer.Graphics.Add(graphic);
}
Draw surface用于获取Geometries,Geometries可添加到Graphics Layer或用作identify和buffer操作。
使用Draw surface,你必须
(1)设置绘图操作的Symbols
(2)设置Draw surface的地图
(3)执行逻辑以激活|解除surface
(4)处理Geometries,以在surface上绘图
示例代码如下:
xaml文件:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<esriSymbols:SimpleFillSymbol x:Name="RedFillSymbol" Fill="#66FF0000" BorderBrush="Red" BorderThickness="2" />
</Grid.Resources>
<esri:Map …>
…
cs文件:
MyDrawObject = new Draw(MyMap)
{ LineSymbol =LayoutRoot.Resources["DrawLineSymbol"] as LineSymbol,
FillSymbol =LayoutRoot.Resources["DrawFillSymbol"] as FillSymbol };
MyDrawObject.DrawComplete += MyDrawObject_DrawComplete;
MyDrawObject.DrawMode = DrawMode.Polygon;
MyDrawObject.IsEnabled = true;
private void MyDrawObject_DrawComplete(object sender, ESRI.ArcGIS.Client.DrawEventArgs args)
{
Graphic graphic = new Graphic() { Geometry = args.Geometry, Symbol = RedFillSymbol };
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.Graphics.Add(graphic);
}
Symbols定义了Graphic的非几何学方面的显示特性,如颜色、边框宽度、透明度等。
Renderers定义了一个或多个应用于Graphics layer的Symbols,指定哪些Graphics属性与哪个Symbol相符。
Symbols和Geometries类型:
Symbol |
Geometry |
描述 |
SimpleMarkerSymbol |
Point |
用简单形状来表现点 |
PictureMarkerSymbol |
Point |
用images来表现点 |
SimpleLineSymbol |
Polyline |
用预定义的风格来表现线 |
CartographicLineSymbol |
Polyline |
用定制的风格来表现线 |
SimpleFillSymbol |
Polygon |
用Silverlight Brush来填充多边形 |
PictureFillSymbol |
Polygon |
用images填充多边形 |
通常,视觉定义在xaml文件中,行为逻辑定义在.cs文件中,让表现层和业务逻辑层分开,使得应用程序更容易开发、维护和扩展。
Symbol的使用:
(1)添加命名空间:symbol类定义在ESRI.ArcGIS.Client.Symbols命名空间中(ESRI.ArcGIS.Client集)
xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
(2)xaml文件中定义Symbol
<Grid.Resources>
<esriSymbols:SimpleFillSymbol x:Name="MyRedFillSymbol" Fill="#66FF0000" BorderBrush="Red" BorderThickness="2" />
</Grid.Resources>
//Symbol运用于FeatureLayer
<esri:FeatureLayer ID="." Where="1=1" FeatureSymbol="{StaticResource MyRedFillSymbol}"
Url="http://./ArcGIS/rest/services/./MapServer/5" >
<esri:FeatureLayer.OutFields>
<sys:String>POP07_SQMI</sys:String>
</esri:FeatureLayer.OutFields>
</esri:FeatureLayer>
(3)cs文件中动态生成
SimpleFillSymbol fillSymbol = new SimpleFillSymbol()
{
BorderBrush = new SolidColorBrush(Color.FromArgb(0, 255, 0, 0)),
BorderThickness = 2,
Fill = new SolidColorBrush(Color.FromArgb(alphaVal, redVal, greenVal, blueVal))
};
//Symbol运用于GraphicsLayer的每一个graphic
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
foreach (Graphic graphic in graphicsLayer.Graphics)
graphic.Symbol = fillSymbol;
创建Unique Value Renderer:
<Grid.Resources>
<esriSymbols:SimpleFillSymbol x:Name="a" Fill=""BorderBrush=""BorderThickness="" />
<esriSymbols:SimpleFillSymbol x:Name="b" Fill=""BorderBrush=""BorderThickness="" />
<esriSymbols:SimpleFillSymbol x:Name="c" Fill=""BorderBrush=""BorderThickness="" />
<esri:UniqueValueRenderer x:Name="abcRenderer" Attribute="STATE_NAME" >
<esri:UniqueValueRenderer.Infos>
<esri:UniqueValueInfo Value="California" Symbol="{StaticResource a}" />
<esri:UniqueValueInfo Value="New York" Symbol="{StaticResource b}" />
<esri:UniqueValueInfo Value="Kansas" Symbol="{StaticResource c}" />
</esri:UniqueValueRenderer.Infos>
</esri:UniqueValueRenderer>
</Grid.Resources>
// FeatureLayer中,指定一个过滤,仅仅California、New York、Kansas被绘制
//并且将其STATE_NAME字段的值显示在layer的Graphics中
<esri:FeatureLayer ID=""
Where="(STATE_NAME='California') OR (STATE_NAME='New York') OR (STATE_NAME = 'Kansas')"
Renderer="{StaticResource abcRenderer}"
Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5" >
<esri:FeatureLayer.OutFields>
<sys:String>STATE_NAME</sys:String>
</esri:FeatureLayer.OutFields>
</esri:FeatureLayer>
创建Class Breaks Renderer:即将symbol应用于一组指定范围的graphics中。
<Grid.Resources>
<esriSymbols:SimpleFillSymbol x:Name="a" Fill="" BorderBrush=""BorderThickness="" />
<esriSymbols:SimpleFillSymbol x:Name="b" Fill="" BorderBrush=""BorderThickness="" />
<esriSymbols:SimpleFillSymbol x:Name="c" Fill="" BorderBrush=""BorderThickness="" />
<esri:ClassBreaksRenderer x:Name="abcRenderer" Attribute="POP07_SQMI" >
<esri:ClassBreaksRenderer.Classes>
<esri:ClassBreakInfo MinimumValue="0" MaximumValue="50" Symbol="{StaticResource a}" />
<esri:ClassBreakInfo MinimumValue="51" MaximumValue="125" Symbol="{StaticResource b}" />
<esri:ClassBreakInfo MinimumValue="125" MaximumValue="2000" Symbol="{StaticResource c}" />
</esri:ClassBreaksRenderer.Classes>
</esri:ClassBreaksRenderer>
</Grid.Resources>
// FeatureLayer中,将renderer和Feature layer联系起来,进行地图绘制
//并且将其POP07_SQMI字段的值显示在layer的Graphics中
<esri:FeatureLayer ID="" Where="1=1" Renderer="{StaticResource abcRenderer}"
Url="http://./ArcGIS/rest/services/./MapServer/5" >
<esri:FeatureLayer.OutFields>
<sys:String>POP07_SQMI</sys:String>
</esri:FeatureLayer.OutFields>
</esri:FeatureLayer>
5、使用Clustering(聚类分组,用于render的数量很大时)
当点很多和密集时,使用Clustering将点分组,使得在cluster distance内的多个点用一个点代替。Clustering可用于GraphicsLayer和Feature Layer。
(1)使用FlareClusterer
FlareClusterer可按如下方式添加到GraphicsLayer和FeatureLayer中:
<esri:GraphicsLayer ID="MyGraphicsLayer">
<esri:GraphicsLayer.Clusterer>
<esri:FlareClusterer />
</esri:GraphicsLayer.Clusterer>
</esri:GraphicsLayer>
效果如下图:
FlareClusterer的属性如下表:
FlareClusterer属性 |
描述 |
FlareBackground |
填充的背景颜色(默认红色) |
FlareForeground |
边界和文字颜色(默认白色) |
MaximumFlareCount |
当鼠标移动到cluster时,各小点是否展开的最大数量界限 小于此值时,鼠标移上去会展开各小点;大于此值时,称为large clusters,其颜色和大小会根据点多少变化。(默认=10) |
Radius |
被cluster的半径,单位pixels(默认20) |
Gradient |
LinearGradientBrush线性渐变刷用于large clusters (默认:Default = LinearGradientBrush; MappingMode = RelativeToBoundingBox; GradientStop1: Offset = 0, Argb = 127,255,255,0, GradientStop2: Offset = 1, Argb = 127,255,0,0) |
示例:修改FlareClusterer的默认属性
<Grid.Resources>
<LinearGradientBrush x:Name="aGradient" MappingMode="RelativeToBoundingBox" >
<GradientStop Color="#990011FF" Offset="0"/>
<GradientStop Color="#990055FF" Offset="0.25"/>
<GradientStop Color="#990099FF" Offset="0.5"/>
<GradientStop Color="#9900CCFF" Offset="0.75"/>
<GradientStop Color="#9900FFFF" Offset="1"/>
</LinearGradientBrush>
</Grid.Resources>
<esri:Map x:Name="MyMap">
<esri:Map.Layers>
<esri:GraphicsLayer ID="MyGraphicsLayer">
<esri:GraphicsLayer.Clusterer>
<esri:FlareClusterer FlareBackground="Yellow" FlareForeground="#99000000"
MaximumFlareCount="5" Radius="15" Gradient="{StaticResource aGradient}" />
</esri:GraphicsLayer.Clusterer>
</esri:GraphicsLayer>
</esri:Map.Layers>
</esri:Map>
(2)扩展GraphicsClusterer
为了定制cluster的外观,你可以创建一个继承自ESRI.ArcGIS.Client.GraphicsClusterer的类,并重写OnCreateGraphic()方法来定义cluster graphic。示例代码如下:
public class SumClusterer : GraphicsClusterer
{
public SumClusterer()
{
MinimumColor = Colors.Red;
MaximumColor = Colors.Yellow;
SymbolScale = 1;
base.Radius = 50;
}
public string AggregateColumn { get; set; }
public double SymbolScale { get; set; }
public Color MinimumColor { get; set; }
public Color MaximumColor { get; set; }
protected override Graphic OnCreateGraphic(GraphicCollection cluster, MapPoint point, int maxClusterCount)
{
if (cluster.Count == 1) return cluster[0];
Graphic graphic = null;
double sum = 0;
foreach (Graphic g in cluster)
{
if (g.Attributes.ContainsKey(AggregateColumn))
{try{sum += Convert.ToDouble(g.Attributes[AggregateColumn]); }}
}
double size = (sum + 450) / 30;
size = (Math.Log(sum * SymbolScale / 10) * 10 + 20);
if (size < 12) size = 12;
graphic=new Graphic(){Symbol=new ClusterSymbol() {Size = size},Geometry= point};
graphic.Attributes.Add("Count", sum);
graphic.Attributes.Add("Size", size);
graphic.Attributes.Add("Color", InterpolateColor(size - 12, 100));
return graphic;
}
private static Brush InterpolateColor(double value, double max)
{
value = (int)Math.Round(value * 255.0 / max);
if (value > 255) value = 255;
else if (value < 0) value = 0;
return new SolidColorBrush(Color.FromArgb(127, 255, (byte)value, 0));
}
}