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#]
privatevoid 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]
PrivateSub SnapToSpatialReference(ByVal featureClass As IFeatureClass)
If featureClass IsNothingThenExitSubEndIfDim 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 AsDouble, yMin AsDouble, xMax AsDouble, yMax AsDouble
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)
EndSub