-
Notifications
You must be signed in to change notification settings - Fork 3
/
sentinel_manifest_metadata_extractor.py
658 lines (505 loc) · 36.2 KB
/
sentinel_manifest_metadata_extractor.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
'''Python script for extracting metadata fields from sentinel manifests or from opensearch API
Sentinel 1
Sentinel 2
Assumes there is a location where several manifests are stored
February 2016'''
import lxml
from lxml import etree
import os
import re
from xml.dom import minidom
import requests
from requests.auth import HTTPBasicAuth
import time
from datetime import date, timedelta
import sys
#TODO add logging instead of printing
#import logging
#logging.basicConfig(level=logging.DEBUG)
#https://docs.python.org/2/howto/logging.html
DHUS_USER ='guest'
DHUS_PASS ='guest'
FILEPATH = "/tmp/harvested/manifests/"
class SentinelMetadataExtractor:
filepath = ''
tree = ""
root = ""
file_error_count=0
filenames_error=[]
total_files=0
productMetadata={}
productMetadataEtrees={}
def __init__(self, filepath=FILEPATH):
self.filepath = filepath
def _downloadProduct(self,filename,link,outputFolder,user=DHUS_USER, password=DHUS_PASS):
'''download product and shows a progress bar'''
print 'Download product from ' + link
with open(filename, "wb") as f:
print "Downloading %s" % filename
response = requests.get(link, stream=True, auth=HTTPBasicAuth(user, password),verify=True)
total_length = response.headers.get('content-length')
if total_length is None: # no content length header
f.write(response.content)
else:
dl = 0
total_length = int(total_length)
for data in response.iter_content():
dl += len(data)
f.write(data)
done = int(50 * dl / total_length)
sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)) )
sys.stdout.flush()
print 'Finished Download'
def extractMetadataFromAPIForToday(self,downloadManifests = False,user=DHUS_USER, password=DHUS_PASS, outputFolder=FILEPATH):
'''queries the dhus api and uses its metadata,
can download manifests and stores the manifests at specified location (filepath on init)'''
#odata - url = "https://scihub.copernicus.eu/apihub/odata/v1/Products?$filter=year(IngestionDate) eq 2016 and month(IngestionDate) eq 02 and day(IngestionDate) eq 1&$skip=0&$top=99"
if downloadManifests: #if folder doesnt exist and we want to write to file, exit
if not self._folderExists(outputFolder):
return None
today = time.strftime('%Y-%m-%d')
yesterday_d = date.today() - timedelta(1)
yesterday = yesterday_d.strftime('%Y-%m-%d')
NOW = str(today)+'T00:00:00.000Z'#Thh:mm:ss.SSSZ
YESTERDAY = str(yesterday)+'T00:00:00.000Z'#Thh:mm:ss.SSSZ
##first request to get total results
url = "https://scihub.copernicus.eu/dhus/search?q=ingestionDate:["+YESTERDAY+" TO "+NOW+"]"
r = requests.get(url, auth=HTTPBasicAuth(user, password),verify=False)
if r.status_code != 200:
print 'Wrong authentication? status code != 200'
return None
#print r.text
xml = minidom.parseString(r.text)
total_results = xml.getElementsByTagName("opensearch:totalResults")[0].firstChild.data
#total_results = str(10)
print 'found ' +total_results +' results'
for i in range(0, int(total_results), 100):
url = "https://scihub.copernicus.eu/dhus/search?q=ingestionDate:["+YESTERDAY+" TO "+NOW+"]&start="+str(i)+"&rows=99"
r = requests.get(url, auth=HTTPBasicAuth(user, password),verify=False)
xml = minidom.parseString(r.text)
products=xml.getElementsByTagName("entry")
for prod in products:
metadata = {}
ident=prod.getElementsByTagName("id")[0].firstChild.data
#print ident
link=prod.getElementsByTagName("link")[0].attributes.items()[0][1]
#print link
title=prod.getElementsByTagName("title")[0].firstChild.data
#print title
metadata['uuid'] = ident
print metadata['uuid']
metadata['downloadLink'] = link
metadata['thumbnail'] = "https://"+user+":"+password+"@scihub.copernicus.eu/apihub/odata/v1/Products('"+ident+"')/Products('Quicklook')/$value"
if downloadManifests:
self._downloadProduct(title.lower()+'.SAFE.zip',link,outputFolder,user,password)
for node in prod.getElementsByTagName("date"):
(name,value)=node.attributes.items()[0]
if value=="beginposition":
metadata['StartTime'] = node.firstChild.data
if value=="endposition":
metadata['StopTime'] = node.firstChild.data
for node in prod.getElementsByTagName("str"):
(name,value)=node.attributes.items()[0]
#metadata[value]=''
#print name,value
if value=="filename":
#filename= str(node.toxml()).split('>')[1].split('<')[0]#ugly, but minidom is not straightforward
filename = node.firstChild.data
metadata['manifestLink'] = "https://scihub.copernicus.eu/apihub/odata/v1/Products('" + ident +"')/Nodes('"+filename+"')/Nodes('manifest.safe')/$value"
if value=="footprint":
#assuming all footprints are polygons
footprint = node.firstChild.data
metadata['Coordinates'] = self._parseCoordinates(self._transformSolrCoordsToSAFECoords(footprint))
if value=="platformname":
metadata['FamilyName'] = node.firstChild.data
if value=="instrumentshortname":
metadata['InstrumentFamilyName'] = node.firstChild.data
if value=="instrumentname":
metadata['InstrumentName'] = node.firstChild.data
if value=="polarisationmode":
metadata['TransmitterReceiverPolarisation'] = node.firstChild.data
if value=="sensoroperationalmode":
metadata['InstrumentMode'] = node.firstChild.data
if value=="productclass":
metadata['ProductClass'] = node.firstChild.data
if value=="producttype":
metadata['ProductType'] = node.firstChild.data
if value=="productconsolidation":
metadata['ProductConsolidation'] = node.firstChild.data
if value=="acquisitiontype":
metadata['AcquisitionType'] = node.firstChild.data
if value=="orbitdirection":
metadata['OrbitDirection'] = node.firstChild.data
if value=="swathidentifier":
metadata['Swath'] = node.firstChild.data
self.productMetadata[title.lower()+'.manifest.safe'] = metadata #.manifest.safe to mantain consistency across class
print 'finished parsing api results'
def extractMetadataFromManifestFiles(self):
'''main method for metadata extarction'''
for filename in os.listdir(self.filepath):
self.total_files = self.total_files +1
if filename.endswith(".safe") or filename.endswith(".SAFE"):
#print "processing file - "+str(filename)
try:
self.tree = etree.parse(self.filepath+"/"+str(filename))
self.root = self.tree.getroot()
except lxml.etree.XMLSyntaxError:
print 'File XML Syntax Error'
self.file_error_count = self.file_error_count+1
self.filenames_error.append(str(filename))
continue
#####all this names represent files succesfully parsed with this program
raw = ['S1A_S3_RAW__0SDH','S1A_IW_RAW__0SSV','S1A_IW_RAW__0SSH','S1A_IW_RAW__0SDV','S1A_IW_RAW__0SDH','S1A_EW_RAW__0SDH','S1A_EW_RAW__0SSH',
'S1A_EW_RAW__0SDV','S1A_S1_RAW__0SSV','S1A_S6_RAW__0SSV','S1A_S5_RAW__0SSV','S1A_S3_RAW__0SDV','S1A_S1_RAW__0SDH','S1A_S4_RAW__0SSV',
'S1A_S3_RAW__0SSV']
gr = ['S1A_S3_GRDH_1SDH','S1A_IW_SLC__1SSV','S1A_IW__1SSH','S1A_IW_SLC__1SDV','S1A_IW_SLC__1SDH','S1A_IW_GRDH_1SSV','S1A_IW_GRDH_1SSH',
'S1A_IW_GRDH_1SDV','S1A_IW_GRDH_1SDH','S1A_EW_GRDM_1SSH','S1A_EW_GRDM_1SDV','S1A_EW_GRDH_1SDH','S1A_EW_GRDM_1SDH',
'S1A_IW_SLC__1SSH','S1A_S5_GRDH_1SSV','S1A_EW_GRDH_1SSH','S1A_S5_SLC__1SSV','S1A_IW_GRDH_1SDV','S1A_S4_GRDH_1SSV','S1A_S3_GRDH_1SSV',
'S1A_S3_GRDH_1SDV','S1A_S4_SLC__1SSV','S1A_S3_SLC__1SSV','S1A_S1_GRDH_1SDH','S1A_S3_SLC__1SDV']
ocn = ['S1A_IW_OCN__2SDV','S1A_WV_OCN__2SSV','S1A_IW_OCN__2SSV','S1A_EW_OCN__2SDH','S1A_IW_OCN__2SDH']
s2 = ['S2A_OPER_PRD_MSIL1C_PDMC']
processed = False
for sentinel_name in gr:
if filename.startswith(sentinel_name):
processed = True
self.productMetadata[filename.lower()] = self._extractGR()
break
for sentinel_name in raw:
if filename.startswith(sentinel_name):
processed = True
self.productMetadata[filename.lower()] = self._extractRAW()
break
for sentinel_name in ocn:
if filename.startswith(sentinel_name):
processed = True
self.productMetadata[filename.lower()] = self._extractIWOCN()
break
for sentinel_name in s2:
if filename.startswith(sentinel_name):
processed = True
self.productMetadata[filename.lower()] = self._extractS2()
break
if not processed:
self.file_error_count = self.file_error_count+1
self.filenames_error.append(str(filename))
print "FILE NOT IN KNOWN FILES - "+str(filename)
else:
print "File not ending with .SAFE or .safe - "+str(filename)
print "All Done"
def _transformSolrCoordsToSAFECoords(self, coords):
'''receives coordinates in solr format and parses to the one in SAFE format for further processing
FROM #POLYGON ((123.3108 8.0611,123.0589 9.3117,120.8469 9.0181,121.1061 7.7648,123.3108 8.0611,123.3108 8.0611))
TO 8.0611,123.3108 9.3117,123.0589 9.0181,120.8469 7.7648,121.1061 8.0611,123.3108
'''
#remove (( and )), split by commas
regexed = re.findall("POLYGON\s\D\D(.*)\D\D", coords)[0] #[0], grab first (and only) group
coords_split = regexed.split(',')
#coords_split = str(re.findall("POLYGON\s\D\D(.*)\D\D", coords)).split(',')
#add commas in whitespaces, and a whitespace to separate each pair, result is something like this 8.0611,123.3108 9.3117,123.0589 9.0181,120.8469 7.7648,121.1061 8.0611,123.3108
coords_united = ''
for c in range(0,len(coords_split)):
replaced = coords_split[c].replace(" ", ",")
coords_united = coords_united + replaced
if c != (len(coords_split)-1):
coords_united = coords_united +" "
return str(coords_united)
def _parseCoordinates(self,coordinates):
'''parse coordinates to a json format[[[lat1,long1],[lat2,long2],...]]'''
#in '58.893013,-65.056816 59.638775,-57.844292 55.804028,-56.761806 55.089973,-63.272209'
#out [['58.893013', '-65.056816'], ['59.638775', '-57.844292'], ['55.804028', '-56.761806'], ['55.089973', '-63.272209']]
final_list = []
coordsx=[] #lat
coordsy=[] #long
split = re.split(' |,',coordinates.strip())
#print split
for c in range(0,len(split)):
if c % 2 == 0: #pair
coordsx.append(split[c])
else:
coordsy.append(split[c])
for c in range(0,len(coordsx)):
final_list.append([str(coordsx[c]),str(coordsy[c])])
#print final_list
return final_list#lat,long
def _extractS2(self):
metadata = {}
###############S1A_S3_GRDH_1SDH###############S1A_IW_SLC__1SSV###############S1A_IW__1SSH###############S1A_IW_SLC__1SDV
###############S1A_IW_SLC__1SDH###############S1A_IW_GRDH_1SSV###############S1A_IW_GRDH_1SSH###############S1A_IW_GRDH_1SDV
###############S1A_IW_GRDH_1SDH###############S1A_EW_GRDM_1SSH###############S1A_EW_GRDM_1SDV###############S1A_EW_GRDH_1SDH
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:acquisitionPeriod/safe:startTime',self.root.nsmap)
metadata['StartTime'] = extracted[0].text
#
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:platform/safe:familyName',self.root.nsmap)
metadata['FamilyName'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:platform/safe:number',self.root.nsmap)
metadata['FamilyNameNumber'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:platform/safe:instrument/safe:familyName',self.root.nsmap)
metadata['InstrumentFamilyName'] = extracted[0].text
#
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/{http://www.esa.int/safe/sentinel/1.1}frameSet/{http://www.esa.int/safe/sentinel/1.1}footPrint/{http://www.opengis.net/gml}coordinates',self.root.nsmap)
metadata['Coordinates'] = self._parseCoordinates(extracted[0].text)
#
return metadata
def _extractGR(self):
metadata = {}
###############S1A_S3_GRDH_1SDH###############S1A_IW_SLC__1SSV###############S1A_IW__1SSH###############S1A_IW_SLC__1SDV
###############S1A_IW_SLC__1SDH###############S1A_IW_GRDH_1SSV###############S1A_IW_GRDH_1SSH###############S1A_IW_GRDH_1SDV
###############S1A_IW_GRDH_1SDH###############S1A_EW_GRDM_1SSH###############S1A_EW_GRDM_1SDV###############S1A_EW_GRDH_1SDH
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:acquisitionPeriod/safe:startTime',self.root.nsmap)
metadata['StartTime'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:acquisitionPeriod/safe:stopTime',self.root.nsmap)
metadata['StopTime'] = extracted[0].text
#
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:platform/safe:familyName',self.root.nsmap)
metadata['FamilyName'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:platform/safe:number',self.root.nsmap)
metadata['FamilyNameNumber'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:platform/safe:instrument/safe:familyName',self.root.nsmap)
metadata['InstrumentFamilyName'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:platform/safe:instrument/safe:extension/s1sarl1:instrumentMode/s1sarl1:mode',self.root.nsmap)
metadata['InstrumentMode'] = extracted[0].text
#
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sarl1:standAloneProductInformation/s1sarl1:productClass',self.root.nsmap)
metadata['ProductClass'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sarl1:standAloneProductInformation/s1sarl1:productClassDescription',self.root.nsmap)
metadata['ProductClassDescription'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sarl1:standAloneProductInformation/s1sarl1:productComposition',self.root.nsmap)
metadata['ProductComposition'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sarl1:standAloneProductInformation/s1sarl1:productType',self.root.nsmap)
metadata['ProductType'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sarl1:standAloneProductInformation/s1sarl1:transmitterReceiverPolarisation',self.root.nsmap)
if len(extracted) > 1:
metadata['TransmitterReceiverPolarisation'] = extracted[0].text + "/" + extracted[1].text
else:
metadata['TransmitterReceiverPolarisation'] = extracted[0].text
#
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:frameSet/safe:frame/safe:footPrint/gml:coordinates',self.root.nsmap)
metadata['Coordinates'] = self._parseCoordinates(extracted[0].text)
#
return metadata
def _extractRAW(self):
metadata = {}
###############S1A_S3_RAW__0SDH###############S1A_IW_RAW__0SSV###############S1A_IW_RAW__0SSH###############S1A_IW_RAW__0SDV
###############S1A_IW_RAW__0SDH###############S1A_EW_RAW__0SDH
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/{http://www.esa.int/safe/sentinel-1.0}acquisitionPeriod/{http://www.esa.int/safe/sentinel-1.0}startTime',self.root.nsmap)
metadata['StartTime'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/{http://www.esa.int/safe/sentinel-1.0}acquisitionPeriod/{http://www.esa.int/safe/sentinel-1.0}stopTime',self.root.nsmap)
metadata['StopTime'] = extracted[0].text
#
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/{http://www.esa.int/safe/sentinel-1.0}platform/{http://www.esa.int/safe/sentinel-1.0}familyName',self.root.nsmap)
metadata['FamilyName'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/{http://www.esa.int/safe/sentinel-1.0}platform/{http://www.esa.int/safe/sentinel-1.0}number',self.root.nsmap)
metadata['FamilyNameNumber'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/{http://www.esa.int/safe/sentinel-1.0}platform/{http://www.esa.int/safe/sentinel-1.0}instrument/{http://www.esa.int/safe/sentinel-1.0}familyName',self.root.nsmap)
metadata['InstrumentFamilyName'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/{http://www.esa.int/safe/sentinel-1.0}platform/{http://www.esa.int/safe/sentinel-1.0}instrument/{http://www.esa.int/safe/sentinel-1.0}extension/s1sar:instrumentMode/s1sar:mode',self.root.nsmap)
metadata['InstrumentMode'] = extracted[0].text
#
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sar:standAloneProductInformation/s1sar:productClass',self.root.nsmap)
metadata['ProductClass'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sar:standAloneProductInformation/s1sar:productClassDescription',self.root.nsmap)
metadata['ProductClassDescription'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sar:standAloneProductInformation/s1sar:productConsolidation',self.root.nsmap)
metadata['ProductConsolidation'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sar:standAloneProductInformation/s1sar:transmitterReceiverPolarisation',self.root.nsmap)
if len(extracted) > 1:
metadata['TransmitterReceiverPolarisation'] = extracted[0].text + "/" + extracted[1].text
else:
metadata['TransmitterReceiverPolarisation'] = extracted[0].text
#
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/{http://www.esa.int/safe/sentinel-1.0}frameSet/{http://www.esa.int/safe/sentinel-1.0}frame/{http://www.esa.int/safe/sentinel-1.0}footPrint/{http://www.opengis.net/gml}coordinates',self.root.nsmap)
metadata['Coordinates'] = self._parseCoordinates(extracted[0].text)
return metadata
def _extractIWOCN(self):
metadata = {}
###############S1A_IW_OCN__2SDV
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:acquisitionPeriod/safe:startTime',self.root.nsmap)
metadata['StartTime'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:acquisitionPeriod/safe:stopTime',self.root.nsmap)
metadata['StopTime'] = extracted[0].text
#
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:platform/safe:familyName',self.root.nsmap)
metadata['FamilyName'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:platform/safe:number',self.root.nsmap)
metadata['FamilyNameNumber'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:platform/safe:instrument/safe:familyName',self.root.nsmap)
metadata['InstrumentFamilyName'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:platform/safe:instrument/safe:extension/s1sarl2:instrumentMode/s1sarl2:mode',self.root.nsmap)
metadata['InstrumentMode'] = extracted[0].text
#
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sarl2:standAloneProductInformation/s1sarl2:productClass',self.root.nsmap)
metadata['ProductClass'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sarl2:standAloneProductInformation/s1sarl2:productClassDescription',self.root.nsmap)
metadata['ProductClassDescription'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sarl2:standAloneProductInformation/s1sarl2:productComposition',self.root.nsmap)
metadata['ProductComposition'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sarl2:standAloneProductInformation/s1sarl2:productType',self.root.nsmap)
metadata['ProductType'] = extracted[0].text
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/s1sarl2:standAloneProductInformation/s1sarl2:transmitterReceiverPolarisation',self.root.nsmap)
if len(extracted) > 1:
metadata['TransmitterReceiverPolarisation'] = extracted[0].text + "/" + extracted[1].text
else:
metadata['TransmitterReceiverPolarisation'] = extracted[0].text
#
extracted = self.root.findall('./metadataSection/metadataObject/metadataWrap/xmlData/safe:frameSet/safe:frame/safe:footPrint/gml:coordinates',self.root.nsmap)
metadata['Coordinates'] = self._parseCoordinates(extracted[0].text)
return metadata
def checkAllFilesParsed(self):
''' check if any files processed had errors and outputs their name, along with the number of total files processed'''
print 'Processed ' + str(self.total_files) + ' files'
print '\n\n'
print str(self.file_error_count) + " files had errors"
print 'Printing filenames'
for j in range(0,len(self.filenames_error)):
print self.filenames_error[j]
def getProductsMetadata(self):
'''return all ingested products metadata
the actual metadata, dict in which keys are the filenames in lowercase, values are a dict of keys,values
'''
return self.productMetadata
def getXMLStringMetadataInspire(self, template='inspire_template.xml'):
'''return all ingested products metadata in etree
the actual metadata, dict in which keys are the filenames in lowercase, values are an lxml etree
'''
for productName in self.productMetadata.keys(): #generate inspires for all metadata harvested
#write to manifests-inspire, based on the template, a xml inspire metadata document
inspireetree = self.generateInspireFromTemplate(productName,template,'', False)
self.productMetadataEtrees[productName] = etree.tostring(inspireetree,encoding='utf-8', pretty_print=True)
return self.productMetadataEtrees
def _getBoundingBox(self,coords):
'''returns a bounding box for a given set of coordinates
'''
##parse coords
coordsx=[] #lat
coordsy=[] #long
for c in range(0,len(coords)):
coordsx.append(coords[c][0])
coordsy.append(coords[c][1])
xmax = max(coordsx);
xmin = min(coordsx);
ymax = max(coordsy);
ymin = min(coordsy);
#lat max = north bound lat
#lat min = south bound lat
#long max = east bound long
#ong min = west bound long
return [xmax,xmin,ymax,ymin]
def _folderExists(self,outputFolder):
'''check if path exists and then check if path given is a folder, returns boolean'''
if not os.path.exists(outputFolder):
print 'Folder does not exist, creating '+outputFolder +' .'
try:
os.makedirs(outputFolder)
return True
except:
print 'Folder creation not succesfull, maybe you do not have permissions'
return False
try:
os.path.isdir(outputFolder)
return True
except:
print 'given outputFolder path is not a folder'
return False
return False
def generateInspireFromTemplate(self, metadata_key, template='inspire_template.xml', outputFolder = '/tmp/harvested/manifests-inspire/',writeToFile=False):
'''glued with spit and hammered code to generate inspire xml based on a custom template
general idea is to replace the values on the template with the ones from our metadata
check http://inspire-geoportal.ec.europa.eu/editor/
returns the etree
outputFolder can be empty if writeToFile is false
'''
#print metadata_key
if writeToFile: #if folder doesnt exist and we want to write to file, exit
if not self._folderExists(outputFolder):
return None
if metadata_key in self.productMetadata.keys():
try:
template_tree = etree.parse(template)
template_root = template_tree.getroot()
current_metadata = self.productMetadata[metadata_key]
#uuid
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}fileIdentifier/{http://www.isotc211.org/2005/gco}CharacterString',template_root.nsmap)
find[0].text = current_metadata['uuid']
#org name
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}contact/{http://www.isotc211.org/2005/gmd}CI_ResponsibleParty/{http://www.isotc211.org/2005/gmd}organisationName/{http://www.isotc211.org/2005/gco}CharacterString',template_root.nsmap)
find[0].text = 'ESA'
#org contact email
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}contact/{http://www.isotc211.org/2005/gmd}CI_ResponsibleParty/{http://www.isotc211.org/2005/gmd}contactInfo/{http://www.isotc211.org/2005/gmd}CI_Contact/{http://www.isotc211.org/2005/gmd}address/{http://www.isotc211.org/2005/gmd}CI_Address/{http://www.isotc211.org/2005/gmd}electronicMailAddress/{http://www.isotc211.org/2005/gco}CharacterString',template_root.nsmap)
find[0].text = '[email protected]'
#date created
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}dateStamp/{http://www.isotc211.org/2005/gco}Date',template_root.nsmap)
find[0].text = current_metadata['StartTime'][:10]
#filename
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}citation/{http://www.isotc211.org/2005/gmd}CI_Citation/{http://www.isotc211.org/2005/gmd}title/{http://www.isotc211.org/2005/gco}CharacterString',template_root.nsmap)
find[0].text = metadata_key.upper()
#date for metadata creation (this xml)
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}citation/{http://www.isotc211.org/2005/gmd}CI_Citation/{http://www.isotc211.org/2005/gmd}date/{http://www.isotc211.org/2005/gmd}CI_Date/{http://www.isotc211.org/2005/gmd}date/{http://www.isotc211.org/2005/gco}Date',template_root.nsmap)
find[0].text = current_metadata['StartTime'][:10]
#uuid
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}citation/{http://www.isotc211.org/2005/gmd}CI_Citation/{http://www.isotc211.org/2005/gmd}identifier/{http://www.isotc211.org/2005/gmd}RS_Identifier/{http://www.isotc211.org/2005/gmd}code/{http://www.isotc211.org/2005/gco}CharacterString',template_root.nsmap)
find[0].text = current_metadata['uuid']
#abstract
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}abstract/{http://www.isotc211.org/2005/gco}CharacterString',template_root.nsmap)
find[0].text = 'ESA Sentinel Product Metadata'
#responsible org
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}pointOfContact/{http://www.isotc211.org/2005/gmd}CI_ResponsibleParty/{http://www.isotc211.org/2005/gmd}organisationName/{http://www.isotc211.org/2005/gco}CharacterString',template_root.nsmap)
find[0].text = 'ESA'
#responsible org contact
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}pointOfContact/{http://www.isotc211.org/2005/gmd}CI_ResponsibleParty/{http://www.isotc211.org/2005/gmd}contactInfo/{http://www.isotc211.org/2005/gmd}CI_Contact/{http://www.isotc211.org/2005/gmd}address/{http://www.isotc211.org/2005/gmd}CI_Address/{http://www.isotc211.org/2005/gmd}electronicMailAddress/{http://www.isotc211.org/2005/gco}CharacterString',template_root.nsmap)
find[0].text = '[email protected]'
#keywords from INSPIER Data Themes
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}descriptiveKeywords/{http://www.isotc211.org/2005/gmd}MD_Keywords/{http://www.isotc211.org/2005/gmd}keyword/{http://www.isotc211.org/2005/gco}CharacterString',template_root.nsmap)
find[0].text = 'Orthoimagery'
#keywors from repositories
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}descriptiveKeywords/{http://www.isotc211.org/2005/gmd}MD_Keywords/{http://www.isotc211.org/2005/gmd}thesaurusName/{http://www.isotc211.org/2005/gmd}CI_Citation/{http://www.isotc211.org/2005/gmd}title/{http://www.isotc211.org/2005/gco}CharacterString',template_root.nsmap)
find[0].text = 'GEMET - INSPIRE themes, version 1.0'
#licenese conditions
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}resourceConstraints/{http://www.isotc211.org/2005/gmd}MD_Constraints/{http://www.isotc211.org/2005/gmd}useLimitation/{http://www.isotc211.org/2005/gco}CharacterString',template_root.nsmap)
find[0].text = 'Conditions unknown'
#topic category code
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}topicCategory/{http://www.isotc211.org/2005/gmd}MD_TopicCategoryCode',template_root.nsmap)
find[0].text = 'imageryBaseMapsEarthCover'
#start time of data
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}extent/{http://www.isotc211.org/2005/gmd}EX_Extent/{http://www.isotc211.org/2005/gmd}temporalElement/{http://www.isotc211.org/2005/gmd}EX_TemporalExtent/{http://www.isotc211.org/2005/gmd}extent/{http://www.opengis.net/gml}TimePeriod/{http://www.opengis.net/gml}beginPosition',template_root.nsmap)
find[0].text = current_metadata['StartTime'][:10]
#coords
bb = self._getBoundingBox(current_metadata['Coordinates'])
#lat max = north bound lat
#lat min = south bound lat
#long max = east bound long
#ong min = west bound long
#return [xmax,xmin,ymax,ymin]
#westBoundLongitude
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}extent/{http://www.isotc211.org/2005/gmd}EX_Extent/{http://www.isotc211.org/2005/gmd}geographicElement/{http://www.isotc211.org/2005/gmd}EX_GeographicBoundingBox/{http://www.isotc211.org/2005/gmd}westBoundLongitude/{http://www.isotc211.org/2005/gco}Decimal',template_root.nsmap)
find[0].text = bb[0]
#eastBoundLongitude
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}extent/{http://www.isotc211.org/2005/gmd}EX_Extent/{http://www.isotc211.org/2005/gmd}geographicElement/{http://www.isotc211.org/2005/gmd}EX_GeographicBoundingBox/{http://www.isotc211.org/2005/gmd}eastBoundLongitude/{http://www.isotc211.org/2005/gco}Decimal',template_root.nsmap)
find[0].text = bb[1]
#southBoundLatitude
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}extent/{http://www.isotc211.org/2005/gmd}EX_Extent/{http://www.isotc211.org/2005/gmd}geographicElement/{http://www.isotc211.org/2005/gmd}EX_GeographicBoundingBox/{http://www.isotc211.org/2005/gmd}southBoundLatitude/{http://www.isotc211.org/2005/gco}Decimal',template_root.nsmap)
find[0].text = bb[2]
#northBoundLatitude
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}extent/{http://www.isotc211.org/2005/gmd}EX_Extent/{http://www.isotc211.org/2005/gmd}geographicElement/{http://www.isotc211.org/2005/gmd}EX_GeographicBoundingBox/{http://www.isotc211.org/2005/gmd}northBoundLatitude/{http://www.isotc211.org/2005/gco}Decimal',template_root.nsmap)
find[0].text = bb[3]
#end time of data
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}identificationInfo/{http://www.isotc211.org/2005/gmd}MD_DataIdentification/{http://www.isotc211.org/2005/gmd}extent/{http://www.isotc211.org/2005/gmd}EX_Extent/{http://www.isotc211.org/2005/gmd}temporalElement/{http://www.isotc211.org/2005/gmd}EX_TemporalExtent/{http://www.isotc211.org/2005/gmd}extent/{http://www.opengis.net/gml}TimePeriod/{http://www.opengis.net/gml}endPosition',template_root.nsmap)
if 'StopTime' in current_metadata.keys():#sentinel 2 products sometimes dont have stoptime
find[0].text = current_metadata['StopTime'][:10]
else:
find[0].text = current_metadata['StartTime'][:10]
#link for the resource described in the metadata
find = template_root.findall('./{http://www.isotc211.org/2005/gmd}distributionInfo/{http://www.isotc211.org/2005/gmd}MD_Distribution/{http://www.isotc211.org/2005/gmd}transferOptions/{http://www.isotc211.org/2005/gmd}MD_DigitalTransferOptions/{http://www.isotc211.org/2005/gmd}onLine/{http://www.isotc211.org/2005/gmd}CI_OnlineResource/{http://www.isotc211.org/2005/gmd}linkage/{http://www.isotc211.org/2005/gmd}URL',template_root.nsmap)
find[0].text = current_metadata['downloadLink']
if writeToFile:
output_filename = outputFolder+metadata_key.upper().split('.')[0]+'.xml' #remove .manifest.safe , add .xml
template_tree.write(output_filename, pretty_print=True)
return template_tree
except lxml.etree.XMLSyntaxError:
print 'File XML Syntax Error'
return None
#except Exception:
#print 'Unspecified error occured, maybe the file doesn\'t exist'
#return None
else:
print 'Wrong metadata key'
return None