code: public static double calculateFirstLine(
              final ReferencedEnvelope bounds,
              final GridParam layerData,
              final int ordinal) {
          double spaceFromOrigin = bounds.getMinimum(ordinal) - layerData.origin[ordinal];
          double linesBetweenOriginAndMap = Math.ceil(spaceFromOrigin / layerData.spacing[ordinal]);
  
          return linesBetweenOriginAndMap * layerData.spacing[ordinal] + layerData.origin[ordinal];
      }
docs: Calculate the where the grid first line should be drawn when spacing and origin are defined in {@link
  GridParam}.
  
  @param bounds the map bounds
  @param layerData the parameter information
  @param ordinal the direction (x,y) the grid line will be drawn.