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 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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2016-09-06 PPM图片格式及其C读写代码
2016-09-06 linux下vmware的安装、物理分区使用及卸载