Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hello can someone point me to library for this? Which can read .shp files and render them?

share|improve this question
    
As a partial solution, you could use Shapelib to read your shapefiles - but that won't render them too. – anoved Jan 23 '13 at 17:34

I agree @anoved Refer these two articles on reading the shapefile and using opengl to render. http://www.codeproject.com/Articles/32035/Rendering-Shapefile-in-OpenGL http://www.codeproject.com/Tips/1021362/Rendering-Shapefile-in-OpenGL

Per suggestion from @Simbamangu, including excerpts from the source code.There are two main activities in achieving the task-Parse the shapefile vertices ;send these vertices to drawing context using OpenGL.

Shapefile Parsing

The excerpt below shows how to parse shapefiles with point geometry.

//Include the necessary header file
#include "shapelib\shapefil.h"

//Function to Open Shapefile and parse the info 
void OpenShapeFile(char* fileName,int r,int g ,int b,float fTransparency)
{

    vector<float>vMinX;
    vector<float>vMinY;
    vector<float>vMaxX;
    vector<float>vMaxY;


    SHPHandle hSHP=SHPOpen(fileName, "rb" );

    //Open corresponding DBF  file
    string fileNameString(fileName);
    string fileNameWithoutExtension = fileNameString.substr(0, fileNameString.rfind("."));
    fileNameWithoutExtension.append(".dbf");
    DBFHandle hDBF=DBFOpen(fileNameWithoutExtension.c_str(), "rb" );


    //Push Boundary Box Coods of Every Shapefile that is added
    vMinX.push_back(hSHP->adBoundsMin[0]);
    vMinY.push_back(hSHP->adBoundsMin[1]);
    vMinX.push_back(hSHP->adBoundsMax[0]);
    vMaxY.push_back(hSHP->adBoundsMax[1]);

    if(hSHP == NULL) return;

    //Point Shapefile
    if(hSHP->nShapeType == SHPT_POINT)
    {
        SHPObject *psShape;
        for(int i=0;i<hSHP->nRecords;i++)
        {
            psShape = SHPReadObject(hSHP, i);

            double fX = psShape->padfX[0];
            double fY = -psShape->padfY[0];

            //Plot these points
            MyPoint2D pt;
            pt.dX=fX;
            pt.dY=-fY;
            pt.r=r;
            pt.g=g;
            pt.b=b;
            pt.fTransparency=fTransparency;
            strcpy(pt.strName,fileName);
            vPoints.push_back(pt);
        }
    }
    //Line

    //Polygon

}   

Rendering with OpenGL

//Assign Bounding Box Coordinates of Shapefile to glOrtho()
glOrtho(sBoundingBox.fMinX, sBoundingBox.fMaxX,
   sBoundingBox.fMinY,sBoundingBox.fMaxY,-1,1);

//The drawing can be accomplished using below
void draw()
{
  //Render Point Shapefile glColor3f (0.0, 0.0, 1.0);
   glEnable(GL_POINT_SMOOTH) ; 
   glPointSize(5.0); 
   glBegin(GL_POINTS); 
   for(int i=0;i < vPoints.size();i++)  
   {
    glVertex2f(vPoints[i].dX,vPoints[i].dY); 
    glEnd();
   } 

    //Render Line Shapefile
    glColor3f (0.0, 1.0, 0.0);
    for( i=0;i < vLines.size();i++)
    {
        glBegin(GL_LINE_STRIP);
        for(int j=0;< vLines[i].vPointList.size();j++)
        {
          glVertex2f(vLines[i].vPointList[j].dX,vLines[i].vPointList[j].dY);
        }
        glEnd();
    }

    //Render Polygon Shapefile
    glColor3f(1.0,0.0, 0.0);
    for( i=0;i < vPolygons.size();i++)
    {
        glBegin(GL_LINE_LOOP);
        for(int j=0;j <vPolygons[i].vPointList.size();j++)
        {
            glVertex2f(vPolygons[i].vPointList[j].dX,
                    vPolygons[i].vPointList[j].dY);
        }
        glEnd();
    }
    glFlush();  
}

Shapefile and Objective-C

There is objective-c port at https://github.com/initllc/INTShapeFile

share|improve this answer
    
Please give some examples and context! "Link-only answers are not good answers." ... if those links don't work in future, we should have a summary of how to answer the question anyway. – Simbamangu Jun 18 at 7:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.