This document is archived and information here might be outdated.  Recommended version.


How to snap a point to a coordinate grid (ArcObjects .NET 10.5 SDK)

How to snap a point to a coordinate grid


Snapping a point to a coordinate grid

The following code example creates a point, associates it with the spatial reference of a feature class, positions it in the center of the domain of that spatial reference, then snaps its coordinates to the spatial reference's coordinate grid:
[C#]
private void SnapToSpatialReference(IFeatureClass featureClass)
{
    if (featureClass == null)
    {
        return ;
    }
    IGeoDataset geoDataset=featureClass as IGeoDataset;
    ISpatialReference spatialReference=geoDataset.SpatialReference;
    IPoint point=new PointClass();

    // The IPoint interface inherits from the IGeometry interface.
    point.SpatialReference=spatialReference;

    // Assign to the point the mathematical (i.e., full double precision resolution) center of 
    // the x,y domain of this spatial reference.
    double xMin;
    double yMin;
    double xMax;
    double yMax;
    spatialReference.GetDomain(out xMin, out xMax, out yMin, out yMax);

    point.X=(xMin + xMax) * 0.5;
    point.Y=(yMin + yMax) * 0.5;

    // Snap the double precision center of the domain to a location representable in the domain. 
    // Specifically, a multiple of the resolution and offset from the xMin, yMin of the domain.
    System.Windows.Forms.MessageBox.Show("Before snapping: " + point.X + ", " +
        point.Y);
    point.SnapToSpatialReference();
    System.Windows.Forms.MessageBox.Show("After snapping: " + point.X + ", " +
        point.Y);
}
[VB.NET]
Private Sub SnapToSpatialReference(ByVal featureClass As IFeatureClass)
    If featureClass Is Nothing Then
        Exit Sub
    End If
    Dim geoDataset As IGeoDataset=CType(featureClass, IGeoDataset)
    Dim spatialReference As ISpatialReference=geoDataset.SpatialReference
    Dim point As IPoint=New PointClass()
    
    ' The IPoint interface inherits from the IGeometry interface.
    point.SpatialReference=spatialReference
    
    ' Assign to the point the mathematical (i.e., full double precision resolution) center of
    ' the x,y domain of this spatial reference.
    Dim xMin As Double, yMin As Double, xMax As Double, yMax As Double
    spatialReference.GetDomain(xMin, xMax, yMin, yMax)
    point.X=(xMin + xMax) * 0.5
    point.Y=(yMin + yMax) * 0.5
    
    ' Snap the double precision center of the domain to a location representable in the domain.
    ' Specifically, a multiple of the resolution and offset from the xMin, yMin of the domain.
    System.Windows.Forms.MessageBox.Show("Before snapping: " & point.X & ", " & point.Y)
    point.SnapToSpatialReference()
    System.Windows.Forms.MessageBox.Show("After snapping: " & point.X & ", " & point.Y)
End Sub


See Also:

Understanding coordinate management in ArcGIS




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced
Engine Developer Kit Engine