Solved

converting .shp to .bmp using c++

Posted on 2004-08-26
4
454 Views
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
0
Comment
Question by:fraughtneo
[X]
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
4 Comments
 

Accepted Solution

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




0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

623 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