Revision 447 org.gvsig.scripting.app/trunk/org.gvsig.scripting.app/org.gvsig.scripting.app.extension/src/main/resources-plugin/scripting/lib/gvsig2_1_0/vectorial/schema.py
schema.py | ||
---|---|---|
1 | 1 |
|
2 |
|
|
2 | 3 |
from org.gvsig.fmap.dal.feature.impl import DefaultFeatureType as JSchema |
4 |
from org.gvsig.fmap.dal.feature.impl import DefaultEditableFeatureType as JEditableSchema |
|
3 | 5 |
|
6 |
from org.gvsig.fmap.dal import DataTypes |
|
7 |
from org.gvsig.app import ApplicationLocator |
|
8 |
|
|
4 | 9 |
class Schema(JSchema): |
5 | 10 |
""" |
6 |
Stores data properties definition. Represents gvSIG FeatureType |
|
11 |
Stores the definition of the attributes of a feature store. |
|
12 |
|
|
13 |
This definition es read-only. If need to modify get a editable |
|
14 |
copy of this with getEditable. |
|
15 |
|
|
16 |
Extends the `java FeatureType`_ class. |
|
17 |
|
|
18 |
.. _`java FeatureType` : FIXME_URL |
|
7 | 19 |
""" |
8 | 20 |
|
21 |
@staticmethod |
|
22 |
def __getitem__(self, name): |
|
23 |
return self.getAttributeDescriptor(name) |
|
24 |
|
|
25 |
@staticmethod |
|
26 |
def __iter__(self): |
|
27 |
return iter(self.getAttributeDescriptors()) |
|
28 |
|
|
29 |
@staticmethod |
|
30 |
def __len__(self): |
|
31 |
return len(self.getAttributeDescriptors()) |
|
32 |
|
|
33 |
|
|
34 |
@staticmethod |
|
35 |
def get(self, name, default=None): |
|
36 |
""" |
|
37 |
Returns a feature attribute descriptor that contains information about |
|
38 |
one feature attribute, such as its name, data type or precision. |
|
39 |
|
|
40 |
:parameters: |
|
41 |
name : string |
|
42 |
name of the attribute of the feature |
|
43 |
default : object |
|
44 |
default value to return when no attribute of name exists in the feature. |
|
45 |
Default value is None. |
|
46 |
:return: |
|
47 |
The FeatureAttributeDescriptor requested |
|
48 |
:returntype: |
|
49 |
FeatureAttributeDescriptor_ |
|
50 |
.. _FeatureAttributeDescriptor : FIXME_URL |
|
51 |
""" |
|
52 |
x = self.getAttributeDescriptor(name) |
|
53 |
if x == None: |
|
54 |
return default |
|
55 |
return x |
|
56 |
|
|
57 |
@staticmethod |
|
58 |
def getAttrNames(self): |
|
59 |
""" |
|
60 |
Returns a list with name of attributes in the schema |
|
61 |
|
|
62 |
:return: |
|
63 |
The names of the attributes |
|
64 |
:returntype: |
|
65 |
string |
|
66 |
""" |
|
67 |
if not self.getAttributeDescriptors(): |
|
68 |
return None |
|
69 |
|
|
70 |
return [attr.getName() for attr in self.getAttributeDescriptors()] |
|
71 |
|
|
72 |
@staticmethod |
|
73 |
def __call__(self): |
|
74 |
""" |
|
75 |
Return the java object represented by this object. |
|
76 |
|
|
77 |
This method is for compatibility with scripting API of gvSIG 2.0 |
|
78 |
|
|
79 |
:ReturnType: |
|
80 |
Schema |
|
81 |
:deprecated: |
|
82 |
With gvSIG 2.1 the call this method isn't necesary |
|
83 |
|
|
84 |
""" |
|
85 |
return self |
|
86 |
|
|
87 |
class EditableSchema(JEditableSchema): |
|
88 |
""" |
|
89 |
Stores the definition of the attributes of a feature store. |
|
90 |
|
|
91 |
This is an editable definition of the schema. |
|
92 |
|
|
93 |
Extends the `java EditableFeatureType`_ class. |
|
94 |
|
|
95 |
.. _`java EditableFeatureType` : FIXME_URL |
|
96 |
""" |
|
97 |
|
|
98 |
@staticmethod |
|
9 | 99 |
def append(self, name, type, size=None, default=None, precision=4): |
10 |
"""Adds new property to feature properties definition. If error occurs |
|
11 |
raises RuntimeError. |
|
12 |
:param name: Feature property name |
|
13 |
:type name: String |
|
14 |
:param type: Feature property type |
|
15 |
:type name: String |
|
16 |
:param size: Feature property size |
|
17 |
:type size: int |
|
18 |
:param default: Feature property default value |
|
19 |
:return: new atribute |
|
20 | 100 |
""" |
21 |
if not isinstance(self._javaobj, EditableFeatureType): |
|
22 |
self.modify() |
|
101 |
Adds a new attribute to the feature definition. |
|
23 | 102 |
|
103 |
:parameters: |
|
104 |
name : string |
|
105 |
name of the new attribute |
|
106 |
type : int, string, DataType_ |
|
107 |
the type of the new attribute |
|
108 |
size : int |
|
109 |
if the attribute is a string, length of this |
|
110 |
default : object |
|
111 |
default value for the new attribute |
|
112 |
precision : int |
|
113 |
the precision for types double and float. |
|
114 |
:return: |
|
115 |
the new FeatureAttributeDescriptor |
|
116 |
:returntype: |
|
117 |
EditableFeatureAttributeDescriptor_ |
|
118 |
.. _EditableFeatureAttributeDescriptor : FIXME_URL |
|
119 |
.. _DataType : FIXME_URL |
|
120 |
|
|
121 |
""" |
|
122 |
application = ApplicationLocator.getManager() |
|
123 |
datamanager = application.getDataManager() |
|
124 |
dataTypes = application.getDataTypesManager() |
|
24 | 125 |
if isinstance(type, str): |
25 | 126 |
try: |
26 |
application = ApplicationLocator.getManager() |
|
27 |
datamanager = application.getDataManager() |
|
28 |
dataTypes = application.getDataTypesManager() |
|
29 |
type = dataTypes.getType(type) #dataType constant value from string |
|
127 |
type = dataTypes.getType(type) # dataType constant value from string |
|
128 |
type = dataTypes.get(type) |
|
30 | 129 |
except: |
31 | 130 |
raise RuntimeError( |
32 | 131 |
"Feature Property Data type (%s) is not valid. name=%s, type=%s, size=%s, default=%s)" % ( |
... | ... | |
37 | 136 |
default |
38 | 137 |
) |
39 | 138 |
) |
40 |
if isinstance(type, int): |
|
139 |
elif isinstance(type, int):
|
|
41 | 140 |
try: |
42 | 141 |
type = dataTypes.get(type) |
43 | 142 |
except: |
... | ... | |
50 | 149 |
default |
51 | 150 |
) |
52 | 151 |
) |
53 |
|
|
152 |
elif !isinstance(type,DataType): |
|
153 |
raise RuntimeError( |
|
154 |
"Data type (%s) is not valid. name=%s, type=%s, size=%s, default=%s)" % ( |
|
155 |
type, |
|
156 |
name, |
|
157 |
type, |
|
158 |
size, |
|
159 |
default |
|
160 |
) |
|
161 |
) |
|
162 |
|
|
54 | 163 |
attribute = self.add(name, type.getType()) |
55 | 164 |
|
56 | 165 |
if size != None: |
... | ... | |
67 | 176 |
|
68 | 177 |
return attribute |
69 | 178 |
|
70 |
def __getitem__(self, name): |
|
71 |
return self.getAttributeDescriptor(name) |
|
72 |
|
|
73 |
def get(self, name, default=None): |
|
74 |
"""Returns a feature attribute descriptor that contains information about |
|
75 |
one feature attribute, such as its name, data type or precision. |
|
76 |
:param name: Attribute name |
|
77 |
:type name: string |
|
78 |
:param default: Value to return if no attribute name found. |
|
79 |
:return: AttributeDescriptor |
|
179 |
@staticmethod |
|
180 |
def __call__(self): |
|
80 | 181 |
""" |
81 |
x = self.getAttributeDescriptor(name) |
|
82 |
if x == None: |
|
83 |
return default |
|
84 |
return x |
|
85 |
|
|
86 |
def getAttrNames(self): |
|
87 |
"""Returns a list with attributes names""" |
|
88 |
if not self.getAttributeDescriptors(): |
|
89 |
return None |
|
90 |
|
|
91 |
return [attr.getName() for attr in self.getAttributeDescriptors()] |
|
182 |
Return the java object represented by this object. |
|
92 | 183 |
|
184 |
This method is for compatibility with scripting API of gvSIG 2.0 |
|
93 | 185 |
|
94 |
def getCopy(self): |
|
95 |
"""Returns a itself clone""" |
|
96 |
return Schema(self().getCopy()) |
|
97 |
|
|
98 |
def modify(self): |
|
99 |
"""Sets edit mode""" |
|
100 |
if not isinstance(self._javaobj, EditableFeatureType): |
|
101 |
self.featureTypeNoEditable = self._javaobj |
|
102 |
self._javaobj = self._javaobj.getEditable() |
|
186 |
:ReturnType: |
|
187 |
EditableSchema |
|
188 |
:deprecated: |
|
189 |
With gvSIG 2.1 the call this method isn't necesary |
|
190 |
|
|
191 |
""" |
|
192 |
return self |
|
103 | 193 |
|
194 |
# |
|
195 |
# Inject new methods in the class JSchema |
|
196 |
# |
|
197 |
JSchema.__call__ = Schema.__call__ |
|
198 |
JSchema.__iter__ = Schema.__iter__ |
|
199 |
JSchema.__len__ = Schema.__len__ |
|
200 |
JSchema.get = Schema.get |
|
201 |
JSchema.getAttrNames = Schema.getAttrNames |
|
202 |
# |
|
203 |
# Inject new methods in the class JSchema |
|
204 |
# |
|
205 |
JEditableSchema.__call__ = EditableSchema.__call__ |
|
206 |
JEditableSchema.__iter__ = EditableSchema.__iter__ |
|
207 |
JEditableSchema.__len__ = EditableSchema.__len__ |
|
208 |
JEditableSchema.get = EditableSchema.get |
|
209 |
JEditableSchema.getAttrNames = EditableSchema.getAttrNames |
|
210 |
JEditableSchema.append = EditableSchema.append |
|
211 |
|
Also available in: Unified diff