# Copyright 2000 Earthstar Geographics.
#
# Script:  ESG_One_to_Two.erb
# Date:    March 2000
# Author:  Eric Augenstein (eric@es-geo.com)
# Summary: Sets the active window to 1:2 viewing resolution.

# This version uses a lib script to detewmine the projection
# units (feet, meters, degrees) so zoom works correctly.

# Notes:
# - Cannot use the current window size/position because
#   cannot "get" info about it.  So we copy the current
#   alg, figure out the new extents, delete the original
#   window, then copy the revised alg to a new 500x500
#   window in the upper-left corner.
#
# - Although we open a 500x500 window, only approx. 480x480 of
#   it is used to display the image (rest is scroll bars).
#   This number varies depending the display resolution.
#   For example, at 1024x768 the wizard will make exact
#   1:2 magnifications, but these will be slightly
#   different at different displays resolutions because the width
#   of the scroll bars in pixels is not constant. 
#   On the UNIX 5.5 platform, we use 500x500 because no scroll 
#   bars to interfere (always get true 1:2). 

$erm_machine_type = getenv "ERM_MACHINE_TYPE"

if ($erm_machine_type == "win32") then goto use_60_settings
$winsize = 500
goto start_processing

use_60_settings:
$winsize = 480

# Start the processing.

start_processing:

if current window then goto get_alg_info

say warning "Please open an image or algorithm first."
exit

get_alg_info:
	copy algorithm from window
	$UL_easting = get algorithm topleft eastings
	$UL_northing = get algorithm topleft northings
	$LR_easting = get algorithm bottomright eastings
	$LR_northing = get algorithm bottomright northings
	first active raster layer
	$xsize = get layer cell sizex
	$ysize = get layer cell sizey
	$alg_units = get algorithm units
	$alg_datum = get algorithm datum
	$alg_proj = get algorithm projection
	$alg_coordsys = get algorithm coordsys

if ($alg_coordsys == raw) then goto need_geo_image
if ($alg_datum == "RAW") then goto need_geo_image
if ($alg_proj == "RAW") then goto need_geo_image
goto get_units

need_geo_image:
say warning "This wizard only works on geocoded images. (Your image has a 'raw' datum, projection or coordinate type.)"
exit

get_units:
	$get_units_arg1 = $alg_proj
	include "lib/ESG_Get_Proj_Units.erb"
	$units = $units_return

if ($units == "Feet") then goto convert_to_feet 
goto get_alg_extents

# Need to convert cell size to feet if using XYs in feet
# in order to calc correct image size and zoom factor.

convert_to_feet:
	$xsize = ($xsize * 3.2808) # convert meters to feet
	$ysize = ($ysize * 3.2808) 

# Figure out the alg extents.

get_alg_extents:
$xextent = ($LR_easting - $UL_easting)
$yextent = ($UL_northing - $LR_northing)

# Figure number of pixels displayed in X and Y
$numx = ($xextent / $xsize)
$numy = ($yextent / $ysize)

# Figure the zoom mag factor if the current extents were
# displayed in a 480x480 window for 6.0 (which has window
# scroll bars) or 500x500 for UNIX 5.5.

$win_factorx = $numx / $winsize
$win_factory = $numy / $winsize

# Figure the E-N extents that will fit in the window (at 1:2)
$new_xextent = ($xextent / $win_factorx) * 2
$new_yextent = ($yextent / $win_factory) * 2

# Calc the display area if they want to center.

show_center:

$xhalfsize = ($xextent / 2) 
$yhalfsize = ($yextent / 2)
$xcenter = ($UL_easting + $xhalfsize)
$ycenter = ($UL_northing - $yhalfsize)

$newx_halfsize = ($new_xextent / 2)
$newy_halfsize = ($new_yextent / 2)

$new_ULX = ($xcenter - $newx_halfsize)
$new_ULY = ($ycenter + $newy_halfsize)
$new_LRX = ($xcenter + $newx_halfsize)
$new_LRY = ($ycenter - $newy_halfsize)

	set algorithm topleft eastings to $new_ULX
	set algorithm topleft northings to $new_ULY
	set algorithm bottomright eastings to $new_LRX
	set algorithm bottomright northings to $new_LRY

display_new_area:
$origwin = current window
delete $origwin
new window 5 5 500 500  # Note: actual display area varies on 6.0
copy algorithm to window
go window

exit



