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 CGDALSetProjection()
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
格式就很簡單粗暴了,不做任何驗證,傳入什么就復制什么.
下面貼出GTiff
和MBTiles
兩種格式對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;
}