"Fossies" - the Fresh Open Source Software Archive

Member "libgeotiff-1.6.0/geo_names.c" (15 Jun 2019, 9092 Bytes) of package /linux/privat/libgeotiff-1.6.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "geo_names.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.5.1_vs_1.6.0.

    1 /*
    2  * geo_names.c
    3  *
    4  *  This encapsulates all of the value-naming mechanism of
    5  *  libgeotiff.
    6  *
    7  *  Written By: Niles Ritter
    8  *
    9  *  copyright (c) 1995   Niles D. Ritter
   10  *
   11  *  Permission granted to use this software, so long as this copyright
   12  *  notice accompanies any products derived therefrom.
   13  *
   14  */
   15 
   16 #include "geotiffio.h"
   17 #include "geonames.h"
   18 #include "geo_tiffp.h" /* for tag names */
   19 #include "geo_keyp.h"
   20 
   21 #include "proj.h"
   22 
   23 static const KeyInfo _formatInfo[] =  {
   24    {TYPE_BYTE,    "Byte"},
   25    {TYPE_SHORT,   "Short"},
   26    {TYPE_LONG,    "Long"},
   27    {TYPE_RATIONAL,"Rational"},
   28    {TYPE_ASCII,   "Ascii"},
   29    {TYPE_FLOAT,   "Float"},
   30    {TYPE_DOUBLE,  "Double"},
   31    {TYPE_SBYTE,   "SignedByte"},
   32    {TYPE_SSHORT,  "SignedShort"},
   33    {TYPE_SLONG,  "SignedLong"},
   34    {TYPE_UNKNOWN, "Unknown"},
   35     END_LIST
   36 };
   37 
   38 static const KeyInfo _tagInfo[] =  {
   39     {GTIFF_PIXELSCALE,  "ModelPixelScaleTag"},
   40     {GTIFF_TRANSMATRIX, "ModelTransformationTag"},
   41     {GTIFF_TIEPOINTS,   "ModelTiepointTag"},
   42      /* This alias maps the Intergraph symbol to the current tag */
   43     {GTIFF_TRANSMATRIX, "IntergraphMatrixTag"},
   44     END_LIST
   45 };
   46 
   47 static const char *FindName(const KeyInfo *info,int key)
   48 {
   49    static char errmsg[80];
   50 
   51    while (info->ki_key>=0 && info->ki_key != key) info++;
   52 
   53    if (info->ki_key<0)
   54    {
   55        sprintf(errmsg,"Unknown-%d", key );
   56        return errmsg;
   57    }
   58    return info->ki_name;
   59 }
   60 
   61 char *GTIFKeyName(geokey_t key)
   62 {
   63    return (char*) FindName( &_keyInfo[0],key);
   64 }
   65 
   66 const char* GTIFKeyNameEx(GTIF* gtif, geokey_t key)
   67 {
   68     const KeyInfo *info;
   69     if( gtif->gt_version == GEOTIFF_SPEC_1_0_VERSION &&
   70         gtif->gt_rev_major == GEOTIFF_SPEC_1_0_KEY_REVISION &&
   71         gtif->gt_rev_minor == GEOTIFF_SPEC_1_0_MINOR_REVISION )
   72     {
   73         info = &_keyInfo[0];
   74     }
   75     else
   76     {
   77         info = &_keyInfoV11[0];
   78     }
   79     while (info->ki_key>=0 && info->ki_key != (int)key) info++;
   80     if (info->ki_key<0)
   81     {
   82         sprintf(gtif->szTmpBufferForGTIFValueNameEx,"Unknown-%d", key );
   83         return gtif->szTmpBufferForGTIFValueNameEx;
   84     }
   85     return info->ki_name;
   86 }
   87 
   88 char *GTIFTypeName(tagtype_t type)
   89 {
   90    return (char*) FindName( &_formatInfo[0],type);
   91 }
   92 
   93 char *GTIFTagName(int tag)
   94 {
   95    return (char*) FindName( &_tagInfo[0],tag);
   96 }
   97 
   98 static const KeyInfo* FindTable(geokey_t key)
   99 {
  100    const KeyInfo *info;
  101 
  102    switch (key)
  103    {
  104     /* All codes using linear/angular/whatever units */
  105     case GeogLinearUnitsGeoKey:
  106     case ProjLinearUnitsGeoKey:
  107     case GeogAngularUnitsGeoKey:
  108     case GeogAzimuthUnitsGeoKey:
  109     case VerticalUnitsGeoKey:
  110                               info=_geounitsValue; break;
  111 
  112     /* put other key-dependent lists here */
  113     case GTModelTypeGeoKey:       info=_modeltypeValue; break;
  114     case GTRasterTypeGeoKey:      info=_rastertypeValue; break;
  115     case GeographicTypeGeoKey:    info=_geographicValue; break;
  116     case GeogGeodeticDatumGeoKey: info=_geodeticdatumValue; break;
  117     case GeogEllipsoidGeoKey:     info=_ellipsoidValue; break;
  118     case GeogPrimeMeridianGeoKey: info=_primemeridianValue; break;
  119     case ProjectedCSTypeGeoKey:   info=_pcstypeValue; break;
  120     case ProjectionGeoKey:        info=_projectionValue; break;
  121     case ProjCoordTransGeoKey:    info=_coordtransValue; break;
  122     case VerticalCSTypeGeoKey:    info=_vertcstypeValue; break;
  123     case VerticalDatumGeoKey:     info=_vdatumValue; break;
  124 
  125     /* And if all else fails... */
  126     default:                      info = _csdefaultValue;break;
  127    }
  128 
  129    return info;
  130 }
  131 
  132 char *GTIFValueName(geokey_t key, int value)
  133 {
  134 
  135    return (char*) FindName(FindTable(key), value);
  136 }
  137 
  138 static void GetNameFromDatabase(GTIF* gtif,
  139                                 const char* pszCode,
  140                                 PJ_CATEGORY category,
  141                                 char* pszOut,
  142                                 size_t nOutSize)
  143 {
  144     PJ* obj = proj_create_from_database(
  145         gtif->pj_context, "EPSG", pszCode, category,
  146         FALSE, NULL);
  147     if( obj )
  148     {
  149         const char* pszName = proj_get_name(obj);
  150         if( pszName )
  151         {
  152             size_t nToCopy = MIN(strlen(pszName), nOutSize - 1);
  153             memcpy(pszOut, pszName, nToCopy);
  154             pszOut[nToCopy] = 0;
  155         }
  156         proj_destroy(obj);
  157     }
  158     else
  159     {
  160         pszOut[0] = 0;
  161     }
  162 }
  163 
  164 const char *GTIFValueNameEx(GTIF* gtif, geokey_t key, int value)
  165 {
  166     const KeyInfo *info = FindTable(key);
  167     int useHardcodedTables = 0;
  168 
  169     if( value == KvUndefined || value == KvUserDefined )
  170     {
  171         useHardcodedTables = 1;
  172     }
  173     else if( gtif->gt_version == GEOTIFF_SPEC_1_0_VERSION &&
  174              gtif->gt_rev_major == GEOTIFF_SPEC_1_0_KEY_REVISION &&
  175              gtif->gt_rev_minor == GEOTIFF_SPEC_1_0_MINOR_REVISION )
  176     {
  177         useHardcodedTables = 1;
  178     }
  179     else if( key == GTModelTypeGeoKey ||
  180              key == GTRasterTypeGeoKey ||
  181              key == ProjCoordTransGeoKey )
  182     {
  183         useHardcodedTables = 1;
  184     }
  185     else if( key == VerticalCSTypeGeoKey &&
  186              value >= 5001 && value <= 5033 )
  187     {
  188         useHardcodedTables = 1;
  189     }
  190     if( useHardcodedTables )
  191     {
  192         while (info->ki_key>=0 && info->ki_key != value) info++;
  193     }
  194 
  195     if ( !useHardcodedTables || info->ki_key<0 )
  196     {
  197         sprintf(gtif->szTmpBufferForGTIFValueNameEx,"Unknown-%d", value );
  198 
  199         if( gtif->pj_context == NULL )
  200         {
  201             gtif->pj_context = proj_context_create();
  202             if( gtif->pj_context )
  203             {
  204                 gtif->own_pj_context = TRUE;
  205             }
  206         }
  207         if( gtif->pj_context )
  208         {
  209             char szCode[12];
  210             char szName[120];
  211 
  212             szName[0] = 0;
  213             sprintf(szCode, "%d", value);
  214 
  215             switch (key)
  216             {
  217                 /* All codes using linear/angular/whatever units */
  218                 case GeogLinearUnitsGeoKey:
  219                 case ProjLinearUnitsGeoKey:
  220                 case GeogAngularUnitsGeoKey:
  221                 case GeogAzimuthUnitsGeoKey:
  222                 case VerticalUnitsGeoKey:
  223                 {
  224                     const char* pszName = NULL;
  225                     if( proj_uom_get_info_from_database(gtif->pj_context,
  226                          "EPSG", szCode, &pszName, NULL, NULL) && pszName )
  227                     {
  228                         strncpy(szName, pszName, sizeof(szName));
  229                         szName[sizeof(szName)-1] = 0;
  230                     }
  231                     break;
  232                 }
  233 
  234                 case GeogGeodeticDatumGeoKey:
  235                 case VerticalDatumGeoKey:
  236                     GetNameFromDatabase(gtif, szCode, PJ_CATEGORY_DATUM,
  237                                         szName, sizeof(szName));
  238                     break;
  239 
  240                 case GeogEllipsoidGeoKey:
  241                     GetNameFromDatabase(gtif, szCode, PJ_CATEGORY_ELLIPSOID,
  242                                         szName, sizeof(szName));
  243                     break;
  244 
  245                 case GeogPrimeMeridianGeoKey:
  246                     GetNameFromDatabase(gtif, szCode,
  247                                         PJ_CATEGORY_PRIME_MERIDIAN,
  248                                         szName, sizeof(szName));
  249                     break;
  250 
  251                 case GeographicTypeGeoKey:
  252                 case ProjectedCSTypeGeoKey:
  253                 case VerticalCSTypeGeoKey:
  254                     GetNameFromDatabase(gtif, szCode,
  255                                         PJ_CATEGORY_CRS,
  256                                         szName, sizeof(szName));
  257                     break;
  258 
  259                 case ProjectionGeoKey:
  260                     GetNameFromDatabase(gtif, szCode,
  261                                         PJ_CATEGORY_COORDINATE_OPERATION,
  262                                         szName, sizeof(szName));
  263                     break;
  264 
  265                 default:
  266                     break;
  267             }
  268 
  269             if( szName[0] != 0 )
  270             {
  271                 sprintf(gtif->szTmpBufferForGTIFValueNameEx,
  272                         "Code-%d (%s)", value, szName );
  273             }
  274 
  275         }
  276 
  277         return gtif->szTmpBufferForGTIFValueNameEx;
  278     }
  279     return info->ki_name;
  280 }
  281 
  282 /*
  283  * Inverse Utilities (name->code)
  284  */
  285 
  286 
  287 static int FindCode(const KeyInfo *info,const char *key)
  288 {
  289    while (info->ki_key>=0 && strcmp(info->ki_name,key) ) info++;
  290 
  291    if (info->ki_key<0)
  292    {
  293     /* not a registered key; might be generic code */
  294     if (!strncmp(key,"Unknown-",8))
  295     {
  296         int code=-1;
  297         sscanf(key,"Unknown-%d",&code);
  298         return code;
  299     } else if (!strncmp(key,"Code-",5))
  300     {
  301         int code=-1;
  302         sscanf(key,"Code-%d",&code);
  303         return code;
  304     }
  305     else return -1;
  306    }
  307    return info->ki_key;
  308 }
  309 
  310 int GTIFKeyCode(const char *key)
  311 {
  312    int ret = FindCode( &_keyInfo[0],key);
  313    if( ret < 0 )
  314        ret = FindCode( &_keyInfoV11[0],key);
  315    return ret;
  316 }
  317 
  318 int GTIFTypeCode(const char *type)
  319 {
  320    return FindCode( &_formatInfo[0],type);
  321 }
  322 
  323 int GTIFTagCode(const char *tag)
  324 {
  325    return FindCode( &_tagInfo[0],tag);
  326 }
  327 
  328 
  329 /*
  330  *  The key must be determined with GTIFKeyCode() before
  331  *  the name can be encoded.
  332  */
  333 int GTIFValueCode(geokey_t key, const char *name)
  334 {
  335    return FindCode(FindTable(key),name);
  336 }