ArcGIS Pro SDK C# – Converting Screen Pixels to Map Units with C# Using ArcGIS Pro SDK

arcgis-pro-sdkc

I am looking to create polygons (halos/rings) around the mouse cursor programmatically with C# using the ArcGIS Pro SDK. I have figured out how to create the halos by using System.Windows.Forms.Control.MousePosition and then converting into lat/long by using MapView.Active.ScreenToMap. I have also used a MouseHook from the Win32Api to give me a MouseMove event. This allows me to associate the polygon to the mouse cursor and move it around the map as an overlay.

The problem that I am having now is that I can only create a polygon (halo/ring) with a radius in pixels. I need to be able to use map units, like feet, meters, miles, kilometers, etc. For example, I want to create a polygon (halo/ring) that has a radius of 100 feet.

Is there some way to accomplish this?

The following is code that I am using that doesn't work. It only functions with pixels.

await QueuedTask.Run(() =>
                {
                    System.Drawing.Point pt = System.Windows.Forms.Control.MousePosition;
                    MapPoint mapPt = MapView.Active.ScreenToMap(new Point(pt.X, pt.Y));
                    double bufferRadius = 100;

                    ArcGIS.Core.Geometry.Polygon poly = CreateHaloPolygon(mapPt, Convert.ToInt32(bufferRadius), out double haloRadius);
                    //_graphics.Add(new GraphicProperties(null, null, polySymRef, poly, lineColor, bufferRadius, bufferInfo[2]));
                    _graphics.Add(new GraphicProperties(null, null, polySymRef, poly, lineColor, haloRadius, bufferInfo[2]));
                });

private ArcGIS.Core.Geometry.Polygon CreateHaloPolygon(MapPoint mapPoint, int pixels, out double radius)
    {
        // Get the halo radius
        Point screenPoint = MapView.Active.MapToScreen(mapPoint);
        Point radiusScreenPoint = new Point((screenPoint.X + pixels), screenPoint.Y);
        MapPoint radiusMapPoint = MapView.Active.ScreenToMap(radiusScreenPoint);
        radius = GeometryEngine.Instance.Distance(mapPoint, radiusMapPoint);

        // Build a circle geometry
        Coordinate2D coord2D = new Coordinate2D(mapPoint);
        EllipticArcSegment arcSegment = EllipticArcBuilder.CreateEllipticArcSegment(coord2D, radius, esriArcOrientation.esriArcClockwise, MapView.Active.Map.SpatialReference);
        PolygonBuilder polyBuilder = new PolygonBuilder(new[] { arcSegment });

        return polyBuilder.ToGeometry();
    }

Best Answer

I have solved this issue by using the GeometryEngine.Instance.GeodesicEllipse method. It takes in a GeodesicEllipseParamter containing all of the necessary parameters to create the ellipse and a spatial reference. Included in the parameters is the radius and the linear units to use. The following is a code snippet of creating the geodesic ellipse:

private ArcGIS.Core.Geometry.Geometry BuildBufferEllipse(MapPoint mapPt, double radius, string linearUnits)
{
    var ellipseParams = new GeodesicEllipseParameter
    {
        Center = new Coordinate2D(mapPt),
        AxisDirection = 0.0,
        LinearUnit = GetLinearUnits(linearUnits),
        OutGeometryType = GeometryType.Polygon,
        SemiAxis1Length = radius,
        SemiAxis2Length = radius,
        VertexCount = 99
    };

    return GeometryEngine.Instance.GeodesicEllipse(ellipseParams, MapView.Active.Map.SpatialReference);
}
Related Question