PageRenderTime 19ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/Proj4/pj_datum_set.c

http://github.com/route-me/route-me
C | 161 lines | 67 code | 22 blank | 72 comment | 35 complexity | 9c2b9a913d70034e3c20e30cd9fca7e0 MD5 | raw file
  1/******************************************************************************
  2 * $Id: pj_datum_set.c,v 1.4 2007/11/29 21:06:50 fwarmerdam Exp $
  3 *
  4 * Project:  PROJ.4
  5 * Purpose:  Apply datum definition to PJ structure from initialization string.
  6 * Author:   Frank Warmerdam, warmerda@home.com
  7 *
  8 ******************************************************************************
  9 * Copyright (c) 2000, Frank Warmerdam
 10 *
 11 * Permission is hereby granted, free of charge, to any person obtaining a
 12 * copy of this software and associated documentation files (the "Software"),
 13 * to deal in the Software without restriction, including without limitation
 14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 15 * and/or sell copies of the Software, and to permit persons to whom the
 16 * Software is furnished to do so, subject to the following conditions:
 17 *
 18 * The above copyright notice and this permission notice shall be included
 19 * in all copies or substantial portions of the Software.
 20 *
 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 27 * DEALINGS IN THE SOFTWARE.
 28 ******************************************************************************
 29 *
 30 * $Log: pj_datum_set.c,v $
 31 * Revision 1.4  2007/11/29 21:06:50  fwarmerdam
 32 * make sure we only look for 7 parameters
 33 *
 34 * Revision 1.3  2007/01/31 06:41:01  fwarmerdam
 35 * dont parse more datum parameters than we have room for in datum_params[]
 36 *
 37 * Revision 1.2  2001/04/04 21:13:21  warmerda
 38 * do arcsecond/radian and ppm datum parm transformation in pj_set_datum()
 39 *
 40 * Revision 1.1  2000/07/06 23:32:27  warmerda
 41 * New
 42 *
 43 */
 44
 45#include "projects.h"
 46#include <string.h>
 47
 48/* SEC_TO_RAD = Pi/180/3600 */
 49#define SEC_TO_RAD 4.84813681109535993589914102357e-6
 50
 51/************************************************************************/
 52/*                            pj_datum_set()                            */
 53/************************************************************************/
 54
 55int pj_datum_set(paralist *pl, PJ *projdef)
 56
 57{
 58    const char *name, *towgs84;
 59
 60    projdef->datum_type = PJD_UNKNOWN;
 61
 62/* -------------------------------------------------------------------- */
 63/*      Is there a datum definition in the parameters list?  If so,     */
 64/*      add the defining values to the parameter list.  Note that       */
 65/*      this will append the ellipse definition as well as the          */
 66/*      towgs84= and related parameters.  It should also be pointed     */
 67/*      out that the addition is permanent rather than temporary        */
 68/*      like most other keyword expansion so that the ellipse           */
 69/*      definition will last into the pj_ell_set() function called      */
 70/*      after this one.                                                 */
 71/* -------------------------------------------------------------------- */
 72    name = pj_param(pl,"sdatum").s;
 73    if( name != NULL )
 74    {
 75        paralist *curr;
 76        const char *s;
 77        int i;
 78
 79        /* find the end of the list, so we can add to it */
 80        for (curr = pl; curr && curr->next ; curr = curr->next) {}
 81        
 82        /* find the datum definition */
 83        for (i = 0; (s = pj_datums[i].id) && strcmp(name, s) ; ++i) {}
 84
 85        if (!s) { pj_errno = -9; return 1; }
 86
 87        if( pj_datums[i].ellipse_id && strlen(pj_datums[i].ellipse_id) > 0 )
 88        {
 89            char	entry[100];
 90            
 91            strcpy( entry, "ellps=" );
 92            strncat( entry, pj_datums[i].ellipse_id, 80 );
 93		  if (curr)
 94		  {
 95                curr->next = pj_mkparam(entry);
 96		      curr = curr->next;
 97		  }
 98        }
 99        
100        if( pj_datums[i].defn && strlen(pj_datums[i].defn) > 0 )
101	   {
102            if (curr)
103                curr->next = pj_mkparam(pj_datums[i].defn);
104	   }
105    }
106
107/* -------------------------------------------------------------------- */
108/*      Check for nadgrids parameter.                                   */
109/* -------------------------------------------------------------------- */
110    if( pj_param(pl,"snadgrids").s != NULL )
111    {
112        /* We don't actually save the value separately.  It will continue
113           to exist int he param list for use in pj_apply_gridshift.c */
114
115        projdef->datum_type = PJD_GRIDSHIFT;
116    }
117
118/* -------------------------------------------------------------------- */
119/*      Check for towgs84 parameter.                                    */
120/* -------------------------------------------------------------------- */
121    else if( (towgs84 = pj_param(pl,"stowgs84").s) != NULL )
122    {
123        int    parm_count = 0;
124        const char *s;
125
126        memset( projdef->datum_params, 0, sizeof(double) * 7);
127
128        /* parse out the parameters */
129        for( s = towgs84; *s != '\0' && parm_count < 7; ) 
130        {
131            projdef->datum_params[parm_count++] = atof(s);
132            while( *s != '\0' && *s != ',' )
133                s++;
134            if( *s == ',' )
135                s++;
136        }
137
138        if( projdef->datum_params[3] != 0.0 
139            || projdef->datum_params[4] != 0.0 
140            || projdef->datum_params[5] != 0.0 
141            || projdef->datum_params[6] != 0.0 )
142        {
143            projdef->datum_type = PJD_7PARAM;
144
145            /* transform from arc seconds to radians */
146            projdef->datum_params[3] *= SEC_TO_RAD;
147            projdef->datum_params[4] *= SEC_TO_RAD;
148            projdef->datum_params[5] *= SEC_TO_RAD;
149            /* transform from parts per million to scaling factor */
150            projdef->datum_params[6] = 
151                (projdef->datum_params[6]/1000000.0) + 1;
152        }
153        else 
154            projdef->datum_type = PJD_3PARAM;
155
156        /* Note that pj_init() will later switch datum_type to 
157           PJD_WGS84 if shifts are all zero, and ellipsoid is WGS84 or GRS80 */
158    }
159
160    return 0;
161}