Skip to content

Instantly share code, notes, and snippets.

@Pavel-Durov
Created November 22, 2023 16:17
Show Gist options
  • Save Pavel-Durov/9d3d54ca47566d79b19c00762910f5c1 to your computer and use it in GitHub Desktop.
Save Pavel-Durov/9d3d54ca47566d79b19c00762910f5c1 to your computer and use it in GitHub Desktop.
Download landcover tile to local filesystem
import os
import requests
from take_home_test_starter.config import Config
BASE_URL = "https://s3-eu-west-1.amazonaws.com/vito.landcover.global/v3.0.1/2019/"
LATITUDES = ["S40", "S20", "N00", "N20", "N40", "N60", "N80"]
LONGTITUDES = [
"W180",
"W160",
"W140",
"W120",
"W100",
"W080",
"W060",
"W040",
"W020",
"E000",
"E020",
"E040",
"E060",
"E080",
"E100",
"E120",
"E140",
"E160",
]
KINDS = ["Discrete"]
DIR = ".landcover"
def build_url(tile_id: str, tile_kind: str) -> str:
return f"{BASE_URL}{tile_id}/{tile_id}_PROBAV_LC100_global_v3.0.1_2019-nrt_{tile_kind}-Classification-map_EPSG-4326.tif"
def download_file(tile_id: str, kind: str) -> str:
file_path = None
response = requests.get(build_url(tile_id, kind), timeout=30)
if response.status_code == 200:
file_path = os.path.join(DIR, f"{tile_id}.tif")
with open(file_path, "wb") as file:
file.write(response.content)
# Remove empty files
if os.path.getsize(file_path) == 0:
os.remove(file_path)
file_path = None
else:
file_path
return file_path
def download_landcover_data() -> list[str]:
if not os.path.exists(DIR):
os.makedirs(DIR)
files = []
# Loop through the combinations of latitude, longitude, and kind and download each file
for latitude in LATITUDES:
for longitude in LONGTITUDES:
for kind in KINDS:
file = download_file(f"{longitude}{latitude}", kind)
if file:
files.append(file)
return files
if __name__ == "__main__":
download_landcover_data()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment