GDALSetProjection使用的一個注意事項


GDALSetProjection 簡述

GDALSetProjection是用來給GDALDataset設定投影信息(坐標系統)的接口,實際上是GDALDataset::SetProjection這個虛函數的轉調而已。官網文檔描述如下:

**CPLErr GDALDataset::SetProjection (const char * pszProjection ) **
Set the projection reference string for this dataset.
The string should be in OGC WKT or PROJ.4 format. An error may occur because of incorrectly specified projection strings, because the dataset is not writable, or because the dataset does not support the indicated projection. Many formats do not support writing projections.
This method is the same as the C GDALSetProjection() function.
Parameters
pszProjection projection reference string.
Returns
CE_Failure if an error occurs, otherwise CE_None.

按照這里描述的,傳入的參數可以是OGC WKT或者PROJ.4格式的字符串,但是可能在寫入坐標系信息的時候發生錯誤,因為數據集不可寫。或者數據集不支持對應的坐標系,很多格式不支持寫入。

注意事項

這里要說的就是這個函數使用的時候要注意的地方,因為這是一個虛函數,在基類GDALDataset中的定義是直接返回CE_Failure的,派生出的各個格式有自己的實現。
因為每個派生出的格式對SetProjection的實現可能都不一致,所以這里使用的時候就需要根據不同的格式傳入不同的參數。
例如GTiff就不支持PROJ.4格式的字符串,只能是OGC WKT格式的。
又比如,MBTiles格式支持EPSG:3857,其他的就不支持了。
又比如,NTIF格式,也僅支持WKT格式字符串傳入,且只支持WGS84地理坐標或者UTM投影坐標。
OGC出的GeoPackage格式是支持最好的,支持各種用戶輸入格式,支持各種坐標系,幾乎沒有限制。
HDF4格式就很簡單粗暴了,不做任何驗證,傳入什么就復制什么.

下面貼出GTiffMBTiles兩種格式對SetProjection的實現。
GDALDataset::SetProjection

CPLErr GDALDataset::SetProjection( CPL_UNUSED const char *pszProjection )
{
    if( !(GetMOFlags() & GMO_IGNORE_UNIMPLEMENTED) )
        ReportError(CE_Failure, CPLE_NotSupported,
                    "Dataset does not support the SetProjection() method.");
    return CE_Failure;
}

GTiffDataset::SetProjection

CPLErr GTiffDataset::SetProjection( const char * pszNewProjection )

{
    if( bStreamingOut && bCrystalized )
    {
        CPLError(
            CE_Failure, CPLE_NotSupported,
            "Cannot modify projection at that point in "
            "a streamed output file" );
        return CE_Failure;
    }

    LoadGeoreferencingAndPamIfNeeded();
    LookForProjection();

    if( !STARTS_WITH_CI(pszNewProjection, "GEOGCS")
        && !STARTS_WITH_CI(pszNewProjection, "PROJCS")
        && !STARTS_WITH_CI(pszNewProjection, "LOCAL_CS")
        && !STARTS_WITH_CI(pszNewProjection, "COMPD_CS")
        && !STARTS_WITH_CI(pszNewProjection, "GEOCCS")
        && !EQUAL(pszNewProjection,"") )
    {
        CPLError( CE_Failure, CPLE_AppDefined,
                "Only OGC WKT Projections supported for writing to GeoTIFF.  "
                "%s not supported.",
                  pszNewProjection );

        return CE_Failure;
    }

    if( EQUAL(pszNewProjection, "") &&
        pszProjection != NULL &&
        !EQUAL(pszProjection, "") )
    {
        bForceUnsetProjection = true;
    }

    CPLFree( pszProjection );
    pszProjection = CPLStrdup( pszNewProjection );

    bGeoTIFFInfoChanged = true;

    return CE_None;
}

MBTilesDataset::SetProjection

CPLErr MBTilesDataset::SetProjection( const char* pszProjection )
{
    if( eAccess != GA_Update )
    {
        CPLError(CE_Failure, CPLE_NotSupported,
                 "SetProjection() not supported on read-only dataset");
        return CE_Failure;
    }

    OGRSpatialReference oSRS;
    if( oSRS.SetFromUserInput(pszProjection) != OGRERR_NONE )
        return CE_Failure;
    if( oSRS.GetAuthorityName(NULL) == NULL ||
        !EQUAL(oSRS.GetAuthorityName(NULL), "EPSG") ||
        oSRS.GetAuthorityCode(NULL) == NULL ||
        !EQUAL(oSRS.GetAuthorityCode(NULL), "3857") )
    {
        CPLError(CE_Failure, CPLE_NotSupported,
                 "Only EPSG:3857 supported on MBTiles dataset");
        return CE_Failure;
    }
    return CE_None;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM