Densify

Download Samples Repository

Description

This sample demonstrates using GeometryEngine.Densify to take an input polygon and return a densified polygon. Original vertices to create the original polygon are shown in red. The returned polygon shows the additional densified vertices in green. To use the sample, click the 'Densify Polygon' button and create a polygon on the map. Double-click to end the polygon sketch and densify the polygon and see the original and densified vertices.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.Densify"
             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>
            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>

            <esri:SimpleMarkerSymbol x:Key="OrigVertexSymbol" Color="Red" Size="12" />
            <esri:SimpleMarkerSymbol x:Key="NewVertexSymbol" Color="LightGreen" Size="8" />
            <esri:SimpleFillSymbol x:Key="PolySymbol" Color="#88000000" Style="Solid">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="Black" Width="1" />
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView" WrapAround="True">
			<esri:Map InitialViewpoint="-15000000,2000000,-7000000,8000000">
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="inputOverlay" />
				<esri:GraphicsOverlay ID="resultsOverlay" />
			</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 'Densify Polygon' button to digitize and densify a polygon. Red points indicate digitized polygon vertices. Green points show all vertices (original and new) in the densified polygon."
                           Width="400" TextAlignment="Left" TextWrapping="Wrap" />
                <Button Content="Densify Polygon" Margin="8,16,8,0" HorizontalAlignment="Center"
                        Click="DensifyButton_Click" />
            </StackPanel>
        </Border>

        <Border Background="White" BorderBrush="Black" BorderThickness="2" Margin="25"
                HorizontalAlignment="Center" VerticalAlignment="Bottom"
                Visibility="{Binding ElementName=MyMapView, Path=Editor.IsActive, Converter={StaticResource BooleanToVisibilityConverter}}">
            <TextBlock Text="Draw a polygon on the map." Margin="8" FontSize="14" />
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// This sample demonstrates using GeometryEngine.Densify to take an input polygon and return a densified polygon. Original vertices to create the original polygon are shown in red. The returned polygon shows the additional densified vertices in green. To use the sample, click the 'Densify Polygon' button and create a polygon on the map. Double-click to end the polygon sketch and densify the polygon and see the original and densified vertices.
	/// </summary>
	/// <title>Densify</title>
	/// <category>Geometry</category>
	public partial class Densify : UserControl
	{
		private Symbol _polySymbol;
		private Symbol _origVertexSymbol;
		private Symbol _newVertexSymbol;

		private GraphicsOverlay _inputOverlay;
		private GraphicsOverlay _resultsOverlay;

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

			_polySymbol = layoutGrid.Resources["PolySymbol"] as Symbol;
			_origVertexSymbol = layoutGrid.Resources["OrigVertexSymbol"] as Symbol;
			_newVertexSymbol = layoutGrid.Resources["NewVertexSymbol"] as Symbol;

			_inputOverlay = MyMapView.GraphicsOverlays["inputOverlay"];
			_resultsOverlay = MyMapView.GraphicsOverlays["resultsOverlay"];
		}

		// Draw and densify a user defined polygon
		private async void DensifyButton_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				_inputOverlay.Graphics.Clear();
				_resultsOverlay.Graphics.Clear();

				// Request polygon from the user
				var poly = await MyMapView.Editor.RequestShapeAsync(DrawShape.Polygon, _polySymbol) as Polygon;

				// Add original polygon and vertices to input graphics layer
				_inputOverlay.Graphics.Add(new Graphic(poly, _polySymbol));
				foreach (var mapPoint in poly.Parts.First().GetPoints())
				{
					_inputOverlay.Graphics.Add(new Graphic(mapPoint, _origVertexSymbol));
				}

                // Get current viewpoints extent from the MapView
                var currentViewpoint = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry);
                var viewpointExtent = currentViewpoint.TargetGeometry.Extent;

				// Densify the polygon
				var densify = GeometryEngine.Densify(poly, viewpointExtent.Width / 100) as Polygon;

				// Add new vertices to result graphics layer
				foreach (var mapPoint in densify.Parts.First().GetPoints())
				{
					_resultsOverlay.Graphics.Add(new Graphic(mapPoint, _newVertexSymbol));
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("Densify Error: " + ex.Message, "Densify Geometry Sample");
			}
		}
	}
}
Feedback on this topic?