// On tap, get an input line and perform a buffer and clip operation private void mapView1_Tap(object sender, MapViewInputEventArgs e) { if (!m_firstPointAdded) { m_firstPointAdded = true; // Clear previous results ClipLines.Clear(); ClippedCounties.Clear(); if (m_clippedCountiesLayer != null) { Layers.Remove(m_clippedCountiesLayer); m_clippedCountiesLayer = null; } // Show instructions StatusText = "Tap to add a point to the line. Double-tap to finish."; StatusVisibility = Visibility.Visible; } else if (BusyVisibility == Visibility.Visible) { MessageBox.Show("Please wait until the current operation is complete."); return; } }
public async void Clip() { //get the user's input line var inputLine = await this.mapView.Editor.RequestShapeAsync(DrawShape.Polyline) as Polyline; // clear the graphics layers this.resultGraphicsLayer.Graphics.Clear(); this.inputGraphicsLayer.Graphics.Clear(); // add new graphic to layer this.inputGraphicsLayer.Graphics.Add(new Graphic { Geometry = inputLine, Symbol = this.simpleInputLineSymbol }); // add the parameters var parameter = new GPInputParameter(); parameter.GPParameters.Add(new GPFeatureRecordSetLayer("Input_Features", inputLine)); parameter.GPParameters.Add(new GPLinearUnit("Linear_unit", LinearUnits.Miles, this.Distance)); // poll the task var result = await SubmitAndPollStatusAsync(parameter); // add successful results to the map if (result.JobStatus == GPJobStatus.Succeeded) { this.Status = "Finished processing. Retrieving results..."; var resultData = await gpTask.GetResultDataAsync(result.JobID, "Clipped_Counties"); if (resultData is GPFeatureRecordSetLayer) { GPFeatureRecordSetLayer gpLayer = resultData as GPFeatureRecordSetLayer; if (gpLayer.FeatureSet.Features.Count == 0) { // the the map service results var resultImageLayer = await gpTask.GetResultImageLayerAsync(result.JobID, "Clipped_Counties"); // make the result image layer opaque GPResultImageLayer gpImageLayer = resultImageLayer; gpImageLayer.Opacity = 0.5; this.mapView.Map.Layers.Add(gpImageLayer); this.Status = "Greater than 500 features returned. Results drawn using map service."; return; } // get the result features and add them to the GraphicsLayer var features = gpLayer.FeatureSet.Features; foreach (Feature feature in features) { this.resultGraphicsLayer.Graphics.Add(feature as Graphic); } } this.Status = "Success!!!"; } }
private void GeoprocessorTask_JobCompleted(object sender, JobInfoEventArgs e) { Geoprocessor geoprocessorTask = sender as Geoprocessor; geoprocessorTask.GetResultDataCompleted += (s1, ev1) => { GraphicsLayer graphicsLayer = MyMap.Layers["MyResultGraphicsLayer"] as GraphicsLayer; if (ev1.Parameter is GPFeatureRecordSetLayer) { GPFeatureRecordSetLayer gpLayer = ev1.Parameter as GPFeatureRecordSetLayer; if (gpLayer.FeatureSet.Features.Count == 0) { geoprocessorTask.GetResultImageLayerCompleted += (s2, ev2) => { GPResultImageLayer gpImageLayer = ev2.GPResultImageLayer; gpImageLayer.Opacity = 0.5; MyMap.Layers.Add(gpImageLayer); ProcessingTextBlock.Text = "Greater than 500 features returned. Results drawn using map service."; _processingTimer.Stop(); }; geoprocessorTask.GetResultImageLayerAsync(e.JobInfo.JobId, "Clipped_Counties"); return; } foreach (Graphic graphic in gpLayer.FeatureSet.Features) { graphic.Symbol = LayoutRoot.Resources["ClipFeaturesFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol; graphicsLayer.Graphics.Add(graphic); } } ProcessingTextBlock.Visibility = Visibility.Collapsed; _processingTimer.Stop(); }; geoprocessorTask.GetResultDataAsync(e.JobInfo.JobId, "Clipped_Counties"); }
// Get the users input line on the map and fire off a GP Job to clip features private async void StartButton_Click(object sender, RoutedEventArgs e) { try { uiPanel.IsEnabled = false; inputLayer.Graphics.Clear(); resultLayer.Graphics.Clear(); foreach (var lyr in mapView.Map.Layers.OfType <GPResultImageLayer>()) { mapView.Map.Layers.Remove(lyr); } //get the user's input line var inputLine = await mapView.Editor.RequestShapeAsync(DrawShape.Polyline) as Polyline; progress.Visibility = Visibility.Visible; inputLayer.Graphics.Add(new Graphic() { Geometry = inputLine }); var parameter = new GPInputParameter(); parameter.GPParameters.Add(new GPFeatureRecordSetLayer("Input_Features", inputLine)); parameter.GPParameters.Add(new GPLinearUnit("Linear_unit", LinearUnits.Miles, Int32.Parse(txtMiles.Text))); var result = await SubmitAndPollStatusAsync(parameter); if (result.JobStatus == GPJobStatus.Succeeded) { txtStatus.Text = "Finished processing. Retrieving results..."; var resultData = await _gpTask.GetResultDataAsync(result.JobID, "Clipped_Counties"); if (resultData is GPFeatureRecordSetLayer) { GPFeatureRecordSetLayer gpLayer = resultData as GPFeatureRecordSetLayer; if (gpLayer.FeatureSet.Features.Count == 0) { var resultImageLayer = await _gpTask.GetResultImageLayerAsync(result.JobID, "Clipped_Counties"); GPResultImageLayer gpImageLayer = resultImageLayer; gpImageLayer.Opacity = 0.5; mapView.Map.Layers.Add(gpImageLayer); txtStatus.Text = "Greater than 500 features returned. Results drawn using map service."; return; } resultLayer.Graphics.AddRange(gpLayer.FeatureSet.Features); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Sample Error"); } finally { uiPanel.IsEnabled = true; progress.Visibility = Visibility.Collapsed; } }
// Waits for the user to draw a line, then performs the buffer and clip operation private async void getInputLineAndClip() { if (m_firstPointAdded) { return; } // Get line from user var clipLine = await Editor.RequestShapeAsync(DrawShape.Polyline, new SimpleLineSymbol() { Color = Colors.Red, Width = 2, Style = SimpleLineStyle.Dash }); ClipLines.Add(new Graphic() { Geometry = clipLine }); // Show busy UI BusyVisibility = Visibility.Visible; StatusText = "Executing..."; string error = null; // Initialize the Geoprocessing task with the buffer and clip service endpoint Geoprocessor task = new Geoprocessor(new Uri("http://serverapps10.esri.com/ArcGIS/rest/services/SamplesNET/" + "USA_Data_ClipTools/GPServer/ClipCounties")); // Initialize input parameters var parameter = new GPInputParameter() { OutSpatialReference = SpatialReferences.WebMercator }; parameter.GPParameters.Add(new GPFeatureRecordSetLayer("Input_Features", clipLine)); // input geometry parameter.GPParameters.Add(new GPLinearUnit("Linear_unit", LinearUnits.Miles, BufferDistance)); // buffer distance try { // Submit the job and await the results var result = await task.SubmitJobAsync(parameter); // Poll the server every two seconds for the status of the job. while (result.JobStatus != GPJobStatus.Cancelled && result.JobStatus != GPJobStatus.Deleted && result.JobStatus != GPJobStatus.Failed && result.JobStatus != GPJobStatus.Succeeded && result.JobStatus != GPJobStatus.TimedOut) { result = await task.CheckJobStatusAsync(result.JobID); // show the status var descriptions = result.Messages.Select(msg => msg.Description); var status = string.Join(Environment.NewLine, descriptions); if (!string.IsNullOrEmpty(status)) { StatusText = status; } await Task.Delay(2000); } if (result.JobStatus == GPJobStatus.Succeeded) { // Get the results var resultData = await task.GetResultDataAsync(result.JobID, "Clipped_Counties"); if (resultData is GPFeatureRecordSetLayer) { GPFeatureRecordSetLayer resultsLayer = resultData as GPFeatureRecordSetLayer; if (resultsLayer.FeatureSet != null && resultsLayer.FeatureSet.Features != null && resultsLayer.FeatureSet.Features.Count != 0) { // Results were returned as graphics. Add them to the ClippedCounties collection ClippedCounties = new ObservableCollection <Graphic>(resultsLayer.FeatureSet.Features); } else // Try to get results as a GPResultImageLayer { StatusText = "Clip operation complete. Retrieving results..."; m_clippedCountiesLayer = await task.GetResultImageLayerAsync(result.JobID, "Clipped_Counties"); // If successful, add the layer to the layers collection if (m_clippedCountiesLayer != null) { m_clippedCountiesLayer.Opacity = 0.5; // Insert the layer below the input layer Layers.Insert(Layers.IndexOf(m_clipLinesLayer), m_clippedCountiesLayer); // Wait until the result layer is initialized await m_clippedCountiesLayer.InitializeAsync(); } else { error = "No results found"; } } } else { error = "Clip operation failed"; } } else { error = "Clip operation failed"; } } catch (Exception ex) { error = "Clip operation failed: " + ex.Message; } // If operation did not succeed, notify user if (error != null) { MessageBox.Show(error); } // Hide busy UI BusyVisibility = StatusVisibility = Visibility.Collapsed; StatusText = ""; m_firstPointAdded = false; getInputLineAndClip(); }
private async void StartGP_Click(object sender, RoutedEventArgs e) { StartGP.IsEnabled = false; ClearGraphics(); ClearButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed; ProcessingTextBlock.Visibility = Visibility.Collapsed; var inputPolyline = await mapView1.Editor.RequestShapeAsync(DrawShape.Polyline); var inputGraphic = new Graphic { Geometry = inputPolyline }; GraphicsLayer inputLayer = mapView1.Map.Layers["InputLayer"] as GraphicsLayer; inputLayer.Graphics.Add(inputGraphic); MyProgressRing.Visibility = Visibility.Visible; MyProgressRing.IsActive = true; string message = null; Geoprocessor task = new Geoprocessor(new Uri(ServiceUri)); var inputParameter = new GPInputParameter(); inputParameter.GPParameters.Add(new GPFeatureRecordSetLayer("Input_Features", inputPolyline)); inputParameter.GPParameters.Add(new GPLinearUnit("Linear_unit", LinearUnits.Miles, Int32.Parse(DistanceTextBox.Text))); try { //Submit the job and await the results var gpJobInfo = await task.SubmitJobAsync(inputParameter); //Poll the server every 5 seconds for the status of the job. //Cancelled, Cancelling, Deleted, Deleting, Executing, Failed, New, Submitted, Succeeded, TimedOut, Waiting while (gpJobInfo.JobStatus != GPJobStatus.Cancelled && gpJobInfo.JobStatus != GPJobStatus.Deleted && gpJobInfo.JobStatus != GPJobStatus.Failed && gpJobInfo.JobStatus != GPJobStatus.Succeeded && gpJobInfo.JobStatus != GPJobStatus.TimedOut) { gpJobInfo = await task.CheckJobStatusAsync(gpJobInfo.JobID); await Task.Delay(5000); } //Now that the job is completed, check whether the service returned the results as Features or as a GPResultImageLayer. //This can happen if the number of features to return exceeds the limit set on the service if (gpJobInfo.JobStatus == GPJobStatus.Succeeded) { var resultData = await task.GetResultDataAsync(gpJobInfo.JobID, "Clipped_Counties"); if (resultData is GPFeatureRecordSetLayer) { GPFeatureRecordSetLayer gpLayer = resultData as GPFeatureRecordSetLayer; if (gpLayer.FeatureSet.Features.Count == 0) { var resultImageLayer = await task.GetResultImageLayerAsync(gpJobInfo.JobID, "Clipped_Counties"); GPResultImageLayer gpImageLayer = resultImageLayer; gpImageLayer.Opacity = 0.5; mapView1.Map.Layers.Add(gpImageLayer); ProcessingTextBlock.Visibility = Visibility.Visible; ProcessingTextBlock.Text = "Greater than 500 features returned. Results drawn using map service."; return; } GraphicsLayer resultLayer = mapView1.Map.Layers["MyResultGraphicsLayer"] as GraphicsLayer; foreach (Graphic g in gpLayer.FeatureSet.Features) { resultLayer.Graphics.Add(g); } } } MyProgressRing.Visibility = Visibility.Collapsed; MyProgressRing.IsActive = false; ClearButton.Visibility = Visibility.Visible; StartGP.IsEnabled = true; } catch (Exception ex) { message = ex.Message; } if (message != null) { await new MessageDialog(message, "GP Failed").ShowAsync(); } }
// Waits for the user to draw a line, then performs the buffer and clip operation private async void getInputLineAndClip() { if (m_firstPointAdded) return; // Get line from user var clipLine = await Editor.RequestShapeAsync(DrawShape.Polyline, new SimpleLineSymbol() { Color = Colors.Red, Width = 2, Style = SimpleLineStyle.Dash }); ClipLines.Add(new Graphic() { Geometry = clipLine }); // Show busy UI BusyVisibility = Visibility.Visible; StatusText = "Executing..."; string error = null; // Initialize the Geoprocessing task with the buffer and clip service endpoint Geoprocessor task = new Geoprocessor(new Uri("http://serverapps10.esri.com/ArcGIS/rest/services/SamplesNET/" + "USA_Data_ClipTools/GPServer/ClipCounties")); // Initialize input parameters var parameter = new GPInputParameter() { OutSpatialReference = SpatialReferences.WebMercator }; parameter.GPParameters.Add(new GPFeatureRecordSetLayer("Input_Features", clipLine)); // input geometry parameter.GPParameters.Add(new GPLinearUnit("Linear_unit", LinearUnits.Miles, BufferDistance)); // buffer distance try { // Submit the job and await the results var result = await task.SubmitJobAsync(parameter); // Poll the server every two seconds for the status of the job. while (result.JobStatus != GPJobStatus.Cancelled && result.JobStatus != GPJobStatus.Deleted && result.JobStatus != GPJobStatus.Failed && result.JobStatus != GPJobStatus.Succeeded && result.JobStatus != GPJobStatus.TimedOut) { result = await task.CheckJobStatusAsync(result.JobID); // show the status var descriptions = result.Messages.Select(msg => msg.Description); var status = string.Join(Environment.NewLine, descriptions); if (!string.IsNullOrEmpty(status)) StatusText = status; await Task.Delay(2000); } if (result.JobStatus == GPJobStatus.Succeeded) { // Get the results var resultData = await task.GetResultDataAsync(result.JobID, "Clipped_Counties"); if (resultData is GPFeatureRecordSetLayer) { GPFeatureRecordSetLayer resultsLayer = resultData as GPFeatureRecordSetLayer; if (resultsLayer.FeatureSet != null && resultsLayer.FeatureSet.Features != null && resultsLayer.FeatureSet.Features.Count != 0) { // Results were returned as graphics. Add them to the ClippedCounties collection ClippedCounties = new ObservableCollection<Graphic>(resultsLayer.FeatureSet.Features); } else // Try to get results as a GPResultImageLayer { StatusText = "Clip operation complete. Retrieving results..."; m_clippedCountiesLayer = await task.GetResultImageLayerAsync(result.JobID, "Clipped_Counties"); // If successful, add the layer to the layers collection if (m_clippedCountiesLayer != null) { m_clippedCountiesLayer.Opacity = 0.5; // Insert the layer below the input layer Layers.Insert(Layers.IndexOf(m_clipLinesLayer), m_clippedCountiesLayer); // Wait until the result layer is initialized await m_clippedCountiesLayer.InitializeAsync(); } else { error = "No results found"; } } } else { error = "Clip operation failed"; } } else { error = "Clip operation failed"; } } catch (Exception ex) { error = "Clip operation failed: " + ex.Message; } // If operation did not succeed, notify user if (error != null) MessageBox.Show(error); // Hide busy UI BusyVisibility = StatusVisibility = Visibility.Collapsed; StatusText = ""; m_firstPointAdded = false; getInputLineAndClip(); }
private void BuildUI() { if (ParamContainer == null || ParameterConfigs == null || ParameterConfigs.Count < 1) { return; } if ((Results == null || Results.Count < 1) && (Errors == null || Errors.Count < 1)) { return; } if (!inputLayerInfoAvailable()) { return; } ParamContainer.Children.Clear(); ParamContainer.ColumnDefinitions.Clear(); ParamContainer.RowDefinitions.Clear(); ParamContainer.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto }); ParamContainer.ColumnDefinitions.Add(new ColumnDefinition()); // { Width = new GridLength(0, GridUnitType.Star) }); HasSimpleResults = false; layerParamNameIDLookup = InputLayers != null ? new Dictionary <string, string>(InputLayers) : new Dictionary <string, string>(); int pendingGPResultImageLayers = 0; #region Results if (Results != null && Results.Count > 0) { #region GP mapserver result MapServiceLayerParameterConfig mapServiceLayerParameterConfig = ParameterConfigs.FirstOrDefault(p => p.Type == GPParameterType.MapServiceLayer) as MapServiceLayerParameterConfig; if (mapServiceLayerParameterConfig != null && mapServiceLayerParameterConfig.SupportsJobResource) { string t = "/rest/services/"; string url = string.Format("{0}/{1}/MapServer/jobs/{2}", TaskUrl.Substring(0, TaskUrl.IndexOf(t, StringComparison.OrdinalIgnoreCase) + t.Length - 1), mapServiceLayerParameterConfig.Name, JobID); ArcGISDynamicMapServiceLayer layer = new ArcGISDynamicMapServiceLayer { Url = url, ProxyURL = UseProxy ? ProxyUrl : null }; layer.SetValue(ESRI.ArcGIS.Mapping.Core.LayerExtensions.ExcludeSerializationProperty, true); addToMap(mapServiceLayerParameterConfig, layer); } #endregion #region display each result foreach (ParameterConfig config in ParameterConfigs) { MapServiceLayerParameterConfig cfg; if (config.Type == GPParameterType.MapServiceLayer && (cfg = config as MapServiceLayerParameterConfig) != null && !cfg.SupportsJobResource) { pendingGPResultImageLayers++; Geoprocessor gp = new Geoprocessor(TaskUrl); gp.OutputSpatialReference = Map.SpatialReference.Clone(); gp.GetResultImageLayerCompleted += (s, e) => { GPResultImageLayer gpImageLayer = e.GPResultImageLayer; setLayerProps(cfg, gpImageLayer); gpImageLayer.ProxyUrl = UseProxy ? ProxyUrl : null; Map.Layers.Add(gpImageLayer); layerParamNameIDLookup.Add(cfg.Name, gpImageLayer.ID); pendingGPResultImageLayers--; if (layerParamNameIDLookup.Count > 1 && pendingGPResultImageLayers == 0) { LayerOrderer.OrderLayers(Map, LayerOrder, layerParamNameIDLookup); } }; // Initialize proxy gp.ProxyURL = UseProxy ? ProxyUrl : null; gp.GetResultImageLayerAsync(JobID, cfg.Name); continue; } GPParameter param = getParameter(config.Name); if (param == null) { continue; } string value = ParameterBase.ParameterToDisplayString(config.Type, param); switch (config.Type) { case GPParameterType.Boolean: case GPParameterType.Double: case GPParameterType.Long: case GPParameterType.String: case GPParameterType.Date: case GPParameterType.LinearUnit: if (value == null) { value = string.Empty; } addparamUI(config.Label, value, false); break; case GPParameterType.FeatureLayer: addToMap(param as GPFeatureRecordSetLayer, config); break; case GPParameterType.RecordSet: if (string.IsNullOrEmpty(value) && param is GPRecordSet) { GPRecordSet rs = param as GPRecordSet; if (string.IsNullOrEmpty(rs.Url) && rs.FeatureSet != null) { value = ESRI.ArcGIS.Mapping.GP.Resources.Strings.OnlyUrlOutputIsSupportedForRecordsets; } else { value = string.Empty; } addparamUI(config.Label, value, false); } else { addparamUI(config.Label, value, true); } break; case GPParameterType.DataFile: case GPParameterType.RasterData: case GPParameterType.RasterDataLayer: if (value == null) { value = string.Empty; } addparamUI(config.Label, value, true); break; } } #endregion if (layerParamNameIDLookup.Count > 1 && pendingGPResultImageLayers == 0) { LayerOrderer.OrderLayers(Map, LayerOrder, layerParamNameIDLookup); } } #endregion #region Errors if (Errors != null && Errors.Count > 0) { foreach (Exception error in Errors) { addparamUI(ESRI.ArcGIS.Mapping.GP.Resources.Strings.LabelError + " ", error.Message, false); HasSimpleResults = true; } } #endregion }