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;
}
原文地址:https://www.cnblogs.com/oloroso/p/9597067.html