Skip to content
This repository
tree: 6d6c093b95
Fetching contributors…

Cannot retrieve contributors at this time

file 143 lines (127 sloc) 5.271 kb
1 2 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 29 30 31 32 33 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
/**********************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Implementation of utility functions related to the PROJ4 library
* Author: DM Solutions Group (assefa@dmsolutions.ca)
*
**********************************************************************
* Copyright (c) 2001, Y. Assefa, DM Solutions Group Inc
*
* 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 of this Software or works derived from this 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
****************************************************************************/

#include <string.h>
#include "mapserver.h"

/************************************************************************/
/* ConvertProjUnitStringToMS */
/* */
/* Returns mapserver's unit code corresponding to the proj */
/* unit passed as argument. */
/* Please refer to ./src/pj_units.c file in the Proj.4 module. */
/************************************************************************/
#ifdef USE_PROJ
static int ConvertProjUnitStringToMS(const char *pszProjUnit)
{
    if (strcmp(pszProjUnit, "m") ==0)
    {
        return MS_METERS;
    }
    else if (strcmp(pszProjUnit, "km") ==0)
    {
        return MS_KILOMETERS;
    }
    else if (strcmp(pszProjUnit, "mi") ==0 || strcmp(pszProjUnit, "us-mi") ==0)
    {
        return MS_MILES;
    }
    else if (strcmp(pszProjUnit, "in") ==0 || strcmp(pszProjUnit, "us-in") ==0 )
    {
        return MS_INCHES;
    }
    else if (strcmp(pszProjUnit, "ft") ==0 || strcmp(pszProjUnit, "us-ft") ==0)
    {
        return MS_FEET;
    }
    else if (strcmp(pszProjUnit, "kmi") == 0)
    {
        return MS_NAUTICALMILES;
    }
    
    return -1;
}
#endif /* def USE_PROJ */

/************************************************************************/
/* int GetMapserverUnitUsingProj(projectionObj *psProj) */
/* */
/* Return a mapserver unit corresponding to the projection */
/* passed. Retunr -1 on failure */
/************************************************************************/
int GetMapserverUnitUsingProj(projectionObj *psProj)
{
#ifdef USE_PROJ
    char *proj_str;

    if( pj_is_latlong( psProj->proj ) )
        return MS_DD;

    proj_str = pj_get_def( psProj->proj, 0 );

/* -------------------------------------------------------------------- */
/* Handle case of named units. */
/* -------------------------------------------------------------------- */
    if( strstr(proj_str,"units=") != NULL )
    {
        char units[32];
        char *blank;

        strlcpy( units, (strstr(proj_str,"units=")+6), sizeof(units) );
        pj_dalloc( proj_str );

        blank = strchr(units, ' ');
        if( blank != NULL )
            *blank = '\0';

        return ConvertProjUnitStringToMS( units );
    }

/* -------------------------------------------------------------------- */
/* Handle case of to_meter value. */
/* -------------------------------------------------------------------- */
    if( strstr(proj_str,"to_meter=") != NULL )
    {
        char to_meter_str[32];
        char *blank;
        double to_meter;

        strlcpy(to_meter_str,(strstr(proj_str,"to_meter=")+9),
                sizeof(to_meter_str));
        pj_dalloc( proj_str );

        blank = strchr(to_meter_str, ' ');
        if( blank != NULL )
            *blank = '\0';

        to_meter = atof(to_meter_str);

        if( fabs(to_meter-1.0) < 0.0000001 )
            return MS_METERS;
        else if( fabs(to_meter-1000.0) < 0.00001 )
            return MS_KILOMETERS;
        else if( fabs(to_meter-0.3048) < 0.0001 )
            return MS_FEET;
        else if( fabs(to_meter-0.0254) < 0.0001 )
            return MS_INCHES;
        else if( fabs(to_meter-1609.344) < 0.001 )
            return MS_MILES;
        else if( fabs(to_meter-1852.0) < 0.1 )
            return MS_NAUTICALMILES;
        else
            return -1;
    }

    pj_dalloc( proj_str );
#endif
    return -1;
}
Something went wrong with that request. Please try again.