Population for an Area

Download Samples Repository

Description

This sample uses a geoprocessing task to estimate the number of people within a polygon you draw on the map. Click 'Summarize Population', then freehand a line on the map surrounding an area to analyze. When you release the mouse button, the polygon will auto-complete and you'll see how many people are estimated to live within the polygon boundaries.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.PopulationForArea"
             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:SimpleFillSymbol x:Key="AreaSymbol" Color="#660000FF" Style="Solid">
                <esri:SimpleFillSymbol.Outline>
                    <esri:SimpleLineSymbol Color="Blue" Style="DashDot" Width="2" />
                </esri:SimpleFillSymbol.Outline>
            </esri:SimpleFillSymbol>
            
            <esri:SimpleRenderer x:Key="AreaRenderer" Symbol="{StaticResource AreaSymbol}" />
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView" WrapAround="True">
			<esri:Map InitialViewpoint="-13879981, 3490335, -7778090, 6248898">
                <esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
				<esri:ArcGISDynamicMapServiceLayer Opacity="0.5"
                    ServiceUri="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="areaOverlay" Renderer="{StaticResource AreaRenderer}"/>
			</esri:MapView.GraphicsOverlays>
		</esri:MapView>
		
		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top"
				Margin="30" Padding="20"
				Width="450">
			<Border.Effect>
				<DropShadowEffect/>
			</Border.Effect>
			<StackPanel>
                <TextBlock Text="Click 'Summarize Population' and draw a line on the map surrounding an area to analyze. A geoprocessing task will be called to summarize the population within the polygon."
                           TextAlignment="Left" TextWrapping="Wrap" />

                <Button Content="Summarize Population" Margin="12,12,12,0" Padding="20,4"
                        HorizontalAlignment="Center" Click="SummarizePopulationButton_Click" />

                <ProgressBar x:Name="progress" IsIndeterminate="True" Margin="12,16,12,0" Visibility="Collapsed" />

                <TextBlock x:Name="txtResult" Margin="12,16,12,0" Visibility="Collapsed" 
                           FontSize="14" FontWeight="Bold" HorizontalAlignment="Center" />
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Tasks.Geoprocessing;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample uses a geoprocessing task to estimate the number of people within a polygon you draw on the map. Click 'Summarize Population', then freehand a line on the map surrounding an area to analyze. When you release the mouse button, the polygon will auto-complete and you'll see how many people are estimated to live within the polygon boundaries.
    /// </summary>
    /// <title>Population for an Area</title>
	/// <category>Tasks</category>
	/// <subcategory>Geoprocessing</subcategory>
	public partial class PopulationForArea : UserControl
    {
        private const string PopulationSummaryServiceUrl =
            "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/GPServer/PopulationSummary";

		private GraphicsOverlay _areaOverlay;

        /// <summary>Construct Population for Area sample control</summary>
        public PopulationForArea()
        {
            InitializeComponent();

			_areaOverlay = MyMapView.GraphicsOverlays["areaOverlay"];
        }

        // Accept user boundary line and run the Geoprocessing Task to summarize population
        private async void SummarizePopulationButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                txtResult.Visibility = System.Windows.Visibility.Collapsed;
				_areaOverlay.Graphics.Clear();

                var boundary = await MyMapView.Editor.RequestShapeAsync(DrawShape.Freehand) as Polyline;
                var polygon = new Polygon(boundary.Parts, MyMapView.SpatialReference);
                polygon = GeometryEngine.Simplify(polygon) as Polygon;
				_areaOverlay.Graphics.Add(new Graphic() { Geometry = polygon });

                progress.Visibility = Visibility.Visible;

                Geoprocessor geoprocessorTask = new Geoprocessor(new Uri(PopulationSummaryServiceUrl));

                var parameter = new GPInputParameter() { OutSpatialReference = MyMapView.SpatialReference };
                parameter.GPParameters.Add(new GPFeatureRecordSetLayer("inputPoly", polygon));

                var result = await geoprocessorTask.ExecuteAsync(parameter);

                GPRecordSet rs = result.OutParameters.OfType<GPRecordSet>().FirstOrDefault();
                if (rs != null && rs.FeatureSet.Features != null)
                {
                    int population = Convert.ToInt32(rs.FeatureSet.Features[0].Attributes["SUM"]);
                    txtResult.Visibility = System.Windows.Visibility.Visible;
                    txtResult.Text = string.Format("Area Population: {0:N0}", population);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Geoprocessor service failed: " + ex.Message, "Sample Error");
            }
            finally
            {
                progress.Visibility = Visibility.Collapsed;
            }
        }
    }
}
Feedback on this topic?