/**********************************************************************
 *
 *  geoextra.c  -- Public routines for GEOTIFF GeoKey access.
 *
 *  Written By: Frank Warmerdam
 *
 *  copyright (c) 1995   Frank Warmerdam
 *
 *  Permission granted to use this software, so long as this copyright
 *  notice accompanies any products derived therefrom.
 *
 * $Log: geoextra.c,v $
 * Revision 1.1  1999/01/22 17:40:43  warmerda
 * New
 *
 * Revision 60.5  1996/10/31 15:43:43  warmerda
 * Fixed computing of SPCS zone when reading.
 *
 * Revision 60.4  1996/10/15 03:14:49  warmerda
 * Avoid pci.h for CCL.
 *
 * Revision 60.3  1996/01/31 14:20:09  lim
 * MacOS: Fixed compiler errors.
 *
 * Revision 60.2  1995/12/01  16:31:42  wilson
 * Correct assignment found in boolean expression
 *
 * Revision 60.1  1995/11/17  18:50:40  warmerda
 * Initial revision.
 *
 *
 **********************************************************************/

#include "geotiff.h"
#include "geo_tiffp.h"
#include "geo_keyp.h"
#include "geovalues.h"

#define MapSys_UTM_North	-9001
#define MapSys_UTM_South	-9002
#define MapSys_State_Plane	-9003

static int StatePlaneTable[] = 
{
    PCS_NAD83_Alabama_East,		Proj_Alabama_CS83_East,
    PCS_NAD83_Alabama_West,		Proj_Alabama_CS83_West,

    PCS_NAD83_Alaska_zone_1,		Proj_Alaska_CS83_1,
    PCS_NAD83_Alaska_zone_2,		Proj_Alaska_CS83_2,
    PCS_NAD83_Alaska_zone_3,		Proj_Alaska_CS83_3,
    PCS_NAD83_Alaska_zone_4,		Proj_Alaska_CS83_4,
    PCS_NAD83_Alaska_zone_5,		Proj_Alaska_CS83_5,
    PCS_NAD83_Alaska_zone_6,		Proj_Alaska_CS83_6,
    PCS_NAD83_Alaska_zone_7,		Proj_Alaska_CS83_7,
    PCS_NAD83_Alaska_zone_8,		Proj_Alaska_CS83_8,
    PCS_NAD83_Alaska_zone_9,		Proj_Alaska_CS83_9,
    PCS_NAD83_Alaska_zone_10,		Proj_Alaska_CS83_10,

    PCS_NAD83_California_1,		Proj_California_CS83_1,
    PCS_NAD83_California_2,		Proj_California_CS83_2,
    PCS_NAD83_California_3,		Proj_California_CS83_3,
    PCS_NAD83_California_4,		Proj_California_CS83_4,
    PCS_NAD83_California_5,		Proj_California_CS83_5,
    PCS_NAD83_California_6,		Proj_California_CS83_6,

    PCS_NAD83_Arizona_East,		Proj_Arizona_CS83_east,
    PCS_NAD83_Arizona_Central,		Proj_Arizona_CS83_Central,
    PCS_NAD83_Arizona_West,		Proj_Arizona_CS83_west,

    PCS_NAD83_Arkansas_North,		Proj_Arkansas_CS83_North,
    PCS_NAD83_Arkansas_South,		Proj_Arkansas_CS83_South,

    PCS_NAD83_Colorado_North,		Proj_Colorado_CS83_North,
    PCS_NAD83_Colorado_Central,		Proj_Colorado_CS83_Central,
    PCS_NAD83_Colorado_South,		Proj_Colorado_CS83_South,

    PCS_NAD83_Connecticut,		Proj_Connecticut_CS83,

    PCS_NAD83_Delaware,			Proj_Delaware_CS83,

    PCS_NAD83_Florida_East,		Proj_Florida_CS83_East,
    PCS_NAD83_Florida_North,		Proj_Florida_CS83_North,
    PCS_NAD83_Florida_West,		Proj_Florida_CS83_West,

    PCS_NAD83_Hawaii_zone_1,		Proj_Hawaii_CS83_1,
    PCS_NAD83_Hawaii_zone_2,		Proj_Hawaii_CS83_2,
    PCS_NAD83_Hawaii_zone_3,		Proj_Hawaii_CS83_3,
    PCS_NAD83_Hawaii_zone_4,		Proj_Hawaii_CS83_4,
    PCS_NAD83_Hawaii_zone_5,		Proj_Hawaii_CS83_5,

    PCS_NAD83_Georgia_East,		Proj_Georgia_CS83_East,
    PCS_NAD83_Georgia_West,		Proj_Georgia_CS83_West,

    PCS_NAD83_Idaho_East,		Proj_Idaho_CS83_East,
    PCS_NAD83_Idaho_Central,		Proj_Idaho_CS83_Central,
    PCS_NAD83_Idaho_West,		Proj_Idaho_CS83_West,

    PCS_NAD83_Illinois_East,		Proj_Illinois_CS83_East,
    PCS_NAD83_Illinois_West,		Proj_Illinois_CS83_West,

    PCS_NAD83_Indiana_East,		Proj_Indiana_CS83_East,
    PCS_NAD83_Indiana_West,		Proj_Indiana_CS83_West,

    PCS_NAD83_Iowa_North,      		Proj_Iowa_CS83_North,
    PCS_NAD83_Iowa_South,      		Proj_Iowa_CS83_South,

    PCS_NAD83_Kansas_North,		Proj_Kansas_CS83_North,
    PCS_NAD83_Kansas_South,		Proj_Kansas_CS83_South,

    PCS_NAD83_Kentucky_North,		Proj_Kentucky_CS83_North,
    PCS_NAD83_Kentucky_South,		Proj_Kentucky_CS83_South,

    PCS_NAD83_Louisiana_North,		Proj_Louisiana_CS83_North,
    PCS_NAD83_Louisiana_South,		Proj_Louisiana_CS83_South,

    PCS_NAD83_Maine_East,		Proj_Maine_CS83_East,
    PCS_NAD83_Maine_West,		Proj_Maine_CS83_West,

    PCS_NAD83_Maryland,			Proj_Maryland_CS83,

    PCS_NAD83_Massachusetts,		Proj_Massachusetts_CS83_Mainland,
    PCS_NAD83_Massachusetts_Is,		Proj_Massachusetts_CS83_Island,

    PCS_NAD83_Michigan_North,		Proj_Michigan_CS83_North,
    PCS_NAD83_Michigan_Central,		Proj_Michigan_CS83_Central,
    PCS_NAD83_Michigan_South,		Proj_Michigan_CS83_South,

    PCS_NAD83_Minnesota_North,		Proj_Minnesota_CS83_North,
    PCS_NAD83_Minnesota_Cent,		Proj_Minnesota_CS83_Central,
    PCS_NAD83_Minnesota_South,		Proj_Minnesota_CS83_South,

    PCS_NAD83_Mississippi_East,		Proj_Mississippi_CS83_East,
    PCS_NAD83_Mississippi_West,		Proj_Mississippi_CS83_West,

    PCS_NAD83_Missouri_East,		Proj_Missouri_CS83_East,
    PCS_NAD83_Missouri_Central,		Proj_Missouri_CS83_Central,
    PCS_NAD83_Missouri_West,		Proj_Missouri_CS83_West,

    PCS_NAD83_Montana,			Proj_Montana_CS83,

    PCS_NAD83_Nebraska,			Proj_Nebraska_CS83,

    PCS_NAD83_Nevada_East,		Proj_Nevada_CS83_East,
    PCS_NAD83_Nevada_Central,		Proj_Nevada_CS83_Central,
    PCS_NAD83_Nevada_West,		Proj_Nevada_CS83_West,

    PCS_NAD83_New_Hampshire,		Proj_New_Hampshire_CS83,

    PCS_NAD83_New_Jersey,		Proj_New_Jersey_CS83,

    PCS_NAD83_New_Mexico_East,		Proj_New_Mexico_CS83_East,
    PCS_NAD83_New_Mexico_Cent,		Proj_New_Mexico_CS83_Central,
    PCS_NAD83_New_Mexico_West,		Proj_New_Mexico_CS83_West,

    PCS_NAD83_New_York_East,		Proj_New_York_CS83_East,
    PCS_NAD83_New_York_Central,		Proj_New_York_CS83_Central,
    PCS_NAD83_New_York_West,		Proj_New_York_CS83_West,
    PCS_NAD83_New_York_Long_Is,		Proj_New_York_CS83_Long_Island,

    PCS_NAD83_North_Carolina,	       	Proj_North_Carolina_CS83,

    PCS_NAD83_North_Dakota_N,		Proj_North_Dakota_CS83_North,
    PCS_NAD83_North_Dakota_S,		Proj_North_Dakota_CS83_South,

    PCS_NAD83_Ohio_North,		Proj_Ohio_CS83_North,
    PCS_NAD83_Ohio_South,		Proj_Ohio_CS83_South,

    PCS_NAD83_Oklahoma_North,		Proj_Oklahoma_CS83_North,
    PCS_NAD83_Oklahoma_South,		Proj_Oklahoma_CS83_South,

    PCS_NAD83_Oregon_North,		Proj_Oregon_CS83_North,
    PCS_NAD83_Oregon_South,		Proj_Oregon_CS83_South,

    PCS_NAD83_Pennsylvania_N,		Proj_Pennsylvania_CS83_North,
    PCS_NAD83_Pennsylvania_S,		Proj_Pennsylvania_CS83_South,

    PCS_NAD83_Rhode_Island,		Proj_Rhode_Island_CS83,

    PCS_NAD83_South_Carolina,		Proj_South_Carolina_CS83,

    PCS_NAD83_South_Dakota_N,		Proj_South_Dakota_CS83_North,
    PCS_NAD83_South_Dakota_S,		Proj_South_Dakota_CS83_South,

    PCS_NAD83_Tennessee,		Proj_Tennessee_CS83,

    PCS_NAD83_Texas_North,		Proj_Texas_CS83_North,
    PCS_NAD83_Texas_North_Cen,		Proj_Texas_CS83_North_Central,
    PCS_NAD83_Texas_Central,		Proj_Texas_CS83_Central,
    PCS_NAD83_Texas_South_Cen,		Proj_Texas_CS83_South_Central,
    PCS_NAD83_Texas_South,		Proj_Texas_CS83_South,

    PCS_NAD83_Utah_North,		Proj_Utah_CS83_North,
    PCS_NAD83_Utah_Central,		Proj_Utah_CS83_Central,
    PCS_NAD83_Utah_South,		Proj_Utah_CS83_South,

    PCS_NAD83_Vermont,			Proj_Vermont_CS83,

    PCS_NAD83_Virginia_North,		Proj_Virginia_CS83_North,
    PCS_NAD83_Virginia_South,		Proj_Virginia_CS83_South,

    PCS_NAD83_Washington_North,		Proj_Washington_CS83_North,
    PCS_NAD83_Washington_South,		Proj_Washington_CS83_South,

    PCS_NAD83_West_Virginia_N,		Proj_West_Virginia_CS83_North,
    PCS_NAD83_West_Virginia_S,		Proj_West_Virginia_CS83_South,

    PCS_NAD83_Wisconsin_North,		Proj_Wisconsin_CS83_North,
    PCS_NAD83_Wisconsin_Cen,		Proj_Wisconsin_CS83_Central,
    PCS_NAD83_Wisconsin_South,		Proj_Wisconsin_CS83_South,

    PCS_NAD83_Wyoming_East,		Proj_Wyoming_CS83_East,
    PCS_NAD83_Wyoming_E_Cen,		Proj_Wyoming_CS83_East_Central,
    PCS_NAD83_Wyoming_W_Cen,		Proj_Wyoming_CS83_West_Central,
    PCS_NAD83_Wyoming_West,		Proj_Wyoming_CS83_West,
    
    PCS_NAD83_Puerto_Rico_Virgin_Is,	Proj_Puerto_Rico_Virgin_Is,

    PCS_NAD27_Alabama_East,		Proj_Alabama_CS27_East,
    PCS_NAD27_Alabama_West,		Proj_Alabama_CS27_West,

    PCS_NAD27_Alaska_zone_1,		Proj_Alaska_CS27_1,
    PCS_NAD27_Alaska_zone_2,		Proj_Alaska_CS27_2,
    PCS_NAD27_Alaska_zone_3,		Proj_Alaska_CS27_3,
    PCS_NAD27_Alaska_zone_4,		Proj_Alaska_CS27_4,
    PCS_NAD27_Alaska_zone_5,		Proj_Alaska_CS27_5,
    PCS_NAD27_Alaska_zone_6,		Proj_Alaska_CS27_6,
    PCS_NAD27_Alaska_zone_7,		Proj_Alaska_CS27_7,
    PCS_NAD27_Alaska_zone_8,		Proj_Alaska_CS27_8,
    PCS_NAD27_Alaska_zone_9,		Proj_Alaska_CS27_9,
    PCS_NAD27_Alaska_zone_10,		Proj_Alaska_CS27_10,

    PCS_NAD27_California_I,		Proj_California_CS27_I,
    PCS_NAD27_California_II,		Proj_California_CS27_II,
    PCS_NAD27_California_III,		Proj_California_CS27_III,
    PCS_NAD27_California_IV,		Proj_California_CS27_IV,
    PCS_NAD27_California_V,		Proj_California_CS27_V,
    PCS_NAD27_California_VI,		Proj_California_CS27_VI,
    PCS_NAD27_California_VII,		Proj_California_CS27_VII,

    PCS_NAD27_Arizona_East,		Proj_Arizona_Coordinate_System_east,
    PCS_NAD27_Arizona_Central,		Proj_Arizona_Coordinate_System_Central,
    PCS_NAD27_Arizona_West,		Proj_Arizona_Coordinate_System_west,

    PCS_NAD27_Arkansas_North,		Proj_Arkansas_CS27_North,
    PCS_NAD27_Arkansas_South,		Proj_Arkansas_CS27_South,

    PCS_NAD27_Colorado_North,		Proj_Colorado_CS27_North,
    PCS_NAD27_Colorado_Central,		Proj_Colorado_CS27_Central,
    PCS_NAD27_Colorado_South,		Proj_Colorado_CS27_South,

    PCS_NAD27_Connecticut,		Proj_Connecticut_CS27,

    PCS_NAD27_Delaware,			Proj_Delaware_CS27,

    PCS_NAD27_Florida_East,		Proj_Florida_CS27_East,
    PCS_NAD27_Florida_North,		Proj_Florida_CS27_North,
    PCS_NAD27_Florida_West,		Proj_Florida_CS27_West,

    PCS_NAD27_Hawaii_zone_1,		Proj_Hawaii_CS27_1,
    PCS_NAD27_Hawaii_zone_2,		Proj_Hawaii_CS27_2,
    PCS_NAD27_Hawaii_zone_3,		Proj_Hawaii_CS27_3,
    PCS_NAD27_Hawaii_zone_4,		Proj_Hawaii_CS27_4,
    PCS_NAD27_Hawaii_zone_5,		Proj_Hawaii_CS27_5,

    PCS_NAD27_Georgia_East,		Proj_Georgia_CS27_East,
    PCS_NAD27_Georgia_West,		Proj_Georgia_CS27_West,

    PCS_NAD27_Idaho_East,		Proj_Idaho_CS27_East,
    PCS_NAD27_Idaho_Central,		Proj_Idaho_CS27_Central,
    PCS_NAD27_Idaho_West,		Proj_Idaho_CS27_West,

    PCS_NAD27_Illinois_East,		Proj_Illinois_CS27_East,
    PCS_NAD27_Illinois_West,		Proj_Illinois_CS27_West,

    PCS_NAD27_Indiana_East,		Proj_Indiana_CS27_East,
    PCS_NAD27_Indiana_West,		Proj_Indiana_CS27_West,

    PCS_NAD27_Iowa_North,      		Proj_Iowa_CS27_North,
    PCS_NAD27_Iowa_South,      		Proj_Iowa_CS27_South,

    PCS_NAD27_Kansas_North,		Proj_Kansas_CS27_North,
    PCS_NAD27_Kansas_South,		Proj_Kansas_CS27_South,

    PCS_NAD27_Kentucky_North,		Proj_Kentucky_CS27_North,
    PCS_NAD27_Kentucky_South,		Proj_Kentucky_CS27_South,

    PCS_NAD27_Louisiana_North,		Proj_Louisiana_CS27_North,
    PCS_NAD27_Louisiana_South,		Proj_Louisiana_CS27_South,

    PCS_NAD27_Maine_East,		Proj_Maine_CS27_East,
    PCS_NAD27_Maine_West,		Proj_Maine_CS27_West,

    PCS_NAD27_Maryland,			Proj_Maryland_CS27,

    PCS_NAD27_Massachusetts,		Proj_Massachusetts_CS27_Mainland,
    PCS_NAD27_Massachusetts_Is,		Proj_Massachusetts_CS27_Island,

    PCS_NAD27_Michigan_North,		Proj_Michigan_CS27_North,
    PCS_NAD27_Michigan_Central,		Proj_Michigan_CS27_Central,
    PCS_NAD27_Michigan_South,		Proj_Michigan_CS27_South,

    PCS_NAD27_Minnesota_North,		Proj_Minnesota_CS27_North,
    PCS_NAD27_Minnesota_Cent,		Proj_Minnesota_CS27_Central,
    PCS_NAD27_Minnesota_South,		Proj_Minnesota_CS27_South,

    PCS_NAD27_Mississippi_East,		Proj_Mississippi_CS27_East,
    PCS_NAD27_Mississippi_West,		Proj_Mississippi_CS27_West,

    PCS_NAD27_Missouri_East,		Proj_Missouri_CS27_East,
    PCS_NAD27_Missouri_Central,		Proj_Missouri_CS27_Central,
    PCS_NAD27_Missouri_West,		Proj_Missouri_CS27_West,

    PCS_NAD27_Montana_North,		Proj_Montana_CS27_North,
    PCS_NAD27_Montana_Central,		Proj_Montana_CS27_Central,
    PCS_NAD27_Montana_South,		Proj_Montana_CS27_South,

    PCS_NAD27_Nebraska_North,		Proj_Nebraska_CS27_North,
    PCS_NAD27_Nebraska_South,		Proj_Nebraska_CS27_South,

    PCS_NAD27_Nevada_East,		Proj_Nevada_CS27_East,
    PCS_NAD27_Nevada_Central,		Proj_Nevada_CS27_Central,
    PCS_NAD27_Nevada_West,		Proj_Nevada_CS27_West,

    PCS_NAD27_New_Hampshire,		Proj_New_Hampshire_CS27,

    PCS_NAD27_New_Jersey,		Proj_New_Jersey_CS27,

    PCS_NAD27_New_Mexico_East,		Proj_New_Mexico_CS27_East,
    PCS_NAD27_New_Mexico_Cent,		Proj_New_Mexico_CS27_Central,
    PCS_NAD27_New_Mexico_West,		Proj_New_Mexico_CS27_West,

    PCS_NAD27_New_York_East,		Proj_New_York_CS27_East,
    PCS_NAD27_New_York_Central,		Proj_New_York_CS27_Central,
    PCS_NAD27_New_York_West,		Proj_New_York_CS27_West,
    PCS_NAD27_New_York_Long_Is,		Proj_New_York_CS27_Long_Island,

    PCS_NAD27_North_Carolina,	       	Proj_North_Carolina_CS27,

    PCS_NAD27_North_Dakota_N,		Proj_North_Dakota_CS27_North,
    PCS_NAD27_North_Dakota_S,		Proj_North_Dakota_CS27_South,

    PCS_NAD27_Ohio_North,		Proj_Ohio_CS27_North,
    PCS_NAD27_Ohio_South,		Proj_Ohio_CS27_South,

    PCS_NAD27_Oklahoma_North,		Proj_Oklahoma_CS27_North,
    PCS_NAD27_Oklahoma_South,		Proj_Oklahoma_CS27_South,

    PCS_NAD27_Oregon_North,		Proj_Oregon_CS27_North,
    PCS_NAD27_Oregon_South,		Proj_Oregon_CS27_South,

    PCS_NAD27_Pennsylvania_N,		Proj_Pennsylvania_CS27_North,
    PCS_NAD27_Pennsylvania_S,		Proj_Pennsylvania_CS27_South,

    PCS_NAD27_Rhode_Island,		Proj_Rhode_Island_CS27,

    PCS_NAD27_South_Carolina_N,		Proj_South_Carolina_CS27_North,
    PCS_NAD27_South_Carolina_S,		Proj_South_Carolina_CS27_South,

    PCS_NAD27_South_Dakota_N,		Proj_South_Dakota_CS27_North,
    PCS_NAD27_South_Dakota_S,		Proj_South_Dakota_CS27_South,

    PCS_NAD27_Tennessee,		Proj_Tennessee_CS27,

    PCS_NAD27_Texas_North,		Proj_Texas_CS27_North,
    PCS_NAD27_Texas_North_Cen,		Proj_Texas_CS27_North_Central,
    PCS_NAD27_Texas_Central,		Proj_Texas_CS27_Central,
    PCS_NAD27_Texas_South_Cen,		Proj_Texas_CS27_South_Central,
    PCS_NAD27_Texas_South,		Proj_Texas_CS27_South,

    PCS_NAD27_Utah_North,		Proj_Utah_CS27_North,
    PCS_NAD27_Utah_Central,		Proj_Utah_CS27_Central,
    PCS_NAD27_Utah_South,		Proj_Utah_CS27_South,

    PCS_NAD27_Vermont,			Proj_Vermont_CS27,

    PCS_NAD27_Virginia_North,		Proj_Virginia_CS27_North,
    PCS_NAD27_Virginia_South,		Proj_Virginia_CS27_South,

    PCS_NAD27_Washington_North,		Proj_Washington_CS27_North,
    PCS_NAD27_Washington_South,		Proj_Washington_CS27_South,

    PCS_NAD27_West_Virginia_N,		Proj_West_Virginia_CS27_North,
    PCS_NAD27_West_Virginia_S,		Proj_West_Virginia_CS27_South,

    PCS_NAD27_Wisconsin_North,		Proj_Wisconsin_CS27_North,
    PCS_NAD27_Wisconsin_Cen,		Proj_Wisconsin_CS27_Central,
    PCS_NAD27_Wisconsin_South,		Proj_Wisconsin_CS27_South,

    PCS_NAD27_Wyoming_East,		Proj_Wyoming_CS27_East,
    PCS_NAD27_Wyoming_E_Cen,		Proj_Wyoming_CS27_East_Central,
    PCS_NAD27_Wyoming_W_Cen,		Proj_Wyoming_CS27_West_Central,
    PCS_NAD27_Wyoming_West,		Proj_Wyoming_CS27_West,
    
    PCS_NAD27_Puerto_Rico,		Proj_Puerto_Rico_CS27,

    KvUserDefined
};

/************************************************************************/
/*                          GTIFMapSysToPCS()                           */
/*                                                                      */
/*      Given a Datum, MapSys and zone value generate the best PCS      */
/*      code possible.                                                  */
/************************************************************************/

int	GTIFMapSysToPCS( int MapSys, int Datum, int nZone )

{
    int		PCSCode = KvUserDefined;

    if( MapSys == MapSys_UTM_North )
    {
	if( Datum == GCS_NAD27 )
	    PCSCode = PCS_NAD27_UTM_zone_3N + nZone - 3;
	else if( Datum == GCS_NAD83 )
	    PCSCode = PCS_NAD83_UTM_zone_3N + nZone - 3;
	else if( Datum == GCS_WGS_72 )
	    PCSCode = PCS_WGS72_UTM_zone_1N + nZone - 1;
	else if( Datum == GCS_WGS_72BE )
	    PCSCode = PCS_WGS72BE_UTM_zone_1N + nZone - 1;
	else if( Datum == GCS_WGS_84 )
	    PCSCode = PCS_WGS84_UTM_zone_1N + nZone - 1;
    }
    else if( MapSys == MapSys_UTM_South )
    {
	if( Datum == GCS_WGS_72 )
	    PCSCode = PCS_WGS72_UTM_zone_1S + nZone - 1;
	else if( Datum == GCS_WGS_72BE )
	    PCSCode = PCS_WGS72BE_UTM_zone_1S + nZone - 1;
	else if( Datum == GCS_WGS_84 )
	    PCSCode = PCS_WGS84_UTM_zone_1S + nZone - 1;
    }
    else if( MapSys == MapSys_State_Plane )
    {
	int		i;

	if( Datum == GCS_NAD27 )
	    PCSCode = 10000 + nZone;
	else if( Datum == GCS_NAD83 )
	    PCSCode = 10000 + nZone + 30;

	for( i = 0; StatePlaneTable[i] != KvUserDefined; i += 2 )
	{
	    if( StatePlaneTable[i+1] == PCSCode )
	        PCSCode = StatePlaneTable[i];
	}
    }

    return( PCSCode );
}

/************************************************************************/
/*                          GTIFPCSToMapSys()                           */
/*                                                                      */
/*      Trnanslate a PCS code into a Map system code (eg                */
/*      MapSys_UTM_North, MapSys_UTM_South, MapSys_State_Plane), Datum	*/
/*	(GCS_*) and a zone.                                             */
/************************************************************************/

int GTIFPCSToMapSys( int PCSCode, int * pDatum, int * pZone )

{
    int		Datum = KvUserDefined, Proj = KvUserDefined;
    int		nZone = KvUserDefined, i;

/* -------------------------------------------------------------------- */
/*      UTM with various datums.  Note there are lots of PCS UTM        */
/*      codes not done yet which use strange datums.                    */
/* -------------------------------------------------------------------- */
    if( PCSCode >= PCS_NAD27_UTM_zone_3N && PCSCode <= PCS_NAD27_UTM_zone_22N )
    {
	Datum = GCS_NAD27;
	Proj = MapSys_UTM_North;
	nZone = PCSCode - PCS_NAD27_UTM_zone_3N + 3;
    }
    else if( PCSCode >= PCS_NAD83_UTM_zone_3N 
	     && PCSCode <= PCS_NAD83_UTM_zone_23N )
    {
	Datum = GCS_NAD83;
	Proj = MapSys_UTM_North;
	nZone = PCSCode - PCS_NAD83_UTM_zone_3N + 3;
    }

    else if( PCSCode >= PCS_WGS72_UTM_zone_1N
	     && PCSCode <= PCS_WGS72_UTM_zone_60N )
    {
	Datum = GCS_WGS_72;
	Proj = MapSys_UTM_North;
	nZone = PCSCode - PCS_WGS72_UTM_zone_1N + 1;
    }
    else if( PCSCode >= PCS_WGS72_UTM_zone_1S
	     && PCSCode <= PCS_WGS72_UTM_zone_60S )
    {
	Datum = GCS_WGS_72;
	Proj = MapSys_UTM_South;
	nZone = PCSCode - PCS_WGS72_UTM_zone_1S + 1;
    }

    else if( PCSCode >= PCS_WGS72BE_UTM_zone_1N
	     && PCSCode <= PCS_WGS72BE_UTM_zone_60N )
    {
	Datum = GCS_WGS_72BE;
	Proj = MapSys_UTM_North;
	nZone = PCSCode - PCS_WGS72BE_UTM_zone_1N + 1;
    }
    else if( PCSCode >= PCS_WGS72BE_UTM_zone_1S
	     && PCSCode <= PCS_WGS72BE_UTM_zone_60S )
    {
	Datum = GCS_WGS_72BE;
	Proj = MapSys_UTM_South;
	nZone = PCSCode - PCS_WGS72BE_UTM_zone_1S + 1;
    }

    else if( PCSCode >= PCS_WGS84_UTM_zone_1N
	     && PCSCode <= PCS_WGS84_UTM_zone_60N )
    {
	Datum = GCS_WGS_84;
	Proj = MapSys_UTM_North;
	nZone = PCSCode - PCS_WGS84_UTM_zone_1N + 1;
    }
    else if( PCSCode >= PCS_WGS84_UTM_zone_1S
	     && PCSCode <= PCS_WGS84_UTM_zone_60S )
    {
	Datum = GCS_WGS_84;
	Proj = MapSys_UTM_South;
	nZone = PCSCode - PCS_WGS84_UTM_zone_1S + 1;
    }
    else if( PCSCode >= PCS_SAD69_UTM_zone_18N 
	     && PCSCode <= PCS_SAD69_UTM_zone_22N )
    {
	Datum = KvUserDefined;
	Proj = MapSys_UTM_North;
	nZone = PCSCode - PCS_SAD69_UTM_zone_18N + 18;
    }
    else if( PCSCode >= PCS_SAD69_UTM_zone_17S
	     && PCSCode <= PCS_SAD69_UTM_zone_25S )
    {
	Datum = KvUserDefined;
	Proj = MapSys_UTM_South;
	nZone = PCSCode - PCS_SAD69_UTM_zone_17S + 17;
    }

/* -------------------------------------------------------------------- */
/*      State Plane zones, first we translate any PCS_ codes to		*/
/*	a Proj_ code that we can get a handle on.			*/
/* -------------------------------------------------------------------- */
    for( i = 0; StatePlaneTable[i] != KvUserDefined; i += 2 )
    {
	if( StatePlaneTable[i] == PCSCode )
	    PCSCode = StatePlaneTable[i+1];
    }

    if( PCSCode <= 15900 && PCSCode >= 10000 )
    {
	Proj = MapSys_State_Plane;

	if( (PCSCode % 100) >= 30 )
	    Datum = GCS_NAD83;
	else
	    Datum = GCS_NAD27;
	
	nZone = PCSCode - 10000;
	if( Datum == GCS_NAD83 )
	    nZone -= 30;
    }

    if( pDatum != NULL )
        *pDatum = Datum;

    if( pZone != NULL )
        *pZone = nZone;

    return( Proj );
}

