(FILECREATED "16-Oct-84 10:21:39" {ERIS}<LISPCORE>DEMO>MAP.;3 48160  

      changes to:  (FNS GETDRAWPOSITION)
		   (VARS MAPCOMS)

      previous date: "11-MAY-83 13:07:13" {PHYLUM}<BURTON>ORB>MAP.;37)


(* Copyright (c) 1983, 1984 by Xerox Corporation. All rights reserved.)

(PRETTYCOMPRINT MAPCOMS)

(RPAQQ MAPCOMS ((DECLARE: FIRST (FILES MAPSKETCH))
	(FNS DRAW.MAP MAPINDEXFILE MAPDATAFILE DRAW.MAP.FROM.MAPINFO MAPDESCFROMFIGUREW MAPW.CLOSEFN 
	     MAPW.CREATE.ICON MAPW.FROM.LATLONW MAPW.ICON.BITMAP CREATE.MAPW CREATE.MAPW.FROM.REGION 
	     FIGW.DRAWMAP GETLATLON GETMAPREGION GETWORLD GETMAPFILES MAPREGIONTOWINDOW MAPUNSCALE 
	     MAPW.BUTTONEVENTFN MAPW.DISPLAYER MAPSCALEIN MAPSCALE PLOTSEGMENT ReadSigned16 
	     SCALE.FROM.MAPINFO SCALE.FROM.MAPW MAP.SCALE.POSITION UNSCALE.POSITION.FROM.MAPINFO 
	     SCALE.POSITION.FROM.MAPINFO UPDATE.CURVE.TO.SCALE UPDATE.ELT.TO.SCALE 
	     UPDATE.CIRCLE.TO.SCALE UPDATE.ELLIPSE.TO.SCALE UPDATE.IMAGE.TO.SCALE 
	     UPDATE.LOCAL.FROM.GLOBAL UPDATE.POSITIONS.TO.SCALE UPDATE.TEXT.TO.SCALE UPDATELATLON)
	(FNS READLATLON READ.LATLON.EVENTFN SHOW.LAT.LON CREATE.LAT.LON.DISPLAYER LATLON.CLOSEFN 
	     ADD.LATLON.DISPLAY MAPW.DISPLAY.LAT.LON FILETOREALLAT FILETOREALLON REALTOFILELAT 
	     REALTOFILELON)
	(FNS MAP.DATA.IN.REGION MAPW.DATA ADD.TO.LST.IF.INSIDE MAPW.INSIDE.CIRCLE MAPW.INSIDE.ELLIPSE 
	     MAPW.INSIDE.CURVE MAPW.INSIDE.TEXT MAPW.INSIDE.IMAGE)
	(FNS GETDRAWPOSITION)
	(INITVARS (ALL.MAPWS))
	[VARS MAPW.AUTOSCALEFLG MAPW.LASTCURSORPTY MAPWFONT (MAPW.COMMAND.MENU)
	      (READ.LAT.LON.COMMANDMENU)
	      (BOLDMAPFONT (QUOTE (GACHA 12 BOLD]
	(GLOBALVARS ALL.MAPWS MAPWFONT MAPW.COMMAND.MENU GLOBALMAPINDEXFILE GLOBALMAPDATAFILE)
	(RECORDS MAPPIECE LOCALMAPPIECE)))
(DECLARE: FIRST 
(FILESLOAD MAPSKETCH)
)
(DEFINEQ

(DRAW.MAP
  [LAMBDA (MAPFILE DSP LONGOFF LATOFF LONGSC LATSC CLIPW CLIPE CLIPS CLIPN)
                                                             (* rrb "16-MAR-83 14:07")

          (* draws a piece of the world from MAPFILE into WINDOW. LATOFF LONGOFF LATSC LONGSC define the scale.
	  CLIPx define the region of the world of interest.)


    (PROG (S1 S2 N S E W CNT (POINTSTOSKIP 0))
          (SETQ S1 (OPENSTREAM (MAPINDEXFILE MAPFILE)
			       (QUOTE INPUT)))
          (SETQ S2 (OPENSTREAM (MAPDATAFILE MAPFILE)
			       (QUOTE INPUT)))               (* in case these files were already open)
          (SETFILEPTR S1 0)
          (SETFILEPTR S2 0)
          [until (EOFP S1)
	     do                                              (* When not at end of file)
		(SETQ N (ReadSigned16 S1))
		(SETQ S (ReadSigned16 S1))
		(SETQ E (ReadSigned16 S1))
		(SETQ W (ReadSigned16 S1))
		(SETQ CNT (ReadSigned16 S1))
		(COND
		  ((OR (IGREATERP S CLIPN)
		       (IGREATERP CLIPS N)
		       (IGREATERP W CLIPE)
		       (IGREATERP CLIPW E))                  (* line segment falls outside.)
                                                             (* keep track of how many points to skip in the map 
							     file.)
		    (SETQ POINTSTOSKIP (IPLUS POINTSTOSKIP CNT)))
		  (T                                         (* Clipping in)
		     (COND
		       ((NEQ 0 POINTSTOSKIP)                 (* update the fileptr for the point file.
							     There are two 16 bit word or 4 bytes per point.
							     NIL)
			 (SETFILEPTR S2 (IPLUS (GETFILEPTR S2)
					       (LLSH POINTSTOSKIP 2)))
			 (SETQ POINTSTOSKIP 0)))
		     (PLOTSEGMENT DSP S2 CNT LONGOFF LONGSC LATOFF LATSC]
          (CLOSEF S1)
          (CLOSEF S2])

(MAPINDEXFILE
  [LAMBDA (MAPFILE)                                          (* rrb "16-MAR-83 14:07")
                                                             (* functional interface to the file name for the index 
							     file.)
    GLOBALMAPINDEXFILE])

(MAPDATAFILE
  [LAMBDA (MAPFILE)                                          (* rrb "16-MAR-83 14:07")
                                                             (* functional interface to the file name for the index 
							     file.)
    GLOBALMAPDATAFILE])

(DRAW.MAP.FROM.MAPINFO
  [LAMBDA (DSP MAPPIECE WINDOWREGION)                        (* rrb "28-JAN-83 16:45")
                                                             (* draws a map onto the display stream or window DSP 
							     from information in MAPPIECE)
    (RESETFORM (CURSOR WAITINGCURSOR)
	       (PROG (LATOFF LATSC LONOFF LONSC)
		     (RETURN (DRAW.MAP (fetch (MAPPIECE MAPFILE) of MAPPIECE)
				       DSP
				       (SETQ LONOFF (fetch (MAPPIECE LONGITUDEOFFSET) of MAPPIECE))
				       (SETQ LATOFF (fetch (MAPPIECE LATITUDEOFFSET) of MAPPIECE))
				       (SETQ LONSC (fetch (MAPPIECE LONGITUDESCALE) of MAPPIECE))
				       (SETQ LATSC (fetch (MAPPIECE LATITUDESCALE) of MAPPIECE))
				       (MAPUNSCALE (fetch (REGION LEFT) of WINDOWREGION)
						   LONOFF LONSC)
				       (MAPUNSCALE (fetch (REGION RIGHT) of WINDOWREGION)
						   LONOFF LONSC)
				       (MAPUNSCALE (fetch (REGION BOTTOM) of WINDOWREGION)
						   LATOFF LATSC)
				       (MAPUNSCALE (fetch (REGION TOP) of WINDOWREGION)
						   LATOFF LATSC])

(MAPDESCFROMFIGUREW
  [LAMBDA (FIGUREW)                                          (* rrb "24-JAN-83 18:19")

          (* returns the map piece descriptor from a fiugre window. There might be more than one eventually but for now only
	  consider the case of one.)


    (for SCREENELT in (FIGURESPECS FIGUREW) when (EQ (CAR (fetch (SCREENELT GLOBALPART) of SCREENELT))
						     (QUOTE MAPPIECE))
       do (RETURN (fetch (SCREENELT GLOBALPART) of SCREENELT])

(MAPW.CLOSEFN
  [LAMBDA (MAPW)                                             (* rrb "26-JAN-83 11:25")
                                                             (* close function for map windows.)

          (* FOR NOW this removes it from the global list. In the long run that may not be right because possibly windows 
	  may be closed but still accessible)


    (PROG (LATLONW)
          (SETQ ALL.MAPWS (REMOVE MAPW ALL.MAPWS))
          (COND
	    ((SETQ LATLONW (WINDOWPROP MAPW (QUOTE LATLONDISPLAYER)))

          (* if there is a latitude longitude display, close it. Its closefn will break its link with MAPW so that if MAPW 
	  is reopened it will not come back.)


	      (CLOSEW LATLONW])

(MAPW.CREATE.ICON
  [LAMBDA (MAPW)                                             (* rrb "20-JAN-83 17:22")
                                                             (* makes sure the mapw has an icon which is a picture of
							     itself.)
    (OR (WINDOWPROP MAPW (QUOTE ICON))
	(WINDOWPROP MAPW (QUOTE ICON)
		    (MAPW.ICON.BITMAP MAPW 24 24])

(MAPW.FROM.LATLONW
  [LAMBDA (LATLONW)                                          (* rrb "11-JAN-83 14:54")
                                                             (* returns the active window if any that points to 
							     latlonw.)
    (for W in (ACTIVEWINDOWS) when (EQ LATLONW (WINDOWPROP W (QUOTE LATLONDISPLAYER)))
       do (RETURN W])

(MAPW.ICON.BITMAP
  [LAMBDA (MAPW WDTH HGHT)                                   (* rrb "21-JAN-83 10:03")
                                                             (* create a bitmap that has the same part of the world 
							     shown in MAPW but is only of size WDTH by HGHT.)
    (PROG ((MAPPIECE (MAPDESCFROMFIGUREW MAPW))
	   (DSP (DSPCREATE (BITMAPCREATE WDTH HGHT)))
	   WREGION)
          (SETQ WREGION (fetch (MAPPIECE WORLDREGION) of MAPPIECE))
          (DRAW.MAP (fetch (MAPPIECE MAPFILE) of MAPPIECE)
		    DSP
		    (fetch (MAPPIECE LONGITUDEOFFSET) of MAPPIECE)
		    (fetch (MAPPIECE LATITUDEOFFSET) of MAPPIECE)
		    (IQUOTIENT (ITIMES (fetch (MAPPIECE LONGITUDESCALE) of MAPPIECE)
				       (WINDOWPROP MAPW (QUOTE WIDTH)))
			       WDTH)
		    (IQUOTIENT (ITIMES (fetch (MAPPIECE LATITUDESCALE) of MAPPIECE)
				       (WINDOWPROP MAPW (QUOTE HEIGHT)))
			       HGHT)
		    (fetch (REGION LEFT) of WREGION)
		    (fetch (REGION RIGHT) of WREGION)
		    (fetch (REGION BOTTOM) of WREGION)
		    (fetch (REGION TOP) of WREGION))
          (RETURN (DSPDESTINATION NIL DSP])

(CREATE.MAPW
  [LAMBDA (WREGION ANNOTATIONS SCREENREG TITLE)              (* rrb "26-JAN-83 09:14")
                                                             (* creates a map onto a portion of the world WORLDREGION
							     is in filemap coordinates.)
                                                             (* file should be calculated from the region of the 
							     world requested in WREGION and probably should be a list
							     of files.)
    (PROG ((FILE (QUOTE {DSK}WORLD-COAST-S))
	   (MAPWIN (FIGUREW NIL SCREENREG TITLE))
	   LATSCALE LONGSCALE SCALE PROJFACTOR HGHT WDTH SOUTH WEST)
          [COND
	    [MAPW.AUTOSCALEFLG

          (* calculate the minimum scale that will fit using a Mercateur projection (cosign Latitude) at the center of the 
	  area of the world covered.)


	      (COND
		([IGREATERP [SETQ LATSCALE (IQUOTIENT (SETQ HGHT (fetch (REGION HEIGHT) of WREGION))
						      (WINDOWPROP MAPWIN (QUOTE HEIGHT]
			    (SETQ LONGSCALE
			      (FIX (FTIMES (IQUOTIENT (SETQ WDTH (fetch (REGION WIDTH) of WREGION))
						      (WINDOWPROP MAPWIN (QUOTE WIDTH)))
					   (SETQ PROJFACTOR
					     (COS (FILETOREALLAT (IPLUS (fetch (REGION BOTTOM)
									   of WREGION)
									(IQUOTIENT
									  (fetch (REGION HEIGHT)
									     of WREGION)
									  2]
                                                             (* increase width of world region to fit.)
		  )
		(T                                           (* increase the height of the world region to fit.)
		   ]
	    (T                                               (* set the scales to fit into the window given.)
	       [SETQ LATSCALE (IQUOTIENT (fetch (REGION HEIGHT) of WREGION)
					 (WINDOWPROP MAPWIN (QUOTE HEIGHT]
	       (SETQ LONGSCALE (IQUOTIENT (fetch (REGION WIDTH) of WREGION)
					  (WINDOWPROP MAPWIN (QUOTE WIDTH]
          (FIGW.DISPLAY.AND.ADD.ITEM (create SCREENELT
					     LOCALPART ← NIL
					     GLOBALPART ←(create MAPPIECE
								 MINSCALE ← LONGSCALE
								 MAXSCALE ← LONGSCALE
								 WORLDREGION ← WREGION
								 LATITUDEOFFSET ←(SETQ SOUTH
								   (fetch (REGION BOTTOM)
								      of WREGION))
								 LATITUDESCALE ← LATSCALE
								 LONGITUDEOFFSET ←(SETQ WEST
								   (fetch (REGION LEFT) of WREGION))
								 LONGITUDESCALE ← LONGSCALE
								 MAPFILE ← FILE))
				     MAPWIN LONGSCALE)       (* a mappiece is one of the figure elements that a 
							     figure window knows about.)
          (FIGW.DISPLAY.AND.ADD.ITEMS (UPDATE.POSITIONS.TO.SCALE ANNOTATIONS
								 (SETQ SCALE
								   (create POSITION
									   XCOORD ← LONGSCALE
									   YCOORD ← LATSCALE))
								 (create POSITION
									 XCOORD ← WEST
									 YCOORD ← SOUTH))
				      MAPWIN SCALE)
          (WINDOWPROP MAPWIN (QUOTE SCALE)
		      SCALE)

          (* for now clobber the figure windows button event function. This is ok now because it doesn't do anything;
	  in future there may be interference problems.)


          (WINDOWPROP MAPWIN (QUOTE BUTTONEVENTFN)
		      (FUNCTION MAPW.BUTTONEVENTFN))
          (SETQ ALL.MAPWS (CONS MAPWIN ALL.MAPWS))
          (WINDOWADDPROP MAPWIN (QUOTE CLOSEFN)
			 (FUNCTION MAPW.CLOSEFN))
          (WINDOWPROP MAPWIN (QUOTE SHRINKFN)
		      (FUNCTION MAPW.CREATE.ICON))
          (RETURN MAPWIN])

(CREATE.MAPW.FROM.REGION
  [LAMBDA (ORIGMAPW)                                         (* rrb "26-JAN-83 09:14")
                                                             (* creates a new map window from a region of a map 
							     window.)
    (PROG ((REG (GETMAPREGION ORIGMAPW))
	   MW)                                               (* mark the region as it is created.)
          (DSPFILL (MAPREGIONTOWINDOW REG ORIGMAPW)
		   BLACKSHADE
		   (QUOTE INVERT)
		   ORIGMAPW)
          (SETQ MW (CREATE.MAPW REG (MAP.DATA.IN.REGION (MAPW.DATA ORIGMAPW)
							REG)
				NIL "A piece of the world."))
          (WINDOWPROP MW (QUOTE SUPERMAPW)
		      ORIGMAPW)
          (DSPFILL (MAPREGIONTOWINDOW REG ORIGMAPW)
		   BLACKSHADE
		   (QUOTE INVERT)
		   ORIGMAPW)
          (RETURN MW])

(FIGW.DRAWMAP
  [LAMBDA (MAPELT MAPW WINREG)                               (* rrb "28-JAN-83 16:43")
                                                             (* the display function for a map piece part of a figure
							     window.)
    (DRAW.MAP.FROM.MAPINFO (WINDOWPROP MAPW (QUOTE DSP))
			   (fetch (SCREENELT GLOBALPART) of MAPELT)
			   (OR WINREG (DSPCLIPPINGREGION NIL MAPW])

(GETLATLON
  [LAMBDA (W)                                                (* rrb "10-JAN-83 21:04")
                                                             (* returns the latitude longitude for a point from the 
							     user.)
    (PROG ((PT (GETPOSITION W)))
          (RETURN (create POSITION
			  XCOORD ←[FILETOREALLON (MAPUNSCALE (fetch XCOORD of PT)
							     (WINDOWPROP W (QUOTE LONGITUDEOFFSET))
							     (WINDOWPROP W (QUOTE LONGITUDESCALE]
			  YCOORD ←(FILETOREALLAT (MAPUNSCALE (fetch YCOORD of PT)
							     (WINDOWPROP W (QUOTE LATITUDEOFFSET))
							     (WINDOWPROP W (QUOTE LATITUDESCALE])

(GETMAPREGION
  [LAMBDA (MAPW)                                             (* rrb "13-JAN-83 11:21")
                                                             (* reads a region from the user and translates it into 
							     file map coordinates)
    (PROG ((REG (GETWREGION MAPW))
	   (MAPDESC (MAPDESCFROMFIGUREW MAPW))
	   LONGSCALE LATSCALE)
          (RETURN (create REGION
			  LEFT ←(MAPUNSCALE (fetch (REGION LEFT) of REG)
					    (fetch (MAPPIECE LONGITUDEOFFSET) of MAPDESC)
					    (SETQ LONGSCALE (fetch (MAPPIECE LONGITUDESCALE)
							       of MAPDESC)))
			  BOTTOM ←(MAPUNSCALE (fetch (REGION BOTTOM) of REG)
					      (fetch (MAPPIECE LATITUDEOFFSET) of MAPDESC)
					      (SETQ LATSCALE (fetch (MAPPIECE LATITUDESCALE)
								of MAPDESC)))
			  WIDTH ←(ITIMES (fetch (REGION WIDTH) of REG)
					 LONGSCALE)
			  HEIGHT ←(ITIMES (fetch (REGION HEIGHT) of REG)
					  LATSCALE])

(GETWORLD
  [LAMBDA NIL                                                (* rrb "26-JAN-83 09:15")
    (DECLARE (SPECVARS CHARMICONFONT))                       (* draws a map of the world at a defined scale.)
                                                             (* map doesn't show Asia because it is not in the sample
							     data. To get Asia 422 needs to be 628)
    (GETMAPFILES)
    (PROG (CHARMICONFONT)                                    (* bind CHARMICONFONT to NIL so that the icon menu won't
							     be created for the world.)
          (RETURN (CREATE.MAPW (create REGION
				       LEFT ← -32400
				       BOTTOM ← -13800
				       HEIGHT ← 30000
				       WIDTH ← 42200)
			       NIL
			       (create REGION
				       LEFT ← 300
				       BOTTOM ← 10
				       HEIGHT ←(HEIGHTIFWINDOW 300 NIL)
				       WIDTH ←(WIDTHIFWINDOW 422))
			       NIL])

(GETMAPFILES
  [LAMBDA NIL                                                (* rrb "16-MAR-83 14:07")
                                                             (* gets the map files into core.)
    (COND
      ([NOT (SETQ GLOBALMAPINDEXFILE (INFILEP (QUOTE {CORE}WORLD-COAST-S.INDEX]
	(PROMPTPRINT "copying a map data file into core...")
	(SETQ GLOBALMAPINDEXFILE (COPYFILE (QUOTE <BURTON>MAPS>WORLD-COAST-S.INDEX)
					   (QUOTE {CORE}WORLD-COAST-S.INDEX)))
	(PROMPTPRINT "done!")))
    (COND
      ([NOT (SETQ GLOBALMAPDATAFILE (INFILEP (QUOTE {CORE}WORLD-COAST-S.MAP]
	(PROMPTPRINT "copying a map data file into core...")
	(SETQ GLOBALMAPDATAFILE (COPYFILE (QUOTE <BURTON>MAPS>WORLD-COAST-S.MAP)
					  (QUOTE {CORE}WORLD-COAST-S.MAP)))
	(PROMPTPRINT "done!"])

(MAPREGIONTOWINDOW
  [LAMBDA (REG MAPW)                                         (* rrb "13-JAN-83 11:28")
                                                             (* converts a region in map coordinates into a region in
							     window coordinates.)
    (PROG ((MAPDESC (MAPDESCFROMFIGUREW MAPW))
	   LONGSCALE LATSCALE)
          (SETQ LONGSCALE (fetch (MAPPIECE LONGITUDESCALE) of MAPDESC))
          (SETQ LATSCALE (fetch (MAPPIECE LATITUDESCALE) of MAPDESC))
          (RETURN (create REGION
			  LEFT ←(MAPSCALE (fetch (REGION LEFT) of REG)
					  (fetch (MAPPIECE LONGITUDEOFFSET) of MAPDESC)
					  LONGSCALE)
			  BOTTOM ←(MAPSCALE (fetch (REGION BOTTOM) of REG)
					    (fetch (MAPPIECE LATITUDEOFFSET) of MAPDESC)
					    LATSCALE)
			  WIDTH ←(IQUOTIENT (fetch (REGION WIDTH) of REG)
					    LONGSCALE)
			  HEIGHT ←(IQUOTIENT (fetch (REGION HEIGHT) of REG)
					     LATSCALE])

(MAPUNSCALE
  [LAMBDA (MAPVALUE OFFSET SCALE)                            (* rrb "11-JAN-83 10:32")
                                                             (* converts from a map value in window coordinates into 
							     a file value)
    (IPLUS (ITIMES MAPVALUE SCALE)
	   OFFSET])

(MAPW.BUTTONEVENTFN
  [LAMBDA (MAPW)                                             (* rrb "11-JAN-83 14:55")
                                                             (* the button event function for map windows.)
    (COND
      ((LASTMOUSESTATE MIDDLE)
	(TOTOPW MAPW)
	(SELECTQ [MENU (COND
			 ((type? MENU MAPW.COMMAND.MENU)
			   MAPW.COMMAND.MENU)
			 (T (SETQ MAPW.COMMAND.MENU (create MENU
							    ITEMS ←(QUOTE ((Zoom (QUOTE ZOOM)
										 
						       "Creates another window onto a subregion.")
									    (ShowL&L (QUOTE LATLON)
										     
			 "Creates a window which shows the latitude and longitude of the cursor."]
		 (ZOOM (CREATE.MAPW.FROM.REGION MAPW))
		 (LATLON (ADD.LATLON.DISPLAY MAPW))
		 NIL])

(MAPW.DISPLAYER
  [LAMBDA (MAPW)                                             (* rrb "20-JAN-83 17:11")
                                                             (* the repaint function for a window onto a piece of the
							     world.)
    (PROG ((WREGION (WINDOWPROP MAPW (QUOTE WORLDREGION)))
	   (RETURN (DRAW.MAP (WINDOWPROP MAPW (QUOTE MAPFILE))
			     (WINDOWPROP MAPW (QUOTE DSP))
			     (WINDOWPROP MAPW (QUOTE LONGITUDEOFFSET))
			     (WINDOWPROP MAPW (QUOTE LATITUDEOFFSET))
			     (WINDOWPROP MAPW (QUOTE LONGITUDESCALE))
			     (WINDOWPROP MAPW (QUOTE LATITUDESCALE))
			     (fetch (REGION LEFT) of WREGION)
			     (fetch (REGION RIGHT) of WREGION)
			     (fetch (REGION BOTTOM) of WREGION)
			     (fetch (REGION TOP) of WREGION])

(MAPSCALEIN
  [LAMBDA (offset scale stream)                              (* rrb "11-JAN-83 10:30")
                                                             (* converts from a map value read from a file to a 
							     window coordinate.)
    (IQUOTIENT (IDIFFERENCE (ReadSigned16 stream)
			    offset)
	       scale])

(MAPSCALE
  [LAMBDA (VALUE OFFSET SCALE)                               (* rrb "11-JAN-83 10:31")
                                                             (* converts from a file value to a map coordinate.)
    (IQUOTIENT (IDIFFERENCE VALUE OFFSET)
	       SCALE])

(PLOTSEGMENT
  [LAMBDA (DSP Stream Count Xoff Xscale Yoff Yscale)         (* rrb "25-JAN-83 20:37")
    (PROG (Lat Long PREVLAT PREVLONG)
          (SETQ PREVLAT (MAPSCALEIN Yoff Yscale Stream))
          (SETQ PREVLONG (MAPSCALEIN Xoff Xscale Stream))    (* draw the first point.)
          (DRAWLINE PREVLONG PREVLAT PREVLONG PREVLAT 1 (QUOTE PAINT)
		    DSP)
          (for i to (SUB1 Count)
	     do (SETQ Lat (MAPSCALEIN Yoff Yscale Stream))
		(SETQ Long (MAPSCALEIN Xoff Xscale Stream))
		(COND
		  ((OR (NEQ Lat PREVLAT)
		       (NEQ Long PREVLONG))

          (* look for segments that don't go anywhere. (This saves about 20 percent when drawing the world map and doesn't 
	  cost much for larger scale maps. NIL))


		    (DRAWLINE PREVLONG PREVLAT (SETQ PREVLONG Long)
			      (SETQ PREVLAT Lat)
			      1
			      (QUOTE PAINT)
			      DSP])

(ReadSigned16
  [LAMBDA (S)                                                (* rrb "20-JAN-83 11:18")
    (PROG [(us (LOGOR (LLSH (BIN S)
			    8)
		      (BIN S]
          (RETURN (COND
		    ((IGREATERP us 32767)                    (* Make negative number)
		      (IDIFFERENCE us 65536))
		    (T us])

(SCALE.FROM.MAPINFO
  [LAMBDA (MAPINFO)                                          (* rrb "24-JAN-83 15:30")

          (* gets the scale that is used to determine what is included in a map and what is left out.
	  For now use the longitude scale.)


    (fetch (MAPPIECE LONGITUDESCALE) of MAPINFO])

(SCALE.FROM.MAPW
  [LAMBDA (WINDOW)                                           (* rrb "24-JAN-83 15:30")
                                                             (* gets the scale of a map figure window.)
    (SCALE.FROM.MAPINFO (MAPDESCFROMFIGUREW WINDOW])

(MAP.SCALE.POSITION
  [LAMBDA (POS SCALE SWLATLON)                               (* rrb "24-JAN-83 14:25")
                                                             (* scales a position from latlon onto a piece of a map 
							     whose lower left corner is SWLATLON)
    (create POSITION
	    XCOORD ←(MAPSCALE (fetch (POSITION XCOORD) of POS)
			      (fetch (POSITION XCOORD) of SWLATLON)
			      (fetch (POSITION XCOORD) of SCALE))
	    YCOORD ←(MAPSCALE (fetch (POSITION YCOORD) of POS)
			      (fetch (POSITION YCOORD) of SWLATLON)
			      (fetch (POSITION YCOORD) of SCALE])

(UNSCALE.POSITION.FROM.MAPINFO
  [LAMBDA (POS MAPINFO)                                      (* rrb "25-JAN-83 18:58")
                                                             (* unscales a window position into latitude longitude 
							     coordinates using the information in MAPINFO)
    (create POSITION
	    XCOORD ←(MAPUNSCALE (fetch (POSITION XCOORD) of POS)
				(fetch (MAPPIECE LONGITUDEOFFSET) of MAPINFO)
				(fetch (MAPPIECE LONGITUDESCALE) of MAPINFO))
	    YCOORD ←(MAPUNSCALE (fetch (POSITION YCOORD) of POS)
				(fetch (MAPPIECE LATITUDEOFFSET) of MAPINFO)
				(fetch (MAPPIECE LATITUDESCALE) of MAPINFO])

(SCALE.POSITION.FROM.MAPINFO
  [LAMBDA (POS MAPINFO)                                      (* rrb "26-JAN-83 12:29")
                                                             (* scales a latitude longitude position into window 
							     coordinates using the information in MAPINFO)
    (create POSITION
	    XCOORD ←(MAPSCALE (fetch (POSITION XCOORD) of POS)
			      (fetch (MAPPIECE LONGITUDEOFFSET) of MAPINFO)
			      (fetch (MAPPIECE LONGITUDESCALE) of MAPINFO))
	    YCOORD ←(MAPSCALE (fetch (POSITION YCOORD) of POS)
			      (fetch (MAPPIECE LATITUDEOFFSET) of MAPINFO)
			      (fetch (MAPPIECE LATITUDESCALE) of MAPINFO])

(UPDATE.CURVE.TO.SCALE
  [LAMBDA (CURVEELT SCALE SWLATLON)                          (* rrb "11-MAY-83 13:05")

          (* returns a screen elt for a curve that has a new local part scaled to its position according to a latitude 
	  longitude scale and a lower left corner.)


    (PROG ((GCURVE (fetch (SCREENELT GLOBALPART) of CURVEELT)))
          (RETURN (create SCREENELT
			  LOCALPART ←(create LOCALCURVE
					     KNOTS ←(for PT in (fetch (CURVE LATLONKNOTS)
								  of GCURVE)
						       collect (MAP.SCALE.POSITION PT SCALE SWLATLON))
					     )
			  GLOBALPART ← GCURVE])

(UPDATE.ELT.TO.SCALE
  [LAMBDA (FIGUREELT SCALE SOUTHWESTLATLON)                  (* rrb "25-JAN-83 19:55")
                                                             (* updates a map figure element from its lat-lon using 
							     SCALE.)
                                                             (* this keeps a list of the new updated screen 
							     elements.)
    (PROG (ELT)
          (AND (SETQ ELT (SELECTQ (fetch (PELT PTYPE) of (fetch (SCREENELT GLOBALPART) of FIGUREELT))
				  (CURVE (UPDATE.CURVE.TO.SCALE FIGUREELT SCALE SOUTHWESTLATLON))
				  (TEXT (UPDATE.TEXT.TO.SCALE FIGUREELT SCALE SOUTHWESTLATLON))
				  (IMAGE (UPDATE.IMAGE.TO.SCALE FIGUREELT SCALE SOUTHWESTLATLON))
				  (CIRCLE (UPDATE.CIRCLE.TO.SCALE FIGUREELT SCALE SOUTHWESTLATLON))
				  (ELLIPSE (UPDATE.ELLIPSE.TO.SCALE FIGUREELT SCALE SOUTHWESTLATLON))
				  (MAPPIECE NIL)
				  (ERROR "bad figure element" FIGUREELT)))
	       (SETQ LOCALELTS (CONS ELT LOCALELTS])

(UPDATE.CIRCLE.TO.SCALE
  [LAMBDA (CIRCLEELT SCALE SWLATLON)                         (* rrb "11-MAY-83 13:05")

          (* returns a screen element that has the same global part but a new local for a circle element that has been 
	  scaled according to SCALE and a latitude longitude pair.)


    (PROG ((GCIRCLE (fetch (SCREENELT GLOBALPART) of CIRCLEELT))
	   CENTER RADIUSPT)
          (RETURN (create SCREENELT
			  LOCALPART ←(create LOCALCIRCLE
					     CENTERPOSITION ←(SETQ CENTER (MAP.SCALE.POSITION
						 (fetch (CIRCLE CENTERLATLON) of GCIRCLE)
						 SCALE SWLATLON))
					     RADIUSPOSITION ←(SETQ RADIUSPT
					       (MAP.SCALE.POSITION (fetch (CIRCLE RADIUSLATLON)
								      of GCIRCLE)
								   SCALE SWLATLON))
					     RADIUS ←(DISTANCEBETWEEN CENTER RADIUSPT))
			  GLOBALPART ← GCIRCLE])

(UPDATE.ELLIPSE.TO.SCALE
  [LAMBDA (ELLIPSEELT SCALE SWLATLON)                        (* rrb "11-MAY-83 13:05")

          (* returns a screen element that has the same global part but a new local for a ellipse element that has been 
	  scaled according to SCALE and a latitude longitude pair.)


    (PROG ((GELLIPSE (fetch (SCREENELT GLOBALPART) of ELLIPSEELT))
	   CENTER MINRAD MAJRAD)
          (RETURN (create SCREENELT
			  LOCALPART ←(create LOCALELLIPSE
					     ELLIPSECENTER ←(SETQ CENTER (MAP.SCALE.POSITION
						 (fetch (ELLIPSE ELLIPSECENTERLATLON) of GELLIPSE)
						 SCALE SWLATLON))
					     MINORRADIUSPOSITION ←(SETQ MINRAD
					       (MAP.SCALE.POSITION (fetch (ELLIPSE SEMIMINORLATLON)
								      of GELLIPSE)
								   SCALE SWLATLON))
					     MAJORRADIUSPOSITION ←(SETQ MAJRAD
					       (MAP.SCALE.POSITION (fetch (ELLIPSE SEMIMAJORLATLON)
								      of GELLIPSE)
								   SCALE SWLATLON))
					     SEMIMINORRADIUS ←(DISTANCEBETWEEN CENTER MINRAD)
					     SEMIMAJORRADIUS ←(DISTANCEBETWEEN CENTER MAJRAD))
			  GLOBALPART ← GELLIPSE])

(UPDATE.IMAGE.TO.SCALE
  [LAMBDA (IMAGEELT SCALE SWLATLON)                          (* rrb "11-MAY-83 13:05")

          (* returns a screen element that has the same global part but a new local for a image element that has been scaled
	  according to SCALE and a latitude longitude pair.)


    (PROG ((GIMAGE (fetch (SCREENELT GLOBALPART) of IMAGEELT))
	   LOCALPOS)
          (RETURN (create SCREENELT
			  LOCALPART ←(create LOCALIMAGE
					     IMAGEPOSITION ←(MAP.SCALE.POSITION (fetch (IMAGE 
										    DISPLAYLATLON)
										   of GIMAGE)
										SCALE SWLATLON))
			  GLOBALPART ← GIMAGE])

(UPDATE.LOCAL.FROM.GLOBAL
  [LAMBDA (SCREENELT)                                        (* updates the fields of a screen element from the 
							     global values.)
    ])

(UPDATE.POSITIONS.TO.SCALE
  [LAMBDA (ELTS SCALE SOUTHWESTLATLON)                       (* rrb "25-JAN-83 19:34")
    (DECLARE (SPECVARS LOCALELTS))                           (* goes through a list of fiugure elements and updates 
							     their positions from their latitude longitude values 
							     according to SCALE)

          (* this has the property of "flattening" everything to the same level of grouping but as of now there is no use of
	  grouping anyway.)


    (PROG (LOCALELTS)
          (MAPFIGURESPECS ELTS (FUNCTION UPDATE.ELT.TO.SCALE)
			  SCALE SOUTHWESTLATLON)
          (RETURN (REVERSE LOCALELTS])

(UPDATE.TEXT.TO.SCALE
  [LAMBDA (TEXTELT SCALE SWLATLON)                           (* rrb "11-MAY-83 13:05")

          (* returns a screen element that has the same global part but a new local for a text element that has been scaled 
	  according to SCALE and a latitude longitude pair.)

                                                             (* the text element has a location that identifies its 
							     latitude longitude and a display position that give the 
							     origin of the text.)
    (PROG ((GTEXT (fetch (SCREENELT GLOBALPART) of TEXTELT))
	   LOCALPOS)
          (RETURN (create SCREENELT
			  LOCALPART ←(create LOCALTEXT
					     LOCATIONPOSITION ←(SETQ LOCALPOS
					       (MAP.SCALE.POSITION (fetch (TEXT LOCATIONLATLON)
								      of GTEXT)
								   SCALE SWLATLON))
					     DISPLAYPOSITION ←(PTPLUS LOCALPOS (fetch (TEXT 
										    DISPLAYOFFSET)
										  of GTEXT)))
			  GLOBALPART ← GTEXT])

(UPDATELATLON
  [LAMBDA (MAPW)                                             (* rrb "13-JAN-83 13:31")
                                                             (* checks to see if the latitude longitude display needs
							     to be updated.)
    (COND
      ([OR (AND (NEQ MAPW.LASTCURSORPTX (SETQ MAPW.LASTCURSORPTX (LASTMOUSEX MAPW)))
		(SETQ MAPW.LASTCURSORPTY (LASTMOUSEY MAPW)))
	   (NEQ MAPW.LASTCURSORPTY (SETQ MAPW.LASTCURSORPTY (LASTMOUSEY MAPW]
                                                             (* call it if either point has changed.)
	(MAPW.DISPLAY.LAT.LON MAPW])
)
(DEFINEQ

(READLATLON
  [LAMBDA (INITLATLON)                                       (* rrb "30-JAN-83 10:48")
                                                             (* reads a latitude longitude value via a window.)
    (PROG ((LATLONW (CREATE.LAT.LON.DISPLAYER MAPWFONT "Enter Lat/Lon"))
	   LAT LON)
          [COND
	    (INITLATLON (SETQ LAT (fetch (POSITION YCOORD) of INITLATLON))
			(SETQ LON (fetch (POSITION XCOORD) of INITLATLON)))
	    (T                                               (* start at 0 0)
	       (SETQ LAT (REALTOFILELAT 0.0))
	       (SETQ LON (REALTOFILELON 0.0]                 (* set up the latlonw to save a lat lon and scroll it by
							     actions on the buttons.)
          (WINDOWPROP LATLONW (QUOTE LAT.DISPLAYED)
		      LAT)
          (WINDOWPROP LATLONW (QUOTE LON.DISPLAYED)
		      LON)
          (WINDOWPROP LATLONW (QUOTE BUTTONEVENTFN)
		      (FUNCTION READ.LATLON.EVENTFN))
          (WINDOWPROP LATLONW (QUOTE RIGHTBUTTONFN)
		      (FUNCTION READ.LATLON.EVENTFN))
          (WINDOWPROP LATLONW (QUOTE CURSORINFN)
		      (FUNCTION READ.LATLON.EVENTFN))
          (WINDOWPROP LATLONW (QUOTE CURSORMOVEDFN)
		      (FUNCTION READ.LATLON.EVENTFN))
          (SHOW.LAT.LON LAT LON LATLONW)
          (until (WINDOWPROP LATLONW (QUOTE FINISHED))
	     do                                              (* wait for the background process or task to mark that 
							     the user is finished.)
		(BLOCK))
          (CLOSEW LATLONW)
          (RETURN (AND (WINDOWPROP LATLONW (QUOTE LAT.DISPLAYED))
		       (create POSITION
			       XCOORD ←(WINDOWPROP LATLONW (QUOTE LON.DISPLAYED)
						   NIL)
			       YCOORD ←(WINDOWPROP LATLONW (QUOTE LAT.DISPLAYED)
						   NIL])

(READ.LATLON.EVENTFN
  [LAMBDA (LATLONW)                                          (* rrb "30-JAN-83 12:31")
                                                             (* button event fn for a lat lon reader.)
    (COND
      ((LASTMOUSESTATE UP)                                   (* no buttons down)
	NIL)
      ((LASTMOUSESTATE MIDDLE)                               (* pop up command menu)
	(SELECTQ [MENU (COND
			 ((type? MENU READ.LAT.LON.COMMANDMENU)
			   READ.LAT.LON.COMMANDMENU)
			 (T (SETQ READ.LAT.LON.COMMANDMENU (create MENU
								   ITEMS ←(QUOTE
								     ((OK (QUOTE OK)
									  
						"Sets the position to the indicated coordinates.")
								       (STOP (QUOTE STOP)
									     
							   "Stops without changing the position.")))
								   CENTERFLG ← T
								   MENUFONT ← BOLDMAPFONT]
		 (OK (WINDOWPROP LATLONW (QUOTE FINISHED)
				 T))
		 (STOP                                       (* mark the fact that the user stopped in the latitude 
							     cell. This is tested for in READLATLON.)
		       (WINDOWPROP LATLONW (QUOTE LAT.DISPLAYED)
				   NIL)
		       (WINDOWPROP LATLONW (QUOTE FINISHED)
				   T))
		 NIL))
      (T 

          (* scroll in units of degrees or hundreds of degrees up or down according to the position of the cursor wrt the 
	  line between the two displayed numbers and the decimal points.)


	 (PROG [(DEGSCROLLAMOUNT (CONSTANT (REALTOFILELON 1.0)))
		(HUNDREDTHSCROLLAMOUNT (CONSTANT (REALTOFILELON .01)))
		[NSLINE (CONSTANT (FONTPROP BOLDMAPFONT (QUOTE HEIGHT]
		[EWLINE (CONSTANT (FIX (FTIMES 3.5 (CHARWIDTH (CHCON1 0)
							      BOLDMAPFONT]
		(MINLAT (CONSTANT (REALTOFILELAT -90.0)))
		(MAXLAT (CONSTANT (REALTOFILELAT 90.0)))
		(MINLON (CONSTANT (REALTOFILELON -180.0)))
		(MAXLON (CONSTANT (REALTOFILELON 180.0)))
		(DISMISSTIME 300)
		X Y (WINDOWREG (WINDOWPROP LATLONW (QUOTE REGION)))
		(DISPLAYREG (DSPCLIPPINGREGION NIL LATLONW))
		(LAT (WINDOWPROP LATLONW (QUOTE LAT.DISPLAYED)))
		(LON (WINDOWPROP LATLONW (QUOTE LON.DISPLAYED)))
		(TIMEBOX (CLOCK0 (CONSTANT (\MAKENUMBER 1 1]
                                                             (* these are computed once at compile time to reduce the
							     overhead on each button press.)
	   LP  (GETMOUSESTATE)
	       [COND
		 ([OR (LASTMOUSESTATE UP)
		      (NOT (INSIDE? DISPLAYREG (SETQ X (LASTMOUSEX LATLONW))
				    (SETQ Y (LASTMOUSEY LATLONW]
                                                             (* outside of numbers region or the buttons came up, 
							     return. If he comes back in or moves, this functionwill 
							     get called again.)

          (* restore the lat lon values to the property list of the window. This is not done everytime to reduce the 
	  overhead within the incrementing loop.)


		   (WINDOWPROP LATLONW (QUOTE LAT.DISPLAYED)
			       LAT)
		   (WINDOWPROP LATLONW (QUOTE LON.DISPLAYED)
			       LON)
		   (COND
		     ((AND (LASTMOUSESTATE RIGHT)
			   (INSIDE? WINDOWREG LASTMOUSEX LASTMOUSEY))

          (* check for the right button going down in the title. The window region is in screen coordinates so use 
	  LASTMOUSEX and LASTMOUSEY which are also in screen coordinates.)


		       (DOWINDOWCOM LATLONW)))
		   (RETURN))
		 [(IGREATERP Y NSLINE)                       (* changinge latitude)
		   [SETQ LAT (COND
		       [(LASTMOUSESTATE LEFT)                (* increasing it)
			 (IPLUS LAT (COND
				  ((IGREATERP X EWLINE)
				    HUNDREDTHSCROLLAMOUNT)
				  (T DEGSCROLLAMOUNT]
		       (T                                    (* increasing it)
			  (IDIFFERENCE LAT (COND
					 ((IGREATERP X EWLINE)
					   HUNDREDTHSCROLLAMOUNT)
					 (T DEGSCROLLAMOUNT]
		   (COND
		     ((IGREATERP LAT MAXLAT)
		       (SETQ LAT MAXLAT))
		     ((ILESSP LAT MINLAT)
		       (SETQ LAT MINLAT]
		 (T                                          (* changinge Longitude)
		    [SETQ LON (COND
			[(LASTMOUSESTATE LEFT)               (* increasing it)
			  (IPLUS LON (COND
				   ((IGREATERP X EWLINE)
				     HUNDREDTHSCROLLAMOUNT)
				   (T DEGSCROLLAMOUNT]
			(T                                   (* increasing it)
			   (IDIFFERENCE LON (COND
					  ((IGREATERP X EWLINE)
					    HUNDREDTHSCROLLAMOUNT)
					  (T DEGSCROLLAMOUNT]
		    (COND
		      ((IGREATERP LON MAXLON)
			(SETQ LON MAXLON))
		      ((ILESSP LON MINLON)
			(SETQ LON MINLON]
	       (SHOW.LAT.LON LAT LON LATLONW)
	       [COND
		 ((IGREATERP DISMISSTIME 30)
		   (SETQ DISMISSTIME (IQUOTIENT (ITIMES DISMISSTIME 9)
						10]          (* kill time in a way that doesn't call background NIL)
	       (CLOCK0 TIMEBOX)
	       (until (COND
			((MOUSESTATE UP)                     (* if buttons comes up, reset to longer dismiss time.)
			  (SETQ DISMISSTIME 300))
			((IGREATERP (CLOCKDIFFERENCE TIMEBOX)
				    DISMISSTIME)))
		  do)
	       (GO LP])

(SHOW.LAT.LON
  [LAMBDA (LAT LON W)                                        (* rrb "27-JAN-83 12:55")
                                                             (* converts to real lat lon and displays it in W)
    (PROG (DIR X)
          (MOVETO 0 (WINDOWPROP W (QUOTE ORGY))
		  W)
          (printout W .F6.2 (COND
		      ((FGREATERP (SETQ X (FILETOREALLAT LAT))
				  0.0)
			(SETQ DIR " N")
			X)
		      (T (SETQ DIR " S")
			 (FDIFFERENCE 0.0 X)))
		    DIR T .F6.2 (COND
		      ((FGREATERP (SETQ X (FILETOREALLON LON))
				  0.0)
			(SETQ DIR " E")
			X)
		      (T (SETQ DIR " W")
			 (FDIFFERENCE 0.0 X)))
		    DIR])

(CREATE.LAT.LON.DISPLAYER
  [LAMBDA (FONT TITLE)                                       (* rrb "30-JAN-83 10:37")
                                                             (* creates a window for displaying latitude longitude.)
    (OR FONT (SETQ FONT MAPWFONT))
    (PROG [(LATLONW (CREATEW (GETBOXREGION (WIDTHIFWINDOW (STRINGWIDTH "175.88 W " FONT))
					   (HEIGHTIFWINDOW (ITIMES 2 (FONTPROP FONT (QUOTE HEIGHT)))
							   T))
			     (OR TITLE "Lat Long"]           (* extra space on stringwidth is to allow for the fact 
							     that printout translates into PRIN1 rather than PRIN3.)
          (DSPFONT FONT LATLONW)
          (DSPRESET LATLONW)                                 (* reset its coordinates to the upper left and save the 
							     y position.)
          (WINDOWPROP LATLONW (QUOTE ORGY)
		      (DSPYPOSITION NIL LATLONW))
          (WINDOWPROP LATLONW (QUOTE CLOSEFN)
		      (FUNCTION LATLON.CLOSEFN))
          (RETURN LATLONW])

(LATLON.CLOSEFN
  [LAMBDA (LATLONW)                                          (* rrb "27-JAN-83 12:27")
                                                             (* close function for a window that is keeping track of 
							     the latitude longitude for a map window.
							     It breaks the link to itself.)
    (PROG ((MAPW (MAPW.FROM.LATLONW LATLONW)))
          (OR MAPW (RETURN))
          (WINDOWPROP MAPW (QUOTE LATLONDISPLAYER)
		      NIL)
          (WINDOWPROP MAPW (QUOTE CURSORMOVEDFN)
		      NIL])

(ADD.LATLON.DISPLAY
  [LAMBDA (MAPW)                                             (* rrb "27-JAN-83 12:30")
                                                             (* creates a map onto a portion of the world)
    (WINDOWPROP MAPW (QUOTE LATLONDISPLAYER)
		(CREATE.LAT.LON.DISPLAYER MAPWFONT))
    (WINDOWPROP MAPW (QUOTE CURSORMOVEDFN)
		(FUNCTION MAPW.DISPLAY.LAT.LON])

(MAPW.DISPLAY.LAT.LON
  [LAMBDA (W)                                                (* rrb "27-JAN-83 12:56")
                                                             (* a cursor moved function for a map that shows the 
							     latitude longitude of the cursor.)
    (PROG ((LATLONW (WINDOWPROP W (QUOTE LATLONDISPLAYER)))
	   MAPDESC)
          (OR LATLONW (RETURN))
          (OR (SETQ MAPDESC (MAPDESCFROMFIGUREW W))
	      (RETURN))
          (SHOW.LAT.LON (MAPUNSCALE (LASTMOUSEY W)
				    (fetch (MAPPIECE LATITUDEOFFSET) of MAPDESC)
				    (fetch (MAPPIECE LATITUDESCALE) of MAPDESC))
			(MAPUNSCALE (LASTMOUSEX W)
				    (fetch (MAPPIECE LONGITUDEOFFSET) of MAPDESC)
				    (fetch (MAPPIECE LONGITUDESCALE) of MAPDESC))
			LATLONW])

(FILETOREALLAT
  [LAMBDA (FILEVALUE)                                        (* rrb "27-JAN-83 12:46")

          (* converts from a file value into a real latitude. The file coordinate system has -2↑15 at the south pole and 
	  +2↑15 at the north pole.)


    (FTIMES FILEVALUE (CONSTANT (FQUOTIENT 180.0 (EXPT 2 15])

(FILETOREALLON
  [LAMBDA (FILEVALUE)                                        (* rrb "27-JAN-83 12:47")
                                                             (* converts from a file value into a real longitude.
							     The file coordinate system has -2↑15 at -180.0 and +2↑15
							     at 180.0)
    (FTIMES FILEVALUE (CONSTANT (FQUOTIENT 180.0 (EXPT 2 15])

(REALTOFILELAT
  [LAMBDA (REALLAT)                                          (* rrb "27-JAN-83 12:42")

          (* converts from a real latitude into a file value. The file coordinate system has -2↑15 at the south pole and 
	  +2↑15 at the north pole. NIL)


    (FIX (FTIMES REALLAT (CONSTANT (FQUOTIENT (EXPT 2 15)
					      180.0])

(REALTOFILELON
  [LAMBDA (REALLON)                                          (* rrb "27-JAN-83 12:47")
                                                             (* converts from a real longitude into a file value.
							     The file coordinate system has -2↑15 at -180.0 and +2↑15
							     at 180.0)
    (FIX (FTIMES REALLON (CONSTANT (FQUOTIENT (EXPT 2 15)
					      180.0])
)
(DEFINEQ

(MAP.DATA.IN.REGION
  [LAMBDA (SCREENELTS WORLDREG)
    (DECLARE (SPECVARS INSIDEELTS))                          (* rrb "24-JAN-83 20:24")
                                                             (* returns the screen elements in SCREENELTS that are 
							     inside WORLDREG.)
    (PROG (INSIDEELTS)
          (MAPFIGURESPECS SCREENELTS (FUNCTION ADD.TO.LST.IF.INSIDE)
			  WORLDREG)
          (RETURN (REVERSE INSIDEELTS])

(MAPW.DATA
  [LAMBDA (MAPW)                                             (* rrb "24-JAN-83 19:55")
                                                             (* retrieves the map annotations from a map window.
							     For now the annotations are stored as figure 
							     specifications. NIL)
    (FIGURESPECS MAPW])

(ADD.TO.LST.IF.INSIDE
  [LAMBDA (SCREENELT WORLDREG)                               (* rrb "26-JAN-83 17:29")
                                                             (* checks to see if SCREENELT is inside of WORLDREG and 
							     if so adds it to the list INSIDEELTS.)
    (AND (MAPW.INSIDE.REGION (fetch (SCREENELT GLOBALPART) of SCREENELT)
			     WORLDREG)
	 (SETQ INSIDEELTS (CONS SCREENELT INSIDEELTS])

(MAPW.INSIDE.CIRCLE
  [LAMBDA (GCIRCLE WREG)                                     (* rrb "24-JAN-83 20:19")
                                                             (* determines if the global circle GCIRCLE is inside of 
							     WREG.)

          (* consider a circle inside only if one of its control points is inside. {old code (PROG ((CPOS 
	  (fetch (CIRCLE CENTERLATLON) of LCIRCLE)) CX CY RAD) (SETQ RAD (DISTANCEBETWEEN CPOS (fetch 
	  (CIRCLE RADIUSLATLON) of LCIRCLE))) (RETURN (AND (ILESSP (FETCH (REGION BOTTOM) OF WREG) (DIFFERENCE 
	  (SETQ CY (FETCH (POSITION YCOORD) OF CPOS)) RAD)) (ILESSP (FETCH (REGION LEFT) OF WREG) (DIFFERENCE 
	  (SETQ CX (FETCH (POSITION XCOORD) OF CPOS)) RAD)) (IGREATERP (FETCH (REGION TOP) OF WREG) 
	  (IPLUS CY RAD)) (IGREATERP (FETCH (REGION RIGHT) OF WREG) (IPLUS CX RAD))))) })


    (OR (INSIDEP WREG (fetch (CIRCLE CENTERLATLON) of GCIRCLE))
	(INSIDEP WREG (fetch (CIRCLE RADIUSLATLON) of GCIRCLE])

(MAPW.INSIDE.ELLIPSE
  [LAMBDA (GELLIPSE WREG)                                    (* rrb "24-JAN-83 20:19")
                                                             (* determines if the global ellipse GELLIPSE is inside 
							     of WREG.)
                                                             (* consider an ellipse inside only if one of its control
							     points is inside.)
    (OR (INSIDEP WREG (fetch (ELLIPSE ELLIPSECENTERLATLON) of GELLIPSE))
	(INSIDEP WREG (fetch (ELLIPSE SEMIMINORLATLON) of GELLIPSE))
	(INSIDEP WREG (fetch (ELLIPSE SEMIMAJORLATLON) of GELLIPSE])

(MAPW.INSIDE.CURVE
  [LAMBDA (GCURVE WREG)                                      (* rrb "24-JAN-83 20:20")
                                                             (* determines if the global curve GCURVE is inside of 
							     WREG.)
                                                             (* consider a curve inside only if one of its control 
							     points is inside.)
    (for PT in (fetch (CURVE LATLONKNOTS) of GCURVE) when (INSIDEP WREG PT) do (RETURN T])

(MAPW.INSIDE.TEXT
  [LAMBDA (GTEXT WREG)                                       (* rrb "24-JAN-83 20:23")
                                                             (* determines if a piece of text is inside of a world 
							     region.)
                                                             (* consider it inside only if its location is inside.)
    (INSIDEP WREG (fetch (TEXT LOCATIONLATLON) of GTEXT])

(MAPW.INSIDE.IMAGE
  [LAMBDA (GIMAGE WREG)                                      (* rrb "24-JAN-83 20:24")
                                                             (* determines if an image is inside of a world region.)
                                                             (* consider it inside only if its location is inside.)
    (INSIDEP WREG (fetch (IMAGE DISPLAYLATLON) of GIMAGE])
)
(DEFINEQ

(GETDRAWPOSITION
  [LAMBDA (W CURSOR)                                         (* rrb "13-JAN-83 13:33")

          (* provides a hook for the inputting of a point via mouse from the user. for now it is just reading a point but 
	  later should add features such as yellow button means an existing pt or left means on a grid.)


    (RESETFORM (CURSOR (OR CURSOR CROSSHAIRS))
	       (until (MOUSESTATE LEFT) do (UPDATELATLON W))
	       (until (MOUSESTATE (NOT LEFT)) do (UPDATELATLON W))
	       (create POSITION
		       XCOORD ←(LASTMOUSEX W)
		       YCOORD ←(LASTMOUSEY W])
)

(RPAQ? ALL.MAPWS )

(RPAQQ MAPW.AUTOSCALEFLG NIL)

(RPAQQ MAPW.LASTCURSORPTY 105)

(RPAQQ MAPWFONT (GACHA 12 BOLD))

(RPAQQ MAPW.COMMAND.MENU NIL)

(RPAQQ READ.LAT.LON.COMMANDMENU NIL)

(RPAQQ BOLDMAPFONT (GACHA 12 BOLD))
(DECLARE: DOEVAL@COMPILE DONTCOPY

(GLOBALVARS ALL.MAPWS MAPWFONT MAPW.COMMAND.MENU GLOBALMAPINDEXFILE GLOBALMAPDATAFILE)
)
[DECLARE: EVAL@COMPILE 

(TYPERECORD MAPPIECE (MINSCALE MAXSCALE WORLDREGION LATITUDEOFFSET LATITUDESCALE LONGITUDEOFFSET 
			       LONGITUDESCALE MAPFILE))

(RECORD LOCALMAPPIECE (DUMMY))
]
(PUTPROPS MAP COPYRIGHT ("Xerox Corporation" 1983 1984))
(DECLARE: DONTCOPY
  (FILEMAP (NIL (1790 30885 (DRAW.MAP 1800 . 3619) (MAPINDEXFILE 3621 . 3898) (MAPDATAFILE 3900 . 4175) 
(DRAW.MAP.FROM.MAPINFO 4177 . 5331) (MAPDESCFROMFIGUREW 5333 . 5840) (MAPW.CLOSEFN 5842 . 6573) (
MAPW.CREATE.ICON 6575 . 6945) (MAPW.FROM.LATLONW 6947 . 7328) (MAPW.ICON.BITMAP 7330 . 8531) (
CREATE.MAPW 8533 . 11990) (CREATE.MAPW.FROM.REGION 11992 . 12827) (FIGW.DRAWMAP 12829 . 13247) (
GETLATLON 13249 . 13923) (GETMAPREGION 13925 . 14926) (GETWORLD 14928 . 15859) (GETMAPFILES 15861 . 
16637) (MAPREGIONTOWINDOW 16639 . 17634) (MAPUNSCALE 17636 . 17942) (MAPW.BUTTONEVENTFN 17944 . 18702)
 (MAPW.DISPLAYER 18704 . 19512) (MAPSCALEIN 19514 . 19858) (MAPSCALE 19860 . 20139) (PLOTSEGMENT 20141
 . 21047) (ReadSigned16 21049 . 21365) (SCALE.FROM.MAPINFO 21367 . 21689) (SCALE.FROM.MAPW 21691 . 
21971) (MAP.SCALE.POSITION 21973 . 22638) (UNSCALE.POSITION.FROM.MAPINFO 22640 . 23342) (
SCALE.POSITION.FROM.MAPINFO 23344 . 24058) (UPDATE.CURVE.TO.SCALE 24060 . 24704) (UPDATE.ELT.TO.SCALE 
24706 . 25728) (UPDATE.CIRCLE.TO.SCALE 25730 . 26606) (UPDATE.ELLIPSE.TO.SCALE 26608 . 27753) (
UPDATE.IMAGE.TO.SCALE 27755 . 28405) (UPDATE.LOCAL.FROM.GLOBAL 28407 . 28592) (
UPDATE.POSITIONS.TO.SCALE 28594 . 29254) (UPDATE.TEXT.TO.SCALE 29256 . 30266) (UPDATELATLON 30268 . 
30883)) (30886 42542 (READLATLON 30896 . 32681) (READ.LATLON.EVENTFN 32683 . 37653) (SHOW.LAT.LON 
37655 . 38309) (CREATE.LAT.LON.DISPLAYER 38311 . 39301) (LATLON.CLOSEFN 39303 . 39846) (
ADD.LATLON.DISPLAY 39848 . 40239) (MAPW.DISPLAY.LAT.LON 40241 . 41056) (FILETOREALLAT 41058 . 41391) (
FILETOREALLON 41393 . 41781) (REALTOFILELAT 41783 . 42135) (REALTOFILELON 42137 . 42540)) (42543 46847
 (MAP.DATA.IN.REGION 42553 . 43004) (MAPW.DATA 43006 . 43351) (ADD.TO.LST.IF.INSIDE 43353 . 43793) (
MAPW.INSIDE.CIRCLE 43795 . 44807) (MAPW.INSIDE.ELLIPSE 44809 . 45449) (MAPW.INSIDE.CURVE 45451 . 45977
) (MAPW.INSIDE.TEXT 45979 . 46422) (MAPW.INSIDE.IMAGE 46424 . 46845)) (46848 47506 (GETDRAWPOSITION 
46858 . 47504)))))
STOP