Relate

Download Samples Repository

Description

Sample shows how to use the GeometryEngine.Relate method to test the spatial relationship of two geometries.

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

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.Relate"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             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.  Then test the relationship of Shape #1 to Shape #2 by choosing or entering a DE-9IM value and clicking the 'Test Relationship' button." 
                           Margin="0,0,0,12" FontSize="14" Width="400" TextAlignment="Left" TextWrapping="Wrap" />

                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                    <StackPanel Orientation="Horizontal" Margin="12,0" VerticalAlignment="Center">
                        <TextBlock Text="Shape 1:" Margin="6,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" VerticalAlignment="Center">
                        <TextBlock Text="Shape 2:" Margin="6,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>
                </StackPanel>

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

                <StackPanel Orientation="Horizontal" Margin="12,24,12,0" HorizontalAlignment="Center">
                    <TextBlock Text="Relationship:" Margin="6,0" VerticalAlignment="Center" />
                    <ComboBox x:Name="comboRelate" Width="200" IsEditable="True" FontFamily="Courier New" SelectedIndex="0">
						<sys:String>T*****FF* (Contains)</sys:String>
						<sys:String>T******** (Intersects)</sys:String>
                        <sys:String>T*F**F*** (Within)</sys:String>
                        <sys:String>T*F**FFF* (Equals)</sys:String>
                        <sys:String>FF*FF**** (Disjoint)</sys:String>
                    </ComboBox>
                </StackPanel>

                <Button x:Name="btnTest" Content="Test Relationship" Margin="12" HorizontalAlignment="Center"
                        IsEnabled="False" Click="RelateButton_Click"/>

                <Border x:Name="resultPanel" Margin="12,12,12,0" BorderBrush="Black" BorderThickness="2" Visibility="Collapsed">
                    <TextBlock Text="{Binding}" HorizontalAlignment="Center" FontSize="14" FontWeight="Bold" Margin="8" />
                </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;
using System.Windows.Media;

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

        /// <summary>Construct Relationship sample control</summary>
        public Relate()
        {
            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 void StartDrawingButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnDraw.IsEnabled = false;
                btnTest.IsEnabled = false;
                resultPanel.Visibility = Visibility.Collapsed;

				_graphicsOverlay.Graphics.Clear();

                // Shape One
                DrawShape drawShape1 = (DrawShape)comboShapeOne.SelectedItem;
                Esri.ArcGISRuntime.Geometry.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
                Esri.ArcGISRuntime.Geometry.Geometry shapeTwo = await MyMapView.Editor.RequestShapeAsync(
                    (DrawShape)comboShapeTwo.SelectedItem, _symbols[comboShapeTwo.SelectedIndex]);

				_graphicsOverlay.Graphics.Add(new Graphic(shapeTwo, _symbols[comboShapeTwo.SelectedIndex]));
            }
            catch (TaskCanceledException) { }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, "Relationship Sample");
            }
            finally
            {
                btnDraw.IsEnabled = true;
				btnTest.IsEnabled = (_graphicsOverlay.Graphics.Count >= 2);
            }
        }

        // Checks the specified relationship of the two shapes
        private void RelateButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
				if (_graphicsOverlay.Graphics.Count < 2)
                    throw new ApplicationException("No shapes available for relationship test");

				var shape1 = _graphicsOverlay.Graphics[0].Geometry;
				var shape2 = _graphicsOverlay.Graphics[1].Geometry;

                string relate = comboRelate.Text;
                if (relate.Length < 9)
                    throw new ApplicationException("DE-9IM relate string must be 9 characters");

                relate = relate.Substring(0, 9);

                bool isRelated = GeometryEngine.Relate(shape1, shape2, relate);

                resultPanel.Visibility = Visibility.Visible;
                resultPanel.Background = new SolidColorBrush((isRelated) ? Color.FromArgb(0x66, 0, 0xFF, 0) : Color.FromArgb(0x66, 0xFF, 0, 0));
                resultPanel.DataContext = string.Format("Relationship: '{0}' is {1}", relate, isRelated.ToString());
            }
            catch (Exception ex)
            {
                resultPanel.Visibility = Visibility.Collapsed;
                MessageBox.Show("Error: " + ex.Message, "Relationship Sample");
            }
        }
    }
}
Feedback on this topic?