-
Notifications
You must be signed in to change notification settings - Fork 4
/
apiqtpl.py
588 lines (496 loc) · 19.8 KB
/
apiqtpl.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name : QtCore.Qt API for Catalog Planet Labs
Description : API for Planet Labs
Date : May, 2015
copyright : (C) 2015 by Luiz Motta
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import json, datetime
from PyQt4 import QtCore, QtGui, QtNetwork
class AccessSite(QtCore.QObject):
# Signals
finished = QtCore.pyqtSignal( dict)
send_data = QtCore.pyqtSignal(QtCore.QByteArray)
status_download = QtCore.pyqtSignal(int, int)
status_erros = QtCore.pyqtSignal(list)
ErrorCodeAttribute = {
10: 'Canceled request',
400: 'Bad request syntax',
401: 'Unauthorized',
402: 'Payment required',
403: 'Forbidden',
404: 'Not found',
500: 'Internal error',
501: 'Not implemented',
502: 'Bad Gateway'
}
def __init__(self):
super( AccessSite, self ).__init__()
self.networkAccess = QtNetwork.QNetworkAccessManager(self)
self.totalReady = self.reply = self.triedAuthentication = self.isKilled = None
# Input by self.run
self.credential = self.responseAllFinished = None
def run(self, url, credential=None, responseAllFinished=True, json_request=None):
if credential is None:
credential = {'user': '', 'password': ''}
( self.credential, self.responseAllFinished ) = ( credential, responseAllFinished )
self._connect()
self.totalReady = 0
self.isKilled = False
request = QtNetwork.QNetworkRequest( url )
if json_request is None:
reply = self.networkAccess.get( request )
else:
request.setHeader( QtNetwork.QNetworkRequest.ContentTypeHeader, "application/json" )
data = QtCore.QByteArray( json.dumps( json_request ) )
reply = self.networkAccess.post( request, data )
if reply is None:
response = { 'isOk': False, 'message': "Network error", 'errorCode': -1 }
self._connect( False )
self.finished.emit( response )
return
self.triedAuthentication = False
self.reply = reply
self._connectReply()
def kill(self):
self.isKilled = True
def isRunning(self):
return ( not self.reply is None and self.reply.isRunning() )
def _connect(self, isConnect=True):
ss = [
{ 'signal': self.networkAccess.finished, 'slot': self.replyFinished },
{ 'signal': self.networkAccess.authenticationRequired, 'slot': self.authenticationRequired }
]
if isConnect:
for item in ss:
item['signal'].connect( item['slot'] )
else:
for item in ss:
item['signal'].disconnect( item['slot'] )
def _connectReply(self, isConnect=True):
ss = [
{ 'signal': self.reply.readyRead, 'slot': self.readyRead },
{ 'signal': self.reply.downloadProgress, 'slot': self.downloadProgress },
{ 'signal': self.reply.sslErrors, 'slot': self.sslErrors }
]
if isConnect:
for item in ss:
item['signal'].connect( item['slot'] )
else:
for item in ss:
item['signal'].disconnect( item['slot'] )
def _clearConnect(self):
self._connect( False ) # self.reply.close() -> emit signal self.networkAccess.finished
self._connectReply( False )
self.reply.close()
self.reply.deleteLater();
del self.reply
self.reply = None
def _redirectionReply(self, url):
self._clearConnect()
self._connect()
if url.isRelative():
url = url.resolved( url )
request = QtNetwork.QNetworkRequest( url )
reply = self.networkAccess.get( request )
if reply is None:
response = { 'isOk': False, 'message': "Netwok error", 'errorCode': -1 }
self._connect( False )
self.finished.emit( response )
return
self.reply = reply
self._connectReply()
def _errorCodeAttribute(self, code):
msg = 'Error network' if not code in self.ErrorCodeAttribute.keys() else AccessSite.ErrorCodeAttribute[ code ]
response = { 'isOk': False, 'message': msg, 'errorCode': code }
self._clearConnect()
self.finished.emit( response )
@QtCore.pyqtSlot(QtNetwork.QNetworkReply)
def replyFinished(self, reply) :
if self.isKilled:
self._errorCodeAttribute(10)
if reply.error() != QtNetwork.QNetworkReply.NoError :
response = { 'isOk': False, 'message': reply.errorString(), 'errorCode': reply.error() }
self._clearConnect()
self.finished.emit( response )
return
urlRedir = reply.attribute( QtNetwork.QNetworkRequest.RedirectionTargetAttribute )
if not urlRedir is None and urlRedir != reply.url():
self._redirectionReply( urlRedir )
return
codeAttribute = reply.attribute( QtNetwork.QNetworkRequest.HttpStatusCodeAttribute )
if codeAttribute != 200:
self._errorCodeAttribute( codeAttribute )
return
statusRequest = {
'contentTypeHeader': reply.header( QtNetwork.QNetworkRequest.ContentTypeHeader ),
'lastModifiedHeader': reply.header( QtNetwork.QNetworkRequest.LastModifiedHeader ),
'contentLengthHeader': reply.header( QtNetwork.QNetworkRequest.ContentLengthHeader ),
'statusCodeAttribute': reply.attribute( QtNetwork.QNetworkRequest.HttpStatusCodeAttribute ),
'reasonPhraseAttribute': reply.attribute( QtNetwork.QNetworkRequest.HttpReasonPhraseAttribute )
}
response = { 'isOk': True, 'statusRequest': statusRequest }
if self.responseAllFinished:
response[ 'data' ] = reply.readAll()
else:
response[ 'totalReady' ] = self.totalReady
self._clearConnect()
self.finished.emit( response )
@QtCore.pyqtSlot(QtNetwork.QNetworkReply, QtNetwork.QAuthenticator)
def authenticationRequired (self, reply, authenticator):
if not self.triedAuthentication:
authenticator.setUser( self.credential['user'] )
authenticator.setPassword( self.credential['password'] )
self.triedAuthentication = True
else:
self._errorCodeAttribute( 401 )
@QtCore.pyqtSlot()
def readyRead(self):
if self.isKilled:
self._errorCodeAttribute(10)
return
if self.responseAllFinished:
return
urlRedir = self.reply.attribute( QtNetwork.QNetworkRequest.RedirectionTargetAttribute )
if not urlRedir is None and urlRedir != self.reply.url():
self._redirectionReply( urlRedir )
return
codeAttribute = self.reply.attribute( QtNetwork.QNetworkRequest.HttpStatusCodeAttribute )
if codeAttribute != 200:
self._errorCodeAttribute( codeAttribute )
return
data = self.reply.readAll()
if data is None:
return
self.totalReady += len ( data )
self.send_data.emit( data )
@QtCore.pyqtSlot(int, int)
def downloadProgress(self, bytesReceived, bytesTotal):
if self.isKilled:
self._errorCodeAttribute(10)
else:
self.status_download.emit( bytesReceived, bytesTotal )
@QtCore.pyqtSlot( list )
def sslErrors(self, errors):
lstErros = map( lambda e: e.errorString(), errors )
self.status_erros.emit( lstErros )
self.reply.ignoreSslErrors()
class API_PlanetLabs(QtCore.QObject):
errorCodeLimitOK = (201, 207) # https://en.wikipedia.org/wiki/List_of_HTTP_status_codes (2107-09-30)
errorCodeDownloads = { # Planet DOC (2107-09-30)
299: 'Download quota has been exceeded',
429: 'Request has been denied due to exceeding rate limits.'
}
validKey = None
urlRoot = "https://api.planet.com"
urlQuickSearch = "https://api.planet.com/data/v1/quick-search"
urlThumbnail = "https://api.planet.com/data/v1/item-types/{item_type}/items/{item_id}/thumb"
urlTMS = "https://tiles.planet.com/data/v1/{item_type}/{item_id}/{{z}}/{{x}}/{{y}}.png"
urlAssets = "https://api.planet.com/data/v1/item-types/{item_type}/items/{item_id}/assets"
def __init__(self):
super( API_PlanetLabs, self ).__init__()
self.access = AccessSite()
self.currentUrl = None
def _clearResponse(self, response):
if response.has_key('data'):
response['data'].clear()
del response[ 'data' ]
del response[ 'statusRequest' ]
def kill(self):
self.access.kill()
def isRunning(self):
return self.access.isRunning()
def isHostLive(self, setFinished):
@QtCore.pyqtSlot(dict)
def finished( response):
self.access.finished.disconnect( finished )
if response['isOk']:
response[ 'isHostLive' ] = True
self._clearResponse( response )
else:
if response['errorCode'] == QtNetwork.QNetworkReply.HostNotFoundError:
response[ 'isHostLive' ] = False
response[ 'message' ] += "\nURL = %s" % API_PlanetLabs.urlRoot
else:
response[ 'isHostLive' ] = True
setFinished( response )
self.currentUrl = API_PlanetLabs.urlRoot
url = QtCore.QUrl( self.currentUrl )
self.access.finished.connect( finished )
credential = { 'user': '', 'password': ''}
self.access.run( url, credential )
def setKey(self, key, setFinished):
@QtCore.pyqtSlot(dict)
def finished( response):
self.access.finished.disconnect( finished )
if response['isOk']:
API_PlanetLabs.validKey = key
self._clearResponse( response )
setFinished( response )
self.currentUrl = API_PlanetLabs.urlRoot
url = QtCore.QUrl( self.currentUrl )
self.access.finished.connect( finished )
credential = { 'user': key, 'password': ''}
self.access.run( url, credential )
def getUrlScenes(self, json_request, setFinished):
@QtCore.pyqtSlot(dict)
def finished( response):
self.access.finished.disconnect( finished )
if response[ 'isOk' ]:
data = json.loads( str( response['data'] ) )
response[ 'url_scenes' ] = data['_links']['_self']
response['total'] = len( data['features'] )
data.clear()
self._clearResponse( response )
setFinished( response )
self.currentUrl = API_PlanetLabs.urlQuickSearch
url = QtCore.QUrl( self.currentUrl )
self.access.finished.connect( finished )
credential = { 'user': API_PlanetLabs.validKey, 'password': ''}
self.access.run( url, credential, json_request=json_request )
def getScenes(self, url, setFinished):
@QtCore.pyqtSlot(dict)
def finished( response):
self.access.finished.disconnect( finished )
if response[ 'isOk' ]:
data = json.loads( str( response[ 'data' ] ) )
response[ 'url' ] = data[ '_links' ][ '_next' ]
response[ 'scenes' ] = data[ 'features' ]
self._clearResponse( response )
setFinished( response )
self.currentUrl = url
url = QtCore.QUrl.fromEncoded( url )
self.access.finished.connect( finished )
credential = { 'user': API_PlanetLabs.validKey, 'password': '' }
self.access.run( url, credential )
def getAssetsStatus(self, item_type, item_id, setFinished):
@QtCore.pyqtSlot(dict)
def finished( response):
def setStatus(asset):
def getDateTimeFormat(d):
dt = datetime.datetime.strptime( d, "%Y-%m-%dT%H:%M:%S.%f")
return dt.strftime( formatDateTime )
key = "a_{0}".format( asset )
response['assets_status'][ key ] = {}
r = response['assets_status'][ key ]
if not data.has_key( asset ):
r['status'] = "*None*"
return
if data[ asset ].has_key('status'):
r['status'] = data[ asset ]['status']
if data[ asset ].has_key('_permissions'):
permissions = ",".join( data[ asset ]['_permissions'])
r['permissions'] = permissions
if data[ asset ].has_key('expires_at'):
r['expires_at'] = getDateTimeFormat( data[ asset ]['expires_at'] )
if data[ asset ].has_key('_links'):
if data[ asset ]['_links'].has_key('activate'):
r['activate'] = data[ asset ]['_links']['activate']
if data[ asset ].has_key('location'):
r['location'] = data[ asset ]['location']
self.access.finished.disconnect( finished )
if response[ 'isOk' ]:
formatDateTime = '%Y-%m-%d %H:%M:%S'
date_time = datetime.datetime.now().strftime( formatDateTime )
response['assets_status'] = {
'date_calculate': date_time,
'url': self.currentUrl
}
data = json.loads( str( response[ 'data' ] ) )
setStatus('analytic')
setStatus('udm')
self._clearResponse( response )
setFinished( response )
url = API_PlanetLabs.urlAssets.format(item_type=item_type, item_id=item_id)
self.currentUrl = url
url = QtCore.QUrl.fromEncoded( url )
self.access.finished.connect( finished )
credential = { 'user': API_PlanetLabs.validKey, 'password': ''}
self.access.run( url, credential )
def getThumbnail(self, item_id, item_type, setFinished):
@QtCore.pyqtSlot(dict)
def finished( response ):
self.access.finished.disconnect( finished )
if response['isOk']:
pixmap = QtGui.QPixmap()
pixmap.loadFromData( response[ 'data' ] )
response[ 'pixmap' ] = pixmap
self._clearResponse( response )
setFinished( response )
url = API_PlanetLabs.urlThumbnail.format( item_type=item_type, item_id=item_id )
self.currentUrl = url
url = QtCore.QUrl( url )
self.access.finished.connect( finished )
credential = { 'user': API_PlanetLabs.validKey, 'password': ''}
self.access.run( url, credential )
def activeAsset(self, url, setFinished):
@QtCore.pyqtSlot(dict)
def finished( response ):
self.access.finished.disconnect( finished )
if response['isOk']:
self._clearResponse( response )
setFinished( response ) # response[ 'totalReady' ]
url = QtCore.QUrl.fromEncoded( url )
url = QtCore.QUrl( url )
self.access.finished.connect( finished )
credential = { 'user': API_PlanetLabs.validKey, 'password': ''}
self.access.run( url, credential )
def saveImage(self, url, setFinished, setSave, setProgress):
@QtCore.pyqtSlot(dict)
def finished( response ):
self.access.finished.disconnect( finished )
if response['isOk']:
self._clearResponse( response )
setFinished( response ) # response[ 'totalReady' ]
url = QtCore.QUrl.fromEncoded( url )
self.access.finished.connect( finished )
self.access.send_data.connect( setSave )
self.access.status_download.connect( setProgress )
credential = { 'user': API_PlanetLabs.validKey, 'password': ''}
self.access.run( url, credential, False )
@staticmethod
def getUrlFilterScenesOrtho(filters):
items = []
for item in filters.iteritems():
skey = str( item[0] )
svalue = str( item[1] )
items.append( ( skey, svalue ) )
url = QtCore.QUrl( API_PlanetLabs.urlScenesOrtho) # urlScenesRapideye
url.setQueryItems( items )
return url.toEncoded()
@staticmethod
def getValue(jsonMetadataFeature, keys):
dicMetadata = jsonMetadataFeature
if not isinstance( jsonMetadataFeature, dict):
dicMetadata = json.loads( jsonMetadataFeature )
msgError = None
e_keys = map( lambda item: "'%s'" % item, keys )
try:
value = reduce( lambda d, k: d[ k ], [ dicMetadata ] + keys )
except KeyError as e:
msgError = "Catalog Planet: Have invalid key: %s" % ' -> '.join( e_keys)
except TypeError as e:
msgError = "Catalog Planet: The last key is invalid: %s" % ' -> '.join( e_keys)
if msgError is None and isinstance( value, dict):
msgError = "Catalog Planet: Missing key: %s" % ' -> '.join( e_keys)
return ( True, value ) if msgError is None else ( False, msgError )
@staticmethod
def getTextTreeMetadata( jsonMetadataFeature ):
def fill_item(strLevel, value):
if not isinstance( value, ( dict, list ) ):
items[-1] += ": %s" % value
return
if isinstance( value, dict ):
for key, val in sorted( value.iteritems() ):
items.append( "%s%s" % ( strLevel, key ) )
strLevel += signalLevel
fill_item( strLevel, val )
strLevel = strLevel[ : -1 * len( signalLevel ) ]
return
if isinstance( value, list ):
for val in value:
if not isinstance( value, ( dict, list ) ):
items[-1] += ": %s" % value
else:
text = '[dict]' if isinstance( value, dict ) else '[list]'
items.append( "%s%s" % ( strLevel, text ) )
strLevel += signalLevel
fill_item( strLevel, val )
strLevel = strLevel[ : -1 * len( signalLevel ) ]
signalLevel = "- "
items = []
fill_item( '', json.loads( jsonMetadataFeature ) )
return '\n'.join( items )
@staticmethod
def getHtmlTreeMetadata(value, html):
if isinstance( value, dict ):
html += "<ul>"
for key, val in sorted( value.iteritems() ):
if not isinstance( val, dict ):
html += "<li>%s: %s</li> " % ( key, val )
else:
html += "<li>%s</li> " % key
html = API_PlanetLabs.getHtmlTreeMetadata( val, html )
html += "</ul>"
return html
return html
@staticmethod
def getTextValuesMetadata( dicMetadataFeature ):
def fill_item(value):
def addValue(_value):
_text = "'%s' = %s" % (", ".join( keys ), _value )
items.append( _text )
if not isinstance( value, ( dict, list ) ):
addValue( value )
return
if isinstance( value, dict ):
for key, val in sorted( value.iteritems() ):
keys.append( '"%s"' % key )
fill_item( val )
del keys[ -1 ]
return
if isinstance( value, list ):
for val in value:
if not isinstance( val, ( dict, list ) ):
addValue( val )
else:
text = "[dict]" if isinstance( val, dict ) else "[list]"
keys.append( '"%s"' % text )
fill_item( val )
del keys[ -1 ]
keys = []
items = []
fill_item( dicMetadataFeature )
return '\n'.join( items )
@staticmethod
def getQTreeWidgetMetadata( jsonMetadataFeature, parent=None ):
def createTreeWidget():
tw = QTreeWidget(parent)
tw.setColumnCount( 2 )
tw.header().hide()
tw.clear()
return tw
def fill_item(item, value):
item.setExpanded( True )
if not isinstance( value, ( dict, list ) ):
item.setData( 1, QtCore.Qt.DisplayRole, value )
return
if isinstance( value, dict ):
for key, val in sorted( value.iteritems() ):
child = QTreeWidgetItem()
child.setText( 0, unicode(key) )
item.addChild( child )
fill_item( child, val )
return
if isinstance( value, list ):
for val in value:
if not isinstance( val, ( dict, list ) ):
item.setData( 1, QtCore.Qt.DisplayRole, val )
else:
child = QTreeWidgetItem()
item.addChild( child )
text = '[dict]' if isinstance( value, dict ) else '[list]'
child.setText( 0, text )
fill_item( child , val )
child.setExpanded(True)
tw = createTreeWidget()
fill_item( tw.invisibleRootItem(), json.loads( jsonMetadataFeature ) )
tw.resizeColumnToContents( 0 )
tw.resizeColumnToContents( 1 )
return tw
@staticmethod
def getURL_TMS(feat, sbands):
( ok, item_type ) = API_PlanetLabs.getValue( feat['meta_json'], [ 'item_type' ] )
url = API_PlanetLabs.urlTMS.format( item_type=item_type, item_id=feat['id'] )
return url