I'm trying to write a little C++/OpenGL utility with which I could manage SRTM HDG files. For now, I'm just trying to read the files correctly. So I downloaded a sample tile from the main server, which is associated with this image.

To generate an image like that is exactly what I want to do initially, by code, no lib (if it doesn't became too hard). The problem is that the best I got so far are images like this (compare!).

I have tried a couple of proceedings... Basically, I'm transforming altitude in pixel color (from black/low to white/high), and putting the points per pixel with openGL as if creating an image. I've done it directly already, and now I'm averaging with the points around the point of interest. The results are very alike in both cases.

Before that, when I was visualizing the raw data (the altitude int's), I noticed a lot of strange negative values (like, -100 aside to 512 and so on), and it seems they correspond to some of the dark areas in my images (where they should not be so, if compared to the original image from NASA).

Here is my code for the draw() function (ok, it's just to get something out). Im usign netBeans IDE, on linux/Ubuntu:

void draw() { glClear( GL_COLOR_BUFFER_BIT ); int rows = 0; int cols = 0; byte = new char[2]; file.seekg (0, ios::beg); file.read(byte, 2); while(!file.eof()) {
alt = byte[1] | ( byte[0] << 8 ); if (alt < minimo) minimo = alt; if ( alt > maximo ) maximo = alt; h[cols][rows] = alt; cols++; if ( cols > 3600 ) { rows++; cols = 0; }
file.read(byte, 2); } for (rows = 1; rows < 3599; rows++) { for (cols = 1; cols < 3599; cols++) { altitude[cols][rows] = 0.1111111111*( h[cols][rows] + h[cols-1][rows-1] + h[cols][rows-1] + h[cols+1][rows-1] + h[cols-1][rows] + h[cols+1][rows] + h[cols-1][rows+1] + h[cols][rows+1] + h[cols+1][rows+1]); } } glPointSize(1.0f); glBegin(GL_POINTS); for (rows = 1; rows < 3600; rows++) { for (cols = 1; cols < 3600; cols++) { color = 0.0005555555555556f * (float)altitude[cols][rows]; glColor3f(color, color, color); glVertex3f(cols, rows, 0); } } glEnd(); cout << "lemos tuo - cols = " << cols << ", rows = " << rows << " - minimo: " << minimo << ", maximo: " << maximo << endl; glutSwapBuffers(); }

You may notice, also, the "min" (minimo) and "max" (maximo) values. When I compare with the min and max I get from GRASS, for instance, the max's are the same (1644 m), but my min = -128 (negative), and GRASS's min = 120 (positive)! And GRASS renders it just like SRTM's original.

Can anyway help me? What am I doing wrong? Am I reading wrong or rendering wrong, or both? I've gone through a some official docs and other questions, and all I got is that I must swap the bytes. Some people talk about negative numbers (that should be small, and correctly positioned - there is a river in this image). But I can't manage to get help about some - 100 at the side of some +500's that build strange holes in the image - but GRASS get's it right. So... My mistake. But where?

PS: first time I do things with these kind of dada.

  • 1
    reading an unsigned value into a signed char is probably the issue. Convert to integer: stackoverflow.com/questions/5144080/… – BradHards Sep 20 '15 at 9:40
  • This is great @BradHards. It did the trick perfectly. To be clear, I just declared byte as unsigned char, and then changed file.read .. to file.read((char*)byte, 2); You could public your comment as an answer so I can mark it as answered! – zarathustra Sep 20 '15 at 13:50

When you read two bytes into char, with this code:

   byte = new char[2];
   file.seekg (0, ios::beg);
   file.read(byte, 2);

You are implicitly reading two signed bytes.

However the data is unsigned.

Then when you convert from big endian:

alt = byte[1] | ( byte[0] << 8 );

You are introducing errors

So you need to make sure that you read your SRTM data as unsigned.

There are multiple ways to do that, and it isn't GIS specific (hence off topic). One way is provided at https://stackoverflow.com/questions/5144080/how-can-i-read-a-file-as-unsigned-bytes-in-java

The only real SRTM issue here is making sure you treat it as unsigned.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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