Generalize

Download Samples Repository

Description

This sample demonstrates using the GeometryEngine.Generalize method to take a polyline with numerous vertices and return a generalized polyline with less vertices. Additionally a SliderControl appears after the GeometryService completes that allows adjusting the maximum offset value of the generalized polyline.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.Generalize"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">
    <Grid x:Name="layoutGrid">
        <Grid.Resources>
            <esri:SimpleLineSymbol x:Key="DefaultLineSymbol" Color="#AAFF0000" Width="2" />
            <esri:SimpleMarkerSymbol x:Key="DefaultMarkerSymbol" Color="Red" Size="6" />
            <esri:SimpleMarkerSymbol x:Key="GeneralizedMarkerSymbol" Color="#FF00FFFF" Size="6" />
            <esri:SimpleLineSymbol x:Key="GeneralizedLineSymbol" Color="#AA00FFFF" Width="2"/>
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView" WrapAround="True">
			<esri:Map InitialViewpoint="-12000000,3000000,-7000000,7000000,3857">
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="originalOverlay" />
				<esri:GraphicsOverlay ID="generalizedLineOverlay" />
			</esri:MapView.GraphicsOverlays>
		</esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top"
				Margin="30" Padding="20">
			<Border.Effect>
				<DropShadowEffect/>
			</Border.Effect>
			<StackPanel>
                <TextBlock Text="Click the 'Generalize' button to generalize the line feature. Original line and vertices are red. Generalized line and vertices are cyan. Use the slider to change the maximum offset distance of the generalized line."
                           FontSize="14" Visibility="Visible" Width="250" TextAlignment="Left" TextWrapping="Wrap" />
                <TextBlock x:Name="ResultsTextBlock" 
                           Visibility="Collapsed" Width="250" TextAlignment="Left" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                
                <StackPanel Margin="0,30,0,0"  HorizontalAlignment="Center">
                    <TextBlock Text="Maximum Offset Distance " HorizontalAlignment="Center"/>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,10">
                        <TextBox Width="50" TextAlignment="Right" 
                                 Text="{Binding Value, ElementName=DistanceSlider, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                        <TextBlock Text="km" Margin="10,0" VerticalAlignment="Center" FontSize="14" />
                    </StackPanel>
                    <Slider x:Name="DistanceSlider" Minimum="0" Maximum="200" Value="10" Width="200" 
                            IsSnapToTickEnabled="True" TickPlacement="BottomRight" TickFrequency="10" />
                </StackPanel>
                
                <Button x:Name="GeneralizeButton" Content="Generalize" Click="GeneralizeButton_Click" 
                        IsDefault="True" Margin="0,15,0,0"/>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks.Query;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample demonstrates using the GeometryEngine.Generalize method to take a polyline with numerous vertices and return a generalized polyline with less vertices. Additionally a SliderControl appears after the GeometryService completes that allows adjusting the maximum offset value of the generalized polyline.
    /// </summary>
    /// <title>Generalize</title>
	/// <category>Geometry</category>
	public partial class Generalize : UserControl
    {
		private GraphicsOverlay _originalGraphicsOverlay;
		private GraphicsOverlay _generalizedGraphicsOverlay;
        private SimpleMarkerSymbol _defaultMarkerSymbol;
        private SimpleLineSymbol _defaultLineSymbol;
        private SimpleLineSymbol _generalizedLineSymbol;
        private SimpleMarkerSymbol _generalizedMarkerSymbol;

        /// <summary>Construct Generalize sample control</summary>
        public Generalize()
        {
            InitializeComponent();

			MyMapView.NavigationCompleted += MyMapView_NavigationCompleted;

			_originalGraphicsOverlay = MyMapView.GraphicsOverlays["originalOverlay"];
			_generalizedGraphicsOverlay = MyMapView.GraphicsOverlays["generalizedLineOverlay"];

            _defaultMarkerSymbol = layoutGrid.Resources["DefaultMarkerSymbol"] as SimpleMarkerSymbol;
            _defaultLineSymbol = layoutGrid.Resources["DefaultLineSymbol"] as SimpleLineSymbol;
            _generalizedLineSymbol = layoutGrid.Resources["GeneralizedLineSymbol"] as SimpleLineSymbol;
            _generalizedMarkerSymbol = layoutGrid.Resources["GeneralizedMarkerSymbol"] as SimpleMarkerSymbol;
        }

        // Adds the original river graphic to the map (from an online service)
		async void MyMapView_NavigationCompleted(object sender, EventArgs e)
        {
			MyMapView.NavigationCompleted -= MyMapView_NavigationCompleted; //only listed for it the first time
			try
            {
				if (_originalGraphicsOverlay != null && _originalGraphicsOverlay.Graphics.Count == 0)
                {
                    QueryTask queryTask = new QueryTask(
                        new Uri("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/1"));

                    Query query = new Query("NAME = 'Mississippi'");
                    query.ReturnGeometry = true;
                    query.OutSpatialReference = MyMapView.SpatialReference;

                    var results = await queryTask.ExecuteAsync(query);

                    var river = results.FeatureSet.Features
                        .Select(f => f.Geometry)
                        .OfType<Polyline>()
                        .FirstOrDefault();

					_originalGraphicsOverlay.Graphics.Add(new Graphic(river, _defaultLineSymbol));

                    foreach (var part in river.Parts)
                    {
						foreach (var point in part.GetPoints())
                        {
							var vertex = new Graphic(point, _defaultMarkerSymbol);
							_originalGraphicsOverlay.Graphics.Add(vertex);
                        }
                    }

                    GeneralizeButton.IsEnabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading test line: " + ex.Message, "Generalize Sample");
            }
        }

        // Generalizes the original line graphic
        private void GeneralizeButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
				_generalizedGraphicsOverlay.Graphics.Clear();

                var offset = DistanceSlider.Value * 1000;
				var generalizedPolyline = GeometryEngine.Generalize(
					_originalGraphicsOverlay.Graphics[0].Geometry, offset, false) as Polyline;

                if (generalizedPolyline != null)
                {
                    var graphic = new Graphic(generalizedPolyline, _generalizedLineSymbol);
					_generalizedGraphicsOverlay.Graphics.Add(graphic);

                    foreach (var part in generalizedPolyline.Parts)
                    {
						foreach (var point in part.GetPoints())
                        {
							var vertex = new Graphic(point, _generalizedMarkerSymbol);
							_generalizedGraphicsOverlay.Graphics.Add(vertex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error generalizing line: " + ex.Message, "Generalize Sample");
            }
        }
    }
}
Feedback on this topic?