class OGRSpatialReference #include <ogr_spatialref.h>
OGRSpatialReference is an important class in the GDAL/OGR library, which is used to manage and operate the spatial reference system (Spatial Reference System, SRS) of geospatial data. It provides a set of functions that allow users to define, query, parse, and transform coordinate system and projection information for geospatial data
Public Functions
SetFromUserInput
OGRErr SetFromUserInput(const char*)
Set spatial reference from various text formats
parameter:
pszDefinition -- The textual definition from which to try to infer the SRS.
Returns: OGRERR_NONE on success, or an error code if the name is not recognized, the definition is corrupted, or the EPSG value cannot be looked up successfully
// 创建一个 OGRSpatialReference 对象
OGRSpatialReference srs;
// 定义一个字符串,包含空间参考系统的信息
const char* user_input = "+proj=utm +zone=48 +datum=WGS84 +units=m +no_defs";
// 使用 SetFromUserInput() 函数设置空间参考系统
OGRErr result = srs.SetFromUserInput(user_input);
SetTOWGS84
OGRErr SetTOWGS84(double, double, double, double = 0.0, double = 0.0, double = 0.0, double = 0.0)
Set the Bursa-Wolf transformation to WGS84 This will create the TOWGS84 node as a child of the datum
parameter:
-
dfDX -- X subdivision in meters.
-
dfDY -- Y child in meters.
-
dfDZ -- Z subclass in meters.
-
dfEX – X rotation in arcseconds (optional, defaults to zero).
-
dfEY -- Y rotation in arcseconds (optional, defaults to zero).
-
dfEZ -- Z rotation in arcseconds (optional, defaults to zero).
-
dfPPM -- scale factor (parts per million).
Return: OGRERR_NONE success
// 创建一个 OGRSpatialReference 对象
OGRSpatialReference srs;
// 设置空间参考系统的转换参数到 WGS 84
double dx = -84.0;
double dy = -97.0;
double dz = -119.0;
double ex = 0.0;
double ey = 0.0;
double ez = 0.0;
double ppm = 0.0;
OGRErr result = srs.SetTOWGS84(dx, dy, dz, ex, ey, ez, ppm);
GetTOWGS84
OGRErr GetTOWGS84(double *padfCoef, int nCoeff = 7) const
Get TOWGS84 parameters (if available)
parameter:
-
padfCoeff -- Array to place up to 7 coefficients.
-
nCoeffCount -- size of padfCoeff -- defaults to 7.
Returns: OGRERR_NONE on success, or OGRERR_FAILURE if no TOWGS84 nodes are available
// 创建一个 OGRSpatialReference 对象
OGRSpatialReference srs;
// 设置空间参考系统的转换参数到 WGS 84
double dx = -84.0;
double dy = -97.0;
double dz = -119.0;
double ex = 0.0;
double ey = 0.0;
double ez = 0.0;
double ppm = 0.0;
srs.SetTOWGS84(dx, dy, dz, ex, ey, ez, ppm);
// 获取空间参考系统的转换参数
double transformationParams[7];
OGRErr result = srs.GetTOWGS84(transformationParams, 7);
AddGuessedTOWGS84
OGRErr AddGuessedTOWGS84()
Try adding a 3-parameter or 7-parameter Helmert transform to WGS84
Returns: OGRERR_NONE on success, error code on failure (CRS converted to WGS84 or no match found)
// 创建一个 OGRSpatialReference 对象
OGRSpatialReference srs;
// 定义一个字符串,包含空间参考系统的信息
const char* user_input = "+proj=utm +zone=48 +datum=WGS84 +units=m +no_defs";
// 使用 SetFromUserInput() 函数设置空间参考系统
OGRErr result = srs.SetFromUserInput(user_input);
// 检查是否设置成功
if (result != OGRERR_NONE) {
printf("空间参考系统设置失败!\n");
return 1;
}
// 尝试添加转换参数
OGRErr guessResult = srs.AddGuessedTOWGS84();
GetSemiMajor
double GetSemiMajor(OGRErr* = nullptr) const
Get the semi-major axis of the ellipsoid (in meters, since GDAL 3.0)
// 创建一个 OGRSpatialReference 对象
OGRSpatialReference srs;
// 定义一个字符串,包含空间参考系统的信息
const char* user_input = "+proj=utm +zone=48 +datum=WGS84 +units=m +no_defs";
// 使用 SetFromUserInput() 函数设置空间参考系统
OGRErr result = srs.SetFromUserInput(user_input);
// 获取椭球体的长半轴值
OGRErr err;
double semiMajor = srs.GetSemiMajor(&err);
GetSemiMinor
double GetSemiMinor(OGRErr* = nullptr) const
Get the semi-minor axis of the sphere
parameter:
pnErr -- if non-NULL set to OGRERR_FAILURE if the semi-minor axis can be found.
Returns: semi-minor axis, or WGS84 semi-minor axis if not found
GetInvFlattening
double GetInvFlattening(OGRErr* = nullptr) const
Obtain spherical inverse flattening
GetEccentricity
double GetEccentricity() const
Returns: eccentricity (or -1 on error)
GetSquaredEccentricity
double GetSquaredEccentricity() const
Get ellipsoid squared eccentricity
Returns: square of eccentricity (or -1 for error)
GetEPSGGeogCS
int GetEPSGGeogCS() const
Get the EPSG code for this coordinate system GEOGCS
Returns: EPSG code
// 获取地理坐标系的 EPSG 代码
int epsgCode = srs.GetEPSGGeogCS();
地理坐标系的 EPSG 代码为: 4326
GetAuthorityCode
const char *GetAuthorityCode(const char *pszTargetKey) const
Get the authority code of the node
parameter:
pszTargetKey -- Partial or full path to the node to get permissions from. That is, "PROJCS", "GEOGCS", "GEOGCS|UNIT" or NULL to search for the authority node on the root element.
Returns: the value code from the authorization node, or NULL on failure. The returned value is an internal value and should not be freed or modified
// 获取地理坐标系的标识代码
const char* authorityCode = srs.GetAuthorityCode("GEOGCS");
地理坐标系的标识代码为: EPSG:4326
GetAuthorityName
const char *GetAuthorityName(const char *pszTargetKey) const
Get the authority name of the node
parameter:
pszTargetKey -- Partial or full path to the node to get permissions from. That is, "PROJCS", "GEOGCS", "GEOGCS|UNIT" or NULL to search for the authority node on the root element.
Returns: the value code from the authorization node, or NULL on failure. The returned value is an internal value and should not be freed or modified
GetAreaOfUse
bool GetAreaOfUse(double *pdfWestLongitudeDeg, double *pdfSouthLatitudeDeg, double *pdfEastLongitudeDeg, double *pdfNorthLatitudeDeg, const char **ppszAreaName) const
Returns the usage area of the CRS
parameter:
-
pdfWestLongitudeDeg -- Pointer to double that receives the westernmost longitude, expressed in degrees. May be empty. If the return value is -1000, the bounding box is unknown.
-
pdfSouthLatitudeDeg -- Pointer to a double that receives the southernmost latitude, expressed in degrees. May be empty. If the return value is -1000, the bounding box is unknown.
-
pdfEastLongitudeDeg -- Pointer to double that receives the easternmost longitude, expressed in degrees. May be empty. If the return value is -1000, the bounding box is unknown.
-
pdfNorthLatitudeDeg -- Pointer to a double receiving the northernmost latitude, expressed in degrees. May be empty. If the return value is -1000, the bounding box is unknown.
-
ppszAreaName -- Pointer to a string to receive the usage area name. May be empty. Note that *ppszAreaName is short-lived and may be invalidated by further calls.
Returns: true in case of success
// 定义变量用于接收使用范围信息
double westLongitude, southLatitude, eastLongitude, northLatitude;
const char* areaName;
// 获取使用范围信息
bool success = srs.GetAreaOfUse(&westLongitude, &southLatitude, &eastLongitude, &northLatitude, &areaName);
// 检查是否获取成功
if (success) {
printf("使用范围信息:\n");
printf("西边界经度: %.6f\n", westLongitude);
printf("南边界纬度: %.6f\n", southLatitude);
printf("东边界经度: %.6f\n", eastLongitude);
printf("北边界纬度: %.6f\n", northLatitude);
printf("使用范围名称: %s\n", areaName);
} else {
printf("获取使用范围信息失败!\n");
}
SetProjParm
OGRErr SetProjParm(const char*, double)
Set projection parameter values Add a new parameter under PROJCS with indicated name and value
parameter:
-
pszParamName -- parameter name, should be chosen from macros in ogr_srs_api.h, eg SRS_PP_CENTRAL_MERIDIAN.
-
dfValue – the value to assign.
Return: OGRERR_NONE success
// 创建一个 OGRSpatialReference 对象
OGRSpatialReference srs;
// 设置投影坐标系为 Transverse Mercator 投影
srs.SetUTM(48, TRUE);
// 设置投影坐标系的中央经线参数值为 80.0 度
OGRErr result = srs.SetProjParm("central_meridian", 80.0);
GetProjParm
double GetProjParm(const char*, double = 0.0, OGRErr * = nullptr) const
Get the projection parameter value
parameter:
-
pszName -- The name of the parameter to be taken from the SRS_PP code set in ogr_srs_api.h.
-
dfDefaultValue – The value to return if this parameter does not exist.
-
pnErr – where to place the error code on failure. Ignored if NULL.
Returns: the value of the parameter
SetNormProjParm
OGRErr SetNormProjParm(const char*, double)
Set projection parameters using normalized values
parameter:
-
pszName -- parameter name, should be chosen from macros in ogr_srs_api.h, eg SRS_PP_CENTRAL_MERIDIAN.
-
dfValue – the value to assign.
Return: OGRERR_NONE success
// 创建一个 OGRSpatialReference 对象
OGRSpatialReference srs;
// 设置投影坐标系为 Transverse Mercator 投影
srs.SetUTM(48, TRUE);
// 设置投影坐标系的中央经线参数值为 80.0 度
OGRErr result = srs.SetProjParm("central_meridian", 80.0);
In geospatial data processing, "normalization" or "standardization" refers to converting data into a uniform standard range or unit for easy comparison, analysis, and processing. Geospatial data often comes from different sources, using different projections, coordinate systems, or units, so their value ranges and representations can vary widely. The purpose of normalization is to eliminate these differences, so that data can be compared and analyzed effectively between different data sources.
GetNormProjParm
double GetNormProjParm(const char*, double = 0.0, OGRErr* = nullptr) const
Get the normalized projection parameter value
parameter:
-
pszName -- The name of the parameter to be taken from the SRS_PP code set in ogr_srs_api.h.
-
dfDefaultValue – The value to return if this parameter does not exist.
-
pnErr – where to place the error code on failure. Ignored if NULL.
Returns: the value of the parameter
// 创建一个 OGRSpatialReference 对象
OGRSpatialReference srs;
// 设置投影坐标系为 Transverse Mercator 投影
srs.SetUTM(48, TRUE);
// 设置投影坐标系的中央经线参数值为 80.0 度
OGRErr result = srs.SetProjParm("central_meridian", 80.0);