-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin2tif_ps2.py
executable file
·141 lines (106 loc) · 4.39 KB
/
bin2tif_ps2.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
"""
Author : Emmanuel Gonzalez
Date : 2020-08-20
Purpose: Convert PSII bin files to TIFF images
"""
import argparse
import os
import sys
import datetime
import logging
from typing import Optional
import numpy as np
from terrautils.spatial import scanalyzer_to_latlon
import json
import glob
from terrautils.formats import create_geotiff, create_image
from terrautils.spatial import geojson_to_tuples
# --------------------------------------------------
def get_args():
"""Get command-line arguments"""
parser = argparse.ArgumentParser(
description='Rock the Casbah',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('dir',
nargs='+',
type=str,
#metavar='str',
help='Directory containing bin files')
parser.add_argument('-m',
'--metadata',
help='Cleaned metadata file',
metavar='str',
type=str,
required=True)
parser.add_argument('-z',
'--zoffset',
help='Z-axis offset',
metavar='z-offset',
type=float,
required=True)# Check with gantry operator for correct height for specified season
parser.add_argument('-o',
'--outdir',
help='TIF output directory',
metavar='str',
type=str,
default='bin2tif_out')
return parser.parse_args()
# --------------------------------------------------
def get_boundingbox(metadata):
args = get_args()
z_offset = args.zoffset
with open(metadata) as f:
meta = json.load(f)['lemnatec_measurement_metadata']
loc_gantry_x = float(meta['sensor_fixed_metadata']['location in camera box x [m]'])
loc_gantry_y = float(meta['sensor_fixed_metadata']['location in camera box y [m]'])
loc_gantry_z = float(meta['sensor_fixed_metadata']['location in camera box z [m]'])
gantry_x = float(meta['gantry_system_variable_metadata']['position x [m]']) + loc_gantry_x
gantry_y = float(meta['gantry_system_variable_metadata']['position y [m]']) + loc_gantry_y
gantry_z = float(meta['gantry_system_variable_metadata']['position z [m]']) + z_offset + loc_gantry_z#offset in m
fov_x, fov_y = meta['sensor_fixed_metadata']['field of view X [m]'], meta['sensor_fixed_metadata']['field of view y [m]']
# img_height = int(meta['sensor_fixed_metadata']['camera resolution'].split('x')[0])
# img_width = int(meta['sensor_fixed_metadata']['camera resolution'].split('x')[1])
B = gantry_z
A_x = np.arctan((0.5*float(fov_x))/2)
A_y = np.arctan((0.5*float(fov_y))/2)
L_x = 2*B*np.tan(A_x)
L_y = 2*B*np.tan(A_y)
x_n = gantry_x + (L_x/2)
x_s = gantry_x - (L_x/2)
y_w = gantry_y + (L_y/2)
y_e = gantry_y - (L_y/2)
bbox_nw_latlon = scanalyzer_to_latlon(x_n, y_w)
bbox_se_latlon = scanalyzer_to_latlon(x_s, y_e)
# TERRA-REF
lon_shift = 0.000020308287
# Drone
lat_shift = 0.000018292 #0.000015258894
b_box = (bbox_se_latlon[0] - lat_shift,
bbox_nw_latlon[0] - lat_shift,
bbox_nw_latlon[1] + lon_shift,
bbox_se_latlon[1] + lon_shift )
return b_box
# --------------------------------------------------
def main():
"""Read each bin file and convert to TIFF format"""
args = get_args()
start_timestamp = datetime.datetime.utcnow()
path = os.getcwd()
if not os.path.isdir(args.outdir):
os.makedirs(args.outdir)
file_endings = ["{0:0>4}.bin".format(i) for i in range(0, 101)]
img_width, img_height = 1216, 1936
gps_bounds = get_boundingbox(args.metadata)
png_frames = {}
cnt = 0
for one_file in args.dir:
if one_file[-8:] in file_endings:
cnt += 1
pixels = np.fromfile(one_file, np.dtype('uint8')).reshape(-1, img_height)
reshape_pixels = np.rot90(pixels, 3)
tif_filename = os.path.join(args.outdir, os.path.basename(one_file.replace('.bin', '.tif')))
create_geotiff(reshape_pixels, gps_bounds, tif_filename, None, True, None , None, compress=True)
# --------------------------------------------------
if __name__ == '__main__':
main()