Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Digital Elevation Model file Viewer

  • Filter
  • Time
  • Show
Clear All
new posts

  • Digital Elevation Model file Viewer

    This is the source for a rough and ready .dem file viewer.
    The code is reasonably well commented with a minimum of Magic Numbers

    The picture below is scaled from the 900x900 pixels program output to 320x320 (approximately) in order to make it display quickly.

    ' The Control Key 2008
    ' Released into the public domain without qualification.
    ' Code to display Digital Elevation Model files in PBCC or PBWin
    ' a DEM file is a simple, regularly spaced grid of elevation points
    ' the most common kind of DEM files come from the USGS (U.S. Geological Survey)
    ' The elevation data is stored as 16 bit words in a binary file with a .dem extension.
    ' Normally these are supplied with a few support and configuration files that give information about the grid size
    ' and location etc.
    ' I was looking for height maps for some games that I have been working on sporadically and wanted a quick and dirty
    ' method of generating them.
    ' The code below is the result.
    ' One problem that needed to be overcome is how to display a 16 bit map graphically given that it is only possible
    ' to display 256 levels of grey, so the code scales the elevation appropriately.
    ' Other ways to display it are of course possible, but you will see that this method shows the map pretty clearly.
    ' In the example below, white represents the maximum elevation and black is approximately sea level.
    ' My other criteria was speed and if I do say so myself, it displays it respectably quickly.
    ' The example file contains quite a bit more information than is displayed, but you can have fun playing with the offsets.
    ' This file e100s10.dem is an elevation map of most of Australia.
    ' It has been ages since I looked at this code and some of the comments might be a little hazy as a result.
    #DIM ALL
    LOCAL filename AS STRING                                         ' Filename
    LOCAL row, gwnd, r, g, b, x, c, mscale AS LONG
    LOCAL yoffset, xoffset, pcol AS LONG
    LOCAL temparray() AS INTEGER
    mscale = 3000   ' Map scale factor
                    ' X and Y offsets determine the view window
    xoffset = 1600  ' horizontal offset of elevation map display
    yoffset = 100   ' vertical offset of display
    DIM temparray(4799)     ' Working array
    GRAPHIC WINDOW "Dem Viewer",0,0,900,900 TO gwnd ' Create graphic window
    GRAPHIC ATTACH gwnd,0, REDRAW                   ' Redraw must be used otherwise it display pixel by pixel sloooooowly
    GRAPHIC SCALE (0,0) - (mscale,mscale)           ' Set the window scale
    filename = "e100s10.dem"                        ' filename
    OPEN filename FOR BINARY AS #1                  ' It is a binary file, although it could be opened as a random file
    WHILE NOT EOF(1)                                ' Do this until there isn't any more data
          GET #1,,temparray()                       ' Get a full row of elevation data
          IF row <= (mscale+yoffset) THEN           ' Only process a row if it fits within the desired view window
             ARRAY SCAN temparray(xoffset), <> -3624, TO dat    ' I think that -3624 represents sea level or null data
             IF dat <> 0 THEN                                   ' If the elevation is greater than zero
                FOR x = xoffset + dat TO UBOUND(temparray)      ' process the data within the horizontal window only
                    IF x <= (mscale+xoffset)  THEN
                       dat = temparray(x)                       ' get the value and make sure that it isn't null
                       IF dat <> -3624 THEN
                          dat = MAK(WORD,HI(BYTE,dat),LO(BYTE,dat))  ' The data is big endian and must be made little endian for intel processors
                          pcol = dat\3                               ' Just a convenient scale value for a pleasing picture
                          GRAPHIC SET PIXEL (x-xoffset,row-yoffset),RGB(pcol,pcol,pcol) ' Draw pixel at the chosen grey value
                        END IF
                    END IF
               GRAPHIC REDRAW                                        ' Display the line
             END IF
          END IF
          INCR row                                                   ' next display line
          IF row > (mscale+yoffset) THEN EXIT                        ' if the row is greater than our window go away
    CLOSE #1                                         ' Close the file - all done
    GRAPHIC REDRAW                                   ' Refresh the screen
    BEEP                                             ' All done
    ' Just a bit of fun actually.
    ' I was also interested in seeing how quickly a 55Mb binary file could be processed.
    ' Enjoy
    The .dem, .exe and source can be retrieved from my web space at the following url.

    It is a 4Mb zip that has been compressed to within an inch of its life

    I had fun with it and it killed a few hours sorting it out.
    I hope that someone will find it useful.
    Let me know your thoughts, I am always interested in feedback.

    A google search will reveal several sources of free files, maybe you can find one of your state or country.

    Gary Barnes
    The Control Key

    If you are not part of the solution
    then you are either a gas, solid, plasma or some other form of matter.

  • #2
    [deleted, did not noticed I am in Source Code forum]
    Last edited by Petr Schreiber jr; 30 Mar 2008, 03:39 PM.
    [email protected]