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 / 03_maximun_and_minimun_elevation_geometries.py @ 462
History | View | Annotate | Download (3.24 KB)
1 | 462 | jjdelcerro | |
---|---|---|---|
2 | import gvsig |
||
3 | import commonsdialog |
||
4 | |||
5 | from java.awt import Color |
||
6 | |||
7 | """
|
||
8 | Este script se recorre todas la features de la capa activa, y asumiendo
|
||
9 | que tiene un campo ELEVATION, calcula la elevacion maxima y minima de
|
||
10 | estas, y con esto:
|
||
11 | - Presentar un mensaje al usuario informando de cuales son
|
||
12 | - Seleccionar las geometrias asociadas a la elevacion maxima y minima vista.
|
||
13 | - Pone un punto en el centro de las geometrias de elevacion maxima y minima.
|
||
14 |
|
||
15 | En la carpeta data puede encontrar la capa cv05_3d_polygons usada
|
||
16 | para realizar las pruebas con este scripts.
|
||
17 | """
|
||
18 | |||
19 | def main(): |
||
20 | """
|
||
21 | This script runs all the features of the active layer, and calculates the
|
||
22 | maximum and minimum elevation these, and with this:
|
||
23 | - Shows a message informing the user which are
|
||
24 | - Select the geometries associated with maximum and minimum elevation.
|
||
25 | - Shows maximum and minimum elevation geometries centroid in the view
|
||
26 | graphicsLayer
|
||
27 |
|
||
28 | If no ELEVATION field shows a message and finish script execution.
|
||
29 |
|
||
30 | In the data folder can be found cv05_3d_polygons layer used
|
||
31 | for testing with this script.
|
||
32 | """
|
||
33 | #
|
||
34 | #Invoke gvsig.currentLayer function to get active layer
|
||
35 | #If not view or active layer in view raise RuntimeException
|
||
36 | layer = gvsig.currentLayer() |
||
37 | |||
38 | #
|
||
39 | #Invoke layer.getSchema method to get layer definition data
|
||
40 | schema = layer.getSchema() |
||
41 | |||
42 | #
|
||
43 | #Invoke schema.get method to get ELEVATION field or None
|
||
44 | field = schema.get('ELEVATION', None) |
||
45 | |||
46 | #
|
||
47 | #Check if field exist
|
||
48 | if not field: |
||
49 | #If not shows message and finish
|
||
50 | text = '"ELEVATION" field must exist in the active layer.\nFinish script'
|
||
51 | title = "ERROR"
|
||
52 | message_type = commonsdialog.WARNING |
||
53 | commonsdialog.msgbox(text, title, message_type) |
||
54 | return
|
||
55 | |||
56 | acetato = gvsig.currentView().getGraphicsLayer() |
||
57 | acetato.removeGraphics("example")
|
||
58 | smax = acetato.addSymbol(gvsig.SimplePointSymbol(Color.RED)) |
||
59 | smin = acetato.addSymbol(gvsig.SimplePointSymbol(Color.BLUE)) |
||
60 | |||
61 | emax = 0.0
|
||
62 | emin = 0.0
|
||
63 | fmax = None
|
||
64 | fmin = None
|
||
65 | |||
66 | #
|
||
67 | # Gets features using layer.features method
|
||
68 | features = layer.features() |
||
69 | |||
70 | #
|
||
71 | #Runs layer features
|
||
72 | for feature in features: |
||
73 | #
|
||
74 | # Cheacks if current feature ELEVATION is higher than calculated.
|
||
75 | # If so, stored current ELEVATION value and the feature instance
|
||
76 | if feature.ELEVATION > emax :
|
||
77 | emax = feature.ELEVATION |
||
78 | fmax = feature.getCopy() |
||
79 | |||
80 | # Same but with minimum ELEVATION
|
||
81 | if feature.ELEVATION < emin or emin ==0.0: |
||
82 | emin = feature.ELEVATION |
||
83 | fmin = feature.getCopy() |
||
84 | #
|
||
85 | # Selects features in the layer.
|
||
86 | if fmax!=None: |
||
87 | layer.select(fmax) |
||
88 | #
|
||
89 | # gets geometry centroid to put our point with a label
|
||
90 | point = fmax.geometry().centroid() |
||
91 | #
|
||
92 | # Adds to graphicsLayer the point and symbol with label
|
||
93 | acetato.addGraphic("example", point, smax, "Maximun Elevation") |
||
94 | |||
95 | if fmin!=None: |
||
96 | layer.getSelection().select(fmin) |
||
97 | punto = fmax.geometry().centroid() |
||
98 | acetato.addGraphic("example", punto, smin,"Minimun Elevation") |
||
99 | |||
100 | #
|
||
101 | # Finally show results in a msgbox message.
|
||
102 | text = "Maximum Elevation=%s, Minumun Elevation=%s" % (emax, emin)
|
||
103 | title = "Maximun and minumum values"
|
||
104 | message_type = commonsdialog.IDEA |
||
105 | |||
106 | commonsdialog.msgbox(text, title, message_type) |