Last active
April 26, 2019 22:02
-
-
Save nommuna2/049131e54e135168de71236754bbd6e9 to your computer and use it in GitHub Desktop.
(ArcGIS Runtime SDK for .NET) Practice of the .NET API. Buffering and using geometry engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Esri.ArcGISRuntime.Data; | |
using Esri.ArcGISRuntime.Geometry; | |
using Esri.ArcGISRuntime.Mapping; | |
using Esri.ArcGISRuntime.Symbology; | |
using Esri.ArcGISRuntime.UI; | |
using Esri.ArcGISRuntime.UI.Controls; | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Documents; | |
using System.Windows.Input; | |
using System.Windows.Media; | |
using System.Windows.Media.Imaging; | |
using System.Windows.Navigation; | |
using System.Windows.Shapes; | |
namespace WpfApplication4 | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
private static FeatureLayer m_layer { get; set; } | |
private static FeatureLayer c_layer { get; set; } | |
private static string item; | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
MyMapView.Loaded += async (sender, e)=>{ | |
var map = new Map(Basemap.CreateLightGrayCanvas()); | |
MyMapView.Map = map; | |
MyMapView.Map.OperationalLayers.Add(new FeatureLayer(new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/2"))); | |
m_layer = new FeatureLayer(new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/3")); | |
c_layer = new FeatureLayer(new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/3")); | |
await m_layer.LoadAsync(); | |
m_layer.IsVisible = false; | |
c_layer.IsVisible = false; | |
MyMapView.Map.OperationalLayers.Add(m_layer); | |
MyMapView.Map.OperationalLayers.Add(c_layer); | |
var stateQuery = new ServiceFeatureTable(new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/2")); | |
await stateQuery.LoadAsync(); | |
var results = await stateQuery.QueryFeaturesAsync(new QueryParameters(){WhereClause = "1=1"}); | |
//var list = new List<String>(); | |
//foreach (var item in results) | |
//{ | |
// Debug.WriteLine(item.Attributes["state_name"]); | |
// list.Add(item.Attributes["state_name"].ToString()); | |
//} | |
//cbxState.ItemsSource = list; | |
var items = results.Select(f => f.Attributes["state_name"]).ToList(); | |
items.Sort(); | |
cbxState.ItemsSource = items; | |
cbxState.SelectedIndex = 0; | |
}; | |
cbxState.SelectionChanged += async (sender, e) => | |
{ | |
try | |
{ | |
item = (sender as ComboBox).SelectedItem.ToString(); | |
m_layer.DefinitionExpression = string.Format("state_name = '{0}'", item); | |
var layer = MyMapView.Map.OperationalLayers[0] as FeatureLayer; | |
var state = await layer.FeatureTable.QueryFeaturesAsync(new QueryParameters { WhereClause = string.Format("state_name = '{0}'", item) }); | |
var extent = state.FirstOrDefault().Geometry.Extent; | |
if (extent != null) | |
await MyMapView.SetViewpointAsync(new Viewpoint(extent)); | |
var counties = await m_layer.FeatureTable.QueryFeaturesAsync(new QueryParameters() { WhereClause = "1=1" }); | |
var itemsCounties = counties.Select(f => f.Attributes["name"]).ToList(); | |
itemsCounties.Sort(); | |
cbxCounty.ItemsSource = itemsCounties; | |
//cbxState.SelectedIndex = 0; | |
if (!m_layer.IsVisible) | |
m_layer.IsVisible = true; | |
} | |
catch (Exception ex) | |
{ | |
Debug.WriteLine(ex.Message); | |
//throw; | |
} | |
cbxCounty.SelectionChanged += (sender2, e2) => | |
{ | |
var item2 = (sender2 as ComboBox).SelectedItem.ToString(); //Grab the selected item from the ComboBox | |
c_layer.DefinitionExpression = string.Format("name = '{0}'",item2); | |
var sfs = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Colors.DodgerBlue, new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Colors.Red, 2)); | |
var renderer = new SimpleRenderer(sfs); | |
c_layer.Renderer = renderer; | |
if (!c_layer.IsVisible) | |
c_layer.IsVisible = true; | |
//Call the buffer method on button click | |
btnBuffer.Click += (sender3, e3) => | |
{ | |
Buffer(item2); | |
}; | |
}; | |
}; | |
} | |
async public static void Buffer (string itemGeo) | |
{ | |
try | |
{ | |
//Create the Query table | |
var countyQuery = new ServiceFeatureTable(new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/3")); | |
//Store the result of the Query in geometryResult | |
var geometryResult = await countyQuery.QueryFeaturesAsync(new QueryParameters() { WhereClause = string.Format("name = '{0}'", itemGeo), ReturnGeometry = true }); | |
//Call GeometryEngine to buffer the geometry result by 2 meters | |
var buffer = GeometryEngine.Buffer(geometryResult.FirstOrDefault().Geometry, 1); | |
//Use the Difference tool to get the geometry of the (buffer - geometryResult) | |
var cutBuffer = GeometryEngine.Difference(buffer, geometryResult.FirstOrDefault().Geometry); | |
//Create a Simple Fill Symbol | |
var symbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Colors.Red, new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Colors.Black, 2)); | |
//Create a new graphic object with the cutBuffer and the symbol | |
var graphic = new Graphic(cutBuffer, symbol); | |
//Create a new Graphic Overlay | |
GraphicsOverlay overlay = new GraphicsOverlay(); | |
//Add the graphic to the overlay | |
overlay.Graphics.Add(graphic); | |
//Grab the window from the Application Namespace and cast it as MainWindow | |
var window = (Application.Current.MainWindow) as MainWindow; | |
//Reference the GraphicsOverlays to add the Overlay to the MapView | |
window.MyMapView.GraphicsOverlays.Add(overlay); | |
//Set the ViewPoint of the buffer extent | |
if (buffer != null) | |
await window.MyMapView.SetViewpointAsync(new Viewpoint(buffer.Extent)); | |
//Referene the window instance to hook into the button click event | |
window.btnClear.Click += (sender, e ) => | |
{ | |
//overlay.IsVisible = false; | |
}; | |
} | |
catch (Exception ex) | |
{ | |
Debug.WriteLine(ex.Message); | |
//throw; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="WpfApplication4.MainWindow" | |
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:local="clr-namespace:WpfApplication4" | |
xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013" | |
mc:Ignorable="d" | |
Title="MainWindow" Height="500" Width="700"> | |
<Grid> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="*"/> | |
<RowDefinition Height="125"/> | |
<RowDefinition Height="40"/> | |
</Grid.RowDefinitions> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition /> | |
<ColumnDefinition /> | |
</Grid.ColumnDefinitions> | |
<Grid Grid.Row="1" Grid.ColumnSpan="2"> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition /> | |
<ColumnDefinition /> | |
<ColumnDefinition /> | |
<ColumnDefinition /> | |
</Grid.ColumnDefinitions> | |
<ComboBox Grid.Column="1" x:Name="cbxState" Margin="10" /> | |
<ComboBox Grid.Column="3" x:Name="cbxCounty" Margin="10" /> | |
<Label Grid.Column="0" Content="State" HorizontalAlignment="Center" VerticalAlignment="Center"></Label> | |
<Label Grid.Column="2" Content="County" HorizontalAlignment="Center" VerticalAlignment="Center"></Label> | |
</Grid> | |
<esri:MapView Grid.ColumnSpan="4" x:Name="MyMapView"></esri:MapView> | |
<Button x:Name="btnBuffer" Grid.Row="2" Grid.ColumnSpan="1" Content="Buffer"></Button> | |
<Button x:Name="btnClear" Grid.Row="2" Grid.Column="2" Content="Clear Cut"></Button> | |
</Grid> | |
</Window> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment