forked from centreborelli/rpcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_file_readers.py
292 lines (225 loc) · 10.4 KB
/
rpc_file_readers.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Copyright (C) 2015-19, Carlo de Franchis <carlo.de-franchis@cmla.ens-cachan.fr>
# Copyright (C) 2015-19, Gabriele Facciolo <facciolo@cmla.ens-cachan.fr>
# Copyright (C) 2015-19, Enric Meinhardt <enric.meinhardt@cmla.ens-cachan.fr>
from xml.etree import ElementTree
def read_rpc_file(rpc_file):
"""
Read RPC from a file deciding the format from the extension of the filename.
xml : spot6, pleiades, worldview
txt (others) : ikonos
Args:
rpc_file: RPC sidecar file path
Returns:
dictionary read from the RPC file, or an empty dict if fail
"""
with open(rpc_file) as f:
rpc_content = f.read()
if rpc_file.lower().endswith('xml'):
try:
rpc = read_rpc_xml(rpc_content)
except NotImplementedError:
raise NotImplementedError('XML file {} not supported'.format(rpc_file))
else:
# we assume that non xml rpc files follow the ikonos convention
rpc = read_rpc_ikonos(rpc_content)
return rpc
def read_rpc_ikonos(rpc_content):
"""
Read RPC file assuming the ikonos format
Args:
rpc_content: content of RPC sidecar file path read as a string
Returns:
dictionary read from the RPC file
"""
import re
lines = rpc_content.split('\n')
d = {}
for l in lines:
ll = l.split()
if len(ll) > 1:
k = re.sub(r"[^a-zA-Z0-9_]","",ll[0])
d[k] = ll[1]
def parse_coeff(dic, prefix, indices):
""" helper function"""
return ' '.join([dic["%s_%s" % (prefix, str(x))] for x in indices])
d['SAMP_NUM_COEFF'] = parse_coeff(d, "SAMP_NUM_COEFF", range(1, 21))
d['SAMP_DEN_COEFF'] = parse_coeff(d, "SAMP_DEN_COEFF", range(1, 21))
d['LINE_NUM_COEFF'] = parse_coeff(d, "LINE_NUM_COEFF", range(1, 21))
d['LINE_DEN_COEFF'] = parse_coeff(d, "LINE_DEN_COEFF", range(1, 21))
# if the LON/LAT coefficients are present then it must be an "extended ikonos"
if 'LON_NUM_COEFF_1' in d:
d['LON_NUM_COEFF'] = parse_coeff(d, "LON_NUM_COEFF", range(1, 21))
d['LON_DEN_COEFF'] = parse_coeff(d, "LON_DEN_COEFF", range(1, 21))
d['LAT_NUM_COEFF'] = parse_coeff(d, "LAT_NUM_COEFF" , range(1, 21))
d['LAT_DEN_COEFF'] = parse_coeff(d, "LAT_DEN_COEFF" , range(1, 21))
return d
def read_rpc_xml(rpc_content):
"""
Read RPC file assuming the XML format and determine whether it's a pleiades, spot-6 or worldview image
Args:
rpc_content: content of RPC sidecar file path read as a string (XML format)
Returns:
dictionary read from the RPC file
Raises:
NotImplementedError: if the file format is not handled (the expected keys are not found)
"""
# parse the xml file content
tree = ElementTree.fromstring(rpc_content)
# determine wether it's a pleiades, spot-6 or worldview image
a = tree.find('Metadata_Identification/METADATA_PROFILE') # PHR_SENSOR
b = tree.find('IMD/IMAGE/SATID') # WorldView
parsed_rpc = None
if a is not None:
if a.text in ['PHR_SENSOR', 'S6_SENSOR', 'S7_SENSOR']:
parsed_rpc = read_rpc_xml_pleiades(tree)
elif a.text in ['PNEO_SENSOR']:
parsed_rpc = read_rpc_xml_pleiades_neo(tree)
elif b is not None:
if b.text == 'WV02' or b.text == 'WV01' or b.text == 'WV03':
parsed_rpc = read_rpc_xml_worldview(tree)
if not parsed_rpc:
raise NotImplementedError()
return parsed_rpc
def read_rpc_xml_pleiades(tree):
"""
Read RPC fields from a parsed XML tree assuming the pleiades, spot-6 XML format
Also reads the inverse model parameters
Args:
tree: parsed XML tree
Returns:
dictionary read from the RPC file, or empty dict in case of failure
"""
m = {}
def parse_coeff(element, prefix, indices):
""" helper function"""
return ' '.join([element.find("%s_%s" % (prefix, str(x))).text for x in indices])
# direct model (LOCALIZATION)
d = tree.find('Rational_Function_Model/Global_RFM/Direct_Model')
m['LON_NUM_COEFF'] = parse_coeff(d, "SAMP_NUM_COEFF", range(1, 21))
m['LON_DEN_COEFF'] = parse_coeff(d, "SAMP_DEN_COEFF", range(1, 21))
m['LAT_NUM_COEFF'] = parse_coeff(d, "LINE_NUM_COEFF", range(1, 21))
m['LAT_DEN_COEFF'] = parse_coeff(d, "LINE_DEN_COEFF", range(1, 21))
#m['ERR_BIAS'] = parse_coeff(d, "ERR_BIAS", ['X', 'Y'])
## inverse model (PROJECTION)
i = tree.find('Rational_Function_Model/Global_RFM/Inverse_Model')
m['SAMP_NUM_COEFF'] = parse_coeff(i, "SAMP_NUM_COEFF", range(1, 21))
m['SAMP_DEN_COEFF'] = parse_coeff(i, "SAMP_DEN_COEFF", range(1, 21))
m['LINE_NUM_COEFF'] = parse_coeff(i, "LINE_NUM_COEFF", range(1, 21))
m['LINE_DEN_COEFF'] = parse_coeff(i, "LINE_DEN_COEFF", range(1, 21))
m['ERR_BIAS'] = parse_coeff(i, "ERR_BIAS", ['ROW', 'COL'])
# validity domains
v = tree.find('Rational_Function_Model/Global_RFM/RFM_Validity')
#vd = v.find('Direct_Model_Validity_Domain')
#m.firstRow = float(vd.find('FIRST_ROW').text)
#m.firstCol = float(vd.find('FIRST_COL').text)
#m.lastRow = float(vd.find('LAST_ROW').text)
#m.lastCol = float(vd.find('LAST_COL').text)
#vi = v.find('Inverse_Model_Validity_Domain')
#m.firstLon = float(vi.find('FIRST_LON').text)
#m.firstLat = float(vi.find('FIRST_LAT').text)
#m.lastLon = float(vi.find('LAST_LON').text)
#m.lastLat = float(vi.find('LAST_LAT').text)
# scale and offset
# the -1 in line and column offsets is due to Pleiades RPC convention
# that states that the top-left pixel of an image has coordinates
# (1, 1)
m['LINE_OFF' ] = float(v.find('LINE_OFF').text) - 1
m['SAMP_OFF' ] = float(v.find('SAMP_OFF').text) - 1
m['LAT_OFF' ] = float(v.find('LAT_OFF').text)
m['LONG_OFF' ] = float(v.find('LONG_OFF').text)
m['HEIGHT_OFF' ] = float(v.find('HEIGHT_OFF').text)
m['LINE_SCALE' ] = float(v.find('LINE_SCALE').text)
m['SAMP_SCALE' ] = float(v.find('SAMP_SCALE').text)
m['LAT_SCALE' ] = float(v.find('LAT_SCALE').text)
m['LONG_SCALE' ] = float(v.find('LONG_SCALE').text)
m['HEIGHT_SCALE'] = float(v.find('HEIGHT_SCALE').text)
return m
def read_rpc_xml_pleiades_neo(tree):
"""
Read RPC fields from a parsed XML tree assuming the pleiades NEO XML format
Also reads the inverse model parameters
Args:
tree: parsed XML tree
Returns:
dictionary read from the RPC file, or empty dict in case of failure
"""
m = {}
def parse_coeff(element, prefix, indices):
""" helper function"""
return ' '.join([element.find("%s_%s" % (prefix, str(x))).text for x in indices])
# direct model (LOCALIZATION)
d = tree.find('Rational_Function_Model/Global_RFM/ImagetoGround_Values')
m['LON_NUM_COEFF'] = parse_coeff(d, "LON_NUM_COEFF", range(1, 21))
m['LON_DEN_COEFF'] = parse_coeff(d, "LON_DEN_COEFF", range(1, 21))
m['LAT_NUM_COEFF'] = parse_coeff(d, "LAT_NUM_COEFF", range(1, 21))
m['LAT_DEN_COEFF'] = parse_coeff(d, "LAT_DEN_COEFF", range(1, 21))
#m['ERR_BIAS'] = parse_coeff(d, "ERR_BIAS", ['X', 'Y'])
## inverse model (PROJECTION)
i = tree.find('Rational_Function_Model/Global_RFM/GroundtoImage_Values')
m['SAMP_NUM_COEFF'] = parse_coeff(i, "SAMP_NUM_COEFF", range(1, 21))
m['SAMP_DEN_COEFF'] = parse_coeff(i, "SAMP_DEN_COEFF", range(1, 21))
m['LINE_NUM_COEFF'] = parse_coeff(i, "LINE_NUM_COEFF", range(1, 21))
m['LINE_DEN_COEFF'] = parse_coeff(i, "LINE_DEN_COEFF", range(1, 21))
m['ERR_BIAS'] = parse_coeff(i, "ERR_BIAS", ['ROW', 'COL'])
# validity domains
v = tree.find('Rational_Function_Model/Global_RFM/RFM_Validity')
vd = v.find('ImagetoGround_Validity_Domain')
m['FIRST_ROW'] = float(vd.find('FIRST_ROW').text)
m['FIRST_COL'] = float(vd.find('FIRST_COL').text)
#m.lastRow = float(vd.find('LAST_ROW').text)
#m.lastCol = float(vd.find('LAST_COL').text)
#vi = v.find('Inverse_Model_Validity_Domain')
#m.firstLon = float(vi.find('FIRST_LON').text)
#m.firstLat = float(vi.find('FIRST_LAT').text)
#m.lastLon = float(vi.find('LAST_LON').text)
#m.lastLat = float(vi.find('LAST_LAT').text)
# scale and offset
# the -1 in line and column offsets is due to Pleiades RPC convention
# that states that the top-left pixel of an image has coordinates
# (1, 1)
m['LINE_OFF' ] = float(v.find('LINE_OFF').text) - 1 - m['FIRST_ROW'] + m['FIRST_COL']
m['SAMP_OFF' ] = float(v.find('SAMP_OFF').text) - 1 + m['FIRST_COL']
m['LAT_OFF' ] = float(v.find('LAT_OFF').text)
m['LONG_OFF' ] = float(v.find('LONG_OFF').text)
m['HEIGHT_OFF' ] = float(v.find('HEIGHT_OFF').text)
m['LINE_SCALE' ] = float(v.find('LINE_SCALE').text)
m['SAMP_SCALE' ] = float(v.find('SAMP_SCALE').text)
m['LAT_SCALE' ] = float(v.find('LAT_SCALE').text)
m['LONG_SCALE' ] = float(v.find('LONG_SCALE').text)
m['HEIGHT_SCALE'] = float(v.find('HEIGHT_SCALE').text)
return m
def read_rpc_xml_worldview(tree):
"""
Read RPC fields from a parsed XML tree assuming the worldview XML format
Args:
tree: parsed XML tree
Returns:
dictionary read from the RPC file, or empty dict in case of failure
"""
m = {}
# inverse model (PROJECTION)
im = tree.find('RPB/IMAGE')
l = im.find('LINENUMCOEFList/LINENUMCOEF')
m['LINE_NUM_COEFF'] = l.text
l = im.find('LINEDENCOEFList/LINEDENCOEF')
m['LINE_DEN_COEFF'] = l.text
l = im.find('SAMPNUMCOEFList/SAMPNUMCOEF')
m['SAMP_NUM_COEFF'] = l.text
l = im.find('SAMPDENCOEFList/SAMPDENCOEF')
m['SAMP_DEN_COEFF'] = l.text
m['ERR_BIAS'] = float(im.find('ERRBIAS').text)
# scale and offset
m['LINE_OFF' ] = float(im.find('LINEOFFSET').text)
m['SAMP_OFF' ] = float(im.find('SAMPOFFSET').text)
m['LAT_OFF' ] = float(im.find('LATOFFSET').text)
m['LONG_OFF' ] = float(im.find('LONGOFFSET').text)
m['HEIGHT_OFF' ] = float(im.find('HEIGHTOFFSET').text)
m['LINE_SCALE' ] = float(im.find('LINESCALE').text)
m['SAMP_SCALE' ] = float(im.find('SAMPSCALE').text)
m['LAT_SCALE' ] = float(im.find('LATSCALE').text)
m['LONG_SCALE' ] = float(im.find('LONGSCALE').text)
m['HEIGHT_SCALE'] = float(im.find('HEIGHTSCALE').text)
# # image dimensions
# m.lastRow = int(tree.find('IMD/NUMROWS').text)
# m.lastCol = int(tree.find('IMD/NUMCOLUMNS').text)
return m