source: branches/blt4_trunk/examples/mapviewer/feature/feature_multiple_selection_by_callback.tcl @ 6414

Last change on this file since 6414 was 6414, checked in by ldelgass, 8 years ago

merge from trunk

File size: 3.4 KB
Line 
1package require Tk
2package require Rappture
3package require RapptureGUI
4
5proc handleSelect {option {args ""}} {
6    global selectedFeatures
7
8    switch $option {
9        "annotation" {
10            # User clicked a map annotation (not a feature) like a user-added pin, etc.
11         }
12        "clear" {
13            # Clear any previous selection, user clicked on background where there is no feature
14            # No args
15            set selectedFeatures ""
16        }
17        "feature" {
18            # args: op featureIDList layerName
19            foreach {op featureIdList layerName} $args break
20
21            puts "select feature $op:\tfeatureIDList=\"$featureIdList\"\tlayerName=\"$layerName\""
22
23            # strip off the mapId from the layerName
24            regexp {([^-]*)-(.*)} $layerName matched mapId layerName
25
26            switch $op {
27                "add" {
28                    set selectedFeatures [concat $selectedFeatures $featureIdList]
29                }
30                "set" {
31                    set selectedFeatures $featureIdList
32                }
33                "remove" {
34                    foreach i $featureIdList {
35                        set id [lsearch -exact $selectedFeatures $i]
36                        if {$id > 0} {
37                            set selectedFeatures [lreplace $selectedFeatures $id $id]
38                        }
39                    }
40                }
41            }
42            updateHighlightedFeatures $layerName $selectedFeatures
43        }
44    }
45}
46
47proc updateHighlightedFeatures {layerName selectedFeatures} {
48    global map
49    global mapviewer
50    global selector2
51
52    $map deleteSelector $layerName $selector2(id)
53    set selector2(query) [concat "FID IN (" [join $selectedFeatures ","] ")"]
54    $map addSelector $layerName $selector2(id) [array get selector2]
55
56    puts "refreshing mapviewer"
57    $mapviewer refresh
58}
59
60
61Rappture::resources::load
62
63set commondir [file join [file dirname [info script]] .. common]
64source [file join $commondir geovis_settings.tcl]
65
66set width 400
67set height 300
68wm geometry . ${width}x${height}
69update
70
71set mapviewer [Rappture::MapViewer .g]
72
73pack .g -expand yes -fill both
74
75
76# Parameters for feature layer
77array set ogrParams {
78    url {local://afr_elas.shp}
79}
80array set countries {
81    label   "Countries"
82    opacity 1.0
83}
84set stylesheet {
85  s1 {
86    fill: #98AFC7;
87    stroke: #000000;
88    stroke-width: 3;
89    altitude-clamping: terrain-drape;
90  }
91  s2 {
92    fill: #00FFFF;
93    stroke: #000000;
94    stroke-width: 3;
95    altitude-clamping: terrain-drape;
96  }
97}
98array set selector1 {
99    id    1
100    style s1
101    query "POP2005 >= 0"
102}
103array set selector2 {
104    id    2
105    style s2
106    query "FID IN (0,1)"
107}
108set numSelectors 2
109for {set i 1} {$i <= $numSelectors} {incr i} {
110   lappend selectors [array get selector$i]
111}
112
113# Track of which feature ids have been selected
114set selectedFeatures "0 1"
115
116# Create a map object
117set map [Rappture::Map #auto]
118
119# Configure layers
120set layerId 0
121$map addLayer feature \
122    countries$layerId [array get countries] \
123    ogr [array get ogrParams] \
124    $stylesheet "" $selectors
125
126# Add map to viewer
127$mapviewer add $map
128$mapviewer scale $map
129
130$mapviewer setSelectCallback "handleSelect"
131
132after 2000 {
133    $mapviewer camera zoom layer $map countries$layerId
134}
135
136after 5000 {
137    # simulating selecting features from outside of the map
138    $mapviewer select feature add "44 48 43 21" $map-countries$layerId
139    puts "select feature add ..."
140}
Note: See TracBrowser for help on using the repository browser.