Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / scripts / examples / vectorial_data / 02_maximun_and_minimun_elevation_select_features.py @ 475

History | View | Annotate | Download (2.07 KB)

1

    
2
import gvsig
3
import commonsdialog
4

    
5
def main(): 
6
  """
7
  This script runs all the features of the active layer, and calculates the 
8
  maximum and minimum elevation these, and with this:
9
    - Shows a message informing the user which are
10
    - Select the geometries associated with maximum and minimum elevation. 
11
  
12
  If no ELEVATION field shows a message and finish script execution.
13
  
14
  In the data folder can be found polygons_with_elevation layer used
15
  for testing with this script.
16
  """
17
  
18
  #
19
  #Invoke gvsig.currentLayer function to get active layer
20
  #If not view or active layer in view raise RuntimeException
21
  layer = gvsig.currentLayer()
22
  
23
  #
24
  #Invoke layer.getSchema method to get layer definition data 
25
  schema = layer.getSchema()
26
  
27
  #
28
  #Invoke schema.get method to get ELEVATION field or None
29
  field = schema.get('ELEVATION', None)
30
  
31
  #
32
  #Check if field exist
33
  if not field:
34
      #If not shows message and finish
35
      text = '"ELEVATION" field must exist in the active layer.\nFinish script'
36
      title = "ERROR"
37
      message_type = commonsdialog.WARNING
38
      commonsdialog.msgbox(text, title, message_type)
39
      return
40

    
41
  emax = 0.0
42
  emin = 0.0
43
  fmax = None
44
  fmin = None
45
  #
46
  # Gets features using layer.features method
47
  features = layer.features()
48
  
49
  #
50
  #Runs layer features
51
  for feature in features:
52
    # 
53
    # Cheacks if current feature ELEVATION is higher than calculated. 
54
    # If so, stored current ELEVATION value and the feature instance
55
    if feature.ELEVATION > emax :
56
        emax = feature.ELEVATION
57
        fmax = feature.getCopy()
58
      
59
    # Same but with minimum ELEVATION
60
    if feature.ELEVATION < emin or emin ==0.0:
61
        emin = feature.ELEVATION
62
        fmin = feature.getCopy()
63
  #
64
  # Selects features in the layer.
65
  if fmax!=None:
66
    layer.select(fmax)
67
  if fmin!=None:
68
    layer.select(fmin)
69
  #
70
  # Finally show results in a msgbox message.
71
  text = "Maximum Elevation=%s, Minumun Elevation=%s" % (emax, emin)
72
  title = "Maximun and minumum values"
73
  message_type = commonsdialog.IDEA
74
  
75
  commonsdialog.msgbox(text, title, message_type)
76