📄 osr_tutorial.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>OGR: OGR Projections Tutorial</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.1 --><div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> </ul></div><h1><a class="anchor" name="osr_tutorial">OGR Projections Tutorial</a></h1><h2><a class="anchor" name="osr_tutorial_intro">Introduction</a></h2>The <a class="el" href="classOGRSpatialReference.html">OGRSpatialReference</a>, and <a class="el" href="classOGRCoordinateTransformation.html">OGRCoordinateTransformation</a> classes provide services to represent coordinate systems (projections and datums) and to transform between them. These services are loosely modelled on the OpenGIS Coordinate Transformations specification, and use the same Well Known Text format for describing coordinate systems.<p>Some background on OpenGIS coordinate systems and services can be found in the Simple Features for COM, and Spatial Reference Systems Abstract Model documents available from <a href="http://www.opengis.org/">www.opengis.org</a>. The GeoTIFF Projections Transform List (<a href="http://www.remotesensing.org/geotiff/proj_list">http://www.remotesensing.org/geotiff/proj_list</a>) may also be of assistance in understanding formulations of projections in WKT. The <a href="http://www.epsg.org">EPSG</a> Geodesy web page is also a useful resource.<h2><a class="anchor" name="osr_tutorial_cs">Defining a Geographic Coordinate System</a></h2>Coordinate systems are encapsulated in the <a class="el" href="classOGRSpatialReference.html">OGRSpatialReference</a> class. There are a number of ways of initializing an <a class="el" href="classOGRSpatialReference.html">OGRSpatialReference</a> object to a valid coordinate system. There are two primary kinds of coordinate systems. The first is geographic (positions are measured in long/lat) and the second is projected (such as UTM - positions are measured in meters or feet).<p>A Geographic coordinate system contains information on the datum (which implies an spheroid described by a semi-major axis, and inverse flattening), prime meridian(normally Greenwich), and an angular units type which is normally degrees. The following code initializes a geographic coordinate system on supplying all this information along with a user visible name for the geographic coordinate system.<p><div class="fragment"><pre class="fragment"> <a class="code" href="classOGRSpatialReference.html">OGRSpatialReference</a> oSRS; oSRS.<a class="code" href="classOGRSpatialReference.html#1c8d3eaf2751d9912797c75f477549fe">SetGeogCS</a>( <span class="stringliteral">"My geographic coordinate system"</span>, <span class="stringliteral">"WGS_1984"</span>, <span class="stringliteral">"My WGS84 Spheroid"</span>, SRS_WGS84_SEMIMAJOR, SRS_WGS84_INVFLATTENING, <span class="stringliteral">"Greenwich"</span>, 0.0, <span class="stringliteral">"degree"</span>, SRS_UA_DEGREE_CONV );</pre></div><p>Of these values, the names "My geographic coordinate system", "My WGS84 Spheroid", "Greenwich" and "degree" are not keys, but are used for display to the user. However, the datum name "WGS_1984" is used as a key to identify the datum, and there are rules on what values can be used. NOTE: Prepare writeup somewhere on valid datums!<p>The <a class="el" href="classOGRSpatialReference.html">OGRSpatialReference</a> has built in support for a few well known coordinate systems, which include "NAD27", "NAD83", "WGS72" and "WGS84" which can be defined in a single call to SetWellKnownGeogCS().<p><div class="fragment"><pre class="fragment"> oSRS.<a class="code" href="classOGRSpatialReference.html#096b8dde4fd2eb475acd376060940b02">SetWellKnownGeogCS</a>( <span class="stringliteral">"WGS84"</span> );</pre></div><p>Furthermore, any geographic coordinate system in the EPSG database can be set by it's GCS code number if the EPSG database is available.<p><div class="fragment"><pre class="fragment"> oSRS.<a class="code" href="classOGRSpatialReference.html#096b8dde4fd2eb475acd376060940b02">SetWellKnownGeogCS</a>( <span class="stringliteral">"EPSG:4326"</span> );</pre></div><p>For serializization, and transmission of projection definitions to other packages, the OpenGIS Well Known Text format for coordinate systems is used. An <a class="el" href="classOGRSpatialReference.html">OGRSpatialReference</a> can be initialized from well known text, or converted back into well known text.<p><div class="fragment"><pre class="fragment"> <span class="keywordtype">char</span> *pszWKT = NULL; oSRS.<a class="code" href="classOGRSpatialReference.html#096b8dde4fd2eb475acd376060940b02">SetWellKnownGeogCS</a>( <span class="stringliteral">"WGS84"</span> ); oSRS.<a class="code" href="classOGRSpatialReference.html#596b8e527d66a5054408159f0006fdac">exportToWkt</a>( &pszWKT ); printf( <span class="stringliteral">"%s\n"</span>, pszWKT );</pre></div><p>gives something like:<p><pre>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["DMSH",0.0174532925199433,AUTHORITY["EPSG",9108]],AXIS["Lat",NORTH],AXIS["Long",EAST],AUTHORITY["EPSG",4326]]</pre><p>or in more readable form:<p><pre>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["DMSH",0.0174532925199433,AUTHORITY["EPSG",9108]], AXIS["Lat",NORTH], AXIS["Long",EAST], AUTHORITY["EPSG",4326]]</pre><p>The <a class="el" href="classOGRSpatialReference.html#b74cfc985bd05404a4c61d2d633a6343">OGRSpatialReference::importFromWkt()</a> method can be used to set an <a class="el" href="classOGRSpatialReference.html">OGRSpatialReference</a> from a WKT coordinate system definition.<h2><a class="anchor" name="osr_tutorial_proj">Defining a Projected Coordinate System</a></h2>A projected coordinate system (such as UTM, Lambert Conformal Conic, etc) requires and underlying geographic coordinate system as well as a definition for the projection transform used to translate between linear positions (in meters or feet) and angular long/lat positions. The following code defines a UTM zone 17 projected coordinate system with and underlying geographic coordinate system (datum) of WGS84.<p><div class="fragment"><pre class="fragment"> <a class="code" href="classOGRSpatialReference.html">OGRSpatialReference</a> oSRS; oSRS.<a class="code" href="classOGRSpatialReference.html#208118581246feb29646acf2ea5d4237">SetProjCS</a>( <span class="stringliteral">"UTM 17 (WGS84) in northern hemisphere."</span> ); oSRS.<a class="code" href="classOGRSpatialReference.html#096b8dde4fd2eb475acd376060940b02">SetWellKnownGeogCS</a>( <span class="stringliteral">"WGS84"</span> ); oSRS.<a class="code" href="classOGRSpatialReference.html#0c4f6fcab663c9eb6d465787cad3833c">SetUTM</a>( 17, TRUE );</pre></div><p>Calling SetProjCS() sets a user name for the projected coordinate system and establishes that the system is projected. The SetWellKnownGeogCS() associates a geographic coordinate system, and the SetUTM() call sets detailed projection transformation parameters. At this time the above order is important in order to create a valid definition, but in the future the object will automatically reorder the internal representation as needed to remain valid. For now <b>be careful of the order of steps defining an OGRSpatialReference!</b><p>The above definition would give a WKT version that looks something like the following. Note that the UTM 17 was expanded into the details transverse mercator definition of the UTM zone.<p><pre>PROJCS["UTM 17 (WGS84) in northern hemisphere.", 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["DMSH",0.0174532925199433,AUTHORITY["EPSG",9108]], AXIS["Lat",NORTH], AXIS["Long",EAST], AUTHORITY["EPSG",4326]], PROJECTION["Transverse_Mercator"], PARAMETER["latitude_of_origin",0], PARAMETER["central_meridian",-81], PARAMETER["scale_factor",0.9996], PARAMETER["false_easting",500000], PARAMETER["false_northing",0]]</pre><p>There are methods for many projection methods including SetTM() (Transverse Mercator), SetLCC() (Lambert Conformal Conic), and SetMercator().<h2><a class="anchor" name="osr_tutorial_query">Querying Coordinate System</a></h2>Once an <a class="el" href="classOGRSpatialReference.html">OGRSpatialReference</a> has been established, various information about it can be queried. It can be established if it is a projected or geographic coordinate system using the <a class="el" href="classOGRSpatialReference.html#19a25da2b419aba9dd37854ffb62a79f">OGRSpatialReference::IsProjected()</a> and <a class="el" href="classOGRSpatialReference.html#4136ec52866199f8449fcc870e3bc8ce">OGRSpatialReference::IsGeographic()</a> methods. The <a class="el" href="classOGRSpatialReference.html#e4a85fc76fb96e5d253897d0589aea33">OGRSpatialReference::GetSemiMajor()</a>, <a class="el" href="classOGRSpatialReference.html#e004d56063c3dedf18bc3a8c705deacb">OGRSpatialReference::GetSemiMinor()</a> and <a class="el" href="classOGRSpatialReference.html#b8e5f09148eb80ea4c8242d30591ba32">OGRSpatialReference::GetInvFlattening()</a> methods can be used to get information about the spheroid. The <a class="el" href="classOGRSpatialReference.html#cc815e7bd21c1c052304368c0eefe8be">OGRSpatialReference::GetAttrValue()</a> method can be used to get the PROJCS, GEOGCS, DATUM, SPHEROID, and PROJECTION names strings. The <a class="el" href="classOGRSpatialReference.html#603c654bc0616f26c9300b3c66952cdb">OGRSpatialReference::GetProjParm()</a> method can be used to get the projection parameters. The <a class="el" href="classOGRSpatialReference.html#f2821ab555a3bb2d6bb1df49d076a198">OGRSpatialReference::GetLinearUnits()</a> method can be used to fetch the linear units type, and translation to meters.<p>The following code (from ogr_srs_proj4.cpp) demonstrates use of GetAttrValue() to get the projection, and GetProjParm() to get projection parameters. The GetAttrValue() method searches for the first "value" node associated with the named entry in the WKT text representation. The define'ed constants for projection parameters (such as SRS_PP_CENTRAL_MERIDIAN) should be used when fetching projection parameter with GetProjParm(). The code for the Set methods of the various projections in ogrspatialreference.cpp can be consulted to find which parameters apply to which projections.<p><div class="fragment"><pre class="fragment"> <span class="keyword">const</span> <span class="keywordtype">char</span> *pszProjection = poSRS->GetAttrValue(<span class="stringliteral">"PROJECTION"</span>); <span class="keywordflow">if</span>( pszProjection == NULL ) { <span class="keywordflow">if</span>( poSRS->IsGeographic() ) sprintf( szProj4+strlen(szProj4), <span class="stringliteral">"+proj=longlat "</span> ); <span class="keywordflow">else</span> sprintf( szProj4+strlen(szProj4), <span class="stringliteral">"unknown "</span> ); } <span class="keywordflow">else</span> <span class="keywordflow">if</span>( EQUAL(pszProjection,SRS_PT_CYLINDRICAL_EQUAL_AREA) ) { sprintf( szProj4+strlen(szProj4), <span class="stringliteral">"+proj=cea +lon_0=%.9f +lat_ts=%.9f +x_0=%.3f +y_0=%.3f "</span>, poSRS->GetProjParm(SRS_PP_CENTRAL_MERIDIAN,0.0), poSRS->GetProjParm(SRS_PP_STANDARD_PARALLEL_1,0.0), poSRS->GetProjParm(SRS_PP_FALSE_EASTING,0.0), poSRS->GetProjParm(SRS_PP_FALSE_NORTHING,0.0) ); } ...</pre></div><h2><a class="anchor" name="osr_tutorial_transform">Coordinate Transformation</a></h2>The <a class="el" href="classOGRCoordinateTransformation.html">OGRCoordinateTransformation</a> class is used for translating positions between different coordinate systems. New transformation objects are created using <a class="el" href="ogr__spatialref_8h.html#ae11bd08e45cdb2e71e1d9c31f1e550f">OGRCreateCoordinateTransformation()</a>, and then the <a class="el" href="classOGRCoordinateTransformation.html#bf141396940b4f7b1f8118ea025d569b">OGRCoordinateTransformation::Transform()</a> method can be used to convert points between coordinate systems.<p><div class="fragment"><pre class="fragment"> <a class="code" href="classOGRSpatialReference.html">OGRSpatialReference</a> oSourceSRS, oTargetSRS; <a class="code" href="classOGRCoordinateTransformation.html">OGRCoordinateTransformation</a> *poCT; <span class="keywordtype">double</span> x, y; oSourceSRS.<a class="code" href="classOGRSpatialReference.html#8a5b8c9a205eedc6b88a14aa0c219969">importFromEPSG</a>( atoi(papszArgv[i+1]) ); oTargetSRS.<a class="code" href="classOGRSpatialReference.html#8a5b8c9a205eedc6b88a14aa0c219969">importFromEPSG</a>( atoi(papszArgv[i+2]) ); poCT = <a class="code" href="ogr__spatialref_8h.html#ae11bd08e45cdb2e71e1d9c31f1e550f">OGRCreateCoordinateTransformation</a>( &oSourceSRS, &oTargetSRS ); x = atof( papszArgv[i+3] );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -