<br><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">Hello,<div><br></div><div>I'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 = "ESRI Shapefile";</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()->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( "%s driver not available.\n", 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->CreateDataSource( "Polygon.shp", 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( "Creation of output file failed.\n" );</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->CreateLayer( "countries", 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( "Layer creation failed.\n" );</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( "Name", 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->CreateField( &oField ) != OGRERR_NONE )</div>
<div> {</div><div><span style="white-space: pre-wrap; ">                </span>printf( "Creating Name field failed.\n" );</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 ("/home/jjmf/Desktop/Shape files/polygon/ospy_data2/ut_counties.txt", 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, ":");</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, ",");</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(" ");</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(&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->GetLayerDefn() );</div><div><br></div><div><br>
</div><div> poFeature->SetField( "Name", szName.c_str() );</div><div><br></div><div><br></div><div> poFeature->SetGeometry(&myPoligon);</div><div><br></div><div><br></div><div> if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )</div>
<div> {</div><div><span style="white-space: pre-wrap; ">        </span> printf( "Failed to create feature in shapefile.\n" );</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"><<a href="mailto:jormarfe@gmail.com">jormarfe@gmail.com</a>></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 <<a href="mailto:pcorti@gmail.com">pcorti@gmail.com</a>> escribió:<br>
<div><div></div><div class="h5"><br>
>> I`m new developing software with GDAL and OGR libraries. I has used the code<br>
>> from the OGR web that read and write a shapefile (points). What I really<br>
>> want is to read and write shapefiles with polygons.<br>
><br>
> Hi Jorge<br>
> if you are using Python this [0] is the best resource out there.<br>
> Look at the sample in week 2 of the tutorial for effectively writing<br>
> (and reading) a Polygon shapefile.<br>
> If you are not using Python you should not have too much problems for<br>
> converting the Python code to the language you are using.<br>
> Another very good resource is to look at the source code: again,<br>
> supposing that you are using Python, this sample [1] should work for<br>
> you<br>
><br>
> ciao<br>
> P<br>
><br>
> [0] <a href="http://www.gis.usu.edu/~chrisg/python/2009/" target="_blank">http://www.gis.usu.edu/~chrisg/python/2009/</a><br>
> [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>
><br>
> --<br>
> Paolo Corti<br>
> GIS specialist and web developer<br>
> web: <a href="http://www.paolocorti.net" target="_blank">http://www.paolocorti.net</a><br>
> twitter: @paolo_corti<br>
</div></div></blockquote></div><br>