Click here to Skip to main content
13,351,457 members (44,911 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 29 Dec 2008

Rendering Shapefile in OpenGL

, 29 Dec 2008
Rate this:
Please Sign up or sign in to vote.
Rendering ESRI Shapefiles(.shp) using OpenGL


ESRI shapefiles are well known vector data formats used for Mapping a.k.a GIS applications. There are many open source softwares like JUMP, SharpMap that allow the users to view the shapefiles. This article focuses on rendering them in OpenGL console.


Shapefiles hold geographic information in the form of points, lines and polygons. For instance- political boundaries like Countries, States are treated as Polygon shapefiles; linear features such as roads, rivers are Line shapefiles and; Airports or BusBays are sometimes treated as point shapefiles.

Using the Code

I assume that the intended audience is familiar with OpenGL and understands concepts like linking with static or dynamic libraries in Microsoft Visual Studio environment.

To parse the shapefile, I have used the Shapelib ( Include the shapefile.h at the beginning. Make use of the shapelib.lib by adding shapelib\shapelib.lib to object/library Modules by going to Project>Settings>Link.

#include "shapelib\shapefil.h"

Call the following function to Open Shapefile by passing the .shp file name. The function parses the geographic coordinate information and stores them in the Vectors vPoints, vLines and vPolygons.

//Function to Open Shapefile and parse the info
void OpenShapeFile(char* fileName)

SBoundingBox is a structure defined to hold the bounding box of the shapefile. Bounding box coordinates are read in OpenShapeFile function and assigned as glOrtho() parameters, which would restrict the limits of display to the rectangle enveloping the area of interest.

//Assign Bounding Box Coordinates of Shapefile to glOrtho()
glOrtho(sBoundingBox.fMinX, sBoundingBox.fMaxX,

Finally, it is just a matter of setting appropriate OpenGL primitives(GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP) to render Point, Line or Polygon type shapefile.

void draw()
  //Render Point Shapefile glColor3f (0.0, 0.0, 1.0);
   glEnable(GL_POINT_SMOOTH) ; 
   for(int i=0;i < vPoints.size();i++)  
	//Render Line Shapefile
	glColor3f (0.0, 1.0, 0.0);
	for( i=0;i < vLines.size();i++)
		for(int j=0;< vLines[i].vPointList.size();j++)
	//Render Polygon Shapefile
	glColor3f(1.0,0.0, 0.0);
	for( i=0;i < vPolygons.size();i++)
		for(int j=0;j <vPolygons[i].vPointList.size();j++)


Sample Output- OpenGL Console

Points of Interest

It would be nice if someone comes up with ways to read/apply/reproject the .shp file by reading the projection information from .prj files accompanied with .shp files. PROJ.4 is an Open Source projection library if you want to give it a try.

Experimenting with pan and zoom features will be a good challenge.

Maintaining Aspect Ratio is a predominant feature in GIS applications. I leave it to you as another feature to explore.


  • 29th December, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Durga Prasad Dhulipudi
Software Developer
India India
He works in the GIS and allied technologies. His areas of Interest are GIS/CAD ,Defence,Aerospace and Law Enforcement.

You may also be interested in...

Comments and Discussions

Questionconstant aspect ratio, pan, and zoom Pin
Neuroprogrammer10-Dec-15 13:40
memberNeuroprogrammer10-Dec-15 13:40 
QuestionI see rendering artifacts in the form of unwanted lines that connect to some vertices when using the shapefile package.. Pin
Member 1150723412-May-15 0:28
memberMember 1150723412-May-15 0:28 
QuestionCode renders unwanted lines at specific verticies Pin
Alphadog7618-Jul-14 9:47
memberAlphadog7618-Jul-14 9:47 
QuestionAre you sure the OpenShapeFile function parses the geographic coordinate information and stores them Pin
sepinaz15-Sep-13 3:17
membersepinaz15-Sep-13 3:17 
QuestionHow to use projrction of the layer in order to have a better view in OpenGL Pin
sepinaz14-Sep-13 21:02
membersepinaz14-Sep-13 21:02 
QuestionGDI? Pin
Nite12-Mar-10 18:54
memberNite12-Mar-10 18:54 
AnswerRe: GDI? Pin
Durga Prasad Dhulipudi25-Mar-10 19:26
memberDurga Prasad Dhulipudi25-Mar-10 19:26 
Let me try doing it in GDI.I guess the challenge would be to divide the screen coordinates into map coordinates.Let me know whether you are looking for solution in GDI or GDI+ or WPF.
Don't be ruffled if now and then you get a brush from the world; it will be over in no time, and everything will be all right.(Swami Vivekananda)

GeneralRe: GDI? Pin
Nite26-Mar-10 4:25
memberNite26-Mar-10 4:25 
GeneralRe: GDI? Pin
Durga Prasad Dhulipudi9-Apr-10 0:04
memberDurga Prasad Dhulipudi9-Apr-10 0:04 
Generalno shaders :( Pin
Adrian Pasik2-Nov-09 8:42
memberAdrian Pasik2-Nov-09 8:42 
GeneralUse Shapelib On Mac OS X Pin
andyce856-Aug-09 12:11
memberandyce856-Aug-09 12:11 
GeneralRe: Use Shapelib On Mac OS X Pin
Catalin Curta27-Nov-12 22:59
memberCatalin Curta27-Nov-12 22:59 
QuestionHow to use this code in c++ builder? Pin
josephspeak20-Jul-09 23:14
memberjosephspeak20-Jul-09 23:14 
QuestionHow Draw polygon with holes Pin
Member 155633111-Mar-09 17:17
memberMember 155633111-Mar-09 17:17 
AnswerRe: How Draw polygon with holes Pin
Durga Prasad Dhulipudi8-Apr-09 19:08
memberDurga Prasad Dhulipudi8-Apr-09 19:08 
Generalwhat's the benifite Pin
Martial Spirit6-Jan-09 2:05
memberMartial Spirit6-Jan-09 2:05 
GeneralRe: what's the benifite Pin
Durga Prasad Dhulipudi8-Jan-09 1:21
memberDurga Prasad Dhulipudi8-Jan-09 1:21 
GeneralNeeds glut32.dll Pin
gxdata5-Jan-09 14:19
membergxdata5-Jan-09 14:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 29 Dec 2008
Article Copyright 2008 by Durga Prasad Dhulipudi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid