Statistics
| Revision:

gvsig-scripting / 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 @ 447

History | View | Annotate | Download (5.62 KB)

1

    
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
5

    
6
from org.gvsig.fmap.dal import DataTypes
7
from org.gvsig.app import ApplicationLocator
8

    
9
class Schema(JSchema):
10
  """
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  
19
  """
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
99
  def append(self, name, type, size=None, default=None, precision=4):
100
    """
101
    Adds a new attribute to the feature definition.
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()
125
    if isinstance(type, str):
126
      try:
127
        type = dataTypes.getType(type) # dataType constant value from string
128
        type = dataTypes.get(type)
129
      except:
130
        raise RuntimeError(
131
            "Feature Property Data type (%s) is not valid.  name=%s, type=%s, size=%s, default=%s)" % (
132
                type, 
133
                name, 
134
                type, 
135
                size, 
136
                default
137
            )
138
        )
139
    elif isinstance(type, int):
140
      try:
141
        type = dataTypes.get(type)
142
      except:
143
        raise RuntimeError(
144
            "Data type (%s) is not valid.  name=%s, type=%s, size=%s, default=%s)" % (
145
                type, 
146
                name, 
147
                type, 
148
                size, 
149
                default
150
            )
151
        )
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
    
163
    attribute = self.add(name, type.getType())
164
 
165
    if size != None: 
166
      attribute.setSize(size)
167
    
168
    if default != None:
169
      attribute.setDefaultValue(default)
170
      
171
    if precision != None and type.getType() in (DataTypes.DOUBLE, DataTypes.FLOAT):
172
      attribute.setPrecision(precision)
173
    
174
    if type.getType() == DataTypes.GEOMETRY and self.getDefaultGeometryAttributeName()==None:
175
      self.setDefaultGeometryAttributeName(name)
176
    
177
    return attribute
178

    
179
  @staticmethod
180
  def __call__(self):
181
    """
182
    Return the java object represented by this object.
183
    
184
    This method is for compatibility with scripting API of gvSIG 2.0
185
    
186
    :ReturnType:
187
      EditableSchema
188
    :deprecated: 
189
      With gvSIG 2.1 the call this method isn't necesary
190
      
191
    """
192
    return self    
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