二篇、Silverlight客户端调用GP服务
利用ArcGIS API for Silverlight实现GP服务调用,这里的雨量数据是通过一个WebService获取而来,主要信息是雨量站点的经纬度坐标值和某个时间段内的降雨量值三个主要字段。
以下是核心代码部分:
[csharp] view plain copy <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/Expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:esri="http://schemas.esri.com/arcgis/clIEnt/2009" xmlns:controlsToolkit="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Toolkit" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="TestDZX.MainPage2" mc:Ignorable="d" d:DesignHeight="300" d:DesignWIDth="400"> <controlsToolkit:BusyIndicator x:name="busyIndicator" BusyContent="正在加载,请稍等..." IsBusy="True"> <GrID x:name="LayoutRoot" Background="White"> <esri:Map x:name="myMap" IslogoVisible="False" ZoomDuration="0:00:00" PanDuration="0:00:00" Extent="117.647738815324,29.4704217183843,118.446182957997,30.4124245048916"> <esri:ArcGISTiledMapServiceLayer ID="BaseLayer" Url="http://localhost/arcgis/rest/services/HSDQ/MapServer/"/> <!--等值线和等直面图层--> <esri:Graphicslayer ID="GraphicsDZX"/> </esri:Map> <GrID x:name="grIDMain" HorizontalAlignment="left" WIDth="215" margin="1,0"> <GrID.ColumnDeFinitions> <ColumnDeFinition WIDth="200" x:name="grIDSub2" /> <ColumnDeFinition WIDth="15" x:name="grIDSub1"/> </GrID.ColumnDeFinitions> <GrID GrID.Column="1" Background="#FFD2D4D1" WIDth="15" margin="-1,0" d:LayoutOverrIDes="Horizontalmargin"> <border borderBrush="#FFA5C6E5" borderThickness="2" WIDth="15"> <Image x:name="imgExpandOrCollapsed" margin="0,136,117" Source="Images/ss.png" Stretch="Fill" Height="45" MouseleftbuttonDown="imgExpandOrCollapsed_MouseleftbuttonDown" MouseleftbuttonUp="imgExpandOrCollapsed_MouseleftbuttonUp" Cursor="Hand" tooltipService.tooltip="收缩/展开"/> </border> </GrID> <GrID WIDth="200" HorizontalAlignment="left" Background="#FFD2D4D1"> <GrID.RowDeFinitions> <RowDeFinition Height="0.095*"/> <RowDeFinition Height="0.905*"/> </GrID.RowDeFinitions> <StackPanel margin="0" GrID.Row="1" VerticalAlignment="top"> <GrID Height="40" d:LayoutOverrIDes="Height, Verticalmargin" VerticalAlignment="top"> <sdk:DatePicker x:name="dpStart" margin="38,6,65,6"/> <ComboBox x:name="cbStart" HorizontalAlignment="Right" margin="0,16,6" WIDth="49"/> <TextBlock HorizontalAlignment="left" margin="3,0" textwrapPing="Wrap" Text="起始:" WIDth="40" FontSize="13.333" FontFamily="Microsoft YaHei" VerticalAlignment="Center"/> <TextBlock textwrapPing="Wrap" Text="时" FontSize="13.333" FontFamily="Microsoft YaHei" VerticalAlignment="Center" HorizontalAlignment="Right" WIDth="15" margin="0,1,0"/> </GrID> > <sdk:DatePicker x:name="dpEnd" margin="38,248); line-height:18px"> <ComboBox x:name="cbEnd" HorizontalAlignment="Right" margin="0,0" textwrapPing="Wrap" Text="截至:" WIDth="40" FontSize="13.333" FontFamily="Microsoft YaHei" VerticalAlignment="Center"/> <ComboBox x:name="cbRainGrade" margin="68,248); line-height:18px"> <TextBlock HorizontalAlignment="left" margin="4,0" textwrapPing="Wrap" Text="雨量级别:" WIDth="69" FontSize="13.333" FontFamily="Microsoft YaHei" VerticalAlignment="Center"/> <Radiobutton x:name="rb1" Content="稀" margin="70,11,67,1" FontSize="13.333" FontFamily="Microsoft YaHei" Groupname="rbGroup" IsChecked="True" Cursor="Hand" Click="rb_Click"/> textwrapPing="Wrap" Text="过滤级:" WIDth="55" FontSize="13.333" FontFamily="Microsoft YaHei" VerticalAlignment="Center"/> <Radiobutton x:name="rb2" Content="密" margin="0,12,8,0" FontSize="13.333" FontFamily="Microsoft YaHei" HorizontalAlignment="Right" WIDth="55" Groupname="rbGroup" Cursor="Hand" Click="rb_Click"/> </GrID> <GrID Height="40" d:LayoutOverrIDes="Height, Verticalmargin"> <button x:name="btnAnalysis" Content="等值线分析" margin="64,63,0" VerticalAlignment="Center" Cursor="Hand" FontSize="13.333" FontFamily="Microsoft YaHei" d:LayoutOverrIDes="WIDth" Click="btnAnalysis_Click"/> </StackPanel> <border borderBrush="#FF459BFB" borderThickness="2" Background="#FF449DFF" Height="30" VerticalAlignment="top"> <TextBlock HorizontalAlignment="Center" margin="0" textwrapPing="Wrap" Text="专 题 分 析" WIDth="82" FontSize="16" FontFamily="Microsoft YaHei" VerticalAlignment="Center" Foreground="White"/> </border> </GrID> </GrID> </GrID> </controlsToolkit:BusyIndicator> </UserControl> using System; using System.Collections.Generic; using System.linq; using System.Net; using System.windows; using System.windows.Controls; using System.windows.documents; using System.windows.input; using System.windows.Media; using System.windows.Media.Animation; using System.windows.Shapes;using ESRI.ArcGIS.ClIEnt; using ESRI.ArcGIS.ClIEnt.Geometry; using ESRI.ArcGIS.ClIEnt.Tasks; using System.Net.browser; using ESRI.ArcGIS.ClIEnt.Symbols; using TestDZX.ServiceReference1; using System.Collections.ObjectModel; using System.Collections; using System.windows.Media.Imaging; using System.Threading; using System.Globalization; using Syit.CommonClass;namespace TestDZX { public partial class MainPage2 : UserControl { /******************GP参数* 2012-08-29***********************/ private Geoprocessor _ContourTask; //等值线GP public struct EvaluationPointStruct { double Latitute; //纬度 double Longitute; //经度 double YL; //雨量 }; public EvaluationPointStruct[] evaluatePoints; private Featureset featureset;//作为GP输入参数的要素集 ESRI.ArcGIS.ClIEnt.Projection.WebMercator mercator = new ESRI.ArcGIS.ClIEnt.Projection.WebMercator(); public int Contour_interval = 5; //默认是5表示稀,1表示密 /*********************************************************/ public MainPage2() InitializeComponent(); //设置日期格式 Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone(); Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd"; //加载日期 this.dpStart.SelectedDate = (DateTime?)DateTime.Parse(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")); this.dpEnd.SelectedDate = (DateTime?)DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")); //加载小时及雨量级别 BindElement(); //初次加载前日八时到现在小时的数据 DrawContourUseGP(); } #region 页面上数据绑定 voID BindElement() { //起始小时绑定 this.cbStart.Items.Add("00"); this.cbStart.Items.Add("01"); this.cbStart.Items.Add("02"); this.cbStart.Items.Add("03"); this.cbStart.Items.Add("04"); this.cbStart.Items.Add("05"); this.cbStart.Items.Add("06"); this.cbStart.Items.Add("07"); this.cbStart.Items.Add("08"); this.cbStart.Items.Add("09"); this.cbStart.Items.Add("10"); this.cbStart.Items.Add("11"); this.cbStart.Items.Add("12"); this.cbStart.Items.Add("13"); this.cbStart.Items.Add("14"); this.cbStart.Items.Add("15"); this.cbStart.Items.Add("16"); this.cbStart.Items.Add("17"); this.cbStart.Items.Add("18"); this.cbStart.Items.Add("19"); this.cbStart.Items.Add("20"); this.cbStart.Items.Add("21"); this.cbStart.Items.Add("22"); this.cbStart.Items.Add("23"); this.cbStart.Selectedindex = 8; //结束时间绑定 this.cbEnd.Items.Add("00"); this.cbEnd.Items.Add("01"); this.cbEnd.Items.Add("02"); this.cbEnd.Items.Add("03"); this.cbEnd.Items.Add("04"); this.cbEnd.Items.Add("05"); this.cbEnd.Items.Add("06"); this.cbEnd.Items.Add("07"); this.cbEnd.Items.Add("08"); this.cbEnd.Items.Add("09"); this.cbEnd.Items.Add("10"); this.cbEnd.Items.Add("11"); this.cbEnd.Items.Add("12"); this.cbEnd.Items.Add("13"); this.cbEnd.Items.Add("14"); this.cbEnd.Items.Add("15"); this.cbEnd.Items.Add("16"); this.cbEnd.Items.Add("17"); this.cbEnd.Items.Add("18"); this.cbEnd.Items.Add("19"); this.cbEnd.Items.Add("20"); this.cbEnd.Items.Add("21"); this.cbEnd.Items.Add("22"); this.cbEnd.Items.Add("23"); DateTime dt = DateTime.Now; string hour = dt.Hour.ToString(); if (hour.Length == 1) { hour = "0" + hour; } switch (hour) case "00": this.cbEnd.Selectedindex = 0; break; case "01": this.cbEnd.Selectedindex = 1; break; case "02": this.cbEnd.Selectedindex = 2; case "03": this.cbEnd.Selectedindex = 3; case "04": this.cbEnd.Selectedindex = 4; case "05": this.cbEnd.Selectedindex = 5; case "06": this.cbEnd.Selectedindex = 6; case "07": this.cbEnd.Selectedindex = 7; case "08": this.cbEnd.Selectedindex = 8; case "09": this.cbEnd.Selectedindex = 9; case "10": this.cbEnd.Selectedindex = 10; case "11": this.cbEnd.Selectedindex = 11; case "12": this.cbEnd.Selectedindex = 12; case "13": this.cbEnd.Selectedindex = 13; case "14": this.cbEnd.Selectedindex = 14; case "15": this.cbEnd.Selectedindex = 15; case "16": this.cbEnd.Selectedindex = 16; case "17": this.cbEnd.Selectedindex = 17; case "18": this.cbEnd.Selectedindex = 18; case "19": this.cbEnd.Selectedindex = 19; case "20": this.cbEnd.Selectedindex = 20; case "21": this.cbEnd.Selectedindex = 21; case "22": this.cbEnd.Selectedindex = 22; case "23": this.cbEnd.Selectedindex = 23; } //绑定雨量级别 this.cbRainGrade.Items.Add("A:5,10,25,50,100,200"); this.cbRainGrade.Items.Add("B:10,200,300"); this.cbRainGrade.Items.Add("C:50,300,400,500,600"); this.cbRainGrade.Items.Add("D:100,600,700"); this.cbRainGrade.Items.Add("E:10,20,30,40,60"); this.cbRainGrade.Selectedindex = 0; } #endregion #region 调用GP服务绘制等值线 /// <summary> /// 调用GP服务绘制等值线 /// </summary> voID DrawContourUseGP() Thread.Sleep(1000); //加载之前,显示等待 busyIndicator.IsBusy = true; try { getXQYJInfoSoapClIEnt clIEnt = new getXQYJInfoSoapClIEnt(); //水位数据 clIEnt.getSWRainByTimeSpanCompleted += new EventHandler<getSWRainByTimeSpanCompletedEventArgs>(clIEnt_getSWRainByTimeSpanCompleted); clIEnt.getSWRainByTimeSpanAsync(DateTime.Parse(this.dpStart.SelectedDate.ToString().Split(' ')[0] + " " + this.cbStart.SelectedItem.ToString() + ":00:00"), DateTime.Parse(this.dpEnd.SelectedDate.ToString().Split(' ')[0] + " " + this.cbEnd.SelectedItem.ToString() + ":00:00")); catch (Exception) } voID clIEnt_getSWRainByTimeSpanCompleted(object sender, getSWRainByTimeSpanCompletedEventArgs e) //获取到所有的水文局雨量点 ObservableCollection<RainFall> Lists = e.Result; int PointsNum = Lists.Count;//点的个数 evaluatePoints = new EvaluationPointStruct[PointsNum]; int index = 0; foreach (RainFall item in Lists) { if (item.YL24 != 0) { evaluatePoints[index].Latitute = item.Latitute; evaluatePoints[index].Longitute = item.Longitute; evaluatePoints[index].YL = item.YL24; index++; } } Utility.AddPointToMapLayer(myMap, evaluatePoints, out featureset); AccessGPService(featureset); } private voID AccessGPService(Featureset featureset) try httpWebRequest.RegisterPrefix("http://", System.Net.browser.WebRequestCreator.ClIEnthttp); _ContourTask = new Geoprocessor("http://localhost/arcgis/rest/services/ContourServicetool/GPServer/Contour"); List<GPParameter> parameters = new List<GPParameter>(); parameters.Add(new GPFeatureRecordSetLayer("input_point_features", featureset)); parameters.Add(new GPDouble("Contour_interval", Contour_interval)); _ContourTask.UpdateDelay = 5000; // 10s的更新时间 _ContourTask.OutputSpatialReference = myMap.SpatialReference; //设置输出空间参考系 _ContourTask.JobCompleted += new EventHandler<JobInfoEventArgs>(geoprocessorTask_JobCompleted); _ContourTask.Failed += new EventHandler<TaskFailedEventArgs>(geoprocessorTask_Failed); _ContourTask.submitJobAsync(parameters); catch (Exception) } /********************************事件处理程序段***************************************/ voID geoprocessorTask_JobCompleted( Geoprocessor gp = sender as Geoprocessor; //注册前缀 httpWebRequest.RegisterPrefix("http://", System.Net.browser.WebRequestCreator.ClIEnthttp); gp.GetResultDataCompleted += new EventHandler<GPParameterEventArgs>(gp_GetResultDataCompleted); gp.GetResultDataAsync(e.JobInfo.JobID, "Contour_IDw_11_Clip_Smoothli_shp"); //加载完成,隐藏 busyIndicator.IsBusy = false; } voID gp_GetResultDataCompleted( Graphicslayer layer = myMap.Layers["GraphicsDZX"] as Graphicslayer; layer.Cleargraphics(); GPFeatureRecordSetLayer gplayer = e.Parameter as GPFeatureRecordSetLayer; foreach (Graphic graphic in gplayer.Featureset.Features) if (IsRainInGrade(graphic.Attributes["CONTOUR"].ToString())) graphic.Symbol = new ESRI.ArcGIS.ClIEnt.Symbols.Simplelinesymbol() Style = ESRI.ArcGIS.ClIEnt.Symbols.Simplelinesymbol.linestyle.solID, color = new SolIDcolorBrush(colors.Red), WIDth = 2 }; layer.Graphics.Add(graphic); //添加红色的等值线 /****************************************************************************************/ //标注数值 TextSymbol textSymbol = new TextSymbol() { FontFamily = new System.windows.Media.FontFamily("Microsoft YaHei"),248); line-height:18px"> Foreground = new System.windows.Media.solIDcolorBrush(color.FromArgb(255, 0, 0)), FontSize = 12, Text = graphic.Attributes["CONTOUR"].ToString() }; //寻找线段的中心点坐标位置 Graphic graphicText; //封闭曲线在曲线中间显示数值 Graphic graphicStart; //非封闭曲线的起始点 Graphic graphicEnd; //非封闭曲线的终止点 if (gplayer.Featureset.GeometryType == GeometryType.polyline) ESRI.ArcGIS.ClIEnt.Geometry.polyline pl = gplayer.Featureset.Features[index].Geometry as ESRI.ArcGIS.ClIEnt.Geometry.polyline; MapPoint mp = pl.Extent.GetCenter(); if (pl.Paths != null && pl.Paths.Count > 0) { ESRI.ArcGIS.ClIEnt.Geometry.PointCollection path = pl.Paths[0]; if (path != null && path.Count > 0) { int mID = path.Count / 2; mp = path[mID]; if (path.Count == 2) { // path里面只有两个点的情况 double x1 = (path[0] as MapPoint).X; double y1 = (path[0] as MapPoint).Y; double x2 = (path[1] double y2 = (path[1] mp.X = x2 + (x1 - x2) / 2; mp.Y = y2 + (y1 - y2) / 2; //封闭的曲线 graphicText = new Graphic() { Geometry = mercator.FromGeographic(mp),248); line-height:18px"> Symbol = textSymbol }; //封闭的曲线在中间位置 layer.Graphics.Add(graphicText); //添加数值 } else //起始 graphicStart = new Graphic() { Geometry = mercator.FromGeographic(new MapPoint(path[0].X, path[0].Y)),108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px"> Symbol = textSymbol }; //结束 graphicEnd = new MapPoint(path[path.Count - 1].X, path[path.Count - 1].Y)),108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px"> layer.Graphics.Add(graphicStart); //标注起始数值 layer.Graphics.Add(graphicEnd); //标注结束数值 } } } //索引自增字段 index++; /// <summary> /// 地理处理失败,返回失败原因 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> voID geoprocessorTask_Failed( MessageBox.Show(e.Error.ToString()); /// 等值线分析按钮 voID btnAnalysis_Click( DrawContourUseGP(); } #endregion #region 菜单处理选项及事件 voID imgExpandOrCollapsed_MouseleftbuttonDown( e.Handled = true; voID imgExpandOrCollapsed_MouseleftbuttonUp( (this.grIDSub2.WIDth == new GrIDLength(200, GrIDUnitType.Pixel)) //处于展开状态 this.imgExpandOrCollapsed.Tag = "收缩面板"; GrID.SetColumnSpan(grIDMain, 1); this.grIDSub2.WIDth = new GrIDLength(0, GrIDUnitType.Pixel); Uri uri = new Uri("Images/zk.png", UriKind.relative); BitmAPImage image = new BitmAPImage(uri); this.imgExpandOrCollapsed.source = image; else //处于收缩状态 this.imgExpandOrCollapsed.Tag = "展开面板"; new Uri("Images/ss.png",248); line-height:18px"> } /// 过滤级单选按钮的切换事件 /// <param name="sender"></param> /// <param name="e"></param> voID rb_Click( Radiobutton currRadiobutton; currRadiobutton = sender as Radiobutton; if (currRadiobutton != null && currRadiobutton.IsChecked.Value) if (currRadiobutton.name == "rb1") { //首先赋值Contour_interval Contour_interval = 5; //改变值后,重新加载等值线 DrawContourUseGP(); else if (currRadiobutton.name == "rb2") //首先赋值Contour_interval Contour_interval = 1; //改变值后,重新加载等值线 DrawContourUseGP(); } } #endregion #region 根据雨量等级进行数据筛选 /// 判断一个降雨值取整数后,是否在雨量级别的数值中 /// <param name="value"></param> /// <returns>在数值中的话,返回true,否则返回false</returns> bool IsRainInGrade(string value) bool flag = false; string[] List = this.cbRainGrade.SelectedItem.ToString().Trim().Split(':')[1].Split(','); foreach (string s in List) if (value.Trim() == s) flag = return flag; } #endregion } } 使用到的另一个cs类文件如下Utility.cs文件如下 using System; using System.Net; using System.windows; using System.windows.Controls; using System.windows.documents; using System.windows.Ink; using System.windows.Shapes; using ESRI.ArcGIS.ClIEnt.Tasks; using ESRI.ArcGIS.ClIEnt.Symbols;namespace TestDZX { class Utility { static voID AddPointToMapLayer(ESRI.ArcGIS.ClIEnt.Map map, MainPage2.EvaluationPointStruct[] evaluatePoints, out ESRI.ArcGIS.ClIEnt.Tasks.Featureset featureset) ESRI.ArcGIS.ClIEnt.Projection.WebMercator mercator = new ESRI.ArcGIS.ClIEnt.Projection.WebMercator(); #region 动态添加预测点图层 if (map.Layers["YLPointsLayer"] != null) map.Layers.Remove(map.Layers["YLPointsLayer"]); Graphicslayer graphicslayer = new Graphicslayer() ID = "YLPointsLayer",248); line-height:18px"> }; map.Layers.Add(graphicslayer); #endregion #region 将降雨量点添加到图层中 featureset = new Featureset(); if (evaluatePoints.Length != 0) foreach (MainPage2.EvaluationPointStruct evaluationpoint in evaluatePoints) Graphic g = Geometry = mercator.FromGeographic(new MapPoint(evaluationpoint.Latitute, evaluationpoint.Longitute)),108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px"> Symbol = new ESRI.ArcGIS.ClIEnt.Symbols.SimpleMarkerSymbol() { Style = ESRI.ArcGIS.ClIEnt.Symbols.SimpleMarkerSymbol.SimpleMarkerStyle.Circle,248); line-height:18px"> Size = 5,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px"> color = new SolIDcolorBrush(colors.Red) } g.Attributes.Add("YL", evaluationpoint.YL); featureset.Features.Add(g); graphicslayer.Graphics.Add(g); //动态添加文本 TextSymbol textSymbol = new TextSymbol() FontFamily = Foreground = FontSize = 12,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px"> Text = evaluationpoint.YL.ToString(),248); line-height:18px"> OffsetX = 12,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px"> OffsetY = -5 }; Graphic graphicText = Symbol = textSymbol }; graphicslayer.Graphics.Add(graphicText); #endregion } }
显示效果如下:
总结
以上是内存溢出为你收集整理的ArcGIS API for Silverlight 调用GP服务加载等值线图层全部内容,希望文章能够帮你解决ArcGIS API for Silverlight 调用GP服务加载等值线图层所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)