I have written a small utility app that I think would make a nice
addition to the GDAL Utilities.

gdalsrsinfo (provisional name) processes an input SRS definition (or
dataset) and outputs the SRS definition in one or all of the formats
which GDAL can export to (WKT, PROJ.4, ESRI WKT, mapserver, xml).

It accepts a valid GDAL dataset or any of the usual GDAL/OGR forms. It
could be extended to accept OGR datasets (already supports .prj
files).

In my opinion it is useful because it allows easy access to the
different SRS forms supported by GDAL and conversions between those
forms.

> gdalsrsinfo -h

Usage: gdalsrsinfo [options] srs_def

srs_def may be the filename of a dataset from which to extract SRS information
OR any of the usual GDAL/OGR forms
(complete WKT, PROJ.4, EPSG:n or a file containing the SRS)

Options:
   [--help-general] [-h]  Show help and exit
   [-p]                         Pretty-print where applicable (e.g. WKT)
   [-s]                         Be as silent as possible
   [-o out_type]           Output type {all,proj4,wkt,esri,mapinfo,xml}


Please give me your opinion on this, and if I should go ahead and
commit it to svn trunk.  Also what name it should have.

Attaching source code and modified GNUMakefile to build. It is written
in c to make it easily available to all GDAL installations.

Regards, Etienne

Posting some examples:

>  ./gdalsrsinfo  -o all  landsat.tif

PROJ.4 : '+proj=utm +zone=19 +south +datum=WGS84 +units=m +no_defs '

OGC WKT :
PROJCS["WGS 84 / UTM zone 19S",GEOGCS["WGS
84",DATUM["WGS_1984",SPHEROID["WGS
84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-69],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",10000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","32719"]]

ESRI WKT :
PROJCS["WGS_1984_UTM_Zone_19S",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-69],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",10000000],UNIT["Meter",1]]

MAPINFO : 'Earth Projection 8, 104, "m", -69, 0, 0.9996, 500000, 10000000'


> ./gdalsrsinfo  -o proj4 "EPSG:32722"

PROJ.4 : '+proj=utm +zone=22 +south +datum=WGS84 +units=m +no_defs '

> ./gdalsrsinfo  -o wkt -s  "EPSG:32722"

PROJCS["WGS 84 / UTM zone 22S",GEOGCS["WGS
84",DATUM["WGS_1984",SPHEROID["WGS
84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-51],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",10000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32722"]]


> ./gdalsrsinfo  -o wkt -p "EPSG:4326"

OGC WKT :
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]
/******************************************************************************
 * $Id$
 *
 * Project:  GDAL Utilities
 * Purpose:  Commandline application to list info about a file.
 * Author:   Frank Warmerdam, [email protected]
 *
 * ****************************************************************************
 * Copyright (c) 1998, Frank Warmerdam
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

#include "gdal.h"
//#include "gdal_alg.h"
#include "ogr_srs_api.h"
#include "cpl_string.h"
#include "cpl_conv.h"
#include "cpl_multiproc.h"

CPL_CVSID("$Id$");

/************************************************************************/
/*                               Usage()                                */
/************************************************************************/

void Usage()

{
    /* printf( "Usage: gdalsrsinfo [options] srs_def[--help-general] [-h]\n" */
    /*         "       [-p] [-s] [-o {all,proj4,wkt,esri,mapinfo,xml}]\n"  */
    /*         "       srs_def\n"); */
    printf( "\nUsage: gdalsrsinfo [options] srs_def\n"
            "\n"
            "srs_def may be the filename of a dataset from which to extract SRS information\n"
            "OR any of the usual GDAL/OGR forms\n"
            "(complete WKT, PROJ.4, EPSG:n or a file containing the SRS)\n"
            "\n"          
            "Options: \n"
            "   [--help-general] [-h]  Show help and exit\n"
            "   [-p]                   Pretty-print where applicable (e.g. WKT)\n"
            "   [-s]                   Be as silent as possible\n"
            "   [-o out_type]          Output type {all,proj4,wkt,esri,mapinfo,xml}\n\n" ); 

    exit( 1 );
}

/************************************************************************/
/*                                main()                                */
/************************************************************************/

int main( int argc, char ** argv ) 

{
    int i;
    int bGotSRS = FALSE;
    int bPretty = FALSE;
    int bOutputAll = FALSE;
    int bSilent = FALSE;

    char  *pszOutput = NULL;
    const char  *pszOutputType = "wkt";
    const char  *pszInput = NULL;

    OGRSpatialReferenceH hSRS = OSRNewSpatialReference( NULL );

    /* Check that we are running against at least GDAL 1.5 */
    /* Note to developers : if we use newer API, please change the requirement */
    if (atoi(GDALVersionInfo("VERSION_NUM")) < 1500)
    {
        fprintf(stderr, "At least, GDAL >= 1.5.0 is required for this version of %s, "
                "which was compiled against GDAL %s\n", argv[0], GDAL_RELEASE_NAME);
        exit(1);
    }

    /* Must process GDAL_SKIP before GDALAllRegister(), but we can't call */
    /* GDALGeneralCmdLineProcessor before it needs the drivers to be registered */
    /* for the --format or --formats options */
    for( i = 1; i < argc; i++ )
    {
        if( EQUAL(argv[i],"--config") && i + 2 < argc && EQUAL(argv[i + 1], "GDAL_SKIP") )
        {
            CPLSetConfigOption( argv[i+1], argv[i+2] );

            i += 2;
        }
    }

    GDALAllRegister();

    argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 );
    if( argc < 1 )
        exit( -argc );

/* -------------------------------------------------------------------- */
/*      Parse arguments.                                                */
/* -------------------------------------------------------------------- */
    for( i = 1; i < argc; i++ )
    {
        if( EQUAL(argv[i], "--utility_version") )
        {
            printf("%s was compiled against GDAL %s and is running against GDAL %s\n",
                   argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
            return 0;
        }
        else if( EQUAL(argv[i], "-h") )
            Usage();
        else if( EQUAL(argv[i], "-o") )
            pszOutputType =  argv[++i];
        else if( EQUAL(argv[i], "-p") )
            bPretty = TRUE;
        else if( EQUAL(argv[i], "-s") )
            bSilent = TRUE;
        else if( ! bGotSRS ) {

            int bGotSRSDef = FALSE;
            FILE *file;
            GDALDatasetH	hDataset;
            char  *pszProjection = NULL;

            pszInput = argv[i];
       
            /* If argument is a file, try to open it with GDALOpen() and get the projection */
            file = fopen(pszInput, "r");
            if ( file )  {
                fclose(file);
                /* supress error messages temporarily */
                CPLSetErrorHandler ( CPLQuietErrorHandler );	
                hDataset = GDALOpen( pszInput, GA_ReadOnly );
                if ( hDataset != NULL && GDALGetProjectionRef( hDataset ) != NULL ) {
                    pszProjection = (char *) GDALGetProjectionRef( hDataset ); 
                    if( OSRImportFromWkt( hSRS, &pszProjection ) == CE_None )
                        bGotSRSDef = TRUE;
                    GDALClose( hDataset );
                }
                GDALDestroyDriverManager();
                CPLSetErrorHandler ( NULL );	
            }

            /* If didn't get the projection from the file, try OSRSetFromUserInput() */
            if ( ! bGotSRSDef ) {
                if( OSRSetFromUserInput( hSRS, pszInput ) != OGRERR_NONE ) {
                    fprintf( stderr, "Failed to process SRS definition: %s\n",
                             pszInput );
                    exit( 1 );
                }
            }
        }
        else
            Usage();
    }

    if ( EQUAL("all", pszOutputType ) )
        bOutputAll = TRUE;

    printf("\n");

    if ( bOutputAll || EQUAL("proj4", pszOutputType ) ) {
        if ( ! bSilent) printf("PROJ.4 : ");
        OSRExportToProj4( hSRS, &pszOutput );
        printf("\'%s\'\n\n",pszOutput);
    }

    if ( bOutputAll || EQUAL("wkt", pszOutputType ) ) {
        if ( ! bSilent) printf("OGC WKT :\n");
        if ( bPretty ) 
            OSRExportToPrettyWkt( hSRS, &pszOutput, FALSE );
        else
            OSRExportToWkt( hSRS, &pszOutput );
        printf("%s\n\n",pszOutput);
    }

    if ( bOutputAll || EQUAL("esri", pszOutputType ) ) {
        if ( ! bSilent) printf("ESRI WKT :\n");
        OSRMorphToESRI( hSRS );
        if ( bPretty ) 
            OSRExportToPrettyWkt( hSRS, &pszOutput, FALSE );
        else
            OSRExportToWkt( hSRS, &pszOutput );
        printf("%s\n\n",pszOutput);
    }

    if ( bOutputAll || EQUAL("mapinfo", pszOutputType ) ) {
        if ( ! bSilent) printf("MAPINFO : ");
        OSRExportToMICoordSys( hSRS, &pszOutput );
        printf("\'%s\'\n\n",pszOutput);
    }

    /* if ( bOutputAll || EQUAL("xml", pszOutputType ) ) { */
    if ( EQUAL("xml", pszOutputType ) ) {
        if ( ! bSilent) printf("XML :\n");
        OSRExportToXML( hSRS, &pszOutput, NULL );
        printf("%s\n\n",pszOutput);
    }


    CSLDestroy( argv );
    OSRDestroySpatialReference( hSRS );

    exit( 0 );

}

Attachment: GNUmakefile
Description: Binary data

_______________________________________________
gdal-dev mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/gdal-dev

Reply via email to