-
Notifications
You must be signed in to change notification settings - Fork 4
/
pl_expressions.py
105 lines (87 loc) · 3.91 KB
/
pl_expressions.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
"""
/***************************************************************************
Name : PlanetLabs expressions
Description : Set of expressions for QGIS ( 2.8 or above )
Date : April, 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. *
* *
***************************************************************************/
"""
from qgis.core import ( qgsfunction )
from catalogpl_plugin import API_PlanetLabs
@qgsfunction(args=1, group='Planet Labs')
def getValueFromMetadata(values, feature, parent):
"""
<h4>Return</h4>Get value of key of 'meta_json' field
<p><h4>Syntax</h4>getValueFromMetadata('list_keys')</p>
<p><h4>Argument</h4>list_keys -> String with a sequence of keys names - '"key1","key2",...'</p>
<p><h4>Example</h4>getValueFromMetadata( '"item_type"' )</p><p>Return: Item type</p>
"""
if values[0].count('"') % 2 != 0:
raise Exception("Catalog Planet: Error! Key need double quotes: %s." % values[0] )
if len( values[0] ) < 1:
raise Exception("Catalog Planet: Error! Field is empty." )
name_metadata_json = 'meta_json'
id_metadata_json = feature.fieldNameIndex( name_metadata_json )
if id_metadata_json == -1:
raise Exception("Catalog Planet: Error! Need have '%s' field." % name_metadata_json )
lstKey = map( lambda item: item.strip(), values[ 0 ].split(",") )
lstKey = map( lambda item: item.strip('"'), lstKey )
metadata_json = feature.attributes()[ id_metadata_json ]
try:
( success, valueKey) = API_PlanetLabs.getValue( metadata_json, lstKey )
if not success:
raise Exception( valueKey )
except Exception as e:
raise Exception( e.message )
return valueKey
@qgsfunction(args=0, group='Planet Labs')
def getLocationAnalytic(values, feature, parent):
"""
<h4>Return</h4>Get value of location of 'meta_json' field
<p><h4>Syntax</h4>getLocation()</p>
<p><h4>Argument</h4>None</p>
<p><h4>Example</h4>getLocation()</p><p>Return: Value of location</p>
"""
name_metadata_json = 'meta_json'
id_metadata_json = feature.fieldNameIndex( name_metadata_json )
if id_metadata_json == -1:
raise Exception("Catalog Planet: Error! Need have '%s' field." % name_metadata_json )
metadata_json = feature.attributes()[ id_metadata_json ]
lstKey = ['assets_status','a_analytic','location']
try:
( success, valueKey) = API_PlanetLabs.getValue( metadata_json, lstKey )
if not success:
return "'location' not found"
except Exception as e:
pass
return valueKey
@qgsfunction(args=0, group='Planet Labs')
def getLocationUDM(values, feature, parent):
"""
<h4>Return</h4>Get value of location of 'meta_json' field
<p><h4>Syntax</h4>getLocation()</p>
<p><h4>Argument</h4>None</p>
<p><h4>Example</h4>getLocation()</p><p>Return: Value of location</p>
"""
name_metadata_json = 'meta_json'
id_metadata_json = feature.fieldNameIndex( name_metadata_json )
if id_metadata_json == -1:
raise Exception("Catalog Planet: Error! Need have '%s' field." % name_metadata_json )
metadata_json = feature.attributes()[ id_metadata_json ]
lstKey = ['assets_status','a_udm','location']
try:
( success, valueKey) = API_PlanetLabs.getValue( metadata_json, lstKey )
if not success:
return "'location' not found"
except Exception as e:
pass
return valueKey