Question

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..

Was it helpful?

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

OTHER TIPS

Please, read the OGR API Tutorial where you can learn how to read vector data from sources like Shapefile. Next, check the OGR Projections Tutorial where you can learn about how to use information about projection and spatial reference system read from OGR sources.

GDAL/OGR has everything you need to load a vector file, then convert any coordinates. I understand your frustration with GDAL as the documentation is not the greatest. If you want a good intro to using the API, look at gdalinfo.c and ogrinfo.cpp in the GDAL subversion tree. Source can be seen at https://svn.osgeo.org/gdal/trunk/gdal.

If that does not help, I have two basic examples that I use to parse vector info and do coordinate conversion. They are really bad, but they may help get the point.

Vector Loading

Coordinate Conversion

Finally, if you are not familiar with GIS formats, I would consider reading the ArcGIS introduction here under Guide Books/Map Projections. I can compete with experts despite no cartography training due to these guides. Another good source is Wikipedia.

When in doubt, just pick a UTM grid you want to stick with and use UTM as your coordinate system. It uses X (Easting), Y(Northing), and Z(Altitude). The only key is picking a single UTM Grid and making sure all coordinates use that as a reference. UTM is easy to test code with as there are a lot of guides online. You also can find conversion code using OGR/GDAL or other resources. Other projected coordinate systems are worthwhile and may be better, but I would look at that to start with.

Finally, if all else fails, take a look at the NGA GeoTrans. That is a great testing tool.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top