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

📄 ogrspatialreference.cpp

📁 用于读取TAB、MIF、SHP文件的类
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*      Do we already have a GEOGCS?  If so, blow it away so it can     *//*      be properly replaced.                                           *//* -------------------------------------------------------------------- */    if( GetAttrNode( "GEOGCS" ) != NULL )    {        OGR_SRSNode *poPROJCS;        if( EQUAL(GetRoot()->GetValue(),"GEOGCS") )            Clear();        else if( (poPROJCS = GetAttrNode( "PROJCS" )) != NULL                 && poPROJCS->FindChild( "GEOGCS" ) != -1 )            poPROJCS->DestroyChild( poPROJCS->FindChild( "GEOGCS" ) );        else            return OGRERR_FAILURE;    }/* -------------------------------------------------------------------- *//*      Find the GEOGCS node on the source.                             *//* -------------------------------------------------------------------- */    poGeogCS = poSrcSRS->GetAttrNode( "GEOGCS" );    if( poGeogCS == NULL )        return OGRERR_FAILURE;/* -------------------------------------------------------------------- *//*      Attach below the PROJCS if there is one, or make this the root. *//* -------------------------------------------------------------------- */    if( GetRoot() != NULL && EQUAL(GetRoot()->GetValue(),"PROJCS") )        poRoot->InsertChild( poGeogCS->Clone(), 1 );    else        SetRoot( poGeogCS->Clone() );    return OGRERR_NONE;}/************************************************************************//*                         OSRCopyGeogCSFrom()                          *//************************************************************************/OGRErr OSRCopyGeogCSFrom( OGRSpatialReferenceH hSRS,                           OGRSpatialReferenceH hSrcSRS ){    return ((OGRSpatialReference *) hSRS)->CopyGeogCSFrom(         (const OGRSpatialReference *) hSrcSRS );}/************************************************************************//*                          SetFromUserInput()                          *//************************************************************************//** * Set spatial reference from various text formats. * * This method will examine the provided input, and try to deduce the * format, and then use it to initialize the spatial reference system.  It * may take the following forms: * * <ol> * <li> Well Known Text definition - passed on to importFromWkt(). * <li> "EPSG:n" - number passed on to importFromEPSG().  * <li> "AUTO:proj_id,unit_id,lon0,lat0" - WMS auto projections. * <li> "urn:ogc:def:crs:EPSG::n" - ogc urns * <li> PROJ.4 definitions - passed on to importFromProj4(). * <li> filename - file read for WKT, XML or PROJ.4 definition. * <li> well known name accepted by SetWellKnownGeogCS(), such as NAD27, NAD83, * WGS84 or WGS72.  * </ol> * * It is expected that this method will be extended in the future to support * XML and perhaps a simplified "minilanguage" for indicating common UTM and * State Plane definitions.  * * This method is intended to be flexible, but by it's nature it is  * imprecise as it must guess information about the format intended.  When * possible applications should call the specific method appropriate if the * 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(papszTo

⌨️ 快捷键说明

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