import os import requests import time from google.cloud import storage from firebase_admin import credentials import traceback def download_image(url, directory, tile_name): try: response = requests.get(url) response.raise_for_status() # Extracting the filename from the URL #filename = os.path.join(directory, os.path.basename(url)) filename = os.path.join(directory, tile_name) # Saving the image to the local directory with open(filename, 'wb') as file: file.write(response.content) print(f"Image downloaded successfully: {filename}") except requests.exceptions.RequestException as e: print(f"Error downloading image: {e}") def delete_local_file(file_path): try: os.remove(file_path) print(f"Local file deleted: {file_path}") except OSError as e: print(f"Error deleting local file: {e}") def upload_to_gcs(local_file_path, bucket_name, destination_blob_name): try: storage_client = storage.Client(project='farmbase-b2f7e') bucket = storage_client.bucket(bucket_name) # Upload the file to GCS blob = bucket.blob(destination_blob_name) blob.upload_from_filename(local_file_path) print(f"File uploaded to GCS: {destination_blob_name}") except Exception as e: print(f"Error uploading file to GCS: {e}") def file_exists_in_gcs(bucket_name, blob_name): try: storage_client = storage.Client() bucket = storage_client.bucket(bucket_name) blob = bucket.blob(blob_name) return blob.exists() except Exception as e: print(f"Error checking if file exists in GCS: {e}") return False # URL of the image you want to download # 80, 180 # N -> 80 to 0 # S -> 0 to 50 # E -> 0 to 170 # W -> 170 to 0 lat_t = ['N', 'S'] lng_t = ['W', 'E'] latlngs = [] # Local directory to save the image local_directory = "trst_tif" startint = 0 endint = 4 # Create the local directory if it doesn't exist os.makedirs(local_directory, exist_ok=True) for single_lat in lat_t: for single_lng in lng_t: for lng in range(startint,endint): for lat in range(startint,endint): if lat == 0: new_lat = single_lat + str(lat) else: new_lat = single_lat + str(lat) + '0' if lng == 0: new_lng = single_lng + str(lng) else: new_lng = single_lng + str(lng) + '0' lat_lng = 'lat='+new_lat + '&lon='+new_lng #latlngs.append(lat_lng) print(lat_lng) #print(latlngs) image_url = "https://ies-ows.jrc.ec.europa.eu/iforce/gfc2020/download.py?type=tile&" + lat_lng print(image_url) # Download the image file_name = new_lat + new_lng + '.tif' gcs_bucket_name = "farmbase-b2f7e.appspot.com" gcs_destination_blob_name = "trst_forest_2020/" + file_name obj = time.gmtime(0) epoch = time.asctime(obj) #print("The epoch is:",epoch) curr_time = round(time.time()*1000) print("Milliseconds since epoch:",curr_time) if file_exists_in_gcs(gcs_bucket_name, file_name): print('file in bucket') else: try: download_image(image_url, local_directory, file_name) # Google Cloud Storage configuration upload_to_gcs((local_directory + '/' + file_name), gcs_bucket_name, gcs_destination_blob_name) delete_local_file((local_directory + '/' + file_name)) #time.sleep(30) except: print(traceback.format_exc())