ArcGIS API for Silverlight调用GP生成等值线输入要素集FeatureSet

来源:互联网 发布:张怡宁 知乎 编辑:程序博客网 时间:2024/06/18 07:01
已经建立好GP模型,模拟可以生成等值线,现在在Silverlight客户端调用该GP工具时,提供模型输入参数要素集FeatureSet,但是输入参数中的Fields始终为null值,最终导致模型调用失败,无任何输出。


主要代码如下:
  private FeatureSet featureSet;//作为GP输入参数的要素集


        public MainPage2()
        {
            InitializeComponent();


            getXQYJInfoSoapClient client = new getXQYJInfoSoapClient();
            client.GetAllSHRainCompleted += new EventHandler<GetAllSHRainCompletedEventArgs>(client_GetAllSHRainCompleted);
            client.GetAllSHRainAsync();
        }


        void client_GetAllSHRainCompleted(object sender, GetAllSHRainCompletedEventArgs e)
        {
            //获取到所有的山洪雨量点
            ObservableCollection<RainFall> lists = e.Result;
            int PointsNum = lists.Count;//点的个数
            evaluatePoints = new EvaluationPointStruct[PointsNum];
            int index = 0;
            foreach (RainFall item in lists)
            {
                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)
        {
            HttpWebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp);
            _ContourTask = new Geoprocessor("GP服务地址");
            List<GPParameter> parameters = new List<GPParameter>();
            parameters.Add(new GPFeatureRecordSetLayer("RainPoint", featureset)); //这里的featureSet的Fields为null值,就是输入参数没有模型中的字段,请问是怎么回事呢?
            parameters.Add(new GPDouble("Contour_interval", 5));
            _ContourTask.UpdateDelay = 10000;
            _ContourTask.OutputSpatialReference = myMap.SpatialReference; //设置输出空间参考系
            _ContourTask.JobCompleted += new EventHandler<JobInfoEventArgs>(geoprocessorTask_JobCompleted);
            _ContourTask.Failed += new EventHandler<TaskFailedEventArgs>(geoprocessorTask_Failed);
            _ContourTask.SubmitJobAsync(parameters);
        }


        /********************************事件处理程序段***********************************/


        void geoprocessorTask_JobCompleted(object sender, JobInfoEventArgs e)
        {
            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_Kriging_Clip");
        }


        void gp_GetResultDataCompleted(object sender, GPParameterEventArgs e)
        {
            //找到显示等值线图层并清空,然后再次加载
            GraphicsLayer layer = myMap.Layers["GraphicsDZX"] as GraphicsLayer;
            layer.ClearGraphics();
            GPFeatureRecordSetLayer gplayer = e.Parameter as GPFeatureRecordSetLayer;
            MessageBox.Show("结果图层个数:" + gplayer.FeatureSet.Features.Count.ToString()); //这里的gplayer.FeatureSet为null值,错误就出现在这里
            foreach (Graphic graphic in gplayer.FeatureSet.Features)
            {
                graphic.Symbol = new ESRI.ArcGIS.Client.Symbols.SimpleLineSymbol()
                {
                    Style = ESRI.ArcGIS.Client.Symbols.SimpleLineSymbol.LineStyle.Solid,
                    Color = new SolidColorBrush(Colors.Blue),
                    Width = 3
                };
                layer.Graphics.Add(graphic);
            }
        }


        void geoprocessorTask_Failed(object sender, TaskFailedEventArgs e)
        {
            MessageBox.Show(e.Error.ToString());
        }


        //分类渲染
        public Symbol getSymbol(Graphic graphic)
        {
            //线符号
            SimpleLineSymbol symbol = new SimpleLineSymbol();
            double yl = double.Parse(graphic.Attributes["YL"].ToString());
            if (yl > 10)
            {
                symbol.Color = new SolidColorBrush(Colors.Red);
            }
            else
            {
                symbol.Color = new SolidColorBrush(Colors.Blue);
            }
            return symbol;
        }


调用的Utility.cs类代码如下:


    public class Utility
    {
        public 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",
            };


            map.Layers.Add(graphicslayer);


            #endregion


            #region 将网格划分点添加到图层中


            GraphicCollection graphicCollection = new GraphicCollection();
            foreach (MainPage2.EvaluationPointStruct evaluationpoint in evaluatePoints)
            {
                Graphic g = new Graphic()
                {
                    Geometry = mercator.FromGeographic(new MapPoint(evaluationpoint.Latitute, evaluationpoint.Longitute)),
                    Symbol = new ESRI.ArcGIS.Client.Symbols.SimpleMarkerSymbol()
                    {
                        Style = ESRI.ArcGIS.Client.Symbols.SimpleMarkerSymbol.SimpleMarkerStyle.Circle,
                        Size = 9,
                        Color = new SolidColorBrush(Colors.Red)
                    }
                };
                g.Attributes.Add("OBJECTID", "");
                g.Attributes.Add("SHAPE", "Point");
                g.Attributes.Add("YL", evaluationpoint.YL);
                graphicCollection.Add(g);
                graphicslayer.Graphics.Add(g);
            }
            featureset = new FeatureSet(graphicCollection);


            #endregion
        }
    }




转自地信网:http://bbs.3s001.com/thread-108518-1-1.html


原创粉丝点击