/
dynamic.py
68 lines (54 loc) · 2.91 KB
/
dynamic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
import json
import boto3
import botocore
from osgeo import gdal
from shapely import geometry, ops
from tiletanic import tilecover, tileschemes
def get_latest_version(project_name):
bucket_name, prefix = 'flame-projects', project_name + '/'
client = boto3.client('s3')
result = client.list_objects(Bucket=bucket_name, Prefix=prefix, Delimiter='/')
versions = [o.get('Prefix').split('/')[-2] for o in result.get('CommonPrefixes')]
versions.reverse()
return versions[0]
def materialize_tif(project_name, version, aoi_geojson, output_filename = 'dynamic.tif'):
gdal.SetConfigOption('GDAL_DISABLE_READDIR_ON_OPEN', 'TRUE')
gdal.SetConfigOption('VSI_CACHE', 'TRUE')
# gdal.SetConfigOption('CPL_CURL_VERBOSE', 'YES') # see the http calls
gdal.SetConfigOption('CPL_VSIL_CURL_ALLOWED_EXTENSIONS', 'TIF')
# a box in australia:
#aoi_geojson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[153.42390060424805,-28.00989892967722],[153.43647480010986,-28.00989892967722],[153.43647480010986,-27.999933788434422],[153.42390060424805,-27.999933788434422],[153.42390060424805,-28.00989892967722]]]}}]}'
aoi = ops.unary_union([geometry.shape(f['geometry']) for f in json.loads(aoi_geojson)['features']])
#project_name = 'DYNAMIC-Australia-Q32017/1'
bucket_name, prefix = 'flame-projects', project_name + '/' + version + '/raster_tiles/'
s3 = boto3.resource('s3')
tiler = tileschemes.DGTiling()
paths = []
for key in (prefix + tiler.quadkey(tile) + '.tif' for tile in tilecover.cover_geometry(tiler, aoi, 12)):
# Check if tif exists.
print key
try:
s3.Object(bucket_name=bucket_name, key=key).load()
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
continue
else:
raise
paths.append('/vsis3/{}/{}'.format(bucket_name, key))
if len(paths) > 4:
raise Exception('Too big of an AOI requested.')
if not paths:
raise Exception('No data found to materialize.')
# Build an inmem vrt and translate.
try:
ds = gdal.BuildVRT('/vsimem/combo.vrt', paths, outputBounds=aoi.bounds)
ds_out = gdal.Translate(output_filename, ds)
finally:
ds_out = None
ds = None
if __name__ == '__main__':
aoi_geojson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[153.42390060424805,-28.00989892967722],[153.43647480010986,-28.00989892967722],[153.43647480010986,-27.999933788434422],[153.42390060424805,-27.999933788434422],[153.42390060424805,-28.00989892967722]]]}}]}'
project_name = 'DYNAMIC-Australia-Q32017'
version = '1'
materialize_tif(project_name, version, aoi_geojson, output_filename='/mnt/work/output/data/test.tif')