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_suma3.py @ 900
History | View | Annotate | Download (2.13 KB)
1 |
# encoding: utf-8
|
---|---|
2 |
|
3 |
from gvsig import currentView, currentLayer, getResource |
4 |
from gvsig.commonsdialog import msgbox, inputbox, QUESTION |
5 |
import os |
6 |
|
7 |
"""
|
8 |
Para probar este ejemplo cargue la capa MANZANAS_POB de la carpeta
|
9 |
data y seleccionela en el TOC
|
10 |
"""
|
11 |
|
12 |
from org.gvsig.fmap.mapcontext.events.listeners import ViewPortListener |
13 |
|
14 |
|
15 |
from gvsig.libs.formpanel import ProgressBarWithTaskStatus, FormPanel |
16 |
|
17 |
class MonitorSuma(ViewPortListener,FormPanel): |
18 |
|
19 |
def __init__(self, mapContext, layer, label, fieldname): |
20 |
FormPanel.__init__(self, os.path.join(os.path.dirname(__file__), "monitor_suma3.xml")) |
21 |
|
22 |
self.mapContext = mapContext
|
23 |
self.layer = layer
|
24 |
self.fieldname = fieldname
|
25 |
self.lblEtiqueta.setText(label)
|
26 |
self.calcular_sumatorio()
|
27 |
mapContext.getViewPort().addViewPortListener(self)
|
28 |
|
29 |
def btnCerrar_click(self,*args): |
30 |
self.mapContext.getViewPort().removeViewPortListener(self) |
31 |
self.hide()
|
32 |
|
33 |
def backColorChanged(self,*args): |
34 |
pass
|
35 |
|
36 |
def extentChanged(self,*args): |
37 |
self.calcular_sumatorio()
|
38 |
|
39 |
def projectionChanged(self,*args): |
40 |
pass
|
41 |
|
42 |
def calcular_sumatorio(self): |
43 |
encuadre = self.mapContext.getViewPort().getEnvelope().getGeometry()
|
44 |
lineas = self.layer.getFeatureStore().getFeatureSet().iterator()
|
45 |
suma = 0
|
46 |
for linea in lineas: |
47 |
g = linea.getDefaultGeometry() |
48 |
if encuadre.intersects(g) :
|
49 |
suma += int(linea.get(self.fieldname)) |
50 |
self.txtSuma.setText(str(suma)) |
51 |
|
52 |
def main(*args): |
53 |
if currentView() == None: |
54 |
msgbox("Debera tener una vista abierta y activa")
|
55 |
return
|
56 |
mapContext = currentView().getMapContext() |
57 |
|
58 |
layer = currentLayer() |
59 |
if layer == None: |
60 |
msgbox("Debera tener seleccionada la capa sobre la que desea trabajar")
|
61 |
return
|
62 |
fieldname = inputbox( |
63 |
"Introduzca el nombre del campo para calcular el sumatorio",
|
64 |
"Nombre de campo",
|
65 |
QUESTION, |
66 |
"pob_total"
|
67 |
) |
68 |
if fieldname in ("",None): |
69 |
msgbox("Operacion cancelada")
|
70 |
return
|
71 |
label = inputbox("Introduzca una etiqueta para el sumatorio de '%s':" % fieldname,
|
72 |
"Etiqueta",
|
73 |
QUESTION, |
74 |
fieldname |
75 |
) |
76 |
monitor = MonitorSuma(mapContext, layer, label, fieldname) |
77 |
monitor.showTool("monitor")
|