Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Relationship

Download Samples Repository

Description

Sample shows how to test the spatial relationship of two geometries.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.Relationship"
             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:SimpleLineSymbol x:Key="LineSymbol" Color="Blue" Style="Solid" Width="2" />
            <esri:SimpleMarkerSymbol x:Key="PointSymbol" Color="Red" Style="Circle" Size="12" Outline="{StaticResource LineSymbol}" />
            <esri:SimpleFillSymbol x:Key="FillSymbol" Color="#7F0000FF" Style="DiagonalCross" Outline="{StaticResource LineSymbol}" />
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView" WrapAround="True">
            <esri:Map>
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="graphicsOverlay" />
			</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 'Start Drawing' button to add two shapes to the map and see their spatial relationships." Margin="0,0,0,12"
                           FontSize="14" Width="250" TextAlignment="Left" TextWrapping="Wrap" />

                <StackPanel Orientation="Horizontal" Margin="12" HorizontalAlignment="Center">
                    <TextBlock Text="Shape 1:" Margin="4,0" VerticalAlignment="Center" />
                    <ComboBox x:Name="comboShapeOne" SelectedIndex="2" Width="80">
                        <esri:DrawShape>Point</esri:DrawShape>
                        <esri:DrawShape>Polyline</esri:DrawShape>
                        <esri:DrawShape>Polygon</esri:DrawShape>
                    </ComboBox>
                </StackPanel>

                <StackPanel Orientation="Horizontal" Margin="12,0" HorizontalAlignment="Center">
                    <TextBlock Text="Shape 2:" Margin="4,0" VerticalAlignment="Center" />
                    <ComboBox x:Name="comboShapeTwo" SelectedIndex="2" Width="80">
                        <esri:DrawShape>Point</esri:DrawShape>
                        <esri:DrawShape>Polyline</esri:DrawShape>
                        <esri:DrawShape>Polygon</esri:DrawShape>
                    </ComboBox>
                </StackPanel>

                <Button x:Name="btnDraw"  Content="Start Drawing" Margin="12,18,12,0" HorizontalAlignment="Center"
                        IsEnabled="False" Click="StartDrawingButton_Click"/>

                <Border x:Name="resultsPanel" Margin="12,24,12,0" HorizontalAlignment="Center" Visibility="Collapsed"
                        BorderBrush="Gray" BorderThickness="2">
                    <StackPanel Margin="12,6,12,12">
                        <TextBlock Text="Spatial Relationships" Margin="0,0,0,8" />
                        <ListView x:Name="resultsListView">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition />
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Grid.Column="0" Text="{Binding Key}" />
                                        <TextBlock Grid.Column="1" Text="{Binding Value}" FontWeight="Bold" />
                                    </Grid>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </StackPanel>
                </Border>
            </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="Digitize shapes 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.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// Sample shows how to test the spatial relationship of two geometries.
    /// </summary>
    /// <title>Relationship</title>
	/// <category>Geometry</category>
	public partial class Relationship : UserControl
    {
        private List<Symbol> _symbols;
		private GraphicsOverlay _graphicsOverlay;

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

            _symbols = new List<Symbol>();
            _symbols.Add(layoutGrid.Resources["PointSymbol"] as Symbol);
            _symbols.Add(layoutGrid.Resources["LineSymbol"] as Symbol);
            _symbols.Add(layoutGrid.Resources["FillSymbol"] as Symbol);

			_graphicsOverlay = MyMapView.GraphicsOverlays["graphicsOverlay"];

            MyMapView.ExtentChanged += MyMapView_ExtentChanged;
        }

        // Start map interaction
        private void MyMapView_ExtentChanged(object sender, EventArgs e)
        {
            try
            {
                MyMapView.ExtentChanged -= MyMapView_ExtentChanged;
                btnDraw.IsEnabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, "Relationship Sample");
            }
        }

        // Accepts two user shapes and adds them to the graphics layer
        private async Task AcceptShapeAsync()
        {
            // Shape One
            DrawShape drawShape1 = (DrawShape)comboShapeOne.SelectedItem;
            Geometry shapeOne = null;
            if (drawShape1 == DrawShape.Point)
                shapeOne = await MyMapView.Editor.RequestPointAsync();
            else
                shapeOne = await MyMapView.Editor.RequestShapeAsync(drawShape1, _symbols[comboShapeOne.SelectedIndex]);

			_graphicsOverlay.Graphics.Add(new Graphic(shapeOne, _symbols[comboShapeOne.SelectedIndex]));

            // Shape Two
            Geometry shapeTwo = await MyMapView.Editor.RequestShapeAsync(
                (DrawShape)comboShapeTwo.SelectedItem, _symbols[comboShapeTwo.SelectedIndex]);

			_graphicsOverlay.Graphics.Add(new Graphic(shapeTwo, _symbols[comboShapeTwo.SelectedIndex]));

            Dictionary<string, bool> relations = new Dictionary<string, bool>();
            relations["Contains"] = GeometryEngine.Contains(shapeOne, shapeTwo);
            relations["Crosses"] = GeometryEngine.Crosses(shapeOne, shapeTwo);
            relations["Disjoint"] = GeometryEngine.Disjoint(shapeOne, shapeTwo);
            relations["Equals"] = GeometryEngine.Equals(shapeOne, shapeTwo);
            relations["Intersects"] = GeometryEngine.Intersects(shapeOne, shapeTwo);
            relations["Overlaps"] = GeometryEngine.Overlaps(shapeOne, shapeTwo);
            relations["Touches"] = GeometryEngine.Touches(shapeOne, shapeTwo);
            relations["Within"] = GeometryEngine.Within(shapeOne, shapeTwo);

            resultsPanel.Visibility = Visibility.Visible;
            resultsListView.ItemsSource = relations;
        }

        private async void StartDrawingButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnDraw.IsEnabled = false;
                resultsPanel.Visibility = Visibility.Collapsed;

				_graphicsOverlay.Graphics.Clear();
                await AcceptShapeAsync();
            }
            catch (TaskCanceledException) { }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, "Relationship Sample");
            }
            finally
            {
                btnDraw.IsEnabled = true;
            }
        }
    }
}
Feedback on this topic?