How to snap a point to a coordinate grid


Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime

Additional Requirements
  • This article assumes your project includes references to the ESRI Geometry assembly.

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