geo_names.c 7.35 KB
Newer Older
1 2 3
/*
 * geo_names.c
 *
4 5
 *  This encapsulates all of the value-naming mechanism of
 *  libgeotiff.
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 *  Written By: Niles Ritter
 *
 *  copyright (c) 1995   Niles D. Ritter
 *
 *  Permission granted to use this software, so long as this copyright
 *  notice accompanies any products derived therefrom.
 *
 */

#include "geotiffio.h"
#include "geonames.h"
#include "geo_tiffp.h" /* for tag names */
19 20 21
#include "geo_keyp.h"

#include "proj.h"
22

23
static const KeyInfo _formatInfo[] =  {
24 25 26 27 28 29 30 31 32 33 34 35 36 37
   {TYPE_BYTE,    "Byte"},
   {TYPE_SHORT,   "Short"},
   {TYPE_LONG,    "Long"},
   {TYPE_RATIONAL,"Rational"},
   {TYPE_ASCII,   "Ascii"},
   {TYPE_FLOAT,   "Float"},
   {TYPE_DOUBLE,  "Double"},
   {TYPE_SBYTE,   "SignedByte"},
   {TYPE_SSHORT,  "SignedShort"},
   {TYPE_SLONG,  "SignedLong"},
   {TYPE_UNKNOWN, "Unknown"},
    END_LIST
};

38
static const KeyInfo _tagInfo[] =  {
39 40 41 42 43 44 45 46
    {GTIFF_PIXELSCALE,  "ModelPixelScaleTag"},
    {GTIFF_TRANSMATRIX, "ModelTransformationTag"},
    {GTIFF_TIEPOINTS,   "ModelTiepointTag"},
     /* This alias maps the Intergraph symbol to the current tag */
    {GTIFF_TRANSMATRIX, "IntergraphMatrixTag"},
    END_LIST
};

47
static char *FindName(const KeyInfo *info,int key)
48 49
{
   static char errmsg[80];
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
   while (info->ki_key>=0 && info->ki_key != key) info++;

   if (info->ki_key<0)
   {
	   sprintf(errmsg,"Unknown-%d", key );
	   return errmsg;
   }
   return info->ki_name;
}

char *GTIFKeyName(geokey_t key)
{
   return FindName( &_keyInfo[0],key);
}

char *GTIFTypeName(tagtype_t type)
{
   return FindName( &_formatInfo[0],type);
}

char *GTIFTagName(int tag)
{
   return FindName( &_tagInfo[0],tag);
}

76
static const KeyInfo* FindTable(geokey_t key)
77
{
78
   const KeyInfo *info;
79

80 81 82
   switch (key)
   {
	/* All codes using linear/angular/whatever units */
83 84 85 86
	case GeogLinearUnitsGeoKey:
	case ProjLinearUnitsGeoKey:
	case GeogAngularUnitsGeoKey:
	case GeogAzimuthUnitsGeoKey:
87
	case VerticalUnitsGeoKey:
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
		                      info=_geounitsValue; break;

   	/* put other key-dependent lists here */
	case GTModelTypeGeoKey:       info=_modeltypeValue; break;
	case GTRasterTypeGeoKey:      info=_rastertypeValue; break;
	case GeographicTypeGeoKey:    info=_geographicValue; break;
	case GeogGeodeticDatumGeoKey: info=_geodeticdatumValue; break;
	case GeogEllipsoidGeoKey:     info=_ellipsoidValue; break;
	case GeogPrimeMeridianGeoKey: info=_primemeridianValue; break;
	case ProjectedCSTypeGeoKey:   info=_pcstypeValue; break;
	case ProjectionGeoKey:        info=_projectionValue; break;
	case ProjCoordTransGeoKey:    info=_coordtransValue; break;
	case VerticalCSTypeGeoKey:    info=_vertcstypeValue; break;
	case VerticalDatumGeoKey:     info=_vdatumValue; break;

	/* And if all else fails... */
   	default:                      info = _csdefaultValue;break;
   }
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
   return info;
}

char *GTIFValueName(geokey_t key, int value)
{

   return FindName(FindTable(key), value);
}

static void GetNameFromDatabase(GTIF* gtif,
                                const char* pszCode,
                                PJ_CATEGORY category,
                                char* pszOut,
                                size_t nOutSize)
{
    PJ* obj = proj_create_from_database(
        gtif->pj_context, "EPSG", pszCode, category,
        FALSE, NULL);
    if( obj )
    {
        const char* pszName = proj_get_name(obj);
        if( pszName )
        {
            strncpy(pszOut, pszName, nOutSize);
            pszOut[nOutSize-1] = 0;
        }
        proj_destroy(obj);
    }
    else
    {
        pszOut[0] = 0;
    }
}

const char *GTIFValueNameEx(GTIF* gtif, geokey_t key, int value)
{
    const KeyInfo *info = FindTable(key);

    while (info->ki_key>=0 && info->ki_key != value) info++;

    if (info->ki_key<0)
    {
        sprintf(gtif->szTmpBufferForGTIFValueNameEx,"Unknown-%d", value );

        if( gtif->pj_context == NULL )
        {
            gtif->pj_context = proj_context_create();
            if( gtif->pj_context )
            {
                gtif->own_pj_context = TRUE;
            }
        }
        if( gtif->pj_context )
        {
            char szCode[12];
            char szName[120];

            szName[0] = 0;
            sprintf(szCode, "%d", value);

            switch (key)
            {
                /* All codes using linear/angular/whatever units */
                case GeogLinearUnitsGeoKey:
                case ProjLinearUnitsGeoKey:
                case GeogAngularUnitsGeoKey:
                case GeogAzimuthUnitsGeoKey:
                case VerticalUnitsGeoKey:
                {
                    const char* pszName = NULL;
                    if( proj_uom_get_info_from_database(gtif->pj_context,
                         "EPSG", szCode, &pszName, NULL, NULL) && pszName )
                    {
                        strncpy(szName, pszName, sizeof(szName));
                        szName[sizeof(szName)-1] = 0;
                    }
                    break;
                }

                case GeogGeodeticDatumGeoKey:
                case VerticalDatumGeoKey:
                    GetNameFromDatabase(gtif, szCode, PJ_CATEGORY_DATUM,
                                        szName, sizeof(szName));
                    break;

                case GeogEllipsoidGeoKey:
                    GetNameFromDatabase(gtif, szCode, PJ_CATEGORY_ELLIPSOID,
                                        szName, sizeof(szName));
                    break;

                case GeogPrimeMeridianGeoKey:
                    GetNameFromDatabase(gtif, szCode,
                                        PJ_CATEGORY_PRIME_MERIDIAN,
                                        szName, sizeof(szName));
                    break;

                case GeographicTypeGeoKey:
                case ProjectedCSTypeGeoKey:
                case VerticalCSTypeGeoKey:
                    GetNameFromDatabase(gtif, szCode,
                                        PJ_CATEGORY_CRS,
                                        szName, sizeof(szName));
                    break;

                case ProjectionGeoKey:
                    GetNameFromDatabase(gtif, szCode,
                                        PJ_CATEGORY_COORDINATE_OPERATION,
                                        szName, sizeof(szName));
                    break;

                default:
                    break;
            }

            if( szName[0] != 0 )
            {
                sprintf(gtif->szTmpBufferForGTIFValueNameEx,
                        "Code-%d (%s)", value, szName );
            }

        }

        return gtif->szTmpBufferForGTIFValueNameEx;
    }
    return info->ki_name;
232 233
}

234 235
/*
 * Inverse Utilities (name->code)
236 237 238
 */


239
static int FindCode(const KeyInfo *info,char *key)
240 241 242 243 244 245 246 247 248 249 250
{
   while (info->ki_key>=0 && strcmp(info->ki_name,key) ) info++;

   if (info->ki_key<0)
   {
	/* not a registered key; might be generic code */
	if (!strncmp(key,"Unknown-",8))
	{
		int code=-1;
		sscanf(key,"Unknown-%d",&code);
		return code;
251 252 253 254 255
	} else if (!strncmp(key,"Code-",5))
	{
		int code=-1;
		sscanf(key,"Code-%d",&code);
		return code;
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	}
	else return -1;
   }
   return info->ki_key;
}

int GTIFKeyCode(char *key)
{
   return FindCode( &_keyInfo[0],key);
}

int GTIFTypeCode(char *type)
{
   return FindCode( &_formatInfo[0],type);
}

int GTIFTagCode(char *tag)
{
   return FindCode( &_tagInfo[0],tag);
}


/*
 *  The key must be determined with GTIFKeyCode() before
 *  the name can be encoded.
 */
int GTIFValueCode(geokey_t key, char *name)
{
284
   return FindCode(FindTable(key),name);
285
}