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/feature.py

View differences:

feature.py
1 1

  
2

  
3
__docformat__ = "restructuredtext en"
4

  
2 5
from org.gvsig.fmap.dal.feature.impl import DefaultFeature as JFeature
6
from org.gvsig.fmap.dal.feature.impl import DefaultEditableFeature as JEditableFeature
3 7

  
4 8
class Feature(JFeature):
5
  """Represents feature data It's a wrapper from gvSIG 
6 9
  """
7
  def __init__(self,feature):
8
    WrapperToJava.__init__(self,feature)
9
    self.featureNoEditable = None
10
  Represents feature.
11
  
12
  Extends the `java Feature`_.
13
  
14
  .. _`java Feature` : FIXME_URL
15
  """
10 16

  
17
  @staticmethod
11 18
  def geometry(self):
12
    """Returns feature geometry"""
19
    """
20
    Returns the default geometry of the feature.
21
    
22
    :return:
23
      The geometry of the feature
24
    :returntype:
25
      Geometry
26
    .. _Geometry : FIXME_URL
27
    """
13 28
    return self.getDefaultGeometry()
14 29
   
30
  @staticmethod
15 31
  def getValues(self):
16
    """Returns dictionary with the pair name, value, feature attributes"""
32
    """
33
    Returns dictionary with the pair name, value, feature attributes
34
    
35
    :return:
36
      A dict with the pairs attribute-name,attribute-value.
37
    :returntype:
38
      dict
39
    """
17 40
    descriptor = self.getType()
18 41
    items = dict()
19 42
    for attr in descriptor.getAttributeDescriptors():
......
22 45
      items[name] = value
23 46
    return items 
24 47
  
48
  @staticmethod
25 49
  def edit(self):
26
    """Returns editable feature instance"""
27
    if not isinstance(self._javaobj, EditableFeature):
28
      self.featureNoEditable = self._javaobj
29
      self._javaobj = self._javaobj.getEditable()
50
    """
51
    This method is not supported from gvSIG 2.1.0
52
    """
53
    raise Exception("Deprecated method, use import gvSIG_2_0_0 for compatibilitty with fvSIG 2.0.0")
30 54
  
31
  def __getitem__(self,key):
32
    return self.get(key)
55
  @staticmethod
56
  def __getitem__(self,attributeName):
57
    """
58
    Get the value of an attribute of the feature.
59
    
60
    :parameters:
61
      attributeName : string
62
        name of the attribute to retrieve the value
63
    :return:
64
      The value of the attribute specified as parameter
65
    :returntype:
66
      object
67
    """
68
    return self.get(attributeName)
33 69
  
34
  def __getattr__(self,name):
70
  @staticmethod
71
  def __getattr__(self,attributeName):
72
    """
73
    Allow to get the value of an feature attribute as attribute of the python object.
74
    
75
    :parameters:
76
      attributeName : string
77
        name of the feature attribute to retrieve the value
78
    :return:
79
      The value of the feature attribute specified as parameter
80
    :returntype:
81
      object
82
    """
35 83
    #
36
    #FIX console error when try to introspect feature object
84
    # FIX console error when try to introspect feature object
37 85
    if name in ('__methods__'):
38 86
        return dict()
39 87
    elif name in ('__members__'):
......
42 90
        return self.getValues()      
43 91
        
44 92
    try:
45
      v = getattr(self._javaobj, name, None)
93
      v = getattr(self, attributeName, None)
46 94
      if v == None:
47
        v = self().get(name)        
95
        v = self.get(attributeName)        
48 96
      return v
49 97
    except Throwable, ex:
50
      raise RuntimeException("Can't access to attribute %s of feature (%s)" % (name, str(ex)))    
98
      raise RuntimeException("Can't access to attribute %s of feature (%s)" % (attributeName, str(ex)))    
51 99

  
100

  
101
class EditableFeature(JEditableFeature,Feature):
102
  """
103
  Represents feature.
104
  
105
  Extends the `java EditableFeature`_.
106
  
107
  .. _`java EditableFeature` : FIXME_URL
108
  """
109

  
110
  @staticmethod
111
  def setGeometry(self,*args):
112
    if len(args)==1:
113
      self.setDefaultGeometry(args[0])
114
      return
115
    if len(args)==2:
116
      self.set(args[0],args[1])
117
      return
118
    raise IllegalArgumentException("Incorrent number of arguments ("+len(args)+"), expected 1 or 2")
119
    
120
  @staticmethod
121
  def setValues(self,*args,**kwargs):
122
    """
123
    Set the value of an attribute of the feature.
124

  
125
    Can receibe two parameters, the first the name of the attribute to set the 
126
    value, an the second the value of the attribute.
127
    
128
    Alternativement you can pass parameters by name; the name is the name of the attribute to set,
129
    and the value the value to set in the attribute.
130
    """
131
    if len(args)!=0:
132
      if len(args)!=2:
133
        raise IllegalArgumentException("Incorrent number of arguments ("+len(args)+"), expected 2.")
134
      self.set(args[0],args[1])
135
      return
136
    for k,v in kwargs.iteritems():
137
      self.set(k,v)
138

  
139
  @staticmethod
140
  def __setitem__(self,attributeName, value):
141
    """
142
    Set the value of an attribute of the feature.
143
    
144
    :parameters:
145
      attributeName : string
146
        name of the attribute to retrieve the value
147
      value : object
148
        value to set
149
    """
150
    return self.set(attributeName,value)
151
  
152
  @staticmethod
153
  def __setattr__(self,attributeName, value):
154
    """
155
    Allow to set the value of an feature attribute as attribute of the python object.
156
    
157
    :parameters:
158
      attributeName : string
159
        name of the feature attribute to retrieve the value
160
      value : object
161
        value to set
162
    """
163
    try:
164
       self.set(attributeName, value)        
165
    except Throwable, ex:
166
      raise RuntimeException("Can't set the attribute %s of feature (%s)" % (attributeName, str(ex)))    
167

  
168

  
169
#
170
# Inject new methods in the class JFeature
171
#
172
JFeature.getValues = Feature.setValues
173
JFeature.geometry = Feature.geometry
174
JFeature.__getitem__ = Feature.__getitem__
175
JFeature.__getattr__ = Feature.__getattr__
176

  
177
#
178
# Inject new methods in the class JEditableFeature
179
#
180
JEditableFeature.getValues = Feature.setValues
181
JEditableFeature.geometry = Feature.geometry
182
JEditableFeature.__getitem__ = Feature.__getitem__
183
JEditableFeature.__getattr__ = Feature.__getattr__
184
JEditableFeature.setValues = EditableFeature.setValues
185
JEditableFeature.setGeometry = EditableFeature.setGeometry
186
JEditableFeature.__setitem__ = EditableFeature.__setitem__
187
JEditableFeature.__setattr__ = EditableFeature.__setattr__
188

  
189

  

Also available in: Unified diff