⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ogrspatialreference.cpp

📁 mitab,读取MapInfo的地图文件
💻 CPP
📖 第 1 页 / 共 5 页
字号:
 * input is known to be in a particular format. 
 *
 * This method does the same thing as the OSRSetFromUserInput() function.
 * 
 * @param pszDefinition text definition to try to deduce SRS from.
 *
 * @return OGRERR_NONE on success, or an error code if the name isn't
 * recognised, the definition is corrupt, or an EPSG value can't be 
 * successfully looked up.
 */ 

OGRErr OGRSpatialReference::SetFromUserInput( const char * pszDefinition )

{
    int     bESRI = FALSE;
    OGRErr  err;

    if( EQUALN(pszDefinition,"ESRI::",6) )
    {
        bESRI = TRUE;
        pszDefinition += 6;
    }

/* -------------------------------------------------------------------- */
/*      Is it a recognised syntax?                                      */
/* -------------------------------------------------------------------- */
    if( EQUALN(pszDefinition,"PROJCS",6)
        || EQUALN(pszDefinition,"GEOGCS",6)
        || EQUALN(pszDefinition,"LOCAL_CS",8) )
    {
        err = importFromWkt( (char **) &pszDefinition );
        if( err == OGRERR_NONE && bESRI )
            err = morphFromESRI();

        return err;
    }

    if( EQUALN(pszDefinition,"EPSG:",5) )
        return importFromEPSG( atoi(pszDefinition+5) );

    if( EQUALN(pszDefinition,"urn:ogc:def:crs:",16) 
        || EQUALN(pszDefinition,"urn:x-ogc:def:crs:",18) )
        return importFromURN( pszDefinition );

    if( EQUALN(pszDefinition,"AUTO:",5) )
        return importFromWMSAUTO( pszDefinition );

    if( EQUALN(pszDefinition,"DICT:",5) 
        && strstr(pszDefinition,",") )
    {
        char *pszFile = CPLStrdup(pszDefinition+5);
        char *pszCode = strstr(pszFile,",") + 1;
        
        pszCode[-1] = '\0';

        err = importFromDict( pszFile, pszCode );
        CPLFree( pszFile );

        if( err == OGRERR_NONE && bESRI )
            err = morphFromESRI();

        return err;
    }

    if( EQUAL(pszDefinition,"NAD27") 
        || EQUAL(pszDefinition,"NAD83") 
        || EQUAL(pszDefinition,"WGS84") 
        || EQUAL(pszDefinition,"WGS72") )
    {
        Clear();
        return SetWellKnownGeogCS( pszDefinition );
    }

    if( strstr(pszDefinition,"+proj") != NULL 
             || strstr(pszDefinition,"+init") != NULL )
        return importFromProj4( pszDefinition );

/* -------------------------------------------------------------------- */
/*      Try to open it as a file.                                       */
/* -------------------------------------------------------------------- */
    FILE        *fp;
    int         nBufMax = 100000;
    char        *pszBufPtr, *pszBuffer;
    int         nBytes;

    fp = VSIFOpen( pszDefinition, "rt" );
    if( fp == NULL )
        return OGRERR_CORRUPT_DATA;

    pszBuffer = (char *) CPLMalloc(nBufMax);
    nBytes = VSIFRead( pszBuffer, 1, nBufMax-1, fp );
    VSIFClose( fp );

    if( nBytes == nBufMax-1 )
    {
        CPLDebug( "OGR", 
                  "OGRSpatialReference::SetFromUserInput(%s), opened file\n"
                  "but it is to large for our generous buffer.  Is it really\n"
                  "just a WKT definition?" );
        CPLFree( pszBuffer );
        return OGRERR_FAILURE;
    }

    pszBuffer[nBytes] = '\0';

    pszBufPtr = pszBuffer;
    while( pszBufPtr[0] == ' ' || pszBufPtr[0] == '\n' )
        pszBufPtr++;

    if( pszBufPtr[0] == '<' )
        err = importFromXML( pszBufPtr );
    else if( strstr(pszBuffer,"+proj") != NULL 
             || strstr(pszBuffer,"+init") != NULL )
        err = importFromProj4( pszBufPtr );
    else
    {
        err = importFromWkt( &pszBufPtr );
        if( err == OGRERR_NONE && bESRI )
            err = morphFromESRI();
    }

    CPLFree( pszBuffer );

    return err;
}

/************************************************************************/
/*                        OSRSetFromUserInput()                         */
/************************************************************************/

OGRErr CPL_STDCALL OSRSetFromUserInput( OGRSpatialReferenceH hSRS, 
                                        const char *pszDef )

{
    return ((OGRSpatialReference *) hSRS)->SetFromUserInput( pszDef );
}

/************************************************************************/
/*                           importFromURN()                            */
/*                                                                      */
/*      See OGC recommendation paper 06-023r1 or later for details.     */
/************************************************************************/

/**
 * Initialize from OGC URN. 
 *
 * Initializes this spatial reference from a coordinate system defined
 * by an OGC URN prefixed with "urn:ogc:def:crs:" per recommendation 
 * paper 06-023r1.  Currently EPSG and OGC authority values are supported, 
 * including OGC auto codes, but not including CRS1 or CRS88 (NAVD88). 
 *
 * This method is also support through SetFromUserInput() which can
 * normally be used for URNs.
 * 
 * @param pszURN the urn string. 
 *
 * @return OGRERR_NONE on success or an error code.
 */

OGRErr OGRSpatialReference::importFromURN( const char *pszURN )

{
    const char *pszCur = pszURN + 16;

    if( EQUALN(pszURN,"urn:ogc:def:crs:",16) )
        pszCur = pszURN + 16;
    else if( EQUALN(pszURN,"urn:x-ogc:def:crs:",18) )
        pszCur = pszURN + 18;
    else
    {
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "URN %s not a supported format.", pszURN );
        return OGRERR_FAILURE;
    }

/* -------------------------------------------------------------------- */
/*      Find code (ignoring version) out of string like:                */
/*                                                                      */
/*      authority:version:code                                          */
/* -------------------------------------------------------------------- */
    const char *pszAuthority = pszCur;

    // skip authority
    while( *pszCur != ':' && *pszCur )
        pszCur++;
    if( *pszCur == ':' )
        pszCur++;

    // skip version
    while( *pszCur != ':' && *pszCur )
        pszCur++;
    if( *pszCur == ':' )
        pszCur++;

    const char *pszCode = pszCur;

/* -------------------------------------------------------------------- */
/*      Is this an EPSG code?                                           */
/* -------------------------------------------------------------------- */
    if( EQUALN(pszAuthority,"EPSG:",5) )
        return importFromEPSG( atoi(pszCode) );

/* -------------------------------------------------------------------- */
/*      Is this an OGC code?                                            */
/* -------------------------------------------------------------------- */
    if( !EQUALN(pszAuthority,"OGC:",4) )
    {
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "URN %s has unrecognised authority.", 
                  pszURN );
        return OGRERR_FAILURE;
    }

    if( EQUALN(pszCode,"CRS84",5) )
        return SetWellKnownGeogCS( pszCode );
    else if( EQUALN(pszCode,"CRS83",5) )
        return SetWellKnownGeogCS( pszCode );
    else if( EQUALN(pszCode,"CRS27",5) )
        return SetWellKnownGeogCS( pszCode );

/* -------------------------------------------------------------------- */
/*      Handle auto codes.  We need to convert from format              */
/*      AUTO42001:99:8888 to format AUTO:42001,99,8888.                 */
/* -------------------------------------------------------------------- */
    else if( EQUALN(pszCode,"AUTO",4) )
    {
        char szWMSAuto[100];
        int i;

        if( strlen(pszCode) > sizeof(szWMSAuto)-2 )
            return OGRERR_FAILURE;

        strcpy( szWMSAuto, "AUTO:" );
        strcpy( szWMSAuto + 5, pszCode + 4 );
        for( i = 5; szWMSAuto[i] != '\0'; i++ )
        {
            if( szWMSAuto[i] == ':' )
                szWMSAuto[i] = ',';
        }

        return importFromWMSAUTO( szWMSAuto );
    }

/* -------------------------------------------------------------------- */
/*      Not a recognise OGC item.                                       */
/* -------------------------------------------------------------------- */
    CPLError( CE_Failure, CPLE_AppDefined, 
              "URN %s value not supported.", 
              pszURN );

    return OGRERR_FAILURE;
}

/************************************************************************/
/*                         importFromWMSAUTO()                          */
/*                                                                      */
/*      Note that the WMS 1.3 specification does not include the        */
/*      units code, while apparently earlier specs do.  We try to       */
/*      guess around this.                                              */
/************************************************************************/

OGRErr OGRSpatialReference::importFromWMSAUTO( const char * pszDefinition )

{
    char **papszTokens;
    int nProjId, nUnitsId;
    double dfRefLong, dfRefLat = 0.0;
    
/* -------------------------------------------------------------------- */
/*      Tokenize                                                        */
/* -------------------------------------------------------------------- */
    if( EQUALN(pszDefinition,"AUTO:",5) )
        pszDefinition += 5;

    papszTokens = CSLTokenizeStringComplex( pszDefinition, ",", FALSE, TRUE );

    if( CSLCount(papszTokens) == 4 )
    {
        nProjId = atoi(papszTokens[0]);
        nUnitsId = atoi(papszTokens[1]);
        dfRefLong = atof(papszTokens[2]);
        dfRefLat = atof(papszTokens[3]);
    }
    else if( CSLCount(papszTokens) == 3 && atoi(papszTokens[0]) == 42005 )
    {
        nProjId = atoi(papszTokens[0]);
        nUnitsId = atoi(papszTokens[1]);
        dfRefLong = atof(papszTokens[2]);
        dfRefLat = 0.0;
    }
    else if( CSLCount(papszTokens) == 3 )
    {
        nProjId = atoi(papszTokens[0]);
        nUnitsId = 9001;
        dfRefLong = atof(papszTokens[1]);
        dfRefLat = atof(papszTokens[2]);

    }
    else if( CSLCount(papszTokens) == 2 && atoi(papszTokens[0]) == 42005 ) 
    {
        nProjId = atoi(papszTokens[0]);
        nUnitsId = 9001;
        dfRefLong = atof(papszTokens[1]);
    }
    else
    {
        CSLDestroy( papszTokens );
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "AUTO projection has wrong number of arguments, expected\n"
                  "AUTO:proj_id,units_id,ref_long,ref_lat or"
                  "AUTO:proj_id,ref_long,ref_lat" );
        return OGRERR_FAILURE;
    }

    CSLDestroy( papszTokens );

/* -------------------------------------------------------------------- */
/*      Build coordsys.                                                 */
/* -------------------------------------------------------------------- */
    Clear();

    switch( nProjId )
    {
      case 42001: // Auto UTM
        SetUTM( (int) floor( (dfRefLong + 180.0) / 6.0 ) + 1, 
                dfRefLat >= 0.0 );
        break;

      case 42002: // Auto TM (strangely very UTM-like).
        SetTM( 0, dfRefLong, 0.9996, 
               500000.0, (dfRefLat >= 0.0) ? 0.0 : 10000000.0 );
        break;

      case 42003: // Auto Orthographic.
        SetOrthographic( dfRefLat, dfRefLong, 0.0, 0.0 );
        break;

      case 42004: // Auto Equirectangular
        SetEquirectangular( dfRefLat, dfRefLong, 0.0, 0.0 );
        break;

      case 42005:
        SetMollweide( dfRefLong, 0.0, 0.0 );
        break;

      default:
        CPLError( CE_Failure, CPLE_AppDefined,
                  "Unsupported projection id in importFromWMSAUTO(): %d", 
                  nProjId );
        return OGRERR_FAILURE;
    }

/* -------------------------------------------------------------------- */
/*      Set units.                                                      */
/* -------------------------------------------------------------------- */

    switch( nUnitsId )
    {
      case 9001:
        SetLinearUnits( SRS_UL_METER, 1.0 );
        break;

      case 9002

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -