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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

820 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