【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(二)
五、Graphics layer
1、新增Graphics layer
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>
?
2、管理Graphics features
在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);
}
?
3、使用Draw surface
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);
}
?
4、Symbols和Renderers
?????? 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));
??? }
}
轉載于:https://www.cnblogs.com/changbaishan/p/3305961.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: step by step设置postgr
- 下一篇: 手机与笔记本蓝牙配对