GDAL C++ API Learning Road (2) Spatial Reference System OGRSpatialReference Class

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

importFromWkt

OGRErr importFromWkt(char**)

import from WKT string

parameter:

ppszInput  -- Pointer to input. The pointer will be updated to point to the remaining unused input text.

Returns: OGRERR_NONE whether the import was successful, if the import failed, OGRERR_CORRUPT_DATA whether the import failed for any reason

    // WGS 84 经纬度坐标系的 WKT 字符串
    const char* wkt = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]";

    // 创建一个空的 OGRSpatialReference 对象
    OGRSpatialReference srs;

    // 从 WKT 字符串中导入空间参考系统
    OGRErr err = srs.importFromWkt(const_cast<char**>(&wkt));

importFromProj4

OGRErr importFromProj4const char*)

Import PROJ coordinate strings

parameter:

pszProj4  -- PROJ style string.

Returns: OGRERR_NONE on success or OGRERR_CORRUPT_DATA on failure

    // Proj.4 格式的字符串,表示 WGS 84 经纬度坐标系
    const char* proj4String = "+proj=longlat +datum=WGS84 +no_defs";

    // 创建一个空的 OGRSpatialReference 对象
    OGRSpatialReference srs;

    // 从 Proj.4 字符串中导入空间参考系统
    OGRErr err = srs.importFromProj4(proj4String);

importFromEPSG

OGRErr importFromEPSG(int)

Initialize SRS based on EPSG geographic, projected or vertical CRS codes

parameter:

nCode  -- GCS or PCS code in the horizontal coordinate system table.

Returns: OGRERR_NONE when the coordinate system is successfully imported from ESRI .prj format, and an error code when it fails

    // 定义 EPSG 代码,例如,4326 表示 WGS 84 经纬度坐标系
    int epsgCode = 4326;

    // 创建一个空的 OGRSpatialReference 对象
    OGRSpatialReference srs;

    // 从 EPSG 代码中导入空间参考系统
    OGRErr err = srs.importFromEPSG(epsgCode);

importFromESRI

OGRErr importFromESRI(char**)

Import coordinate system from ESRI .prj format

parameter:

papszPrj  -- A NULL-terminated list containing a list of defined strings.

Returns: OGRERR_NONE Error code on success or failure

    // ESRI 格式的字符串,表示 WGS 84 经纬度坐标系
    const char* esriString = "GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]";

    // 创建一个空的 OGRSpatialReference 对象
    OGRSpatialReference srs;

    // 从 ESRI 字符串中导入空间参考系统
    OGRErr err = srs.importFromESRI(const_cast<char**>(&esriString));

importFromPCI

OGRErr importFromPCI(const char*, const char* = nullptrconst double* = nullptr)

Import a coordinate system from a PCI projection definition

parameter:

  • pszProj  -- Contains the defined NULL-terminated string. Looks like "pppppp Ennn" or "pppppp Dnnn", where "pppppp" is the projection code, "Ennn" is the ellipsoid code, and "Dnnn" is the datum code.

  • pszUnits  – Grid unit codes ("DEGREE" or "METRE"). If empty "METRE" will be used.

  • padfPrjParams  -- an array of 17 coordinate system parameters:

Returns: OGRERR_NONE Error code on success or failure

importFromUSGS

OGRErr importFromUSGS(long iProjSys, long iZone, double *padfPrjParams, long iDatum, int nUSGSAngleFormat = USGS_ANGLE_PACKEDDMS)

Import a coordinate system from a USGS projection definition

parameter:

  • iProjSys  -- Input projection system code, for GCTP.

  • iZone  -- The input zone for UTM and the US National Plane Projection System. For Southern Hemisphere UTM, use negative zone codes. For all other projections, iZone is ignored.

  • padfPrjParams  -- Array of 15 coordinate system parameters. These parameters vary for different projections.

  • iDatum  -- Input ellipsoid.

  • nUSGSAngleFormat  -- One of USGS_ANGLE_DECIMALDEGREES , USGS_ANGLE_PACKEDDMS , or USGS_ANGLE_RADIANS (default is USGS_ANGLE_PACKEDDMS ).

Returns: OGRERR_NONE Error code on success or failure

    // 定义 USGS 参数
    long iProjSys = 3; // 3 表示 UTM 投影
    long iZone = 10;   // UTM 投影的带号为 10
    double padfPrjParams[7] = {500000.0, 0.0, 0.9996, 0.0, 0.0, 0.0, 0.0}; // UTM 投影的参数
    long iDatum = 6326; // WGS 84 椭球

    // 创建一个空的 OGRSpatialReference 对象
    OGRSpatialReference srs;

    // 从 USGS 参数中导入空间参考系统
    OGRErr err = srs.importFromUSGS(iProjSys, iZone, padfPrjParams, iDatum);

importFromPanorama

OGRErr importFromPanorama(long, long, long, double*, bool bNorth = true)

Import a coordinate system from a Panorama GIS projection definition

parameter:

  • iProjSys  -- Enter the projection system code, used for GIS "panoramas".

  • iDatum  -- Input coordinate system.

  • iEllips  -- Input ellipsoids.

  • padfPrjParams  -- array of 8 coordinate system parameters:

  • bNorth  – False if true for the northern hemisphere. The default value is true.

Returns: OGRERR_NONE Error code on success or failure

    // 定义 Panorama 参数
    long linearUnits = 104; // 线性单位的编号
    long angularUnits = 0;  // 角度单位的编号
    long primeMeridian = 0; // 本初子午线的编号
    double parameters[7] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; // 参数数组
    bool bNorth = true; // 是否为北半球(默认为 true)

    // 创建一个空的 OGRSpatialReference 对象
    OGRSpatialReference srs;

    // 从 Panorama 参数中导入空间参考系统
    OGRErr err = srs.importFromPanorama(linearUnits, angularUnits, primeMeridian, parameters, bNorth);

importVertCSFromPanorama

OGRErr  importVertCSFromPanorama(int )

Import a vertical coordinate system from a "panorama" GIS projection definition

parameter:

iVCS  -- input vertical coordinate system ID

   // 定义 Panorama 垂直坐标系参数
    int vertCS = 3; // 垂直坐标系编号

    // 创建一个空的 OGRSpatialReference 对象
    OGRSpatialReference srs;

    // 从 Panorama 垂直坐标系参数中导入垂直坐标系信息
    OGRErr err = srs.importVertCSFromPanorama(vertCS);

importFromXML

OGRErr importFromXMLconst char*)

Import coordinate system from XML format (currently GML only)

parameter:

pszXML  -- the XML string to import

Returns: OGRERR_NONE on success or OGRERR_CORRUPT_DATA on failure

    // 定义 XML 格式的字符串,表示 WGS 84 经纬度坐标系
    const char* xmlString =
        "<SRS xmlns=\"http://www.opengis.net/gml\"><gml:name>WGS 84</gml:name>"
        "<gml:srsID srsName=\"EPSG:4326\">urn:ogc:def:crs:EPSG::4326</gml:srsID>"
        "<gml:usedBy xlink:href=\"urn:ogc:def:extent-polygon:EPSG::4326\"/>"
        "<gml:usesEllipsoidalCS xlink:href=\"urn:ogc:def:cs:EPSG::6422\"/>"
        "<gml:usesGeodeticDatum xlink:href=\"urn:ogc:def:datum:EPSG::6326\"/>"
        "</SRS>";

    // 创建一个空的 OGRSpatialReference 对象
    OGRSpatialReference srs;

    // 从 XML 字符串中导入空间参考系统
    OGRErr err = srs.importFromXML(xmlString);

importFromUrl

OGRErr importFromUrlconst char*)

Set spatial reference from URL

parameter:

pszUrl  -- The textual definition from which to try to infer the SRS.

Returns: OGRERR_NONE on success, an error code with a curl error message if the data could not be downloaded

    // 定义 URL 地址,表示空间参考系统信息的来源
    const char* url = "http://example.com/srs_info.xml"; // 替换为实际的 URL 地址

    // 创建一个空的 OGRSpatialReference 对象
    OGRSpatialReference srs;

    // 从 URL 地址中导入空间参考系统
    OGRErr err = srs.importFromUrl(url);

importFromMICoordSys

OGRErr importFromMICoordSysconst char*)

Import map info style coordinate system definition

parameter:

pszCoordSys -- Mapinfo style CoordSys definition string.

Returns: OGRERR_NONE on success, OGRERR_FAILURE on failure, OGRERR_UNSUPPORTED_OPERATION Whether the MITAB library is not linked

    // 定义 MICoordSys 格式的字符串,表示 WGS 84 经纬度坐标系
    const char* miCoordSysString = "CoordSys Earth Projection 1, 104";

    // 创建一个空的 OGRSpatialReference 对象
    OGRSpatialReference srs;

    // 从 MICoordSys 字符串中导入空间参考系统
    OGRErr err = srs.importFromMICoordSys(miCoordSysString);

EPSGTreatsAsLatLong

int EPSGTreatsAsLatLong() const

This method returns TRUE if EPSG believes that this geographic coordinate system should be treated as having longitude/longitude coordinate ordering

    // 定义一个 WGS 84 经纬度坐标系的WKT字符串
    const char* wktString = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]";
    
    // 创建一个 OGRSpatialReference 对象,并从WKT字符串中导入空间参考系统
    OGRSpatialReference srs;
    srs.importFromWkt(&wktString);
    
    // 检查空间参考系统是否被视为经纬度坐标系
    int treatsAsLatLong = srs.EPSGTreatsAsLatLong();
    
    if (treatsAsLatLong) {
        printf("该空间参考系统被视为经纬度坐标系。\n");
    } else {
        printf("该空间参考系统不是经纬度坐标系。\n");
    }

EPSGTreatsAsNorthingEasting

int EPSGTreatsAsNorthingEasting() const

This method returns TRUE if EPSG believes that this projected coordinate system should be treated as having a northing/easting coordinate ordering

Guess you like

Origin blog.csdn.net/qq_69574549/article/details/132066506