<br><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">Hello,<div><br></div><div>I&#39;ve tried to develope the program that create a shape file with polygons. But the GIS open the shp but there is nothing inside only the attributes, not the polygons. I have the code from Python but I do not know the problem.</div>
<div><br></div><div>Here I paste the code:</div><div><br></div><div><div>int write_shapefilePolygon() {</div><div><br></div><div><br></div><div><span style="white-space: pre-wrap; ">        </span>const char *pszDriverName = &quot;ESRI Shapefile&quot;;</div>
<div><span style="white-space: pre-wrap; ">        </span>OGRSFDriver *poDriver;</div><div><br></div><div><span style="white-space: pre-wrap; ">        </span>OGRRegisterAll();</div><div><br></div><div><span style="white-space: pre-wrap; ">        </span>poDriver = OGRSFDriverRegistrar::GetRegistrar()-&gt;GetDriverByName(pszDriverName);</div>
<div><br></div><div><span style="white-space: pre-wrap; ">        </span>if( poDriver == NULL )</div><div><span style="white-space: pre-wrap; ">        </span>{</div><div><span style="white-space: pre-wrap; ">                </span> printf( &quot;%s driver not available.\n&quot;, pszDriverName );</div>
<div><span style="white-space: pre-wrap; ">                </span>exit( 1 );</div><div><span style="white-space: pre-wrap; ">        </span> }</div><div><br></div><div><span style="white-space: pre-wrap; ">        </span>OGRDataSource *poDS;</div><div>
<br></div><div><span style="white-space: pre-wrap; ">        </span>poDS = poDriver-&gt;CreateDataSource( &quot;Polygon.shp&quot;, NULL );</div><div><span style="white-space: pre-wrap; ">                        </span>    </div><div>       if( poDS == NULL )</div>
<div><span style="white-space: pre-wrap; ">        </span>{</div><div><span style="white-space: pre-wrap; ">                </span> printf( &quot;Creation of output file failed.\n&quot; );</div><div><span style="white-space: pre-wrap; ">                </span>exit( 1 );</div>
<div><span style="white-space: pre-wrap; ">        </span> }</div><div><br></div><div><span style="white-space: pre-wrap; ">        </span>OGRLayer *poLayer;</div><div><br></div><div><span style="white-space: pre-wrap; ">        </span> poLayer = poDS-&gt;CreateLayer( &quot;countries&quot;, NULL, wkbPolygon, NULL );</div>
<div><span style="white-space: pre-wrap; ">        </span> </div><div>          if( poLayer == NULL )</div><div><span style="white-space: pre-wrap; ">        </span> {</div><div><span style="white-space: pre-wrap; ">                </span>printf( &quot;Layer creation failed.\n&quot; );</div>
<div><span style="white-space: pre-wrap; ">                </span>exit( 1 );</div><div><span style="white-space: pre-wrap; ">        </span>}</div><div><br></div><div><span style="white-space: pre-wrap; ">        </span>OGRFieldDefn oField( &quot;Name&quot;, OFTString );</div>
<div><br></div><div><span style="white-space: pre-wrap; ">        </span>oField.SetWidth(32);</div><div><br></div><div><span style="white-space: pre-wrap; ">        </span> if( poLayer-&gt;CreateField( &amp;oField ) != OGRERR_NONE )</div>
<div>        {</div><div><span style="white-space: pre-wrap; ">                </span>printf( &quot;Creating Name field failed.\n&quot; );</div><div><span style="white-space: pre-wrap; ">                </span> exit( 1 );</div><div><span style="white-space: pre-wrap; ">        </span>}</div>
<div><br></div><div><span style="white-space: pre-wrap; ">        </span>double x, y;</div><div><span style="white-space: pre-wrap; ">        </span>string szName;</div><div><br></div><div><br></div><div><span style="white-space: pre-wrap; ">        </span> char Line[4000];</div>
<div><span style="white-space: pre-wrap; ">        </span>char *tmp;</div><div><br></div><div><br></div><div><span style="white-space: pre-wrap; ">        </span> ifstream infile (&quot;/home/jjmf/Desktop/Shape files/polygon/ospy_data2/ut_counties.txt&quot;, ifstream::in);</div>
<div><br></div><div><span style="white-space: pre-wrap; ">        </span>while(infile.getline(Line,4000))</div><div><span style="white-space: pre-wrap; ">        </span> {</div><div><br></div><div><span style="white-space: pre-wrap; ">        </span></div>
<div><span style="white-space: pre-wrap; ">        </span>OGRPolygon myPoligon;</div><div><br></div><div><span style="white-space: pre-wrap; ">        </span>tmp = strtok (Line, &quot;:&quot;);</div><div><br></div><div><span style="white-space: pre-wrap; ">        </span>szName = string (tmp);</div>
<div><br></div><div><br></div><div>       while (tmp != NULL)</div><div>           {</div><div><br></div><div>                    <span style="white-space: pre-wrap; ">        </span></div><div>       OGRLinearRing MyRing;// = (OGRLinearRing*) OGRGeometryFactory::createGeometry(wkbLinearRing);</div>
<div><br></div><div>        tmp = strtok (NULL, &quot;,&quot;);</div><div><br></div><div>        if(tmp)</div><div>        {</div><div><br></div><div>               string Coords = string(tmp);</div><div><br></div><div>              size_t pos = Coords.find(&quot; &quot;);</div>
<div><br></div><div>              string CoordX = Coords.substr(0,pos);</div><div>              string CoordY = Coords.substr(pos);</div><div><br></div><div>              x = atof(CoordX.c_str());</div><div>               y = atof(CoordY.c_str());</div>
<div><br></div><div><span style="white-space: pre-wrap; ">                </span>MyRing.addPoint(x,y);</div><div><br></div><div><br></div><div>        }</div><div><br></div><div>        myPoligon.addRing(&amp;MyRing);</div><div><br></div>
<div>    }</div><div><br></div><div>    OGRFeature *poFeature;</div><div><br></div><div><br></div><div><br></div><div>      poFeature = OGRFeature::CreateFeature( poLayer-&gt;GetLayerDefn() );</div><div><br></div><div><br>
</div><div>       poFeature-&gt;SetField( &quot;Name&quot;, szName.c_str() );</div><div><br></div><div><br></div><div>      poFeature-&gt;SetGeometry(&amp;myPoligon);</div><div><br></div><div><br></div><div>       if( poLayer-&gt;CreateFeature( poFeature ) != OGRERR_NONE )</div>
<div>       {</div><div><span style="white-space: pre-wrap; ">        </span>     printf( &quot;Failed to create feature in shapefile.\n&quot; );</div><div><span style="white-space: pre-wrap; ">        </span>     exit( 1 );</div><div>
        }</div><div><br></div><div>        OGRFeature::DestroyFeature( poFeature );</div><div>  }</div><div><br></div><div>OGRDataSource::DestroyDataSource( poDS );</div><div><br></div><div><br></div><div><br></div><div> infile.close();</div>
<div><br></div><div> exit( 0 );</div><div><br></div><div><br></div><div><br></div><div>}</div></div><div><br></div><div><br></div><div><br></div><div><br></div><div>Thanks,</div><div><br></div><div>Jorge</div></span><div class="gmail_quote">
2011/1/17 Jorge <span dir="ltr">&lt;<a href="mailto:jormarfe@gmail.com">jormarfe@gmail.com</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi,<br>
<br>
Thank you very much in advance. I am developing with C++, but I will try to understand the code. I think it will easy.<br>
<br>
Thank you.<br>
<br>
Jorge<br>
<br>
El 17/01/2011, a las 18:18, Paolo Corti &lt;<a href="mailto:pcorti@gmail.com">pcorti@gmail.com</a>&gt; escribió:<br>
<div><div></div><div class="h5"><br>
&gt;&gt; I`m new developing software with GDAL and OGR libraries. I has used the code<br>
&gt;&gt; from the OGR web that read and write a shapefile (points). What I really<br>
&gt;&gt; want is to read and write shapefiles with polygons.<br>
&gt;<br>
&gt; Hi Jorge<br>
&gt; if you are using Python this [0] is the best resource out there.<br>
&gt; Look at the sample in week 2 of the tutorial for effectively writing<br>
&gt; (and reading) a Polygon shapefile.<br>
&gt; If you are not using Python you should not have too much problems for<br>
&gt; converting the Python code to the language you are using.<br>
&gt; Another very good resource is to look at the source code: again,<br>
&gt; supposing that you are using Python, this sample [1] should work for<br>
&gt; you<br>
&gt;<br>
&gt; ciao<br>
&gt; P<br>
&gt;<br>
&gt; [0] <a href="http://www.gis.usu.edu/~chrisg/python/2009/" target="_blank">http://www.gis.usu.edu/~chrisg/python/2009/</a><br>
&gt; [1] <a href="http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/samples/assemblepoly.py" target="_blank">http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/samples/assemblepoly.py</a><br>
&gt;<br>
&gt; --<br>
&gt; Paolo Corti<br>
&gt; GIS specialist and web developer<br>
&gt; web: <a href="http://www.paolocorti.net" target="_blank">http://www.paolocorti.net</a><br>
&gt; twitter: @paolo_corti<br>
</div></div></blockquote></div><br>