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 / monitor / monitor_suma1.py @ 900

History | View | Annotate | Download (1.14 KB)

1
# encoding: utf-8
2

    
3
from gvsig import currentLayer, currentView
4
from gvsig.commonsdialog import msgbox, inputbox, QUESTION
5

    
6
"""
7
Para probar este ejemplo cargue la capa MANZANAS_POB de la carpeta
8
data y seleccionela en el TOC
9
"""
10

    
11
def calcular_sumatorio(mapContext, layer, fieldname):
12
  encuadre = mapContext.getViewPort().getEnvelope().getGeometry()
13
  lineas = layer.getFeatureStore().getFeatureSet().iterator()
14
  suma = 0
15
  for linea in lineas:
16
    g = linea.getDefaultGeometry()
17
    if encuadre.intersects(g) :
18
      suma += int(linea.get(fieldname))
19
  print "Sumatorio de %s: %s" % (fieldname, suma)
20

    
21
def main(*args):
22
  if currentView() == None:
23
    msgbox("Debera tener una vista abierta y activa")
24
    return
25
  mapContext = currentView().getMapContext()
26
 
27
  layer = currentLayer()
28
  if layer == None:
29
    msgbox("Debera tener seleccionada la capa sobre la que desea trabajar")
30
    return
31
  fieldname = inputbox(
32
    "Introduzca el nombre del campo para calcular el sumatorio",
33
    "Nombre de campo",
34
    QUESTION,
35
    "pob_total"
36
  )
37
  if fieldname in ("",None):
38
    msgbox("Operacion cancelada")
39
    return
40
  calcular_sumatorio(mapContext, layer, fieldname)
41