ArcGIS Pro 3.4 API Reference Guide
ArcGIS.Core.Geometry Namespace / GeometryEngine Class / Within Method
The base geometry.
The comparison geometry.
Example Version

Within Method (GeometryEngine)
Returns true if geometry1 is within geometry2.
Syntax

Parameters

geometry1
The base geometry.
geometry2
The comparison geometry.

Return Value

True if geometry1 is within geometry2.
Exceptions
ExceptionDescription
Either geometry1 or geometry2 or both are null.
The method is not implemented for GeometryBag.
Incompatible spatial references between the input geometries.
Remarks
If both geometry1 and geometry2 are not empty, then geometry1 is within geometry2 if the intersection of their interiors is not empty and geometry1 is a subset of geometry2.

If the geometry2 is empty, then geometry1 is not within geometry2.

If geometry1 is empty and geometry2 is not empty, then geometry1 is within geometry2.

Within is the opposite of Contains. That is, geometry1 is within geometry2 if and only if geometry2 contains geometry1.

GeometryEngine Within

Example
Get the outermost rings of a polygon
/// <summary>
/// The methods retrieves the outer ring(s) of the input polygon.
/// </summary>
/// <param name="inputPolygon">Input Polygon.</param>
/// <returns>The outer most (exterior, clockwise) ring(s) of the polygon. If the input is null or empty, a null pointer is returned.</returns>
public Polygon GetOutermostRings(Polygon inputPolygon)
{
  if (inputPolygon == null || inputPolygon.IsEmpty)
    return null;

  List<Polygon> internalRings = new List<Polygon>();

  // explode the parts of the polygon into a list of individual geometries
  // see the "Get the individual parts of a multipart feature"
  // snippet for MultipartToSinglePart
  var parts = MultipartToSinglePart(inputPolygon);

  // get an enumeration of clockwise geometries (area > 0) ordered by the area
  var clockwiseParts = parts.Where(geom => ((Polygon)geom).Area > 0)
                        .OrderByDescending(geom => ((Polygon)geom).Area);

  // for each of the exterior rings
  foreach (var part in clockwiseParts)
  {
    // add the first (the largest) ring into the internal collection
    if (internalRings.Count == 0)
      internalRings.Add(part as Polygon);

    // use flag to indicate if current part is within the already selection polygons
    bool isWithin = false;

    foreach (var item in internalRings)
    {
      if (GeometryEngine.Instance.Within(part, item))
        isWithin = true;
    }

    // if the current polygon is not within any polygon of the internal collection
    // then it is disjoint and needs to be added to 
    if (isWithin == false)
      internalRings.Add(part as Polygon);
  }

  PolygonBuilderEx outerRings = new PolygonBuilderEx();
  // now assemble a new polygon geometry based on the internal polygon collection
  foreach (var ring in internalRings)
  {
    outerRings.AddParts(ring.Parts);
  }

  // return the final geometry of the outer rings
  return outerRings.ToGeometry();
}
MapPoints, Polylines, Polygons within Polygon
// build a polygon      
List<MapPoint> pts = new List<MapPoint>();
pts.Add(MapPointBuilderEx.CreateMapPoint(1.0, 1.0));
pts.Add(MapPointBuilderEx.CreateMapPoint(1.0, 2.0));
pts.Add(MapPointBuilderEx.CreateMapPoint(2.0, 2.0));
pts.Add(MapPointBuilderEx.CreateMapPoint(2.0, 1.0));

Polygon poly = PolygonBuilderEx.CreatePolygon(pts);

// an inner point
MapPoint innerPt = MapPointBuilderEx.CreateMapPoint(1.5, 1.5);
bool within = GeometryEngine.Instance.Within(innerPt, poly);   // within = true

// point on a boundary
within = GeometryEngine.Instance.Within(pts[0], poly);     // within = false

// an interior line
MapPoint innerPt2 = MapPointBuilderEx.CreateMapPoint(1.25, 1.75);
List<MapPoint> innerLinePts = new List<MapPoint>();
innerLinePts.Add(innerPt);
innerLinePts.Add(innerPt2);

Polyline polyline = PolylineBuilderEx.CreatePolyline(innerLinePts);
within = GeometryEngine.Instance.Within(polyline, poly);   // within = true

// a line that crosses the boundary
MapPoint outerPt = MapPointBuilderEx.CreateMapPoint(3, 1.5);
List<MapPoint> crossingLinePts = new List<MapPoint>();
crossingLinePts.Add(innerPt);
crossingLinePts.Add(outerPt);

polyline = PolylineBuilderEx.CreatePolyline(crossingLinePts);
within = GeometryEngine.Instance.Within(polyline, poly);     // within = false


// polygon in polygon
Envelope env = EnvelopeBuilderEx.CreateEnvelope(innerPt, innerPt2);
within = GeometryEngine.Instance.Within(env, poly);      // within = true
Requirements

Target Platforms: Windows 11, Windows 10

ArcGIS Pro version: 3 or higher.
See Also