Aim: Among a large number of products that support communication, there is one called space air ground integrated networks (SAGIN's), which is the most commonly used to support users in rural and emergency situations. Typicaly in emergency situations SAGIN's use unmanned aerial vehicles (UAVs) in their air layer to temporarily support the ground users. Although the cost of UAVs is lower than that of traditional base stations, and their actions are more flexible, but their battery life problems lead to frequent charging of drones, resulting in many resource losses and unable to provide communication support. In order to mitigate the issues, novel optimization algorithms need to be developed to support the ground users. Methods: In this work, we develop a grid-based deep learning method using the LSTM model to estimate the number of ground users as vehicles in each area, and developed an optimization algorithm to minimize the number of UAVs needed to the user's and meanwhile to satisfy quality of service (QoS) requirements. For optimization, we mainly use the Linear Optimization Tools and an objective function has been developed using density predicted and SINR data to achieve acceptable QoS. Results: The simulation results shows that this approach has improved the quality of the communication by 50%. Conclusion: Using unique grid technique and the LSTM machine learning model, the user densities on each partitioned grid is determined. Finally, a linear optimization algorithm is developed based on the user density to determine the lowest number of UAVs to support the users in each grid while maintaining the QoS.