PageRenderTime 53ms CodeModel.GetById 39ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Proj4/pj_utils.c

http://github.com/route-me/route-me
C | 175 lines | 80 code | 26 blank | 69 comment | 24 complexity | 7ebfb18201086bc086a83a0071ce088d MD5 | raw file
  1/******************************************************************************
  2 * $Id: pj_utils.c,v 1.5 2007/03/12 14:05:35 fwarmerdam Exp $
  3 *
  4 * Project:  PROJ.4
  5 * Purpose:  Some utility functions we don't want to bother putting in
  6 *           their own source files.
  7 * Author:   Frank Warmerdam, warmerdam@pobox.com
  8 *
  9 ******************************************************************************
 10 * Copyright (c) 2001, Frank Warmerdam
 11 *
 12 * Permission is hereby granted, free of charge, to any person obtaining a
 13 * copy of this software and associated documentation files (the "Software"),
 14 * to deal in the Software without restriction, including without limitation
 15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 16 * and/or sell copies of the Software, and to permit persons to whom the
 17 * Software is furnished to do so, subject to the following conditions:
 18 *
 19 * The above copyright notice and this permission notice shall be included
 20 * in all copies or substantial portions of the Software.
 21 *
 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 28 * DEALINGS IN THE SOFTWARE.
 29 ******************************************************************************
 30 *
 31 * $Log: pj_utils.c,v $
 32 * Revision 1.5  2007/03/12 14:05:35  fwarmerdam
 33 * Removed duplicate towgs84 definition code.
 34 *
 35 * Revision 1.4  2005/07/06 14:04:09  fwarmerdam
 36 * Improved precision of es encoding for pj_latlong_from_proj() per:
 37 *   http://bugzilla.remotesensing.org/show_bug.cgi?id=881
 38 *
 39 * Revision 1.3  2002/12/14 20:14:00  warmerda
 40 * added pj_is_geocent, added +pm support to pj_get_def
 41 *
 42 * Revision 1.2  2002/04/30 17:01:51  warmerda
 43 * Removed printf() statement.
 44 *
 45 * Revision 1.1  2001/04/05 04:22:46  warmerda
 46 * New
 47 *
 48 */
 49
 50#define PJ_LIB__
 51
 52#include "projects.h"
 53#include <string.h>
 54#include <math.h>
 55
 56/************************************************************************/
 57/*                           pj_is_latlong()                            */
 58/*                                                                      */
 59/*      Returns TRUE if this coordinate system object is                */
 60/*      geographic.                                                     */
 61/************************************************************************/
 62
 63int pj_is_latlong( PJ *pj )
 64
 65{
 66    return pj == NULL || pj->is_latlong;
 67}
 68
 69/************************************************************************/
 70/*                           pj_is_geocent()                            */
 71/*                                                                      */
 72/*      Returns TRUE if this coordinate system object is geocentric.    */
 73/************************************************************************/
 74
 75int pj_is_geocent( PJ *pj )
 76
 77{
 78    return pj != NULL && pj->is_geocent;
 79}
 80
 81/************************************************************************/
 82/*                        pj_latlong_from_proj()                        */
 83/*                                                                      */
 84/*      Return a PJ* definition defining the lat/long coordinate        */
 85/*      system on which a projection is based.  If the coordinate       */
 86/*      system passed in is latlong, a clone of the same will be        */
 87/*      returned.                                                       */
 88/************************************************************************/
 89
 90PJ *pj_latlong_from_proj( PJ *pj_in )
 91
 92{
 93    char	defn[512];
 94    int		got_datum = FALSE;
 95
 96    pj_errno = 0;
 97    strcpy( defn, "+proj=latlong" );
 98
 99    if( pj_param(pj_in->params, "tdatum").i )
100    {
101        got_datum = TRUE;
102        sprintf( defn+strlen(defn), " +datum=%s", 
103                 pj_param(pj_in->params,"sdatum").s );
104    }
105    else if( pj_param(pj_in->params, "tellps").i )
106    {
107        sprintf( defn+strlen(defn), " +ellps=%s", 
108                 pj_param(pj_in->params,"sellps").s );
109    }
110    else if( pj_param(pj_in->params, "ta").i )
111    {
112        sprintf( defn+strlen(defn), " +a=%s", 
113                 pj_param(pj_in->params,"sa").s );
114            
115        if( pj_param(pj_in->params, "tb").i )
116            sprintf( defn+strlen(defn), " +b=%s", 
117                     pj_param(pj_in->params,"sb").s );
118        else if( pj_param(pj_in->params, "tes").i )
119            sprintf( defn+strlen(defn), " +es=%s", 
120                     pj_param(pj_in->params,"ses").s );
121        else if( pj_param(pj_in->params, "tf").i )
122            sprintf( defn+strlen(defn), " +f=%s", 
123                     pj_param(pj_in->params,"sf").s );
124        else
125            sprintf( defn+strlen(defn), " +es=%.16g", 
126                     pj_in->es );
127    }
128    else
129    {
130        pj_errno = -13;
131
132        return NULL;
133    }
134
135    if( !got_datum )
136    {
137        if( pj_param(pj_in->params, "ttowgs84").i )
138            sprintf( defn+strlen(defn), " +towgs84=%s", 
139                     pj_param(pj_in->params,"stowgs84").s );
140
141        if( pj_param(pj_in->params, "tnadgrids").i )
142            sprintf( defn+strlen(defn), " +nadgrids=%s", 
143                     pj_param(pj_in->params,"snadgrids").s );
144    }
145
146    /* copy over some other information related to ellipsoid */
147    if( pj_param(pj_in->params, "tR").i )
148        sprintf( defn+strlen(defn), " +R=%s", 
149                 pj_param(pj_in->params,"sR").s );
150
151    if( pj_param(pj_in->params, "tR_A").i )
152        sprintf( defn+strlen(defn), " +R_A" );
153
154    if( pj_param(pj_in->params, "tR_V").i )
155        sprintf( defn+strlen(defn), " +R_V" );
156
157    if( pj_param(pj_in->params, "tR_a").i )
158        sprintf( defn+strlen(defn), " +R_a" );
159
160    if( pj_param(pj_in->params, "tR_lat_a").i )
161        sprintf( defn+strlen(defn), " +R_lat_a=%s", 
162                 pj_param(pj_in->params,"sR_lat_a").s );
163
164    if( pj_param(pj_in->params, "tR_lat_g").i )
165        sprintf( defn+strlen(defn), " +R_lat_g=%s", 
166                 pj_param(pj_in->params,"sR_lat_g").s );
167
168    /* copy over prime meridian */
169    if( pj_param(pj_in->params, "tpm").i )
170        sprintf( defn+strlen(defn), " +pm=%s", 
171                 pj_param(pj_in->params,"spm").s );
172
173    return pj_init_plus( defn );
174}
175