source: branches/1.3/gui/scripts/mapviewer.tcl @ 4750

Last change on this file since 4750 was 4750, checked in by ldelgass, 6 years ago

merge r4749 from trunk

File size: 66.8 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: mapviewer - Map object viewer
4#
5#  It connects to the GeoVis server running on a rendering farm,
6#  transmits data, and displays the results.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
10#
11#  See the file "license.terms" for information on usage and
12#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13# ======================================================================
14package require Itk
15package require BLT
16#package require Img
17
18option add *MapViewer.width 4i widgetDefault
19option add *MapViewer*cursor crosshair widgetDefault
20option add *MapViewer.height 4i widgetDefault
21option add *MapViewer.foreground black widgetDefault
22option add *MapViewer.controlBackground gray widgetDefault
23option add *MapViewer.controlDarkBackground #999999 widgetDefault
24option add *MapViewer.plotBackground black widgetDefault
25option add *MapViewer.plotForeground white widgetDefault
26option add *MapViewer.font \
27    -*-helvetica-medium-r-normal-*-12-* widgetDefault
28
29# must use this name -- plugs into Rappture::resources::load
30proc MapViewer_init_resources {} {
31    Rappture::resources::register \
32        geovis_server Rappture::MapViewer::SetServerList
33}
34
35itcl::class Rappture::MapViewer {
36    inherit Rappture::VisViewer
37
38    itk_option define -plotforeground plotForeground Foreground ""
39    itk_option define -plotbackground plotBackground Background ""
40
41    private variable _layersFrame "";   # Name of layers frame widget
42    private variable _mapsettings;      # Global map settings
43
44    constructor { hostlist args } {
45        Rappture::VisViewer::constructor $hostlist
46    } {
47        # defined below
48    }
49    destructor {
50        # defined below
51    }
52    public proc SetServerList { namelist } {
53        Rappture::VisViewer::SetServerList "geovis" $namelist
54    }
55    public method add {dataobj {settings ""}}
56    public method camera {option args}
57    public method delete {args}
58    public method disconnect {}
59    public method download {option args}
60    public method get {args}
61    public method isconnected {}
62    public method parameters {title args} {
63        # do nothing
64    }
65    public method scale {args}
66
67    protected method AdjustSetting {what {value ""}}
68    protected method Connect {}
69    protected method CurrentLayers {args}
70    protected method Disconnect {}
71    protected method DoResize {}
72    protected method DoRotate {}
73    protected method InitSettings { args  }
74    protected method KeyPress { key }
75    protected method KeyRelease { key }
76    protected method MouseClick { button x y }
77    protected method MouseDoubleClick { button x y }
78    protected method MouseDrag { button x y }
79    protected method MouseMotion {}
80    protected method MouseRelease { button x y }
81    protected method MouseScroll { direction }
82    protected method Pan {option x y}
83    protected method Rebuild {}
84    protected method ReceiveMapInfo { args }
85    protected method ReceiveImage { args }
86    protected method Rotate {option x y}
87    protected method Zoom {option {x 0} {y 0}}
88
89    # The following methods are only used by this class.
90    private method BuildCameraTab {}
91    private method BuildDownloadPopup { widget command }
92    private method BuildLayerTab {}
93    private method BuildTerrainTab {}
94    private method ChangeLayerVisibility { dataobj layer }
95    private method EventuallyHandleMotionEvent { x y }
96    private method EventuallyResize { w h }
97    private method EventuallyRotate { dx dy }
98    private method GetImage { args }
99    private method GetNormalizedMouse { x y }
100    private method MapIsGeocentric {}
101    private method SetLayerStyle { dataobj layer }
102    private method SetTerrainStyle { style }
103    private method SetOpacity { dataset }
104    private method UpdateLayerControls {}
105    private method EarthFile {}
106
107    private variable _dlist "";         # list of data objects
108    private variable _obj2datasets
109    private variable _obj2ovride;       # maps dataobj => style override
110    private variable _layers;           # Contains the names of all the
111                                        # layer in the server.
112    private variable _click;            # info used for rotate operations
113    private variable _view;             # view params for 3D view
114    private variable _settings
115    private variable _visibility
116    private variable _style;            # Array of current component styles.
117    private variable _initialStyle;     # Array of initial component styles.
118    private variable _reset 1;          # Indicates that server was reset and
119                                        # needs to be reinitialized.
120    private variable _initCamera 1;
121    private variable _haveTerrain 0
122
123    private variable _first ""     ;# This is the topmost dataset.
124    private variable _start 0
125    private variable _title ""
126
127    common _downloadPopup          ;# download options from popup
128    private common _hardcopy
129    private variable _width 0
130    private variable _height 0
131    private variable _resizePending 0
132    private variable _rotatePending 0
133    private variable _rotateDelay 150
134    private variable _motion
135    private variable _sendEarthFile 0
136    private variable _useServerManip 0
137}
138
139itk::usual MapViewer {
140    keep -background -foreground -cursor -font
141    keep -plotbackground -plotforeground
142}
143
144# ----------------------------------------------------------------------
145# CONSTRUCTOR
146# ----------------------------------------------------------------------
147itcl::body Rappture::MapViewer::constructor {hostlist args} {
148    set _serverType "geovis"
149
150    if { [catch {
151       
152    # Rebuild event
153    $_dispatcher register !rebuild
154    $_dispatcher dispatch $this !rebuild "[itcl::code $this Rebuild]; list"
155
156    # Resize event
157    $_dispatcher register !resize
158    $_dispatcher dispatch $this !resize "[itcl::code $this DoResize]; list"
159
160    # Rotate event
161    $_dispatcher register !rotate
162    $_dispatcher dispatch $this !rotate "[itcl::code $this DoRotate]; list"
163
164    # <Motion> event
165    $_dispatcher register !motion
166    $_dispatcher dispatch $this !motion "[itcl::code $this MouseMotion]; list"
167
168    #
169    # Populate parser with commands handle incoming requests
170    #
171    $_parser alias image    [itcl::code $this ReceiveImage]
172    $_parser alias map      [itcl::code $this ReceiveMapInfo]
173    $_parser alias camera   [itcl::code $this camera]
174
175    # Settings for mouse motion events: these are required
176    # to update the Lat/Long coordinate display
177    array set _motion {
178        compress        0
179        delay           100
180        enable          0
181        pending         0
182        x               0
183        y               0
184    }
185    # This array holds the Viewpoint parameters that the
186    # server sends on "camera get".
187    array set _view {
188        distance        1.0
189        heading         0.0
190        pitch           -89.9
191        srs             ""
192        verticalDatum   ""
193        x               0.0
194        y               0.0
195        z               0.0
196    }
197
198    # Note: grid types are "geodetic", "utm" and "mgrs"
199    # Currently only work in geocentric maps
200    array set _settings [subst {
201        camera-throw           0
202        coords-precision       5
203        coords-units           "latlong_decimal_degrees"
204        coords-visible         1
205        grid                   0
206        grid-type              "geodetic"
207        legend                 1
208        terrain-edges          0
209        terrain-lighting       0
210        terrain-vertscale      1.0
211        terrain-wireframe      0
212    }]
213    itk_component add view {
214        canvas $itk_component(plotarea).view \
215            -highlightthickness 0 -borderwidth 0
216    } {
217        usual
218        ignore -highlightthickness -borderwidth  -background
219    }
220
221    set c $itk_component(view)
222    #bind $c <KeyPress-Left>  [list %W xview scroll 10 units]
223    #bind $c <KeyPress-Right> [list %W xview scroll -10 units]
224    #bind $c <KeyPress-Up>    [list %W yview scroll 10 units]
225    #bind $c <KeyPress-Down>  [list %W yview scroll -10 units]
226    bind $c <Enter> "focus %W"
227    bind $c <Control-F1> [itcl::code $this ToggleConsole]
228
229    # Fix the scrollregion in case we go off screen
230    $c configure -scrollregion [$c bbox all]
231
232    set _map(id) [$c create image 0 0 -anchor nw -image $_image(plot)]
233    set _map(cwidth) -1
234    set _map(cheight) -1
235    set _map(zoom) 1.0
236    set _map(original) ""
237
238    set f [$itk_component(main) component controls]
239    itk_component add reset {
240        button $f.reset -borderwidth 1 -padx 1 -pady 1 \
241            -highlightthickness 0 \
242            -image [Rappture::icon reset-view] \
243            -command [itcl::code $this camera reset]
244    } {
245        usual
246        ignore -highlightthickness
247    }
248    pack $itk_component(reset) -side top -padx 2 -pady 2
249    Rappture::Tooltip::for $itk_component(reset) \
250        "Reset the view to the default zoom level"
251
252    itk_component add zoomin {
253        button $f.zin -borderwidth 1 -padx 1 -pady 1 \
254            -highlightthickness 0 \
255            -image [Rappture::icon zoom-in] \
256            -command [itcl::code $this Zoom in]
257    } {
258        usual
259        ignore -highlightthickness
260    }
261    pack $itk_component(zoomin) -side top -padx 2 -pady 2
262    Rappture::Tooltip::for $itk_component(zoomin) "Zoom in"
263
264    itk_component add zoomout {
265        button $f.zout -borderwidth 1 -padx 1 -pady 1 \
266            -highlightthickness 0 \
267            -image [Rappture::icon zoom-out] \
268            -command [itcl::code $this Zoom out]
269    } {
270        usual
271        ignore -highlightthickness
272    }
273    pack $itk_component(zoomout) -side top -padx 2 -pady 2
274    Rappture::Tooltip::for $itk_component(zoomout) "Zoom out"
275
276    BuildLayerTab
277    BuildTerrainTab
278    BuildCameraTab
279
280    # Legend
281
282    set _image(legend) [image create photo]
283    itk_component add legend {
284        canvas $itk_component(plotarea).legend -width 50 -highlightthickness 0
285    } {
286        usual
287        ignore -highlightthickness
288        rename -background -plotbackground plotBackground Background
289    }
290
291    # Hack around the Tk panewindow.  The problem is that the requested
292    # size of the 3d view isn't set until an image is retrieved from
293    # the server.  So the panewindow uses the tiny size.
294    set w 10000
295    pack forget $itk_component(view)
296    blt::table $itk_component(plotarea) \
297        0,0 $itk_component(view) -fill both -reqwidth $w
298    blt::table configure $itk_component(plotarea) c1 -resize none
299
300    bind $itk_component(view) <Configure> \
301        [itcl::code $this EventuallyResize %w %h]
302
303    if {$_useServerManip} {
304        # Bindings for keyboard events
305        bind $itk_component(view) <KeyPress> \
306            [itcl::code $this KeyPress %N]
307        bind $itk_component(view) <KeyRelease> \
308            [itcl::code $this KeyRelease %N]
309
310        # Bindings for rotation via mouse
311        bind $itk_component(view) <ButtonPress-1> \
312            [itcl::code $this MouseClick 1 %x %y]
313        bind $itk_component(view) <Double-1> \
314            [itcl::code $this MouseDoubleClick 1 %x %y]
315        bind $itk_component(view) <B1-Motion> \
316            [itcl::code $this MouseDrag 1 %x %y]
317        bind $itk_component(view) <ButtonRelease-1> \
318            [itcl::code $this MouseRelease 1 %x %y]
319
320        # Bindings for panning via mouse
321        bind $itk_component(view) <ButtonPress-2> \
322            [itcl::code $this MouseClick 2 %x %y]
323        bind $itk_component(view) <Double-2> \
324            [itcl::code $this MouseDoubleClick 2 %x %y]
325        bind $itk_component(view) <B2-Motion> \
326            [itcl::code $this MouseDrag 2 %x %y]
327        bind $itk_component(view) <ButtonRelease-2> \
328            [itcl::code $this MouseRelease 2 %x %y]
329
330        bind $itk_component(view) <ButtonPress-3> \
331            [itcl::code $this MouseClick 3 %x %y]
332        bind $itk_component(view) <Double-3> \
333            [itcl::code $this MouseDoubleClick 3 %x %y]
334        bind $itk_component(view) <B3-Motion> \
335            [itcl::code $this MouseDrag 3 %x %y]
336        bind $itk_component(view) <ButtonRelease-3> \
337            [itcl::code $this MouseRelease 3 %x %y]
338
339        # Binding for mouse motion events
340        if {$_motion(enable)} {
341            bind $itk_component(view) <Motion> \
342                [itcl::code $this EventuallyHandleMotionEvent %x %y]
343        }
344    } else {
345        # Bindings for panning via mouse
346        bind $itk_component(view) <ButtonPress-1> \
347            [itcl::code $this Pan click %x %y]
348        bind $itk_component(view) <B1-Motion> \
349            [itcl::code $this Pan drag %x %y]
350        bind $itk_component(view) <ButtonRelease-1> \
351            [itcl::code $this Pan release %x %y]
352        bind $itk_component(view) <Button-1> \
353            +[itcl::code $this SendCmd "map setpos %x %y"]
354        bind $itk_component(view) <Double-1> \
355            [itcl::code $this camera go %x %y 0.4]
356
357        # Bindings for rotation via mouse
358        bind $itk_component(view) <ButtonPress-2> \
359            [itcl::code $this Rotate click %x %y]
360        bind $itk_component(view) <B2-Motion> \
361            [itcl::code $this Rotate drag %x %y]
362        bind $itk_component(view) <ButtonRelease-2> \
363            [itcl::code $this Rotate release %x %y]
364
365        # Bindings for zoom via mouse
366        bind $itk_component(view) <ButtonPress-3> \
367            [itcl::code $this Zoom click %x %y]
368        bind $itk_component(view) <B3-Motion> \
369            [itcl::code $this Zoom drag %x %y]
370        bind $itk_component(view) <ButtonRelease-3> \
371            [itcl::code $this Zoom release %x %y]
372        bind $itk_component(view) <Double-3> \
373            [itcl::code $this camera go %x %y 2.5]
374        bind $itk_component(view) <Double-3> \
375            +[itcl::code $this SendCmd "map setpos %x %y"]
376
377        # Bindings for panning via keyboard
378        bind $itk_component(view) <KeyPress-Left> \
379            [itcl::code $this Pan set 10 0]
380        bind $itk_component(view) <KeyPress-Right> \
381            [itcl::code $this Pan set -10 0]
382        bind $itk_component(view) <KeyPress-Up> \
383            [itcl::code $this Pan set 0 -10]
384        bind $itk_component(view) <KeyPress-Down> \
385            [itcl::code $this Pan set 0 10]
386
387        # Binding for mouse motion events
388        set _motion(compress) 1
389        if {$_motion(enable)} {
390            bind $itk_component(view) <Motion> \
391                [itcl::code $this EventuallyHandleMotionEvent %x %y]
392        }
393    }
394
395    bind $itk_component(view) <Shift-KeyPress-Left> \
396        [itcl::code $this Pan set 2 0]
397    bind $itk_component(view) <Shift-KeyPress-Right> \
398        [itcl::code $this Pan set -2 0]
399    bind $itk_component(view) <Shift-KeyPress-Up> \
400        [itcl::code $this Pan set 0 -2]
401    bind $itk_component(view) <Shift-KeyPress-Down> \
402        [itcl::code $this Pan set 0 2]
403
404    # Bindings for zoom via keyboard
405    bind $itk_component(view) <KeyPress-Prior> \
406        [itcl::code $this Zoom out]
407    bind $itk_component(view) <KeyPress-Next> \
408        [itcl::code $this Zoom in]
409
410    bind $itk_component(view) <Enter> "focus $itk_component(view)"
411
412    if {[string equal "x11" [tk windowingsystem]]} {
413        # Bindings for zoom via mouse
414        if {$_useServerManip} {
415            bind $itk_component(view) <4> [itcl::code $this MouseScroll up]
416            bind $itk_component(view) <5> [itcl::code $this MouseScroll down]
417        } else {
418            bind $itk_component(view) <4> [itcl::code $this Zoom out]
419            bind $itk_component(view) <5> [itcl::code $this Zoom in]
420        }
421    }
422
423    set _image(download) [image create photo]
424
425    eval itk_initialize $args
426    Connect
427} errs] != 0 } {
428        puts stderr errs=$errs
429    }
430}
431
432# ----------------------------------------------------------------------
433# DESTRUCTOR
434# ----------------------------------------------------------------------
435itcl::body Rappture::MapViewer::destructor {} {
436    Disconnect
437    $_dispatcher cancel !rebuild
438    $_dispatcher cancel !resize
439    $_dispatcher cancel !rotate
440    image delete $_image(plot)
441    image delete $_image(download)
442}
443
444itcl::body Rappture::MapViewer::DoResize {} {
445    set sendResize 1
446    if { $_width < 2 } {
447        set _width 500
448        set sendResize 0
449    }
450    if { $_height < 2 } {
451        set _height 500
452        set sendResize 0
453    }
454    set _start [clock clicks -milliseconds]
455    if {$sendResize} {
456        SendCmd "screen size $_width $_height"
457    }
458    set _resizePending 0
459}
460
461itcl::body Rappture::MapViewer::DoRotate {} {
462    SendCmd "camera rotate $_view(azimuth) $_view(elevation)"
463    set _rotatePending 0
464}
465
466itcl::body Rappture::MapViewer::EventuallyResize { w h } {
467    set _width $w
468    set _height $h
469    if { !$_resizePending } {
470        set _resizePending 1
471        $_dispatcher event -after 200 !resize
472    }
473}
474
475itcl::body Rappture::MapViewer::EventuallyRotate { dx dy } {
476    set _view(azimuth) $dx
477    set _view(elevation) $dy
478    if { !$_rotatePending } {
479        set _rotatePending 1
480        $_dispatcher event -after $_rotateDelay !rotate
481    }
482}
483
484# ----------------------------------------------------------------------
485# USAGE: add <dataobj> ?<settings>?
486#
487# Clients use this to add a data object to the plot.  The optional
488# <settings> are used to configure the plot.  Allowed settings are
489# -color, -brightness, -width, -linestyle, and -raise.
490# ----------------------------------------------------------------------
491itcl::body Rappture::MapViewer::add {dataobj {settings ""}} {
492    array set params {
493        -color auto
494        -width 1
495        -linestyle solid
496        -brightness 0
497        -raise 0
498        -description ""
499        -param ""
500        -type ""
501    }
502    array set params $settings
503    set params(-description) ""
504    set params(-param) ""
505    array set params $settings
506
507    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
508        # can't handle -autocolors yet
509        set params(-color) black
510    }
511    set pos [lsearch -exact $_dlist $dataobj]
512    if {$pos < 0} {
513        #if {[llength $_dlist] > 0} {
514        #    error "Can't add more than 1 map to mapviewer"
515        #}
516        lappend _dlist $dataobj
517    }
518    set _obj2ovride($dataobj-color) $params(-color)
519    set _obj2ovride($dataobj-width) $params(-width)
520    set _obj2ovride($dataobj-raise) $params(-raise)
521    $_dispatcher event -idle !rebuild
522}
523
524# ----------------------------------------------------------------------
525# USAGE: delete ?<dataobj1> <dataobj2> ...?
526#
527#       Clients use this to delete a dataobj from the plot.  If no dataobjs
528#       are specified, then all dataobjs are deleted.  No data objects are
529#       deleted.  They are only removed from the display list.
530#
531# ----------------------------------------------------------------------
532itcl::body Rappture::MapViewer::delete {args} {
533    if { [llength $args] == 0} {
534        set args $_dlist
535    }
536    # Delete all specified dataobjs
537    set changed 0
538    foreach dataobj $args {
539        set pos [lsearch -exact $_dlist $dataobj]
540        if { $pos < 0 } {
541            continue;                   # Don't know anything about it.
542        }
543        # When a map is marked deleted, we hide its layers.
544        foreach layer [$dataobj layers] {
545            SendCmd "map layer visible 0 $layer"
546            set _visibility($layer) 0
547        }
548        # Remove it from the dataobj list.
549        set _dlist [lreplace $_dlist $pos $pos]
550        array unset _obj2ovride $dataobj-*
551        array unset _settings $dataobj-*
552        set changed 1
553    }
554    # If anything changed, then rebuild the plot
555    if { $changed } {
556        $_dispatcher event -idle !rebuild
557    }
558}
559
560# ----------------------------------------------------------------------
561# USAGE: get ?-objects?
562# USAGE: get ?-visible?
563# USAGE: get ?-image view?
564#
565# Clients use this to query the list of objects being plotted, in
566# order from bottom to top of this result.  The optional "-image"
567# flag can also request the internal images being shown.
568# ----------------------------------------------------------------------
569itcl::body Rappture::MapViewer::get {args} {
570    if {[llength $args] == 0} {
571        set args "-objects"
572    }
573
574    set op [lindex $args 0]
575    switch -- $op {
576        "-objects" {
577            # put the dataobj list in order according to -raise options
578            set dlist {}
579            foreach dataobj $_dlist {
580                if { ![$dataobj isvalid] } {
581                    continue
582                }
583                if {[info exists _obj2ovride($dataobj-raise)] &&
584                    $_obj2ovride($dataobj-raise)} {
585                    set dlist [linsert $dlist 0 $dataobj]
586                } else {
587                    lappend dlist $dataobj
588                }
589            }
590            return $dlist
591        }
592        "-visible" {
593            set dlist {}
594            foreach dataobj $_dlist {
595                if { ![$dataobj isvalid] } {
596                    continue
597                }
598                if { ![info exists _obj2ovride($dataobj-raise)] } {
599                    # No setting indicates that the object isn't visible.
600                    continue
601                }
602                # Otherwise use the -raise parameter to put the object to
603                # the front of the list.
604                if { $_obj2ovride($dataobj-raise) } {
605                    set dlist [linsert $dlist 0 $dataobj]
606                } else {
607                    lappend dlist $dataobj
608                }
609            }
610            return $dlist
611        }           
612        -image {
613            if {[llength $args] != 2} {
614                error "wrong # args: should be \"get -image view\""
615            }
616            switch -- [lindex $args end] {
617                view {
618                    return $_image(plot)
619                }
620                default {
621                    error "bad image name \"[lindex $args end]\": should be view"
622                }
623            }
624        }
625        default {
626            error "bad option \"$op\": should be -objects or -image"
627        }
628    }
629}
630
631itcl::body Rappture::MapViewer::MapIsGeocentric {} {
632    if { [info exists _mapsettings(type)] } {
633        return [expr {$_mapsettings(type) eq "geocentric"}]
634    } else {
635        return 0
636    }
637}
638
639# ----------------------------------------------------------------------
640# USAGE: scale ?<data1> <data2> ...?
641#
642# Sets the default limits for the overall plot according to the
643# limits of the data for all of the given <data> objects.  This
644# accounts for all objects--even those not showing on the screen.
645# Because of this, the limits are appropriate for all objects as
646# the user scans through data in the ResultSet viewer.
647# ----------------------------------------------------------------------
648itcl::body Rappture::MapViewer::scale {args} {
649    array unset _mapsettings
650    set _haveTerrain 0
651
652    # Verify that all the maps have the same global settings. For example,
653    # you can't have one map type "geocentric" and the other "projected".
654
655    foreach dataobj $args {
656        if { ![$dataobj isvalid] } {
657            continue
658        }
659        array unset hints
660        array set hints [$dataobj hints]
661        if { ![info exists _mapsettings(label)] } {
662            set _mapsettings(label) $hints(label)
663        }
664        if { ![info exists _mapsettings(style)] } {
665            set _mapsettings(style) $hints(style)
666        }
667        if { ![info exists _mapsettings(type)] } {
668            set _mapsettings(type) $hints(type)
669        } elseif { $hints(type) != $_mapsettings(type) } {
670            error "maps \"$hints(label)\" have differing types"
671        }
672        if { ![info exists _mapsettings(projection)] } {
673            set _mapsettings(projection) $hints(projection)
674        } elseif { $hints(projection) != $_mapsettings(projection) } {
675            error "maps \"$hints(label)\" have differing projections"
676        }
677        if { $hints(extents) != "" } {
678            if { ![info exists _mapsettings(extents)] } {
679                set _mapsettings(extents) $hints(extents)
680            }
681            foreach {x1 y1 x2 y2} $hints(extents) break
682            if { ![info exists _mapsettings(x1)] || $x1 < $_mapsettings(x1) } {
683                set _mapsettings(x1) $x1
684            }
685            if { ![info exists _mapsettings(y1)] || $y1 < $_mapsettings(y1) } {
686                set _mapsettings(y1) $y1
687            }
688            if { ![info exists _mapsettings(x2)] || $x2 > $_mapsettings(x2) } {
689                set _mapsettings(x2) $x2
690            }
691            if { ![info exists _mapsettings(y2)] || $y2 > $_mapsettings(y2) } {
692                set _mapsettings(y2) $y2
693            }
694        }
695        foreach layer [$dataobj layers] {
696            if { [$dataobj type $layer] == "elevation" } {
697                set _haveTerrain 1
698                break
699            }
700        }
701    }
702    if { $_haveTerrain } {
703        if { [$itk_component(main) exists "Terrain Settings"] } {
704            # TODO: Enable controls like vertical scale that only have
705            # an effect when terrain is present
706        }
707    }
708}
709
710# ----------------------------------------------------------------------
711# USAGE: download coming
712# USAGE: download controls <downloadCommand>
713# USAGE: download now
714#
715# Clients use this method to create a downloadable representation
716# of the plot.  Returns a list of the form {ext string}, where
717# "ext" is the file extension (indicating the type of data) and
718# "string" is the data itself.
719# ----------------------------------------------------------------------
720itcl::body Rappture::MapViewer::download {option args} {
721    switch $option {
722        coming {
723            if {[catch {
724                blt::winop snap $itk_component(plotarea) $_image(download)
725            }]} {
726                $_image(download) configure -width 1 -height 1
727                $_image(download) put #000000
728            }
729        }
730        controls {
731            set popup .mapviewerdownload
732            if { ![winfo exists .mapviewerdownload] } {
733                set inner [BuildDownloadPopup $popup [lindex $args 0]]
734            } else {
735                set inner [$popup component inner]
736            }
737            set _downloadPopup(image_controls) $inner.image_frame
738            set num [llength [get]]
739            set num [expr {($num == 1) ? "1 result" : "$num results"}]
740            set word [Rappture::filexfer::label downloadWord]
741            $inner.summary configure -text "$word $num in the following format:"
742            update idletasks            ;# Fix initial sizes
743            return $popup
744        }
745        now {
746            set popup .mapviewerdownload
747            if {[winfo exists .mapviewerdownload]} {
748                $popup deactivate
749            }
750            switch -- $_downloadPopup(format) {
751                "image" {
752                    return [$this GetImage [lindex $args 0]]
753                }
754            }
755            return ""
756        }
757        default {
758            error "bad option \"$option\": should be coming, controls, now"
759        }
760    }
761}
762
763# ----------------------------------------------------------------------
764# USAGE: Connect ?<host:port>,<host:port>...?
765#
766# Clients use this method to establish a connection to a new
767# server, or to reestablish a connection to the previous server.
768# Any existing connection is automatically closed.
769# ----------------------------------------------------------------------
770itcl::body Rappture::MapViewer::Connect {} {
771    global readyForNextFrame
772    set readyForNextFrame 1
773    set _reset 1
774    set _hosts [GetServerList "geovis"]
775    if { "" == $_hosts } {
776        return 0
777    }
778    set result [VisViewer::Connect $_hosts]
779    if { $result } {
780        if { $_reportClientInfo }  {
781            # Tell the server the viewer, hub, user and session.
782            # Do this immediately on connect before buffering any commands
783            global env
784
785            set info {}
786            set user "???"
787            if { [info exists env(USER)] } {
788                set user $env(USER)
789            }
790            set session "???"
791            if { [info exists env(SESSION)] } {
792                set session $env(SESSION)
793            }
794            lappend info "hub" [exec hostname]
795            lappend info "client" "mapviewer"
796            lappend info "user" $user
797            lappend info "session" $session
798            SendCmd "clientinfo [list $info]"
799        }
800
801        set w [winfo width $itk_component(view)]
802        set h [winfo height $itk_component(view)]
803        EventuallyResize $w $h
804    }
805    return $result
806}
807
808#
809# isconnected --
810#
811#       Indicates if we are currently connected to the visualization server.
812#
813itcl::body Rappture::MapViewer::isconnected {} {
814    return [VisViewer::IsConnected]
815}
816
817#
818# disconnect --
819#
820itcl::body Rappture::MapViewer::disconnect {} {
821    Disconnect
822    set _reset 1
823}
824
825#
826# Disconnect --
827#
828#       Clients use this method to disconnect from the current rendering
829#       server.
830#
831itcl::body Rappture::MapViewer::Disconnect {} {
832    VisViewer::Disconnect
833
834    $_dispatcher cancel !rebuild
835    $_dispatcher cancel !resize
836    $_dispatcher cancel !rotate
837    $_dispatcher cancel !motion
838    # disconnected -- no more data sitting on server
839    array unset _layers
840    array unset _layersFrame
841    global readyForNextFrame
842    set readyForNextFrame 1
843}
844
845# ----------------------------------------------------------------------
846# USAGE: ReceiveImage -bytes <size> -type <type> -token <token>
847#
848# Invoked automatically whenever the "image" command comes in from
849# the rendering server.  Indicates that binary image data with the
850# specified <size> will follow.
851# ----------------------------------------------------------------------
852itcl::body Rappture::MapViewer::ReceiveImage { args } {
853    global readyForNextFrame
854    set readyForNextFrame 1
855    array set info {
856        -token "???"
857        -bytes 0
858        -type image
859    }
860    array set info $args
861    set bytes [ReceiveBytes $info(-bytes)]
862    if { $info(-type) == "image" } {
863        $_image(plot) configure -data $bytes
864    } elseif { $info(type) == "print" } {
865        set tag $this-print-$info(-token)
866        set _hardcopy($tag) $bytes
867    }
868}
869
870#
871# ReceiveMapInfo --
872#
873itcl::body Rappture::MapViewer::ReceiveMapInfo { args } {
874    if { ![isconnected] } {
875        return
876    }
877    set option [lindex $args 0]
878    switch -- $option {
879        "coords" {
880            set len [llength $args]
881            if {$len < 4} {
882                puts stderr "Coords out of range"
883            } elseif {$len < 6} {
884                foreach { x y z } [lrange $args 1 end] break
885                puts stderr "Coords: $x $y $z"
886            } else {
887                foreach { x y z pxX pxY } [lrange $args 1 end] break
888                puts stderr "Coords($pxX,$pxY): $x $y $z"
889            }
890        }
891        "names" {
892            foreach { name } [lindex $args 1] {
893                puts stderr "layer: $name"
894            }
895        }
896        default {
897            error "unknown map option \"$option\" from server"
898        }
899    }
900}
901
902# ----------------------------------------------------------------------
903# USAGE: Rebuild
904#
905# Called automatically whenever something changes that affects the
906# data in the widget.  Clears any existing data and rebuilds the
907# widget to display new data.
908# ----------------------------------------------------------------------
909itcl::body Rappture::MapViewer::Rebuild {} {
910    set w [winfo width $itk_component(view)]
911    set h [winfo height $itk_component(view)]
912    if { $w < 2 || $h < 2 } {
913        $_dispatcher event -idle !rebuild
914        return
915    }
916
917    # Turn on buffering of commands to the server.  We don't want to be
918    # preempted by a server disconnect/reconnect (which automatically
919    # generates a new call to Rebuild).
920    StartBufferingCommands
921
922    if { $_reset } {
923        set _width $w
924        set _height $h
925        DoResize
926
927        if { [info exists _mapsettings(type)] } {
928            # The map must be reset once before any layers are added This
929            # should not be done more than once as it is very expensive.
930            if {$_sendEarthFile} {
931                set bytes [EarthFile]
932                if {0} {
933                    set f [open "/tmp/map.earth" "w"]
934                    puts $f $bytes
935                    close $f
936                }
937                set length [string length $bytes]
938                SendCmd "map load data follows $length"
939                append _outbuf $bytes
940            } else {
941                if { $_mapsettings(type) == "geocentric" } {
942                    SendCmd "map reset geocentric"
943                }  else {
944                    set proj $_mapsettings(projection)
945                    if { $proj == "" } {
946                        SendCmd "map reset projected global-mercator"
947                    } elseif { ![info exists _mapsettings(extents)] || $_mapsettings(extents) == "" } {
948                        SendCmd [list map reset "projected" $proj]
949                    } else {
950                        #foreach {x1 y1 x2 y2} $_mapsettings(extents) break
951                        foreach key "x1 y1 x2 y2" {
952                            set $key $_mapsettings($key)
953                        }
954                        SendCmd [list map reset "projected" $proj $x1 $y1 $x2 $y2]
955                    }
956                }
957                # XXX: Remove these after implementing batch load of layers with reset
958                SendCmd "map layer delete base"
959            }
960
961            # Most terrain settings are global to the map and apply even
962            # if there is no elevation layer.  The exception is the
963            # vertical scale, which only applies if there is an elevation
964            # layer
965            if { [info exists _mapsettings(style)] } {
966                SetTerrainStyle $_mapsettings(style)
967            } else {
968                InitSettings terrain-edges terrain-lighting \
969                    terrain-vertscale terrain-wireframe
970            }
971            InitSettings coords-visible
972        } else {
973            error "No map settings on reset"
974        }
975    }
976
977    set _first ""
978    set count 0
979
980    foreach dataobj [get -objects] {
981        set _obj2datasets($dataobj) ""
982        foreach layer [$dataobj layers] {
983            array unset info
984            array set info [$dataobj layer $layer]
985            if { ![info exists _layers($layer)] } {
986                if { $_reportClientInfo }  {
987                    set cinfo {}
988                    lappend cinfo "tool_id"       [$dataobj hints toolid]
989                    lappend cinfo "tool_name"     [$dataobj hints toolname]
990                    lappend cinfo "tool_title"    [$dataobj hints tooltitle]
991                    lappend cinfo "tool_command"  [$dataobj hints toolcommand]
992                    lappend cinfo "tool_revision" [$dataobj hints toolrevision]
993                    lappend cinfo "dataset_label" [$dataobj hints label]
994                    lappend cinfo "dataset_tag"   $layer
995                    SendCmd [list "clientinfo" $cinfo]
996                }
997                set _layers($layer) 1
998                SetLayerStyle $dataobj $layer
999            }
1000            lappend _obj2datasets($dataobj) $layer
1001            # FIXME: This is overriding all layers' initial visibility setting
1002            if { [info exists _obj2ovride($dataobj-raise)] } {
1003                SendCmd "map layer visible 1 $layer"
1004                set _visibility($layer) 1
1005                #SetLayerOpacity $layer
1006            }
1007        }
1008    }
1009
1010    if {$_reset} {
1011        if {$_initCamera} {
1012            # If this is the first Rebuild, we need to
1013            # set up the initial view settings if there
1014            # are any
1015            if { [info exists _mapsettings(camera)] } {
1016                set location $_mapsettings(camera)
1017                if { $location != "" } {
1018                    array set _view $location
1019                    camera set all
1020                }
1021            }
1022            set _initCamera 0
1023        } else {
1024            # Restore view from before reconnect
1025            camera set all
1026        }
1027    }
1028
1029    UpdateLayerControls
1030    set _reset 0
1031    global readyForNextFrame
1032    set readyForNextFrame 0;            # Don't advance to the next frame
1033                                        # until we get an image.
1034
1035    # Actually write the commands to the server socket.  If it fails, we
1036    # don't care.  We're finished here.
1037    blt::busy hold $itk_component(hull)
1038    StopBufferingCommands
1039    blt::busy release $itk_component(hull)
1040}
1041
1042# ----------------------------------------------------------------------
1043# USAGE: CurrentLayers ?-all -visible? ?dataobjs?
1044#
1045# Returns a list of server IDs for the current datasets being displayed.
1046# This is normally a single ID, but it might be a list of IDs if the
1047# current data object has multiple components.
1048# ----------------------------------------------------------------------
1049itcl::body Rappture::MapViewer::CurrentLayers {args} {
1050    set flag [lindex $args 0]
1051    switch -- $flag {
1052        "-all" {
1053            if { [llength $args] > 1 } {
1054                error "CurrentLayers: can't specify dataobj after \"-all\""
1055            }
1056            set dlist [get -objects]
1057        }
1058        "-visible" {
1059            if { [llength $args] > 1 } {
1060                set dlist {}
1061                set args [lrange $args 1 end]
1062                foreach dataobj $args {
1063                    if { [info exists _obj2ovride($dataobj-raise)] } {
1064                        lappend dlist $dataobj
1065                    }
1066                }
1067            } else {
1068                set dlist [get -visible]
1069            }
1070        }           
1071        default {
1072            set dlist $args
1073        }
1074    }
1075    set rlist ""
1076    foreach dataobj $dlist {
1077        foreach layer [$dataobj layers] {
1078            if { [info exists _layers($layer)] && $_layers($layer) } {
1079                lappend rlist $layer
1080            }
1081        }
1082    }
1083    return $rlist
1084}
1085
1086itcl::body Rappture::MapViewer::KeyPress {k} {
1087    SendCmd "key press $k"
1088}
1089
1090itcl::body Rappture::MapViewer::KeyRelease {k} {
1091    SendCmd "key release $k"
1092}
1093
1094itcl::body Rappture::MapViewer::GetNormalizedMouse {x y} {
1095    set w [winfo width $itk_component(view)]
1096    set h [winfo height $itk_component(view)]
1097    set x [expr {(2.0 * double($x)/$w) - 1.0}]
1098    set y [expr {(2.0 * double($y)/$h) - 1.0}]
1099    return [list $x $y]
1100}
1101
1102itcl::body Rappture::MapViewer::MouseClick {button x y} {
1103    SendCmd "mouse click $button $x $y"
1104}
1105
1106itcl::body Rappture::MapViewer::MouseDoubleClick {button x y} {
1107    SendCmd "mouse dblclick $button $x $y"
1108}
1109
1110itcl::body Rappture::MapViewer::MouseDrag {button x y} {
1111    SendCmd "mouse drag $button $x $y"
1112}
1113
1114itcl::body Rappture::MapViewer::MouseRelease {button x y} {
1115    SendCmd "mouse release $button $x $y"
1116}
1117
1118itcl::body Rappture::MapViewer::MouseMotion {} {
1119    SendCmd "mouse motion $_motion(x) $_motion(y)"
1120    set _motion(pending) 0
1121}
1122
1123itcl::body Rappture::MapViewer::MouseScroll {direction} {
1124    switch -- $direction {
1125        "up" {
1126            SendCmd "mouse scroll 1"
1127        }
1128        "down" {
1129            SendCmd "mouse scroll -1"
1130        }
1131    }
1132}
1133
1134#
1135# EventuallyHandleMotionEvent --
1136#
1137#       This routine compresses (no button press) motion events.  It
1138#       delivers a server mouse command once every 100 milliseconds (if a
1139#       motion event is pending).
1140#
1141itcl::body Rappture::MapViewer::EventuallyHandleMotionEvent {x y} {
1142    set _motion(x) $x
1143    set _motion(y) $y
1144    if { !$_motion(compress) } {
1145        MouseMotion
1146        return
1147    }
1148    if { !$_motion(pending) } {
1149        set _motion(pending) 1
1150        $_dispatcher event -after $_motion(delay) !motion
1151    }
1152}
1153
1154# ----------------------------------------------------------------------
1155# USAGE: Zoom in
1156# USAGE: Zoom out
1157# USAGE: Zoom reset
1158#        $this Zoom click x y
1159#        $this Zoom drag x y
1160#        $this Zoom release x y
1161#
1162# Called automatically when the user clicks on one of the zoom
1163# controls for this widget.  Changes the zoom for the current view.
1164# Also implements mouse zoom.
1165# ----------------------------------------------------------------------
1166itcl::body Rappture::MapViewer::Zoom {option {x 0} {y 0}} {
1167    switch -- $option {
1168        "in" {
1169            # z here is normalized mouse Y delta
1170            set z -0.25
1171            SendCmd "camera zoom $z"
1172        }
1173        "out" {
1174            # z here is normalized mouse Y delta
1175            set z 0.25
1176            SendCmd "camera zoom $z"
1177        }
1178        "reset" {
1179            SendCmd "camera dist $_view(distance)"
1180        }
1181        "click" {
1182            set _click(x) $x
1183            set _click(y) $y
1184            $itk_component(view) configure -cursor hand1
1185        }
1186        "drag" {
1187            if { ![info exists _click(x)] } {
1188                set _click(x) $x
1189            }
1190            if { ![info exists _click(y)] } {
1191                set _click(y) $y
1192            }
1193            set h [winfo height $itk_component(view)]
1194            set dy [expr ($_click(y) - $y)/double($h)]
1195            set _click(x) $x
1196            set _click(y) $y
1197            if {[expr (abs($dy) > 0.0)]} {
1198                SendCmd "camera zoom $dy"
1199            }
1200        }
1201        "release" {
1202            Zoom drag $x $y
1203            $itk_component(view) configure -cursor ""
1204        }
1205    }
1206}
1207
1208# ----------------------------------------------------------------------
1209# USAGE: Rotate click <x> <y>
1210# USAGE: Rotate drag <x> <y>
1211# USAGE: Rotate release <x> <y>
1212#
1213# Called automatically when the user clicks/drags/releases in the
1214# plot area.  Moves the plot according to the user's actions.
1215# ----------------------------------------------------------------------
1216itcl::body Rappture::MapViewer::Rotate {option x y} {
1217    switch -- $option {
1218        "click" {
1219            $itk_component(view) configure -cursor fleur
1220            set _click(x) $x
1221            set _click(y) $y
1222        }
1223        "drag" {
1224            if {[array size _click] == 0} {
1225                Rotate click $x $y
1226            } else {
1227                set w [winfo width $itk_component(view)]
1228                set h [winfo height $itk_component(view)]
1229                if {$w <= 0 || $h <= 0} {
1230                    return
1231                }
1232                set dx [expr ($x - $_click(x))/double($w)]
1233                set dy [expr ($_click(y) - $y)/double($h)]
1234                set _click(x) $x
1235                set _click(y) $y
1236                if {[expr (abs($dx) > 0.0 || abs($dy) > 0.0)]} {
1237                    SendCmd "camera rotate $dx $dy"
1238                    #EventuallyRotate $dx $dy
1239                }
1240            }
1241        }
1242        "release" {
1243            Rotate drag $x $y
1244            $itk_component(view) configure -cursor ""
1245            catch {unset _click}
1246        }
1247        default {
1248            error "bad option \"$option\": should be click, drag, release"
1249        }
1250    }
1251}
1252
1253# ----------------------------------------------------------------------
1254# USAGE: $this Pan set x y
1255#        $this Pan click x y
1256#        $this Pan drag x y
1257#        $this Pan release x y
1258#
1259# Called automatically when the user clicks on one of the zoom
1260# controls for this widget.  Changes the zoom for the current view.
1261# ----------------------------------------------------------------------
1262itcl::body Rappture::MapViewer::Pan {option x y} {
1263    switch -- $option {
1264        "set" {
1265            set w [winfo width $itk_component(view)]
1266            set h [winfo height $itk_component(view)]
1267            set x [expr $x / double($w)]
1268            set y [expr $y / double($h)]
1269            if {[expr (abs($x) > 0.0 || abs($y) > 0.0)]} {
1270                SendCmd "camera pan $x $y"
1271            }
1272            return
1273        }
1274        "click" {
1275            set _click(x) $x
1276            set _click(y) $y
1277            $itk_component(view) configure -cursor hand1
1278        }
1279        "drag" {
1280            if { ![info exists _click(x)] } {
1281                set _click(x) $x
1282            }
1283            if { ![info exists _click(y)] } {
1284                set _click(y) $y
1285            }
1286            set w [winfo width $itk_component(view)]
1287            set h [winfo height $itk_component(view)]
1288            set dx [expr ($x - $_click(x))/double($w)]
1289            set dy [expr ($_click(y) - $y)/double($h)]
1290            set _click(x) $x
1291            set _click(y) $y
1292            if {[expr (abs($dx) > 0.0 || abs($dy) > 0.0)]} {
1293                SendCmd "camera pan $dx $dy"
1294            }
1295        }
1296        "release" {
1297            Pan drag $x $y
1298            $itk_component(view) configure -cursor ""
1299        }
1300        default {
1301            error "unknown option \"$option\": should set, click, drag, or release"
1302        }
1303    }
1304}
1305
1306# ----------------------------------------------------------------------
1307# USAGE: InitSettings <what> ?<value>?
1308#
1309# Used internally to update rendering settings whenever parameters
1310# change in the popup settings panel.  Sends the new settings off
1311# to the back end.
1312# ----------------------------------------------------------------------
1313itcl::body Rappture::MapViewer::InitSettings { args } {
1314    foreach setting $args {
1315        AdjustSetting $setting
1316    }
1317}
1318
1319#
1320# AdjustSetting --
1321#
1322#       Changes/updates a specific setting in the widget.  There are
1323#       usually user-setable option.  Commands are sent to the render
1324#       server.
1325#
1326itcl::body Rappture::MapViewer::AdjustSetting {what {value ""}} {
1327    if { ![isconnected] } {
1328        return
1329    }
1330    switch -- $what {
1331        "coords-visible" - "coords-precision" - "coords-units" {
1332            set bool $_settings(coords-visible)
1333            set units $_settings(coords-units)
1334            set precision $_settings(coords-precision)
1335            SendCmd "map posdisp $bool $units $precision"
1336        }
1337        "grid" - "grid-type" {
1338            set bool $_settings(grid)
1339            set gridType $_settings(grid-type)
1340            SendCmd "map grid $bool $gridType"
1341        }
1342        "camera-throw" {
1343            set bool $_settings($what)
1344            SendCmd "camera throw $bool"
1345        }
1346        "terrain-edges" {
1347            set bool $_settings($what)
1348            SendCmd "map terrain edges $bool"
1349        }
1350        "terrain-lighting" {
1351            set bool $_settings($what)
1352            SendCmd "map terrain lighting $bool"
1353        }
1354        "terrain-palette" {
1355            set cmap [$itk_component(terrainpalette) value]
1356            #SendCmd "map terrain colormap $cmap"
1357        }
1358        "terrain-vertscale" {
1359            set val $_settings($what)
1360            SendCmd "map terrain vertscale $val"
1361        }
1362        "terrain-wireframe" {
1363            set bool $_settings($what)
1364            SendCmd "map terrain wireframe $bool"
1365        }
1366        default {
1367            error "don't know how to fix $what"
1368        }
1369    }
1370}
1371
1372# ----------------------------------------------------------------------
1373# CONFIGURATION OPTION: -plotbackground
1374# ----------------------------------------------------------------------
1375itcl::configbody Rappture::MapViewer::plotbackground {
1376    if { [isconnected] } {
1377        foreach {r g b} [Color2RGB $itk_option(-plotbackground)] break
1378        SendCmd "screen bgcolor $r $g $b"
1379    }
1380}
1381
1382# ----------------------------------------------------------------------
1383# CONFIGURATION OPTION: -plotforeground
1384# ----------------------------------------------------------------------
1385itcl::configbody Rappture::MapViewer::plotforeground {
1386    if { [isconnected] } {
1387        foreach {r g b} [Color2RGB $itk_option(-plotforeground)] break
1388        #fix this!
1389        #SendCmd "color background $r $g $b"
1390    }
1391}
1392
1393itcl::body Rappture::MapViewer::BuildTerrainTab {} {
1394
1395    set fg [option get $itk_component(hull) font Font]
1396    #set bfg [option get $itk_component(hull) boldFont Font]
1397
1398    set inner [$itk_component(main) insert end \
1399        -title "Terrain Settings" \
1400        -icon [Rappture::icon surface]]
1401    $inner configure -borderwidth 4
1402
1403    checkbutton $inner.posdisp \
1404        -text "Show Coordinate Readout" \
1405        -variable [itcl::scope _settings(coords-visible)] \
1406        -command [itcl::code $this AdjustSetting coords-visible] \
1407        -font "Arial 9" -anchor w
1408
1409    checkbutton $inner.grid \
1410        -text "Show Graticule" \
1411        -variable [itcl::scope _settings(grid)] \
1412        -command [itcl::code $this AdjustSetting grid] \
1413        -font "Arial 9" -anchor w
1414
1415    checkbutton $inner.wireframe \
1416        -text "Show Wireframe" \
1417        -variable [itcl::scope _settings(terrain-wireframe)] \
1418        -command [itcl::code $this AdjustSetting terrain-wireframe] \
1419        -font "Arial 9" -anchor w
1420
1421    checkbutton $inner.lighting \
1422        -text "Enable Lighting" \
1423        -variable [itcl::scope _settings(terrain-lighting)] \
1424        -command [itcl::code $this AdjustSetting terrain-lighting] \
1425        -font "Arial 9" -anchor w
1426
1427    checkbutton $inner.edges \
1428        -text "Show Edges" \
1429        -variable [itcl::scope _settings(terrain-edges)] \
1430        -command [itcl::code $this AdjustSetting terrain-edges] \
1431        -font "Arial 9" -anchor w
1432
1433    label $inner.palette_l -text "Palette" -font "Arial 9" -anchor w
1434    itk_component add terrainpalette {
1435        Rappture::Combobox $inner.palette -width 10 -editable no
1436    }
1437    $inner.palette choices insert end [GetColormapList]
1438
1439    $itk_component(terrainpalette) value "BCGYR"
1440    bind $inner.palette <<Value>> \
1441        [itcl::code $this AdjustSetting terrain-palette]
1442
1443    label $inner.vscale_l -text "Vertical Scale" -font "Arial 9" -anchor w
1444    ::scale $inner.vscale -from 0 -to 10 -orient horizontal \
1445        -variable [itcl::scope _settings(terrain-vertscale)] \
1446        -width 10 \
1447        -resolution 0.1 \
1448        -showvalue on \
1449        -command [itcl::code $this AdjustSetting terrain-vertscale]
1450    $inner.vscale set $_settings(terrain-vertscale)
1451
1452    blt::table $inner \
1453        0,0 $inner.posdisp   -cspan 2  -anchor w -pady 2 \
1454        1,0 $inner.grid      -cspan 2  -anchor w -pady 2 \
1455        2,0 $inner.wireframe -cspan 2  -anchor w -pady 2 \
1456        3,0 $inner.lighting  -cspan 2  -anchor w -pady 2 \
1457        4,0 $inner.edges     -cspan 2  -anchor w -pady 2 \
1458        5,0 $inner.vscale_l  -anchor w -pady 2 \
1459        5,1 $inner.vscale    -fill x   -pady 2 \
1460        6,0 $inner.palette_l -anchor w -pady 2 \
1461        6,1 $inner.palette   -fill x   -pady 2 
1462
1463    blt::table configure $inner r* c* -resize none
1464    blt::table configure $inner r8 c1 -resize expand
1465}
1466
1467itcl::body Rappture::MapViewer::BuildLayerTab {} {
1468
1469    set fg [option get $itk_component(hull) font Font]
1470    #set bfg [option get $itk_component(hull) boldFont Font]
1471
1472    set inner [$itk_component(main) insert end \
1473        -title "Layers" \
1474        -icon [Rappture::icon layers]]
1475    $inner configure -borderwidth 4
1476    set f [frame $inner.layers]
1477    blt::table $inner \
1478        0,0 $f -fill both
1479    set _layersFrame $inner
1480}
1481
1482itcl::body Rappture::MapViewer::BuildCameraTab {} {
1483    set inner [$itk_component(main) insert end \
1484        -title "Camera Settings" \
1485        -icon [Rappture::icon camera]]
1486    $inner configure -borderwidth 4
1487
1488    set row 0
1489
1490    set labels { x y z heading pitch distance }
1491    foreach tag $labels {
1492        label $inner.${tag}label -text $tag -font "Arial 9"
1493        entry $inner.${tag} -font "Arial 9"  -bg white \
1494            -textvariable [itcl::scope _view($tag)]
1495        bind $inner.${tag} <KeyPress-Return> \
1496            [itcl::code $this camera set ${tag}]
1497        bind $inner.${tag} <KP_Enter> \
1498            [itcl::code $this camera set ${tag}]
1499        blt::table $inner \
1500            $row,0 $inner.${tag}label -anchor e -pady 2 \
1501            $row,1 $inner.${tag} -anchor w -pady 2
1502        blt::table configure $inner r$row -resize none
1503        incr row
1504    }
1505    set labels { srs verticalDatum }
1506    foreach tag $labels {
1507        label $inner.${tag}label -text $tag -font "Arial 9"
1508        entry $inner.${tag} -font "Arial 9"  -bg white \
1509            -textvariable [itcl::scope _view($tag)]
1510        bind $inner.${tag} <KeyPress-Return> \
1511            [itcl::code $this camera set ${tag}]
1512        bind $inner.${tag} <KP_Enter> \
1513            [itcl::code $this camera set ${tag}]
1514        blt::table $inner \
1515            $row,0 $inner.${tag}label -anchor e -pady 2 \
1516            $row,1 $inner.${tag} -anchor w -pady 2
1517        blt::table configure $inner r$row -resize none
1518        incr row
1519    }
1520
1521    if {0} {
1522    button $inner.get \
1523        -text "Get Camera Settings" \
1524        -font "Arial 9" \
1525        -command [itcl::code $this SendCmd "camera get"]
1526    blt::table $inner \
1527        $row,0 $inner.get -anchor w -pady 2 -cspan 2
1528    blt::table configure $inner r$row -resize none
1529    incr row
1530
1531    button $inner.set \
1532        -text "Apply Camera Settings" \
1533        -font "Arial 9" \
1534        -command [itcl::code $this camera set all]
1535    blt::table $inner \
1536        $row,0 $inner.set -anchor w -pady 2 -cspan 2
1537    blt::table configure $inner r$row -resize none
1538    incr row
1539    }
1540
1541    if {$_useServerManip} {
1542        checkbutton $inner.throw \
1543            -text "Enable Throw" \
1544            -font "Arial 9" \
1545            -variable [itcl::scope _settings(camera-throw)] \
1546            -command [itcl::code $this AdjustSetting camera-throw]
1547        blt::table $inner \
1548            $row,0 $inner.throw -anchor w -pady 2 -cspan 2
1549        blt::table configure $inner r$row -resize none
1550        incr row
1551    }
1552
1553    blt::table configure $inner c* r* -resize none
1554    blt::table configure $inner c2 -resize expand
1555    blt::table configure $inner r$row -resize expand
1556}
1557
1558#
1559#  camera --
1560#
1561# USAGE: camera get
1562#        This is called by the server to transfer the
1563#        current Viewpoint settings
1564# USAGE: camera reset
1565#        Reset the camera to the default view
1566#
1567itcl::body Rappture::MapViewer::camera {option args} {
1568    switch -- $option {
1569        "get" {
1570            # We got the camera settings from the server
1571            foreach name {x y z heading pitch distance srs verticalDatum} value $args {
1572                set _view($name) $value
1573            }
1574            puts stderr "view: $_view(x), $_view(y), $_view(z), $_view(heading), $_view(pitch), $_view(distance), $_view(srs), $_view(verticalDatum)"
1575        }
1576        "go" {
1577            SendCmd "camera go $args"
1578        }
1579        "reset" {
1580            array set _view {
1581                x               0.0
1582                y               0.0
1583                z               0.0
1584                heading         0.0
1585                pitch           -89.9
1586                distance        1.0
1587                srs             ""
1588                verticalDatum   ""
1589            }
1590            if { [info exists _mapsettings(camera)] } {
1591                # Check if the tool specified a default
1592                set location $_mapsettings(camera)
1593                if { $location != "" } {
1594                    array set _view $location
1595                    set duration 0.0
1596                    SendCmd [list camera set $_view(x) $_view(y) $_view(z) $_view(heading) $_view(pitch) $_view(distance) $duration $_view(srs) $_view(verticalDatum)]
1597                } else {
1598                    SendCmd "camera reset"
1599                    # Retrieve the settings
1600                    #SendCmd "camera get"
1601                }
1602            } else {
1603                SendCmd "camera reset"
1604                # Retrieve the settings
1605               # SendCmd "camera get"
1606            }
1607        }
1608        "set" {
1609            set who [lindex $args 0]
1610            if {$who != "all" && $who != "srs" && $who != "verticalDatum"} {
1611                set val $_view($who)
1612                set code [catch { string is double $val } result]
1613                if { $code != 0 || !$result } {
1614                    return
1615                }
1616            }
1617            switch -- $who {
1618                "distance" {
1619                    SendCmd [list camera dist $_view(distance)]
1620                }
1621                "all" - "x" - "y" - "z" - "heading" - "pitch" - "srs" - "verticalDatum" {
1622                    set duration 0.0
1623                    SendCmd [list camera set $_view(x) $_view(y) $_view(z) $_view(heading) $_view(pitch) $_view(distance) $duration $_view(srs) $_view(verticalDatum)]
1624                }
1625            }
1626        }
1627    }
1628}
1629
1630itcl::body Rappture::MapViewer::GetImage { args } {
1631    if { [image width $_image(download)] > 0 &&
1632         [image height $_image(download)] > 0 } {
1633        set bytes [$_image(download) data -format "jpeg -quality 100"]
1634        set bytes [Rappture::encoding::decode -as b64 $bytes]
1635        return [list .jpg $bytes]
1636    }
1637    return ""
1638}
1639
1640itcl::body Rappture::MapViewer::BuildDownloadPopup { popup command } {
1641    Rappture::Balloon $popup \
1642        -title "[Rappture::filexfer::label downloadWord] as..."
1643    set inner [$popup component inner]
1644    label $inner.summary -text "" -anchor w
1645
1646    radiobutton $inner.image_button -text "Image File" \
1647        -variable [itcl::scope _downloadPopup(format)] \
1648        -value image
1649    Rappture::Tooltip::for $inner.image_button \
1650        "Save as digital image."
1651
1652    button $inner.ok -text "Save" \
1653        -highlightthickness 0 -pady 2 -padx 3 \
1654        -command $command \
1655        -compound left \
1656        -image [Rappture::icon download]
1657
1658    button $inner.cancel -text "Cancel" \
1659        -highlightthickness 0 -pady 2 -padx 3 \
1660        -command [list $popup deactivate] \
1661        -compound left \
1662        -image [Rappture::icon cancel]
1663
1664    blt::table $inner \
1665        0,0 $inner.summary -cspan 2  \
1666        2,0 $inner.image_button -anchor w -cspan 2 -padx { 4 0 } \
1667        4,1 $inner.cancel -width .9i -fill y \
1668        4,0 $inner.ok -padx 2 -width .9i -fill y
1669    blt::table configure $inner r3 -height 4
1670    blt::table configure $inner r4 -pady 4
1671    raise $inner.image_button
1672    $inner.image_button invoke
1673    return $inner
1674}
1675
1676itcl::body Rappture::MapViewer::SetTerrainStyle { style } {
1677    array set settings {
1678        -color white
1679        -edgecolor black
1680        -edges 0
1681        -lighting 0
1682        -linewidth 1.0
1683        -vertscale 1.0
1684        -wireframe 0
1685    }
1686    array set settings $style
1687
1688    SendCmd "map terrain edges $settings(-edges)"
1689    set _settings(terrain-edges) $settings(-edges)
1690    #SendCmd "map terrain color [Color2RGB $settings(-color)]"
1691    #SendCmd "map terrain colormode constant"
1692    SendCmd "map terrain lighting $settings(-lighting)"
1693    set _settings(terrain-lighting) $settings(-lighting)
1694    SendCmd "map terrain linecolor [Color2RGB $settings(-edgecolor)]"
1695    #SendCmd "map terrain linewidth $settings(-linewidth)"
1696    SendCmd "map terrain vertscale $settings(-vertscale)"
1697    set _settings(terrain-vertscale) $settings(-vertscale)
1698    SendCmd "map terrain wireframe $settings(-wireframe)"
1699    set _settings(terrain-wireframe) $settings(-wireframe)
1700}
1701
1702itcl::body Rappture::MapViewer::SetLayerStyle { dataobj layer } {
1703    array set info [$dataobj layer $layer]
1704    set _visibility($layer) 1
1705
1706    switch -- $info(type) {
1707        "image" {
1708            array set settings {
1709                -min_level 0
1710                -max_level 23
1711                -opacity 1.0
1712            }
1713            if { [info exists info(style)] } {
1714                array set settings $info(style)
1715            }
1716            if { [info exists info(opacity)] } {
1717                set settings(-opacity) $info(opacity)
1718            }
1719            if {!$_sendEarthFile} {
1720                switch -- $info(driver)  {
1721                    "debug" {
1722                        SendCmd [list map layer add image debug $layer]
1723                    }
1724                    "gdal" {
1725                        SendCmd [list map layer add image gdal \
1726                                     $info(gdal.url) $layer]
1727                    }
1728                    "tms" {
1729                        SendCmd [list map layer add image tms \
1730                                     $info(tms.url) $layer]
1731                    }
1732                    "wms" {
1733                        SendCmd [list map layer add image wms \
1734                                     $info(wms.url) \
1735                                     $info(wms.layers) \
1736                                     $info(wms.format) \
1737                                     $info(wms.transparent) \
1738                                     $layer]
1739                    }
1740                    "xyz" {
1741                        SendCmd [list map layer add image xyz \
1742                                     $info(xyz.url) \
1743                                     $layer]
1744                    }
1745                }                       
1746            }
1747            SendCmd "map layer opacity $settings(-opacity) $layer"
1748        }
1749        "elevation" {
1750            array set settings {
1751                -min_level 0
1752                -max_level 23
1753            }
1754            if { [info exists info(style)] } {
1755                array set settings $info(style)
1756            }
1757            if {!$_sendEarthFile} {
1758                switch -- $info(driver)  {
1759                    "gdal" {
1760                        SendCmd [list map layer add elevation gdal \
1761                                     $info(gdal.url) $layer]
1762                    }
1763                    "tms" {
1764                        SendCmd [list map layer add elevation tms \
1765                                     $info(tms.url) $layer]
1766                    }
1767                }
1768            }
1769        }
1770        "line" {
1771            array set settings {
1772                -color black
1773                -minbias 1000
1774                -opacity 1.0
1775                -width 1
1776            }
1777            if { [info exists info(style)] } {
1778                array set settings $info(style)
1779            }
1780            if { [info exists info(opacity)] } {
1781                set settings(-opacity) $info(opacity)
1782            }
1783            SendCmd [list map layer add line $info(ogr.url) $layer]
1784            SendCmd "map layer opacity $settings(-opacity) $layer"
1785        }
1786        "polygon" {
1787            array set settings {
1788                -color white
1789                -minbias 1000
1790                -opacity 1.0
1791            }
1792            if { [info exists info(style)] } {
1793                array set settings $info(style)
1794            }
1795            if { [info exists info(opacity)] } {
1796                set settings(-opacity) $info(opacity)
1797            }
1798            SendCmd [list map layer add polygon $info(ogr.url) $layer]
1799            SendCmd "map layer opacity $settings(-opacity) $layer"
1800        }
1801        "label" {
1802            array set settings {
1803                -align "center-center"
1804                -color black
1805                -declutter 1
1806                -font Arial
1807                -fontsize 16.0
1808                -halocolor white
1809                -halowidth 2.0
1810                -layout "ltr"
1811                -minbias 1000
1812                -opacity 1.0
1813                -removedupe 1
1814            }
1815            if { [info exists info(style)] } {
1816                array set settings $info(style)
1817            }
1818            if { [info exists info(opacity)] } {
1819                set settings(-opacity) $info(opacity)
1820            }
1821            set contentExpr $info(content)
1822            if {[info exists info(priority)]} {
1823                set priorityExpr $info(priority)
1824            } else {
1825                set priorityExpr ""
1826            }
1827            SendCmd [list map layer add text $info(ogr.url) $contentExpr $priorityExpr $layer]
1828            SendCmd "map layer opacity $settings(-opacity) $layer"
1829        }
1830    }
1831
1832    if { [info exists info(visible)] } {
1833        if { !$info(visible) } {
1834            set _visibility($layer) 0
1835            SendCmd "map layer visible 0 $layer"
1836        }
1837    }
1838}
1839
1840itcl::body Rappture::MapViewer::SetOpacity { dataset } {
1841    foreach {dataobj layer} [split $dataset -] break
1842    set type [$dataobj type $layer]
1843    set val $_settings(-opacity)
1844    set sval [expr { 0.01 * double($val) }]
1845    if { !$_obj2ovride($dataobj-raise) } {
1846        # This is wrong.  Need to figure out why raise isn't set with 1
1847        #set sval [expr $sval * .6]
1848    }
1849    SendCmd "$type opacity $sval $dataset"
1850}
1851
1852itcl::body Rappture::MapViewer::ChangeLayerVisibility { dataobj layer } {
1853    set bool $_visibility($layer)
1854    SendCmd "map layer visible $bool $layer"
1855}
1856
1857itcl::body Rappture::MapViewer::UpdateLayerControls {} {
1858    set row 0
1859    set inner $_layersFrame
1860    if { [winfo exists $inner.layers] } {
1861        foreach w [winfo children $inner.layers] {
1862            destroy $w
1863        }
1864    }
1865    set f $inner.layers
1866    foreach dataobj [get -objects] {
1867        foreach layer [$dataobj layers] {
1868            array unset info
1869            array set info [$dataobj layer $layer]
1870            checkbutton $f.$layer \
1871                -text $info(label) \
1872                -variable [itcl::scope _visibility($layer)] \
1873                -command [itcl::code $this \
1874                              ChangeLayerVisibility $dataobj $layer] \
1875                    -font "Arial 9" -anchor w
1876            blt::table $f $row,0 $f.$layer -anchor w -pady 2
1877            Rappture::Tooltip::for $f.$layer $info(description)
1878            incr row
1879        }
1880    }
1881    if { $row > 0 } {
1882        blt::table configure $f r* c* -resize none
1883        blt::table configure $f r$row c1 -resize expand
1884    }
1885}
1886
1887#
1888# Generate an OSG Earth file to send to server.  This is inteneded
1889# as a stopgap and testing tool until the protocol is fleshed out.
1890#
1891# Note that the lighting settings are required to be "hard-coded"
1892# as below for the runtime control to work.  Don't make those user
1893# configurable.
1894#
1895# Also note: Use "true"/"false" for boolean settings.  Not sure if
1896# the parser in OSG Earth accepts all of Tcl's forms of boolean vals.
1897#
1898itcl::body Rappture::MapViewer::EarthFile {} {
1899    append out "<map"
1900    append out " name=\"$_mapsettings(label)\""
1901    append out " type=\"$_mapsettings(type)\""
1902    append out " version=\"2\""
1903    append out ">\n"
1904    append out " <options lighting=\"true\">\n"
1905    # FIXME: convert color setting to hex
1906    # array set style $_mapsettings(style)
1907    # if {[info exists style(-color)]} {
1908    #     set color "?"
1909    # }
1910    set color "#ffffffff"
1911    append out "  <terrain lighting=\"false\" color=\"$color\"/>\n"
1912    if { [info exists _mapsettings(projection)] } {
1913        append out "  <profile"
1914        append out " srs=\"$_mapsettings(projection)\""
1915        if { [info exists _mapsettings(extents)] } {
1916            append out " xmin=\"$_mapsettings(x1)\""
1917            append out " ymin=\"$_mapsettings(y1)\""
1918            append out " xmax=\"$_mapsettings(x2)\""
1919            append out " ymax=\"$_mapsettings(y2)\""
1920        }
1921        append out "/>\n"
1922    }
1923    append out " </options>\n"
1924
1925    foreach dataobj [get -objects] {
1926        foreach layer [$dataobj layers] {
1927            set _layers($layer) 1
1928            array unset info
1929            array set info [$dataobj layer $layer]
1930            switch -- $info(type) {
1931                "image" {
1932                    append out " <image"
1933                    append out " name=\"$layer\""
1934                    append out " driver=\"gdal\""
1935                    if { [info exists info(opacity)] } {
1936                        append out " opacity=\"$info(opacity)\""
1937                    }
1938                    if { $info(visible) } {
1939                        append out " visible=\"true\""
1940                    } else {
1941                        append out " visible=\"false\""
1942                    }
1943                    append out ">\n"
1944                    append out "  <url>$info(url)</url>\n"
1945                    append out " </image>\n"
1946                }
1947                "elevation" {
1948                    append out " <elevation"
1949                    append out " name=\"$layer\""
1950                    append out " driver=\"gdal\""
1951                    if { $info(visible) } {
1952                        append out " visible=\"true\""
1953                    } else {
1954                        append out " visible=\"false\""
1955                    }
1956                    append out ">\n"
1957                    append out "  <url>$info(url)</url>\n"
1958                    append out " </elevation>\n"
1959                }
1960                default {
1961                    puts stderr "Type $info(type) not implemented in MapViewer::EarthFile"
1962                }
1963            }
1964        }
1965    }
1966    append out "</map>\n"
1967    return $out
1968}
Note: See TracBrowser for help on using the repository browser.