⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ogr_apitut.html

📁 gdal库的学习文档
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    <a class="code" href="ogr__api_8h.html#e904632d0bc86ba5501921ca594e18da">OGRRegisterAll</a>();    poDriver = <a class="code" href="classOGRSFDriverRegistrar.html#fbd5602672ac5f1882055cc459375f8e">OGRSFDriverRegistrar::GetRegistrar</a>()-&gt;<a class="code" href="classOGRSFDriverRegistrar.html#d214c51c2e38d486388f77fb9314143c">GetDriverByName</a>(                pszDriverName );    <span class="keywordflow">if</span>( poDriver == NULL )    {        printf( <span class="stringliteral">"%s driver not available.\n"</span>, pszDriverName );        exit( 1 );    }</pre></div><p>Next we create the datasource. The ESRI Shapefile driver allows us to create a directory full of shapefiles, or a single shapefile as a datasource. In this case we will explicitly create a single file by including the extension in the name. Other drivers behave differently. The second argument to the call is a list of option values, but we will just be using defaults in this case. Details of the options supported are also format specific.<p><div class="fragment"><pre class="fragment">    <a class="code" href="classOGRDataSource.html">OGRDataSource</a> *poDS;    poDS = poDriver-&gt;<a class="code" href="classOGRSFDriver.html#4339101b2d0851e788b6bcfd248780f6">CreateDataSource</a>( <span class="stringliteral">"point_out.shp"</span>, NULL );    <span class="keywordflow">if</span>( poDS == NULL )    {        printf( <span class="stringliteral">"Creation of output file failed.\n"</span> );        exit( 1 );    }</pre></div><p>Now we create the output layer. In this case since the datasource is a single file, we can only have one layer. We pass wkbPoint to specify the type of geometry supported by this layer. In this case we aren't passing any coordinate system information or other special layer creation options.<p><div class="fragment"><pre class="fragment">    <a class="code" href="classOGRLayer.html">OGRLayer</a> *poLayer;    poLayer = poDS-&gt;<a class="code" href="classOGRDataSource.html#39cfc6e0ee790506d7638b0dce03c7da">CreateLayer</a>( <span class="stringliteral">"point_out"</span>, NULL, wkbPoint, NULL );    <span class="keywordflow">if</span>( poLayer == NULL )    {        printf( <span class="stringliteral">"Layer creation failed.\n"</span> );        exit( 1 );    }</pre></div><p>Now that the layer exists, we need to create any attribute fields that should appear on the layer. Fields must be added to the layer before any features are written. To create a field we initialize an <a class="el" href="unionOGRField.html">OGRField</a> object with the information about the field. In the case of Shapefiles, the field width and precision is significant in the creation of the output .dbf file, so we set it specifically, though generally the defaults are OK. For this example we will just have one attribute, a name string associated with the x,y point.<p>Note that the template <a class="el" href="unionOGRField.html">OGRField</a> we pass to CreateField() is copied internally. We retain ownership of the object.<p><div class="fragment"><pre class="fragment">    <a class="code" href="classOGRFieldDefn.html">OGRFieldDefn</a> oField( <span class="stringliteral">"Name"</span>, OFTString );    oField.SetWidth(32);    <span class="keywordflow">if</span>( poLayer-&gt;<a class="code" href="classOGRLayer.html#00b1376a1eabb1298ef278f92f6d84be">CreateField</a>( &amp;oField ) != OGRERR_NONE )    {        printf( <span class="stringliteral">"Creating Name field failed.\n"</span> );        exit( 1 );    }\encodeThe following snipping loops reading lines of the form <span class="stringliteral">"x,y,name"</span> from stdin, and parsing them.  \code    <span class="keywordtype">double</span> x, y;    <span class="keywordtype">char</span> szName[33];    <span class="keywordflow">while</span>( !feof(stdin)            &amp;&amp; fscanf( stdin, <span class="stringliteral">"%lf,%lf,%32s"</span>, &amp;x, &amp;y, szName ) == 3 )    {</pre></div><p>To write a feature to disk, we must create a local <a class="el" href="classOGRFeature.html">OGRFeature</a>, set attributes and attach geometry before trying to write it to the layer. It is imperative that this feature be instantiated from the <a class="el" href="classOGRFeatureDefn.html">OGRFeatureDefn</a> associated with the layer it will be written to.<p><div class="fragment"><pre class="fragment">        <a class="code" href="classOGRFeature.html">OGRFeature</a> *poFeature;        poFeature = <span class="keyword">new</span> <a class="code" href="classOGRFeature.html">OGRFeature</a>( poLayer-&gt;<a class="code" href="classOGRLayer.html#80473bcfd11341e70dd35bebe94026cf">GetLayerDefn</a>() );        poFeature-&gt;<a class="code" href="classOGRFeature.html#4abbe118cc2f3e48bbac7f710b71b531">SetField</a>( <span class="stringliteral">"Name"</span>, szName );</pre></div><p>We create a local geometry object, and assign its copy (indirectly) to the feature. The <a class="el" href="classOGRFeature.html#40a4ddb33f09a5f340b9139be72e277d">OGRFeature::SetGeometryDirectly()</a> differs from <a class="el" href="classOGRFeature.html#f1181ade837a52129ea25b46dd50cf30">OGRFeature::SetGeometry()</a> in that the direct method gives ownership of the geometry to the feature. This is generally more efficient as it avoids an extra deep object copy of the geometry.<p><div class="fragment"><pre class="fragment">        <a class="code" href="classOGRPoint.html">OGRPoint</a> pt;        pt.<a class="code" href="classOGRPoint.html#0eef5e2d9e0edeca0e0f06c7527facb4">setX</a>( x );        pt.<a class="code" href="classOGRPoint.html#b064eaaedb2140ed1410e7c2ce4ad1ca">setY</a>( y );                poFeature-&gt;<a class="code" href="classOGRFeature.html#f1181ade837a52129ea25b46dd50cf30">SetGeometry</a>( &amp;pt ); </pre></div><p>Now we create a feature in the file. The <a class="el" href="classOGRLayer.html#aa1e32016f481596a55e1d988a15a067">OGRLayer::CreateFeature()</a> does not take ownership of our feature so we clean it up when done with it.<p><div class="fragment"><pre class="fragment">        <span class="keywordflow">if</span>( poLayer-&gt;<a class="code" href="classOGRLayer.html#aa1e32016f481596a55e1d988a15a067">CreateFeature</a>( poFeature ) != OGRERR_NONE )        {           printf( <span class="stringliteral">"Failed to create feature in shapefile.\n"</span> );           exit( 1 );        }                <a class="code" href="classOGRFeature.html#5d2602d11f21567119da0ca6b6c5ad45">OGRFeature::DestroyFeature</a>( poFeature );   }</pre></div><p>Finally we need to close down the datasource in order to ensure headers are written out in an orderly way and all resources are recovered.<p><div class="fragment"><pre class="fragment">    OGRDataSource::DestroyDataSource( poDS );}</pre></div><p>The same program all in one block looks like this:<p><div class="fragment"><pre class="fragment"><span class="preprocessor">#include "<a class="code" href="ogrsf__frmts_8h.html">ogrsf_frmts.h</a>"</span><span class="keywordtype">int</span> main(){    <span class="keyword">const</span> <span class="keywordtype">char</span> *pszDriverName = <span class="stringliteral">"ESRI Shapefile"</span>;    <a class="code" href="classOGRSFDriver.html">OGRSFDriver</a> *poDriver;    <a class="code" href="ogr__api_8h.html#e904632d0bc86ba5501921ca594e18da">OGRRegisterAll</a>();    poDriver = <a class="code" href="classOGRSFDriverRegistrar.html#fbd5602672ac5f1882055cc459375f8e">OGRSFDriverRegistrar::GetRegistrar</a>()-&gt;<a class="code" href="classOGRSFDriverRegistrar.html#d214c51c2e38d486388f77fb9314143c">GetDriverByName</a>(                pszDriverName );    <span class="keywordflow">if</span>( poDriver == NULL )    {        printf( <span class="stringliteral">"%s driver not available.\n"</span>, pszDriverName );        exit( 1 );    }    <a class="code" href="classOGRDataSource.html">OGRDataSource</a> *poDS;    poDS = poDriver-&gt;<a class="code" href="classOGRSFDriver.html#4339101b2d0851e788b6bcfd248780f6">CreateDataSource</a>( <span class="stringliteral">"point_out.shp"</span>, NULL );    <span class="keywordflow">if</span>( poDS == NULL )    {        printf( <span class="stringliteral">"Creation of output file failed.\n"</span> );        exit( 1 );    }    <a class="code" href="classOGRLayer.html">OGRLayer</a> *poLayer;    poLayer = poDS-&gt;<a class="code" href="classOGRDataSource.html#39cfc6e0ee790506d7638b0dce03c7da">CreateLayer</a>( <span class="stringliteral">"point_out"</span>, NULL, wkbPoint, NULL );    <span class="keywordflow">if</span>( poLayer == NULL )    {        printf( <span class="stringliteral">"Layer creation failed.\n"</span> );        exit( 1 );    }    <a class="code" href="classOGRFieldDefn.html">OGRFieldDefn</a> oField( <span class="stringliteral">"Name"</span>, OFTString );    oField.SetWidth(32);    <span class="keywordflow">if</span>( poLayer-&gt;<a class="code" href="classOGRLayer.html#00b1376a1eabb1298ef278f92f6d84be">CreateField</a>( &amp;oField ) != OGRERR_NONE )    {        printf( <span class="stringliteral">"Creating Name field failed.\n"</span> );        exit( 1 );    }    <span class="keywordtype">double</span> x, y;    <span class="keywordtype">char</span> szName[33];    <span class="keywordflow">while</span>( !feof(stdin)            &amp;&amp; fscanf( stdin, <span class="stringliteral">"%lf,%lf,%32s"</span>, &amp;x, &amp;y, szName ) == 3 )    {        <a class="code" href="classOGRFeature.html">OGRFeature</a> *poFeature;        poFeature = <span class="keyword">new</span> <a class="code" href="classOGRFeature.html">OGRFeature</a>( poLayer-&gt;<a class="code" href="classOGRLayer.html#80473bcfd11341e70dd35bebe94026cf">GetLayerDefn</a>() );        poFeature-&gt;<a class="code" href="classOGRFeature.html#4abbe118cc2f3e48bbac7f710b71b531">SetField</a>( <span class="stringliteral">"Name"</span>, szName );        <a class="code" href="classOGRPoint.html">OGRPoint</a> pt;                pt.<a class="code" href="classOGRPoint.html#0eef5e2d9e0edeca0e0f06c7527facb4">setX</a>( x );        pt.<a class="code" href="classOGRPoint.html#b064eaaedb2140ed1410e7c2ce4ad1ca">setY</a>( y );         poFeature-&gt;<a class="code" href="classOGRFeature.html#f1181ade837a52129ea25b46dd50cf30">SetGeometry</a>( &amp;pt );         <span class="keywordflow">if</span>( poLayer-&gt;<a class="code" href="classOGRLayer.html#aa1e32016f481596a55e1d988a15a067">CreateFeature</a>( poFeature ) != OGRERR_NONE )        {           printf( <span class="stringliteral">"Failed to create feature in shapefile.\n"</span> );           exit( 1 );        }         <a class="code" href="classOGRFeature.html#5d2602d11f21567119da0ca6b6c5ad45">OGRFeature::DestroyFeature</a>( poFeature );    }    OGRDataSource::DestroyDataSource( poDS );}</pre></div> <hr>Generated for GDAL by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.1.</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -