garbart
garbart

Reputation: 485

GeoTIFF software generation

I have some data about the height of a set of points (for example, from the Google Elevation API). There is a task to save this data in GeoTIFF format, then to use in osgEarth (GDAL). How can this be done? It does not matter in what language.

A quick search on the Internet only gave me the answer to the reverse question (How do I open geotiff images with gdal in python?)

I would be very grateful for any help.

Upvotes: 2

Views: 224

Answers (1)

the_cheff
the_cheff

Reputation: 5040

So i would do this with GDAL from python (You could also use rasterio which is a nice wrapper around gdal for file raster file handling)

You should put your data in a numpy array,let us call it some_nparray.

Then create the tif dataset gtiffDriver.Create(). Here you can provide the name of your file, the dimensions in number of columns and rows of your image, the number of bands (here 1), and the datatype. Here i said float32, however byte, int16 etc could also work, depending on your data (you can check it with heigh_data_array.dtype)

Next you should set the geotransform, which is the information about the corner coordinates and pixel resolution, and you should set the projection you are using. This is done with dataset.SetGeoTransform and dataset.SetProjection. How these are created is not in the scope of this question I believe. If you do not need it, i guess you can even skip that part.

Finally write your array to the file with WriteArray and close the file.

You code should look something like this. Here I use the convention that variables prefixed with some_ should be provided by you.

    from osgeo import gdal

    height_data_array = some_nparray

    gtiffDriver = gdal.GetDriverByName('GTiff')

    dataset = gtiffDriver.Create('result.tif', 
                                 height_data_array.shape[1], 
                                 height_data_array.shape[0], 
                                 1, 
                                 gdal.GDT_Float32)

    dataset.SetGeoTransform(some_geotrans)
    dataset.SetProjection(some_projection)

    dataset.GetRasterBand(1).WriteArray(height_data_array)
    dataset = None

Upvotes: 2

Related Questions