[gdal-dev] Create a shape file with polygons
Jorge Martin
jormarfe at gmail.com
Tue Jan 18 11:28:26 EST 2011
Hello,
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.
Here I paste the code:
int write_shapefilePolygon() {
const char *pszDriverName = "ESRI Shapefile";
OGRSFDriver *poDriver;
OGRRegisterAll();
poDriver =
OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
if( poDriver == NULL )
{
printf( "%s driver not available.\n", pszDriverName );
exit( 1 );
}
OGRDataSource *poDS;
poDS = poDriver->CreateDataSource( "Polygon.shp", NULL );
if( poDS == NULL )
{
printf( "Creation of output file failed.\n" );
exit( 1 );
}
OGRLayer *poLayer;
poLayer = poDS->CreateLayer( "countries", NULL, wkbPolygon, NULL );
if( poLayer == NULL )
{
printf( "Layer creation failed.\n" );
exit( 1 );
}
OGRFieldDefn oField( "Name", OFTString );
oField.SetWidth(32);
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
printf( "Creating Name field failed.\n" );
exit( 1 );
}
double x, y;
string szName;
char Line[4000];
char *tmp;
ifstream infile ("/home/jjmf/Desktop/Shape
files/polygon/ospy_data2/ut_counties.txt", ifstream::in);
while(infile.getline(Line,4000))
{
OGRPolygon myPoligon;
tmp = strtok (Line, ":");
szName = string (tmp);
while (tmp != NULL)
{
OGRLinearRing MyRing;// = (OGRLinearRing*)
OGRGeometryFactory::createGeometry(wkbLinearRing);
tmp = strtok (NULL, ",");
if(tmp)
{
string Coords = string(tmp);
size_t pos = Coords.find(" ");
string CoordX = Coords.substr(0,pos);
string CoordY = Coords.substr(pos);
x = atof(CoordX.c_str());
y = atof(CoordY.c_str());
MyRing.addPoint(x,y);
}
myPoligon.addRing(&MyRing);
}
OGRFeature *poFeature;
poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
poFeature->SetField( "Name", szName.c_str() );
poFeature->SetGeometry(&myPoligon);
if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
{
printf( "Failed to create feature in shapefile.\n" );
exit( 1 );
}
OGRFeature::DestroyFeature( poFeature );
}
OGRDataSource::DestroyDataSource( poDS );
infile.close();
exit( 0 );
}
Thanks,
Jorge
2011/1/17 Jorge <jormarfe at gmail.com>
> Hi,
>
> Thank you very much in advance. I am developing with C++, but I will try to
> understand the code. I think it will easy.
>
> Thank you.
>
> Jorge
>
> El 17/01/2011, a las 18:18, Paolo Corti <pcorti at gmail.com> escribió:
>
> >> I`m new developing software with GDAL and OGR libraries. I has used the
> code
> >> from the OGR web that read and write a shapefile (points). What I really
> >> want is to read and write shapefiles with polygons.
> >
> > Hi Jorge
> > if you are using Python this [0] is the best resource out there.
> > Look at the sample in week 2 of the tutorial for effectively writing
> > (and reading) a Polygon shapefile.
> > If you are not using Python you should not have too much problems for
> > converting the Python code to the language you are using.
> > Another very good resource is to look at the source code: again,
> > supposing that you are using Python, this sample [1] should work for
> > you
> >
> > ciao
> > P
> >
> > [0] http://www.gis.usu.edu/~chrisg/python/2009/
> > [1]
> http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/samples/assemblepoly.py
> >
> > --
> > Paolo Corti
> > GIS specialist and web developer
> > web: http://www.paolocorti.net
> > twitter: @paolo_corti
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20110118/af84b322/attachment-0001.html
More information about the gdal-dev
mailing list