Blame view

Proj4/pj_utils.c 5.82 KB
Joseph G authored
1
/******************************************************************************
2
 * $Id: pj_utils.c 1504 2009-01-06 02:11:57Z warmerdam $
Joseph G authored
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Project:  PROJ.4
 * Purpose:  Some utility functions we don't want to bother putting in
 *           their own source files.
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 ******************************************************************************
 * Copyright (c) 2001, 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.
29
 *****************************************************************************/
Joseph G authored
30 31 32

#define PJ_LIB__
33
#include <projects.h>
Joseph G authored
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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
#include <string.h>
#include <math.h>

/************************************************************************/
/*                           pj_is_latlong()                            */
/*                                                                      */
/*      Returns TRUE if this coordinate system object is                */
/*      geographic.                                                     */
/************************************************************************/

int pj_is_latlong( PJ *pj )

{
    return pj == NULL || pj->is_latlong;
}

/************************************************************************/
/*                           pj_is_geocent()                            */
/*                                                                      */
/*      Returns TRUE if this coordinate system object is geocentric.    */
/************************************************************************/

int pj_is_geocent( PJ *pj )

{
    return pj != NULL && pj->is_geocent;
}

/************************************************************************/
/*                        pj_latlong_from_proj()                        */
/*                                                                      */
/*      Return a PJ* definition defining the lat/long coordinate        */
/*      system on which a projection is based.  If the coordinate       */
/*      system passed in is latlong, a clone of the same will be        */
/*      returned.                                                       */
/************************************************************************/

PJ *pj_latlong_from_proj( PJ *pj_in )

{
    char	defn[512];
    int		got_datum = FALSE;

    pj_errno = 0;
    strcpy( defn, "+proj=latlong" );

    if( pj_param(pj_in->params, "tdatum").i )
    {
        got_datum = TRUE;
        sprintf( defn+strlen(defn), " +datum=%s", 
                 pj_param(pj_in->params,"sdatum").s );
    }
    else if( pj_param(pj_in->params, "tellps").i )
    {
        sprintf( defn+strlen(defn), " +ellps=%s", 
                 pj_param(pj_in->params,"sellps").s );
    }
    else if( pj_param(pj_in->params, "ta").i )
    {
        sprintf( defn+strlen(defn), " +a=%s", 
                 pj_param(pj_in->params,"sa").s );
            
        if( pj_param(pj_in->params, "tb").i )
            sprintf( defn+strlen(defn), " +b=%s", 
                     pj_param(pj_in->params,"sb").s );
        else if( pj_param(pj_in->params, "tes").i )
            sprintf( defn+strlen(defn), " +es=%s", 
                     pj_param(pj_in->params,"ses").s );
        else if( pj_param(pj_in->params, "tf").i )
            sprintf( defn+strlen(defn), " +f=%s", 
                     pj_param(pj_in->params,"sf").s );
        else
            sprintf( defn+strlen(defn), " +es=%.16g", 
                     pj_in->es );
    }
    else
    {
        pj_errno = -13;

        return NULL;
    }

    if( !got_datum )
    {
        if( pj_param(pj_in->params, "ttowgs84").i )
            sprintf( defn+strlen(defn), " +towgs84=%s", 
                     pj_param(pj_in->params,"stowgs84").s );

        if( pj_param(pj_in->params, "tnadgrids").i )
            sprintf( defn+strlen(defn), " +nadgrids=%s", 
                     pj_param(pj_in->params,"snadgrids").s );
    }

    /* copy over some other information related to ellipsoid */
    if( pj_param(pj_in->params, "tR").i )
        sprintf( defn+strlen(defn), " +R=%s", 
                 pj_param(pj_in->params,"sR").s );

    if( pj_param(pj_in->params, "tR_A").i )
        sprintf( defn+strlen(defn), " +R_A" );

    if( pj_param(pj_in->params, "tR_V").i )
        sprintf( defn+strlen(defn), " +R_V" );

    if( pj_param(pj_in->params, "tR_a").i )
        sprintf( defn+strlen(defn), " +R_a" );

    if( pj_param(pj_in->params, "tR_lat_a").i )
        sprintf( defn+strlen(defn), " +R_lat_a=%s", 
                 pj_param(pj_in->params,"sR_lat_a").s );

    if( pj_param(pj_in->params, "tR_lat_g").i )
        sprintf( defn+strlen(defn), " +R_lat_g=%s", 
                 pj_param(pj_in->params,"sR_lat_g").s );

    /* copy over prime meridian */
    if( pj_param(pj_in->params, "tpm").i )
        sprintf( defn+strlen(defn), " +pm=%s", 
                 pj_param(pj_in->params,"spm").s );

    return pj_init_plus( defn );
}