# GeoBrowse -- a TK interface for browsing the WorldDataBank map database
#  v. 0.2  (April 92) Ray R. Larson
#
proc geobrowse {} {

catch {destroy .mp}

toplevel .mp -relief {raised}
wm geometry .mp +150+100
wm maxsize .mp 1100 850
wm minsize .mp 10 10
#wm positionfrom .mp program
wm title .mp "Geographic Browser"

global BIGFONT

# values set by mouselatlon
global MOUSE_LAT_DEG
global MOUSE_LAT_MIN
global MOUSE_LON_DEG
global MOUSE_LON_MIN

# the map data file
global WDBMAPFILE

frame .mp.buttons -borderwidth {2}  -geometry {20x20}  -relief {raised}

button .mp.buttons.cancel -text {Cancel Display} -command {
                                          destroymapicons
                                          destroy .mp}  

button .mp.buttons.helpgeo -text {GeoBrowse Help} -command "createHelpListBox GeoBrowse_Help geobrowse.help"  


button .mp.buttons.geoclip -text {AVHRR Clip} -command "doimages .mp.win1.mapwin" 


button .mp.buttons.dispicons -text {Display Icons} -command {setmapmenuicons}


button .mp.buttons.dispmap -text {Display Map}    -command { 
                removemapicons 
                drawmap .mp.win1.mapwin $WDBMAPFILE \
                [.mp.scales1.latdeg get] [.mp.scales2.latmin get] \
                [.mp.scales1.londeg get] [.mp.scales2.lonmin get] \
                [.mp.scales1.zoom get] [.mp.scales2.aspect get] 
		replacemapicons
		update
              }

pack append .mp.buttons \
              .mp.buttons.dispmap {left frame center expand fill} \
              .mp.buttons.dispicons {left frame center padx 15 pady 18 expand fill} \
              .mp.buttons.helpgeo {left frame center padx 15 pady 18 expand fill} \
              .mp.buttons.geoclip {left frame center padx 15 pady 18 expand fill} \
              .mp.buttons.cancel {left frame center padx 15 fill}
frame .mp.scales1 -borderwidth {2}  -relief {raised}

scale .mp.scales1.zoom -label {Zoom (times)}  -length {320}  -orient {horizontal}  -relief {raised}  -showvalue {true} -from 1 -to 100

scale .mp.scales1.londeg -label {Longitude (degrees)}  -length {320}  -orient {horizontal}  -relief {raised}  -showvalue {true} -from {-180} -to {180}

scale .mp.scales1.latdeg -label {Latitude (degrees)}  -length {320}  -orient {horizontal}  -relief {raised}  -showvalue {true} -from {-90} -to {90}

pack append .mp.scales1 \
              .mp.scales1.latdeg {left frame center padx 20 fill} \
              .mp.scales1.londeg {left frame center padx 20 expand fill} \
              .mp.scales1.zoom {left frame center padx 20 expand fill}

frame .mp.scales2 -borderwidth {2} -relief {raised}

scale .mp.scales2.aspect -label {Aspect Correction}  -length {320}  -orient {horizontal}  -relief {raised}  -showvalue {true} -from 50 -to 150

scale .mp.scales2.lonmin -label {Longitude (minutes)}  -length {320}  -orient {horizontal}  -relief {raised}  -showvalue {true} -from {0} -to {59}

scale .mp.scales2.latmin -label {Latitude (minutes)}  -length {320}  -orient {horizontal}  -relief {raised}  -showvalue {true} -from {0} -to {59}

pack append .mp.scales2 \
              .mp.scales2.latmin {left frame center padx 20 fill} \
              .mp.scales2.lonmin {left frame center padx 20 expand fill} \
              .mp.scales2.aspect {left frame center padx 20 expand fill}

frame .mp.win1 -borderwidth {3}  -geometry {1050x620}  -relief {raised}

canvas .mp.win1.mapwin -background {#000000}  -borderwidth {2}  -width 1000 -height 600  -relief {sunken}

pack append .mp.win1 \
              .mp.win1.mapwin {top frame center}

bind .mp.win1.mapwin <Button-1> { centermap %W %x %y }
bind .mp.win1.mapwin <Button-3> { zoommap %W %x %y }

pack append .mp \
              .mp.win1 {top frame center expand fillx filly} \
              .mp.scales1 {top frame center} \
              .mp.scales2 {top frame center} \
              .mp.buttons {top frame center padx 42 expand fillx}


.mp.scales1.latdeg set 0
.mp.scales1.londeg set 0
.mp.scales1.zoom set 1
.mp.scales2.latmin set 0
.mp.scales2.lonmin set 0
.mp.scales2.aspect set 120

update
waitcursor
WaitWindow {retrieving map data from POSTGRES}

# draw the map with lat 0 0 lon 0 0 zoom 1
drawmap .mp.win1.mapwin $WDBMAPFILE 0 0 0 0 1 120 ;

catch {destroy .waitwin}
normalcursor

}

#-------------------------------------------------------
# handle mouse actions
#-------------------------------------------------------
proc centermap {w x y} {

     global MOUSE_LAT_DEG
     global MOUSE_LAT_MIN
     global MOUSE_LON_DEG
     global MOUSE_LON_MIN
     global WDBMAPFILE

     mouselatlon $w $x $y

     .mp.scales1.latdeg set $MOUSE_LAT_DEG
     .mp.scales1.londeg set $MOUSE_LON_DEG
     .mp.scales2.latmin set $MOUSE_LAT_MIN
     .mp.scales2.lonmin set $MOUSE_LON_MIN

     waitcursor
     removemapicons

     drawmap $w $WDBMAPFILE  $MOUSE_LAT_DEG $MOUSE_LAT_MIN \
             $MOUSE_LON_DEG $MOUSE_LON_MIN  [.mp.scales1.zoom get] [.mp.scales2.aspect get]

     replacemapicons
     normalcursor

}

proc zoommap {w x y} {

     global MOUSE_LAT_DEG
     global MOUSE_LAT_MIN
     global MOUSE_LON_DEG
     global MOUSE_LON_MIN
     global WDBMAPFILE

     mouselatlon $w $x $y

     .mp.scales1.latdeg set $MOUSE_LAT_DEG
     .mp.scales1.londeg set $MOUSE_LON_DEG
     .mp.scales2.latmin set $MOUSE_LAT_MIN
     .mp.scales2.lonmin set $MOUSE_LON_MIN
     set zoomval [expr [.mp.scales1.zoom get]+2 ]
     .mp.scales1.zoom set $zoomval
     
     waitcursor
     removemapicons

     drawmap $w $WDBMAPFILE  $MOUSE_LAT_DEG $MOUSE_LAT_MIN \
             $MOUSE_LON_DEG $MOUSE_LON_MIN  $zoomval [.mp.scales2.aspect get]

     replacemapicons
     normalcursor
}

proc doimages {win} {


     global MOUSE_LON_DEG MOUSE_LAT_DEG

     mouselatlon $win 0 0
     set toplon $MOUSE_LON_DEG 
     set toplat $MOUSE_LAT_DEG
     mouselatlon $win [winfo width $win] [winfo height $win]
     set botlon $MOUSE_LON_DEG 
     set botlat $MOUSE_LAT_DEG
     set zoom [.mp.scales1.zoom get]

     if {$toplon >= -124 && $botlon <= -112 && $toplat <= 37 && $botlat >= 31 && $zoom > 30 } {

       WaitWindow {Loading TM satellite data}
       exec xv /share2/VIS/img/tm.1.tiff &
       puts stdout "Santa Barbara eh?"
       catch {destroy .waitwin}
     }
     if {$toplon >= -150 && $botlon <= -40 && $toplat <= 58 && $botlat >= 17 && $zoom > 1 && $zoom < 10 } {
       WaitWindow {Loading AVHRR satellite data}
       exec xv /share2/VIS/img/avhrr.1.tiff &
       puts stdout "US AVHRR"
       catch {destroy .waitwin}
     }


}
