Skip to content

Instantly share code, notes, and snippets.

@sloev
Last active July 21, 2020 22:23
Show Gist options
  • Select an option

  • Save sloev/6a0ffe8a81a193440ae6091bcbc50e1b to your computer and use it in GitHub Desktop.

Select an option

Save sloev/6a0ffe8a81a193440ae6091bcbc50e1b to your computer and use it in GitHub Desktop.
Download newest landsat recordings

used to download newest satelist images strips off landsat satelites

install

use python3.5+
pip install numpy pillow requests

usage

mkdir images
python download.py
import requests
import json
import base64
from PIL import Image
import numpy as np
from io import BytesIO
import datetime
with open("spacecrafts.json") as f:
spacecrafts = json.load(f)
url = "https://earthnow.usgs.gov/observer/passes?op=recorded"
recordings = requests.get(url).json()
def remove_black_horizontal_lines(arr):
h, w, *_ = arr.shape
img_arr = arr.copy()
img_arr[img_arr < 10] = 0
black_pixels_mask = np.all(img_arr == [0, 0, 0], axis=-1)
img_arr[black_pixels_mask] = [255,0,0]
black_pixels_mask = np.argwhere(black_pixels_mask)
delete_ys = []
for y,x in sorted(zip(*np.unique(black_pixels_mask[:,0], return_counts=True)), key=lambda x: x[1], reverse=True):
if w-x < (w/3):
delete_ys.extend(range(max(y-5, 0), min(y+5, h-1)))
new_img_arr = np.delete(arr, delete_ys, 0)
return new_img_arr
def find_where(l, key, value):
return [d for d in l if d[key] == value][0]
for recording in recordings:
spacecraft = find_where(spacecrafts, 'id', recording['spacecraft'])
true_color_bands = find_where(spacecraft['bandCombinations'], 'name', 'True colour')['id']
start_time = recording['acquisition-date']['epochSecond']
start_time_dt = datetime.datetime.fromtimestamp(start_time)
print(recording['spacecraft'], start_time_dt.isoformat())
max_count = 200
request_data = {"op": "allSegments", "pass": recording['id'], "time": start_time, "band": true_color_bands, "count": max_count};
resp = requests.get('https://earthnow.usgs.gov/observer/passes', params=request_data)
data = resp.json()
segments = []
for segment in sorted(data, key=lambda d: int(d['id'])):
id = segment["id"]
b64_data = segment.get('band')
if not b64_data:
continue
bytes = base64.b64decode(b64_data)
img = Image.open(BytesIO(bytes))
segments.append(np.asarray(img))
if not segments:
print("no data")
continue
imgs_comb = np.vstack( segments )
imgs_comb = remove_black_horizontal_lines(imgs_comb)
imgs_comb = Image.fromarray( imgs_comb)
imgs_comb.save(f"images/{recording['id']}.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment