Search code examples
c++linuxopenglgdal

using GDAL/OGR api to read vector data (shapefile)--How?


I am working on an application that involves some gis stuff. There would be some .shp files to be read and plotted onto an opengl screen. The current opengl screen is using the orthographic projection as set from glOrtho() and is already displaying a map using coordinates from a simple text file..

Now the map to be plotted is to be read from a shapefile.

I have the following doubts:

  1. How to use the WGS84 projection of the .shp file(as read from the .prj file of the shapefile,WKT format) into my existing glOrtho projection..is there any conversion that needs to be done? and how is it different from what the glOrtho() sets up?basically how to use this information?

  2. My application needs to be setup in such a way that i can know the exact lat/long of a point on the map.for eg. if i am hovering on X city,its correct lat/long could be fetched.I know that this can be done by using opensource utils/apis like GDAL/OGR but i am messed up as the documentation of these apis are not getting into my head. I tried to find some sample c++ progs but couldnt find one.

  3. I have already written my own logic to read the coordinates from a shapefile containing either points/polyline/polygon(using C-shapelib) and plotted over my opengl screen.I found a OGR sample code in doc to read a POINTS shapefile but none for POLYGON shapefile.And the problem is that this application has to be so dynamic that upon loading the shapefile,it should correctly setup the projection of the opengl screen depending upon the projection of the .shp file being read..eg WGS84,LCC,EVEREST MODIFIED...etc. how to achieve this from OGR api?

Kindly give your inputs on this problem.. I am really keen to make this work but im not getting the right start..


Solution

    1. Shapefile rendering is quite straight forward in OpenGL. You may require "shapelib",a free shapefile parsing library in C(Google it). Use GL_POINTS for point shapefile, GL_LINES for line shapefile and GL_LINE_LOOP for polygon shapefile. Set your bounding box coords to the Ortho.

    2. What you read from the .prj file is projection info. WGS84 gives you lat/long coords(Spherical). But your display system is 2D(Rectangular). So, you need to convert 3D Spherical coords to 2D Rectangular coords(This is the meaning of Projection).Projection types are numerous,depending on the area of interest on the globe(remember projection distorts area/shape/size of features).Projection types range from Polyconic, Modified Everest, NAD, UTM, etc.,

    3. If you simply need WGS84 ,then read bounding box coords of your .sh file and assign them to glOrtho. If you have any projection(eg:-UTM), then you convert your bounding box coords into Projection coords and then assign the newly projected coords to glOrtho. For converting lat/long into any Projection, you may require projection libraries like "Projlib" or "GeotransEngine" and etc.

    For further clarifications you may contact me on dgplinux@ y a h o o . c o m