ui/src/main/java/ucar/nc2/ui/gis/SpatialGrid.java
private Object findClosestAlongPerimeter(Point2D pt, int centerX, int centerY, int perimeter) {
    Object closestO = null;
    double closestD = MAX_DOUBLE;

    // top and bottom row
    for (int y=centerY-perimeter; y<=centerY+perimeter; y+=2*perimeter)
      for (int x=centerX-perimeter; x<=centerX+perimeter; x++) {
        double distance = distanceSq(pt,x,y);
        if (distance < closestD) {
          closestO = gridArray[y][x].o;
          closestD = distance;
          if (debugClosest) System.out.println("   closest "+ gridArray[y][x]);
        }
      }

    // middle rows
    for (int y=centerY-perimeter+1; y<=centerY+perimeter-1; y++)
      for (int x=centerX-perimeter; x<=centerX+perimeter; x+=2*perimeter) {
        double distance = distanceSq(pt,x,y);
        if (distance < closestD) {
          closestO = gridArray[y][x].o;
          closestD = distance;
          if (debugClosest) System.out.println("   closest "+ gridArray[y][x]);
        }
      }

    return closestO;
  }
private Object findClosestAlongPerimeter(Point2D pt, int centerX, int centerY, int perimeter) {
    Object closestO = null;
    double closestD = MAX_DOUBLE;

    // top and bottom row
    for (int y=centerY-perimeter; y<=centerY+perimeter; y+=2*perimeter)
      for (int x=centerX-perimeter; x<=centerX+perimeter; x++) {
        double distance = distanceSq(pt,x,y);
        if (distance < closestD) {
          closestO = gridArray[y][x].o;
          closestD = distance;
          if (debugClosest) System.out.println("   closest "+ gridArray[y][x]);
        }
      }

    // middle rows
    for (int y=centerY-perimeter+1; y<=centerY+perimeter-1; y++)
      for (int x=centerX-perimeter; x<=centerX+perimeter; x+=2*perimeter) {
        double distance = distanceSq(pt,x,y);
        if (distance < closestD) {
          closestO = gridArray[y][x].o;
          closestD = distance;
          if (debugClosest) System.out.println("   closest "+ gridArray[y][x]);
        }
      }

    return closestO;
  }
with center cell[x,y] and side of length 2*perimeter+1