1 | package require Tk |
---|
2 | package require Rappture |
---|
3 | package require RapptureGUI |
---|
4 | |
---|
5 | proc 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 | |
---|
47 | proc 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 | |
---|
61 | Rappture::resources::load |
---|
62 | |
---|
63 | set commondir [file join [file dirname [info script]] .. common] |
---|
64 | source [file join $commondir geovis_settings.tcl] |
---|
65 | |
---|
66 | set width 400 |
---|
67 | set height 300 |
---|
68 | wm geometry . ${width}x${height} |
---|
69 | update |
---|
70 | |
---|
71 | set mapviewer [Rappture::MapViewer .g] |
---|
72 | |
---|
73 | pack .g -expand yes -fill both |
---|
74 | |
---|
75 | |
---|
76 | # Parameters for feature layer |
---|
77 | array set ogrParams { |
---|
78 | url {local://afr_elas.shp} |
---|
79 | } |
---|
80 | array set countries { |
---|
81 | label "Countries" |
---|
82 | opacity 1.0 |
---|
83 | } |
---|
84 | set 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 | } |
---|
98 | array set selector1 { |
---|
99 | id 1 |
---|
100 | style s1 |
---|
101 | query "POP2005 >= 0" |
---|
102 | } |
---|
103 | array set selector2 { |
---|
104 | id 2 |
---|
105 | style s2 |
---|
106 | query "FID IN (0,1)" |
---|
107 | } |
---|
108 | set numSelectors 2 |
---|
109 | for {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 |
---|
114 | set selectedFeatures "0 1" |
---|
115 | |
---|
116 | # Create a map object |
---|
117 | set map [Rappture::Map #auto] |
---|
118 | |
---|
119 | # Configure layers |
---|
120 | set 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 | |
---|
132 | after 2000 { |
---|
133 | $mapviewer camera zoom layer $map countries$layerId |
---|
134 | } |
---|
135 | |
---|
136 | after 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 | } |
---|