I have answered the question and also changed the title of the question from How to access vertices in a polygon layer using ogr library with c++ to How to extract vertexes of geometries in ESRI shapefiles using ogr library with c++. The code can be used to show shapes with OpenGL.
Any suggestion to make it better will be appreciated.
For example do you think that I should use projection of the layer in order to enhance the view in OpenGL? How can I use the projection and apply it to the OpenGL window?
up to now I have tried these classes in order to extract the vertices of polygons:
- OGRPolygon : It doesn't have any method to get number of vertices, start point and end point.
- OGRLineString : when I use
getNumPoints()
method, I get 1 for all of the polygon features in my layer. - OGRLinearRing : when I use
getNumPoints()
method, I get 1 for all of the polygon features in my layer.
what should I do to get the number of vertices and coordinates of vertices in a polygon shapefile in order to draw the polygon in an OpenGL window.
I found the answer:
You should care some information about ESRI shapefiles.
Point layer : Can have only features of type wkbpoint. So you can define a data structure named MyPoint2D and store the coordinates of this layer in a vector with members of type MyPoint2D.
Multipoint layer : Can have only features of type wkbMultipoint. So you can store each feature in a vector with members of type MyPoint2D and a collection of such vectors will hold the coordinates of all of the features in the layer.
Polyline layer : Can have features of type wkbLineString and wkbMultiLineString. So you can have a data structure MyLine2D, which is a vector with MyPoint2D members and a vector with members of type MyLine2D will hold the LineFeature and finally the features in the layer will be stored in a vector with members of type LineFeature.
Polygon Layer : About the polygons you should be aware that each polygon is consisted of an Exterior ring and several Interior rings ( this was my real problem which caused me to ask the question ). And also the layer can have features of type wkbPolygon and wkbMultiPolygon.
So at first we should define these data structures:
And this is the whole code, I have used in order to extract the coordinates of vertexes in ESRI's shapefiles: