Disease prediction and management in cattle farming present significant challenges due to the complex interplay of environmental factors, disease transmission dynamics and the need for accurate localization of disease-carrying cattle. Traditional approaches often fall short in optimizing anchor node selection and accurately forecasting disease occurrences. To address these limitations, we propose OptiLoc, a novel framework that integrates advanced optimization techniques and disease forecasting algorithms. The proposed OptiLoc framework leverages Enhanced Sand Cat Swarm Optimization (SCSO) for optimizing anchor node selection within the farm area network. By dynamically adjusting the exploration rate and incorporating an opposition-based memory mechanism, OptiLoc ensures thorough exploration of the search space while preventing premature convergence. Furthermore, the multi-objective optimization aspect of SCSO considers factors such as coverage, energy efficiency, and connectivity, resulting in optimal anchor node placement. Additionally, OptiLoc integrates disease forecasting capabilities using logistic regression classifiers with dictionary learning. By extracting meaningful features from sensor data and training linear classifiers, OptiLoc predicts disease outbreaks among the cattle population. These predictions are seamlessly integrated into the optimization process through an expanded fitness function, which considers disease-related parameters alongside traditional optimization objectives. Furthermore, OptiLoc enhances disease management and control efforts by localizing sensor nodes using the DV-Hop method. By estimating sensor node positions based on hop distances to anchor nodes and dynamically calibrating hop distances based on real-time data on disease prevalence and environmental factors, OptiLoc achieves precise localization results. The proposed OptiLoc framework offers several advantages over traditional approaches. It optimizes anchor node placement, accurately forecasts disease outbreaks, and enhances localization accuracy, thereby empowering farmers with actionable insights to safeguard the health and well-being of their livestock. Performance evaluation reveals for the anchor nodes, it measures a mean localization error of 3.2% to 9.1% with a mean error of 5.91%. Similarly, for the sensor nodes, the mean localization error is 3.45% to 8.25% with a mean error of 5.445%. The AUC of the logistic regression classifier with dictionary learning is 0.97. Through its comprehensive approach, OptiLoc represents a significant advancement in optimizing WSNs in agricultural environments.