This document is archived and information here might be outdated. Recommended version. |
Provides access to the Center And Scale Map Area Interface.
Use ICenterAndScale to apply a new geographic extent to a map by specifying a center point and map scale.
Classes | Description |
---|---|
CenterAndScale | The Center And Scale coclass allows you to change the spatial extent of a map by specifying the center and scale. |
One way to change the map extent is using the object CenterAndScale. Create a new CenterAndScale object and set the geographic extent of the map by setting a center point and a map scale. If the spatial reference of the MapServer object has changed, remember to adjust the spatial reference of the center point to match the new spatial reference.
It is important to remember that CenterAndScale does not compute map extent. When used in ExportMapImage function, the returned object IMapImage can be used to get the updated map extent. To compute map extent based on a center point and a scale without calling the ArcGIS Server, a client side solution is required. Please see the code snippet below.
The following sample codes show (1) how to zoom to a specified scale using ICenterAndScale, (2) how to compute the map extent based on a scale and a center point, and (3) how to compute the map extent based on a scale and a center point when the esriDisplay library is not available (in case of WSDL). Last two code snippets may be modified to accept a point and a scale instead of a ICenterAndScale.
It assumes that you already have a valid MapServer, MapDescription and ImageDescription objects and that you are not working with a server context. However, if you are developing an ArcGIS for Server application using a server context, you should not use New to create local ArcObjects, but you should always create objects within the server by calling CreateObject on IServerContext.
Example# 1: Zoom to a specified scale using CenterAndScale object
IMapDescription mapDesc;
IEnvelope extent = mapDesc.MapArea.Extent;
double lXMin, lXMax, lYMin, lYMax;
lXMin = extent.XMin;
lXMax = extent.XMax;
lYMin = extent.YMin;
lYMax = extent.YMax;
// Calculate center point of current map extent.
IPoint centerPoint = new PointClass();
centerPoint.SpatialReference = mapDesc.SpatialReference;
centerPoint.X = lXMin + (lXMax - lXMin) / 2;
centerPoint.Y = lYMin + (lYMax - lYMin) / 2;
// Assign center point and map scale to new CenterAndScale object.
ICenterAndScale centerAndScale = new CenterAndScaleClass();
centerAndScale.Center = centerPoint;
centerAndScale.MapScale = 2000000;
// Assign new CenterAndScale object to MapDescription.
mapDesc.MapArea = (IMapArea)centerAndScale;
//Exporting map to the specified scaleIMapImage pMapImage = pMapServer.ExportMapImage(mapDesc, pImageDescriptipn);
Example# 2: Compute Map Extent with a location and scale
/* ** esriDisplay library must be referenced.
The function requires MapServerInfo, ImageDisplay, CenterAndScale
object and returns IEnvelope
ServerContext is optional, only required when your application works
with ServerContext */
private IEnvelope ComputeExtent(IMapServerInfo pMapServerInfo, IImageDisplay pImageDisplay, ICenterAndScale pCenterAndScale, IServerContext pServerContex)
{
try
{
/* aRect holds information about the client side�s map size in pixel */
tagRECT aRect = new tagRECT();
aRect.top = 0;
aRect.left = 0;
aRect.bottom = pImageDisplay.Height;
aRect.right = pImageDisplay.Width;
/* when application uses ServerContext, DisplayTransformation will be created in ServerContext */
IDisplayTransformation pDT;
if (pServerContex != null)
pDT = pServerContex.CreateObject("esriDisplay.DisplayTransformation") as IDisplayTransformation;
else
pDT = new DisplayTransformationClass();
/* Setting different DisplayTransformation properties to compute extent */
pDT.Bounds = pMapServerInfo.FullExtent;
IEnvelope pMDscExtent = pMapServerInfo.DefaultMapDescription.MapArea.Extent;
pMDscExtent.CenterAt(pCenterAndScale.Center);
pDT.VisibleBounds = pMDscExtent;
pDT.SpatialReference = pMapServerInfo.DefaultMapDescription.SpatialReference;
pDT.Units = pMapServerInfo.MapUnits;
pDT.set_DeviceFrame(ref aRect);
pDT.Resolution = pImageDisplay.DeviceResolution;
pDT.ScaleRatio = pCenterAndScale.MapScale;
/* printing map extent */
Debug.Print("New Map Extent - After ScaleRatio: " + pDT.FittedBounds.XMin.ToString() + ", " + pDT.FittedBounds.YMin.ToString() + ", " + pDT.FittedBounds.XMax.ToString() + ", " + pDT.FittedBounds.YMax.ToString());
//returning new map extent
return pDT.FittedBounds;
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, "Error in ComputeExtent", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
Example# 3: Compute Map Extent with a location and scale in WSDL
/* The function requires MapServerInfo, ImageDisplay, CenterAndScale
object and an already created Envelope object (by ref) which will be
modified */
private void ComputeExtent(IMapServerInfo pMSI, IImageDisplay pImageDisplay, ICenterAndScale pCenterAndScale, ref IEnvelope pOutEnvelope)
{
try
{
/* dividing image width by DPI to get it in Inch */
double dblImgWidthInInch = pImageDisplay.Width / pImageDisplay.DeviceResolution;
double dblImgHeightInInch = pImageDisplay.Height / pImageDisplay.DeviceResolution;
/* converting Inch to meter (assume the map is in meter) */
double dblImgWidthInMapUnit = dblImgWidthInInch * 0.0254;
double dblImgHeightInMapUnit = dblImgHeightInInch * 0.0254;
/* calculating half of map�s height & width at the specific scale */
double dX = (dblImgWidthInMapUnit * pCenterAndScale.MapScale) / 2;
double dY = (dblImgHeightInMapUnit * pCenterAndScale.MapScale) / 2;
/* using the center point and width & height created before, it is computing map�s extent */
IPoint pCtrPnt = pCenterAndScale.Center;
double minX = pCtrPnt.X - dX;
double maxX = pCtrPnt.X + dX;
double minY = pCtrPnt.Y - dY;
double maxY = pCtrPnt.Y + dY;
/* assinging values to the Envelope object passed by the caller function */
pOutEnvelope.XMin = minX;
pOutEnvelope.XMax = maxX;
pOutEnvelope.YMin = minY;
pOutEnvelope.YMax = maxY;
/* printing out the extent */
Debug.Print("ComputeExtent Result: " + pOutEnvelope.XMin.ToString() + ", " + pOutEnvelope.YMin.ToString() + ", " + pOutEnvelope.XMax.ToString() + ", " + pOutEnvelope.YMax.ToString());
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, "Error in ComputeExtent", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}