Skip to content

Instantly share code, notes, and snippets.

@vincentsarago
Created December 3, 2019 20:46
Show Gist options
  • Save vincentsarago/51072993acbc07df7d3fb6af741e7fcc to your computer and use it in GitHub Desktop.
Save vincentsarago/51072993acbc07df7d3fb6af741e7fcc to your computer and use it in GitHub Desktop.
import requests
import datetime
import urllib.parse
from concurrent import futures
from supermercado.burntiles import tile_extrema
endpoint = "https://landsatlive.live"
# Define AOI
bounds = [-6.17431640625, 42.79540065303723, 8.28369140625, 51.0275763378024]
# Date Filters
start = datetime.datetime.strptime("2013-01-01", "%Y-%m-%d").strftime("%Y-%m-%dT00:00:00Z")
end = datetime.datetime.strptime("2019-12-01", "%Y-%m-%d").strftime("%Y-%m-%dT23:59:59Z")
# STAC QUERY (sat-api)
query = {
"bbox": bounds,
"time": f"{start}/{end}",
"query": {
"eo:sun_elevation": {"gt": 0},
"landsat:tier": {"eq": "T1"},
"collection": {"eq": "landsat-8-l1"},
"eo:cloud_cover": {"gte": 0, "lt": 5} # Cloud Filters
},
"sort": [{
"field": "eo:cloud_cover",
"direction": "asc"
}],
}
# We post the query to the mosaic endpoint
# `optimized_selection: True` is an option to force only one PATH-ROW scene per tile
# Because we will use `pixel_selection:"first"` this will minimized
# the number of image fetched for each tile request
r = requests.post(
f"{endpoint}/mosaic/create",
json=query,
params={
"optimized_selection": True,
"seasons": "spring" # this should be a "," separated list of seasons
},
)
results = r.json()
# Create tile url
query_params = dict(
bands="4,3,2", # True Color RGB
color_ops="gamma RGB 3.5, saturation 1.7, sigmoidal RGB 15 0.35",
pixel_selection="first",
)
tiles_url = results["tiles"][0] + urllib.parse.urlencode(query_params)
# Get Mercator tiles covering the AOI
zoom = results["maxzoom"]
extrema = tile_extrema(bounds, zoom)
tiles = []
for x in range(extrema["x"]["min"], extrema["x"]["max"]):
for y in range(extrema["y"]["min"], extrema["y"]["max"]):
tiles.append(f"{zoom}-{x}-{y}")
def worker(tile):
# Do Whatever you want here
# e.g send tile to an inference pipeline
pass
with futures.ThreadPoolExecutor() as executor:
executor.map(worker, tiles)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment