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.
[Java]
static void snapToSpatialReference(IFeatureClass featureClass)throws Exception{
    if (featureClass == null)
        return ;

    IGeoDataset geoDataset = (IGeoDataset)featureClass;
    ISpatialReference spatialReference = geoDataset.getSpatialReference();
    IPoint point = new Point();

    //The IPoint interface inherits from the IGeometry interface.
    point.setSpatialReferenceByRef(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[] = new double[1];
    double yMin[] = new double[1];
    double xMax[] = new double[1];
    double yMax[] = new double[1];
    spatialReference.getDomain(xMin, xMax, yMin, yMax);

    point.setX((xMin[0] + xMax[0]) * 0.5);
    point.setY((yMin[0] + yMax[0]) * 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.out.println("Before snapping: " + point.getX() + ", " + point.getY());
    point.snapToSpatialReference();
    System.out.println("After snapping: " + point.getX() + ", " + point.getY());
}


See Also:

Understanding coordinate management in ArcGIS




Development licensingDeployment licensing
ArcGIS for Desktop BasicArcGIS for Desktop Basic
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine