-
Notifications
You must be signed in to change notification settings - Fork 1
/
geotiff-prepare.py
101 lines (86 loc) · 2.52 KB
/
geotiff-prepare.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Ingest training data from the command-line.
"""
import uuid
import os
import rasterio
import glob
import click
import yaml
import logging
def get_projection(path):
with rasterio.open(str(path)) as img:
left, bottom, right, top = img.bounds
return {
'spatial_reference': str(getattr(img, 'crs_wkt', None) or img.crs.wkt),
'geo_ref_points': {
'ul': {'x': left, 'y': top},
'ur': {'x': right, 'y': top},
'll': {'x': left, 'y': bottom},
'lr': {'x': right, 'y': bottom},
}
}
def prep_dataset(path):
#left, right, top, bottom
with rasterio.open(str(path)) as img:
left, bottom, right, top = img.bounds
creation_dt='2016-04-26T00:00:00'
doc = {
'id': str(uuid.uuid4()),
'product_type': 'dsm',
'creation_dt': creation_dt,
'extent': {
'coord':{
'ul':{'lon': left, 'lat': top},
'ur':{'lon': right, 'lat': top},
'll':{'lon': left, 'lat': bottom},
'lr':{'lon': right, 'lat': bottom},
},
'from_dt': creation_dt,
'to_dt': creation_dt,
'center_dt': creation_dt
},
'format': {'name': 'GeoTiff'},
'grid_spatial': {
'projection': {
'spatial_reference': str(getattr(img, 'crs_wkt', None) or img.crs.wkt),
'geo_ref_points': {
'ul': {'x': left, 'y': top},
'ur': {'x': right, 'y': top},
'll': {'x': left, 'y': bottom},
'lr': {'x': right, 'y': bottom},
}
}
},
'image': {
'bands': {
'band1': {
'path': path,
'layer': 1
},
'band2': {
'path': path,
'layer': 2
},
'band3': {
'path': path,
'layer': 3
}
}
},
'lineage': {
'source_datasets': {}
}
}
return doc
@click.command(help="Prepare metadata for indexing into a datacube.")
@click.argument('dataloc',
type=click.Path(exists=True, readable=True),
nargs=-1)
@click.option('--output', required=True, help="Write datasets into this file",
type=click.Path(exists=False, writable=True, dir_okay=False))
def main(dataloc, output):
with open(output, 'w') as stream:
yaml.dump_all((prep_dataset(path) for path in glob.glob(str(dataloc[0])+'*.tif')), stream)
if __name__ == "__main__":
main()