public Coordinate snapToGrid( Point centerPoint, IMap map ) {
List<ILayer> layers = map.getMapLayers();
// by default choose something that will work
ILayer found=layers.get(0);
GridMapGraphic graphic=new GridMapGraphic();
for( ILayer layer : layers ) {
if( layer.hasResource(GridMapGraphic.class) ){
found = layer;
try {
graphic = layer.getResource(GridMapGraphic.class, ProgressManager.instance().get());
} catch (IOException e) {
throw (RuntimeException) new RuntimeException( ).initCause( e );
}
break;
}
}
double[] closest;
try {
closest = graphic.closest(centerPoint.getX(), centerPoint.getY(), found);
} catch (FactoryException e) {
EditPlugin.log(null, e);
throw (RuntimeException) new RuntimeException( ).initCause( e );
}
return new Coordinate(closest[0], closest[1], 0);