converting .shp to .bmp using c++

Posted on 2004-08-26
Last Modified: 2010-08-05
Hi ,

Can anyone help to solve the following problem,

How to convert shape files to bmp images using c++

How to create a bmp image using c++

waiting for ur replies
Question by:fraughtneo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

mikeant78 earned 125 total points
ID: 12065750
I assume that you are working on a Windows platform. Based on Google search it looks like .shp file is a text format with graphics instructions. If you are willing to parse that format yourself you can do the following:

1. read in the .shp file
2. Create a Windows DIB section (in-memory) bitmap. This can be done similar to the following:

                               UByte* pbitmapData;
                               UInt bytespp = 3, bitspp = bytespp *8;
                    // Bytes/line
                    UInt bpl = ((bytespp*width)+3)&(~3);

            struct      {
                  BITMAPINFOHEADER      Header;
                  RGBQUAD                        Colors[256];
                  }      Info;
            // Initialize header for bmp info
            Info.Header.biSize            = sizeof(BITMAPINFOHEADER);
            Info.Header.biWidth            = width;
            Info.Header.biHeight            = -SInt(height);
            Info.Header.biPlanes            = 1;
            Info.Header.biBitCount      = bitspp;
            Info.Header.biCompression      = BI_RGB;
            Info.Header.biSizeImage      = bpl*height;
            Info.Header.biXPelsPerMeter      = 0;
            Info.Header.biYPelsPerMeter      = 0;
            Info.Header.biClrUsed            = bytespp*256;
            Info.Header.biClrImportant      = Info.Header.biClrUsed;

            HBITMAP hbmp = ::CreateDIBSection(0, (BITMAPINFO*)&Info, DIB_RGB_COLORS, (VOID**)&pbitmapData, NULL, NULL);

3. Create a device context and select bitmap into it. You will be able to draw into the bitmap with it.

      HDC hdc=CreateCompatibleDC(0);
      SelectObject(hdc,hbmp );

4. Parse the .shp file and use Windows GDI to draw into the HDC. This will draw into the bitmap.

5. Close the hdc when you are done with DeleteDC(hdc).

6. Serialize the bitmap to file. This can be done similar to the following (you will need to modify this based on file IO mechanism, etc.):

      BITMAPFILEHEADER            bmh;
      BITMAPINFOHEADER            bmi;
      SInt                  size, w;      
      SInt                  height = pbitmap->GetHeight();
      UByte                  *pbuffer;      

// Determine the data size
      size      = height * bpl;

#define      PACK_INT16_B(b1,b2)      SInt16(UInt8(b1)|(UInt8(b2)<<8))

      // Initialize file header
      bmh.bfType            = PACK_INT16_B('B','M');
      bmh.bfSize            = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + size;
      bmh.bfReserved1            =
      bmh.bfReserved2            = 0;
      bmh.bfOffBits            = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
      // And bitmap header
      bmi.biSize                  = sizeof(BITMAPINFOHEADER);
      bmi.biWidth            = width;
      bmi.biHeight            = height;
      bmi.biPlanes            = 1;
      bmi.biBitCount            = 24;
      bmi.biCompression       = BI_RGB;
      bmi.biSizeImage            = size;
      bmi.biXPelsPerMeter   =
      bmi.biYPelsPerMeter   = 0;
      bmi.biClrUsed      = 1<<24;
      bmi.biClrImportant      = 0;

      // Last line in temporary bitmap's used as a buffer
      pbuffer      = pbitmapData + size;
      w      = bpl;

      // We need to flip the bitmap data
      // Windows bitmaps are bottom - up
      for (SInt i=0; i<(height>>1); i++)
            memcpy(pbuffer, pbitmapData+i*w, w);
            memcpy(pbitmapData+i*w, pbitmapData+(height-i-1)*w, w);
            memcpy(pbitmapData+(height-i-1)*w, pbuffer, w);

      // And finally write the data
      pfile->Write((UByte*)&bmh, sizeof(BITMAPFILEHEADER));
      pfile->Write((UByte*)&bmi, sizeof(BITMAPINFOHEADER));
      if (pfile->Write(pbitmapData, size)<size)
            return 0;

Note that this is just sample code to give you the idea. You will need to modify it for your purpose.

Of couse, it may be a better idea to try to find a libraray/program which already does what you need.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question