source: trunk/examples/mapviewer/feature/feature_multiple_selection_by_new_layer.tcl @ 6370

Last change on this file since 6370 was 6370, checked in by dkearney, 6 years ago

adding feature multiple selection examples, not all ideas work right now.
using the afr_elas shapefile because they include country elasticity values from the landuse tool.
adding an example of using large stylesheets with multiple colors.
adding example of performing multiple selection by:

  1. deleting and adding a layer by the same name
  2. using a callback
  3. updating a layer with map object's addSelector method and mapviewer's refresh method.
File size: 4.8 KB
Line 
1package require Tk
2package require Rappture
3package require RapptureGUI
4
5set colors23 {
6    FF0000FF
7    FF8000FF
8    FFAA00FF
9    FFD500FF
10    FFFF00FF
11    D5FF00FF
12    AAFF00FF
13    80FF00FF
14    55FF00FF
15    2BFF00FF
16    00FF00FF
17    00FF2AFF
18    00FF55FF
19    00FF80FF
20    00FFAAFF
21    00FFD4FF
22    00FFFFFF
23    00D4FFFF
24    00AAFFFF
25    0080FFFF
26    0055FFFF
27    002BFFFF
28    0000FFFF
29}
30
31set cutoffs24 {
32    0.019100
33    0.067600
34    0.083600
35    0.099600
36    0.116000
37    0.132000
38    0.148000
39    0.164000
40    0.180000
41    0.196000
42    0.212000
43    0.228000
44    0.244000
45    0.260000
46    0.276000
47    0.292000
48    0.308000
49    0.324000
50    0.340000
51    0.356000
52    0.372000
53    0.388000
54    0.404000
55    0.420000
56}
57
58
59proc handleSelect {option {args ""}} {
60    global selectedFeatures
61    switch $option {
62        "annotation" {
63            # User clicked a map annotation (not a feature) like a user-added pin, etc.
64         }
65        "clear" {
66            # Clear any previous selection, user clicked on background where there is no feature
67            # No args
68        }
69        "feature" {
70            # args: op featureIDList layerName
71            foreach {op featureIdList layerName} $args break
72            switch $op {
73                "add" {
74                }
75                "set" {
76                    foreach {op featureIdList layerName} $args break
77                    lappend selectedFeatures $featureIdList
78                    adjustLayerHighlight $op $selectedFeatures $layerName
79                }
80                "remove" {
81                }
82            }
83        }
84    }
85}
86
87
88proc adjustLayerHighlight {option featureIdList layerName} {
89    global map
90    global mapviewer
91    global stylesheet
92    global cutoffs24
93    set selectors [generateSelectors $cutoffs24 $featureIdList]
94    $map deleteLayer "countries"
95    set layerName "countries"
96    addCountryLayer $map $layerName $stylesheet $selectors
97    $mapviewer refresh
98}
99
100proc generateStyleSheet {colors} {
101
102    set style_t {
103      sID {
104        fill: #COLOR;
105        stroke: #000000ff;
106        stroke-width: 3;
107        altitude-clamping: terrain-drape;
108      }
109    }
110    set stylesheet ""
111    for {set i 0} {$i < [llength $colors]} {incr i} {
112        # append stylesheet "\n" [string map {ID $i COLOR [lindex $colors $i]} $style_t]
113        set s $style_t
114        regsub ID $s $i s
115        regsub COLOR $s [lindex $colors $i] s
116        append stylesheet $s
117    }
118
119    # add a color for fields marked as NA or -1 where there was no data
120    append stylesheet [string map {ID NA COLOR f2f2f2ff} $style_t]
121
122    # add a color for selected fields
123    append stylesheet [string map {ID SE COLOR 000000FF} $style_t]
124
125    return $stylesheet
126}
127
128proc generateSelectors {cutoffs {featureIdList {}}} {
129    set selectors ""
130    set numSelectors [llength $cutoffs]
131    for {set i 0} {$i < $numSelectors-1} {incr i} {
132       set lower [lindex $cutoffs $i]
133       set upper [lindex $cutoffs [expr $i+1]]
134       lappend selectors [list id $i style s$i query "elas >= $lower and elas < $upper"]
135    }
136
137    # add a selector for fields marked as NA or -1 where there was no data
138    incr i
139    lappend selectors [list id $i style sNA query "elas = -1"]
140
141    # add a selector for selected fields
142    incr i
143    lappend selectors [list id $i style sSE query "FID IN ([join $featureIdList ","])"]
144
145    return $selectors
146}
147
148proc addCountryLayer {map layerName stylesheet selectors} {
149    # Parameters for feature layer
150    array set ogrParams {
151        url {local://afr_elas.shp}
152    }
153    array set countries {
154        label   "Countries"
155        opacity 1.0
156    }
157
158    # Configure layers
159    $map addLayer feature \
160        $layerName [array get countries] \
161        ogr [array get ogrParams] \
162        $stylesheet "" $selectors
163}
164
165
166
167Rappture::resources::load
168
169set commondir [file join [file dirname [info script]] .. common]
170source [file join $commondir geovis_settings.tcl]
171
172set width 400
173set height 300
174wm geometry . ${width}x${height}
175update
176
177set mapviewer [Rappture::MapViewer .g]
178
179pack .g -expand yes -fill both
180
181# setup a global variable to accumulate selected features
182set selectedFeatures {0 1 2 3 4}
183
184set stylesheet [generateStyleSheet $colors23]
185set selectors [generateSelectors $cutoffs24 $selectedFeatures]
186
187# Create a map object
188set map [Rappture::Map #auto]
189
190set layerName "countries"
191addCountryLayer $map $layerName $stylesheet $selectors
192
193# Configure the placard
194set placardAttributes [list \
195    name "Country Name" \
196    ISO3 "Country Code" \
197    elas "Elasticity" \
198]
199set placardStyle {
200    fill: #B6B6B688;
201    text-fill: #000000;
202    text-size: 12.0;
203}
204set placardPadding 5
205$map setPlacardConfig countries \
206    $placardAttributes \
207    $placardStyle \
208    $placardPadding
209
210
211# Add map to viewer
212$mapviewer add $map
213$mapviewer scale $map
214
215# setup feature callback
216$mapviewer setSelectCallback "handleSelect"
217
218after 2000 {
219    $mapviewer camera zoom layer $map $layerName
220}
221
Note: See TracBrowser for help on using the repository browser.