Search code examples
gdalelevation

How to get elevation of every pixel by gdal?


I read an img file by gdal. And I want to get elevation of every pixel. But the data I got like this

[[1551 1551 1553 ... 1388 1387 1386]                                                                                     
[1552 1551 1551 ... 1385 1383 1383]                                                                                     
[1551 1548 1549 ... 1386 1381 1380]                                                                                     
...                                                                                                                     
[2047 2049 2046 ...  531  526  528]                                                                                     
[2016 2035 2051 ...  524  522  525]                                                                                     
[1982 2003 2025 ...  518  518  518]]

What does it means? Incomplete data? Can someone help me? Thanks in advance.


Solution

  • Is this simply the print you get from calling print() on your raster band?

    Your data is probably correct, however when you print a np.array, it is usually cropped, to not show you all the data.

    It simply means that there is a lot of values in between at the ..., but it does not print the values.

    Since the image is probably huge i am not sure that it would make any sense to show this values like this? I think you should look into visualizing it.

    I usually use qgis to visualize geospatial rasters, you should basically be able to drag'n'drop your image into it to see your data.

    However you can of course also visualize the part of the image you are interested in through matplotlib

    If you actually like are interested in the printed values

    You can however use two options to see the entire print.

    1) If you would like to save the values to a text file use np.savetext

    If you have your values in the variable called x this can be done with np.savetxt("values.txt", x)

    2) You can allow numpy to print everything by setting the threshold printoption. You can set it just after you have imported numpy or before the print. Again if we expecty your data to be in x.

    import numpy as np
    np.set_printoptions(threshold=np.nan)
    print(x)
    

    If you like this to be written to a file you can do this instead

    import numpy as np
    np.set_printoptions(threshold=np.nan)
    with open('values.txt', 'w') as f:
        print(x, file=f)