How to successfully project the map extent into a different spatial reference

668
1
Jump to solution
03-03-2022 01:23 PM
DaveLewis73
Occasional Contributor

I am having a really difficult time trying to project the map extent (WGS84) into an envelope of a different spatial reference (Web Mercator or UTM). I have tried multiple ways of changing the spatial reference of an envelope, but none of them are working. I know they aren't working because I am drawing a graphic on the screen for the envelope, but nothing appears when changing the envelope to a different spatial reference. Using the map's spatial reference of WGS84 for the envelope works perfectly and it is drawn to the screen.

As an fyi, I am converting the envelope into a polygon, which I am using for the graphic.

The following are four different code attempts that I have made using Web Mercator. I have also tried with UTM:

SpatialReference sr3857 = SpatialReferenceBuilder.CreateSpatialReference(3857);
// Builder constructors need to run on the MCT.
using (SpatialReferenceBuilder srBuilder = new SpatialReferenceBuilder(3857))
{
    // do something with the builder
    sr3857 = srBuilder.ToSpatialReference();
}
ArcGIS.Core.Geometry.Polygon envPoly = null;
Envelope mapExtent = (Envelope)GeometryEngine.Instance.Project(MapView.Active.Extent, sr3857);

private Envelope BuildEnvelope(MapPoint mapPt, Envelope inputEnvelope)
{
    EnvelopeBuilder envelopeBuilder = new EnvelopeBuilder(SpatialReferences.WebMercator);
    envelopeBuilder.XMin = inputEnvelope.XMin;
    envelopeBuilder.YMin = inputEnvelope.YMin;
    envelopeBuilder.XMax = inputEnvelope.XMax;
    envelopeBuilder.YMax = inputEnvelope.YMax;

    return envelopeBuilder.ToGeometry();
}
Envelope mapExtent = BuildEnvelope(MapView.Active.Extent)

Envelope mapExtent = EnvelopeBuilder.CreateEnvelope(MapView.Active.Extent, SpatialReferences.WebMercator);

Envelope mapExtent = EnvelopeBuilder.CreateEnvelope(MapView.Active.Extent, SpatialReferences.WebMercator);
0 Kudos
1 Solution

Accepted Solutions
DaveLewis73
Occasional Contributor

I have come up with a solution for this problem with the assistance of the Esri ArcGIS Pro SDK team.  The whole idea of attempting to project the map extent into an envelope of a different spatial reference has been scrapped and replaced with a much simpler and straightforward approach.  A rectangle with four corners is created that mirrors the extent of the map, as shown in the following code snippet:

Size mapSize = MapView.Active.GetViewSize();
MapPoint pt1 = MapView.Active.ClientToMap(new Point(0, 0));
MapPoint pt2 = MapView.Active.ClientToMap(new Point(mapSize.Width * scale, 0));
MapPoint pt3 = MapView.Active.ClientToMap(new Point(mapSize.Width * scale, mapSize.Height * scale));
MapPoint pt4 = MapView.Active.ClientToMap(new Point(0, mapSize.Height * scale));

Point mapClientPt = MapView.Active.MapToClient(screenMapPt);

bool ptIntersects = (mapClientPt.X >= 0) && (mapClientPt.Y >= 0) && (mapClientPt.X <= mapSize.Width * scale) && (mapClientPt.Y <= mapSize.Height * scale);

This produces the exact results that I am looking for.  Once I have the rectangle I can then check to see if the mouse intersects it or not.

Thanks again to the Esri ArcGIS Pro SDK team.

View solution in original post

0 Kudos
1 Reply
DaveLewis73
Occasional Contributor

I have come up with a solution for this problem with the assistance of the Esri ArcGIS Pro SDK team.  The whole idea of attempting to project the map extent into an envelope of a different spatial reference has been scrapped and replaced with a much simpler and straightforward approach.  A rectangle with four corners is created that mirrors the extent of the map, as shown in the following code snippet:

Size mapSize = MapView.Active.GetViewSize();
MapPoint pt1 = MapView.Active.ClientToMap(new Point(0, 0));
MapPoint pt2 = MapView.Active.ClientToMap(new Point(mapSize.Width * scale, 0));
MapPoint pt3 = MapView.Active.ClientToMap(new Point(mapSize.Width * scale, mapSize.Height * scale));
MapPoint pt4 = MapView.Active.ClientToMap(new Point(0, mapSize.Height * scale));

Point mapClientPt = MapView.Active.MapToClient(screenMapPt);

bool ptIntersects = (mapClientPt.X >= 0) && (mapClientPt.Y >= 0) && (mapClientPt.X <= mapSize.Width * scale) && (mapClientPt.Y <= mapSize.Height * scale);

This produces the exact results that I am looking for.  Once I have the rectangle I can then check to see if the mouse intersects it or not.

Thanks again to the Esri ArcGIS Pro SDK team.

0 Kudos