#!/bin/sh
# the next line restarts using /usr/local/bin/wish8.6 \
exec /usr/local/bin/wish8.6 "$0" "$@"

# XEartk.tcl --
#
#	This application displays geographic line and place data in an
#	igeomap mega-widget.  See the /usr/local/share/xeartk/README file for data sources.
# 
#  Copyright (c) 2003 Gordon D. Carrie
#  
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#  
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#  
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.
#  
#  Please address questions and bug reports to tkgeomap@users.sourceforge.net
# 
#  @(#) $Id: XEartk.tcl,v 1.24 2005/11/02 20:53:10 tkgeomap Exp $

package require wdgeomap 2

# Set scaling from DPI environment variable, if necessary.  You only have to
# do this if you X server gives the wrong information.  Run
# 'xdpyinfo | grep resolution' to check.

if [info exists env(DPI)] {
    tk scaling [expr {$env(DPI) / 72.0}]
}

# Read outlines from the xdr files in the data directory.
# See /usr/local/share/xeartk/README for sources.

geomap::lnarr fmxdr land /usr/local/share/xeartk/land.xdr
geomap::lnarr fmxdr water /usr/local/share/xeartk/water.xdr
geomap::lnarr fmxdr rivers /usr/local/share/xeartk/rivers.xdr

# Read places from a file in the data directory.
# See /usr/local/share/xeartk/README for sources.

set in [open /usr/local/share/xeartk/cities]
while {[gets $in line] >= 0} {
    if {![regexp {^[ 	]*#} $line] && [llength $line] >= 2} {
	set place [lindex $line 0]
	set latLon [lindex $line 1]
	geomap::place new $place [geomap::dmstodec $latLon]
	lappend places $place
    }
}
close $in

# Create "oceans" and grid lines using procedures from the tkgeomap_procs
# package.  See the tkgeomap_procs (n) man page for details.

geomap::lnarr fmlist oceans [geomap::ocean_list 15]
geomap::lnarr fmlist grids [geomap::grid_list 15]

# Create a wdgeomap widget.  

geomap::wdgeomap::create map .map 				\
	-projname Orthographic 				\
	-scale 1:120000000 				\
	-scales {1:25000000 1:45000000 1:60000000	\
		1:90000000 1:120000000 1:240000000} 	\
	-boundcirclecolor #666666			\
	-width 480 -height 480 -background #666666	\
	-colormenu 1

# Specifiy the bindings to move the map.

geomap::wdgeomap::set_motion_bindings Shift 1
puts "Shift-double-click or shift-drag to adjust view"

# Define color values.

geomap::wdgeomap::setcolor map water #0033cc
geomap::wdgeomap::setcolor map grid #808080
geomap::wdgeomap::setcolor map place Red
geomap::wdgeomap::setcolor map land Wheat

# Define scripts to run when colors change.

geomap::wdgeomap::setcolorscript map water {
    set c [geomap::wdgeomap::getcolor map water]
    geomap::wdgeomap::draw map geomap_lnarr oceans -fill $c
    geomap::wdgeomap::draw map geomap_lnarr rivers -outline $c
    geomap::wdgeomap::draw map geomap_lnarr water -width 0 -fill $c
}
geomap::wdgeomap::setcolorscript map land {
    geomap::wdgeomap::draw map geomap_lnarr land \
	    -fill [geomap::wdgeomap::getcolor map land]
}
geomap::wdgeomap::setcolorscript map place {
    set c [geomap::wdgeomap::getcolor map place]
    [geomap::wdgeomap::map_canvas map] itemconfigure place \
	    -dotcolor $c -textcolor $c
}
geomap::wdgeomap::setcolorscript map grid {
    geomap::wdgeomap::draw map geomap_lnarr grids \
	    -outline [geomap::wdgeomap::getcolor map grid]
}

# Draw the lines and places in the map

set wc [geomap::wdgeomap::getcolor map water]
geomap::wdgeomap::draw map geomap_lnarr oceans -fill $wc -width 0 -tags water
geomap::wdgeomap::draw map geomap_lnarr land -width 0 \
	-fill [geomap::wdgeomap::getcolor map land] -tags land
geomap::wdgeomap::draw map geomap_lnarr rivers -outline $wc -tags water
geomap::wdgeomap::draw map geomap_lnarr water -width 0 -fill $wc -tags water
geomap::wdgeomap::draw map geomap_lnarr grids		\
	-outline [geomap::wdgeomap::getcolor map grid]	\
	-dash 4 -linestyle LineOnOffDash
set pc [geomap::wdgeomap::getcolor map place]
foreach place $places {
    geomap::wdgeomap::draw map geomap_place $place -dotcolor $pc -dotsize 2 \
	    -textcolor $pc -tags place
}

# Have Double-1 mark nearest place with a number and print the place name
# on the terminal.  count stores an integer assigned to each place.  This
# integer is displayed on the map at the place and next to the terminal
# printout for the place.  The binding is applied to items with the 'place'
# tag.  This tag is automatically applied to all items created with the
# 'geomap::wdgeomap::draw' map procedure.

set count 0
set cvs [geomap::wdgeomap::map_canvas map]
$cvs bind place <Double-1> {
set place [%W itemcget [%W find withtag current] -place]
    if { ![info exists index] || ![info exists index($place)] } {
	incr count
	set index($place) $count
	%W itemconfig $place -text $index($place) -anchor s -font Helvetica
    }
    puts "$index($place) $place"
    break
}
$cvs configure -closeenough 3.0
puts "Double-1 to see city info"

# Put a label on the map that indicates the projection, scale, and rotation.
# Create a script called 'Update' that retrieves projection, scale, and
# rotation information and updates the label accordingly.

$cvs create text 5 5 -anchor nw -fill Turquoise -tags maplabel
set Update {
    set projNm [geomap::wdgeomap::cget map -projname]
    set s [geomap::wdgeomap::cget map -scale]
    if [string is double $s] {
	set s [geomap::cartg $s]
    }
    set r [geomap::wdgeomap::cget map -rotation]
    if {$r == 0.0 || $r == "north"} {
	set txt [format {%s %s} $projNm $s]
    } else {
	set txt [format {%s %s. Rotated %s degrees} $projNm $s $r]
    }
    [geomap::wdgeomap::map_canvas map] itemconfigure maplabel -text $txt
}
eval $Update

# Arrange to have script Update called whenever the map projection, scale, or
# rotation changes.

geomap::wdgeomap::configure map -update $Update

# Display the frame that contains the map and menus.

pack .map -fill both -expand true
