1

I am trying to save a numpy array output as a GeoTiff, and have the code running mostly successfully, but the output image has a sepia-toned scale for the data (instead of a normal color scheme like my code produces for the image), and a black background (instead of the white/no-data background that my code produces for the image).

Here's the code for saving my array to a GeoTiff. Can I add a line in somewhere about making no-data = 0, and making the data scheme be colored?

from osgeo import gdal, osr, ogr, os 
from gdalconst import *  

def array2raster(newRasterfn,rasterOrigin,pixelWidth,pixelHeight,array):

    cols = array.shape[1]
    rows = array.shape[0]
    originX = rasterOrigin[0]
    originY = rasterOrigin[1]

    driver = gdal.GetDriverByName( 'GTiff' )
    outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Float32)
    outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
    outband = outRaster.GetRasterBand(1)
    outband.WriteArray(array)
    # outRaster = driver.Create( 'CORDC_GTIFF/working_CA.tiff', 300, 300, 1,     gdal.GDT_Int32)
    proj = osr.SpatialReference()  
    proj.ImportFromEPSG(4326) 
    outRaster.SetProjection(proj.ExportToWkt())  
    # geotransform = (1,0.1,0,40,0,0.1)  


rasterOrigin = (-127,42)
pixelWidth = .01
pixelHeight = .01
newRasterfn = 'CORDC_GTIFF/cordc_working_CA.tif'
array = np.array(spd)


reversed_arr = array[::-1] # reverse array so the tif looks like the array
array2raster(newRasterfn,rasterOrigin,pixelWidth,pixelHeight,reversed_arr) # convert array to raster
1
  • It might be helpful if you could add the correct image and the image with the wrong colour scheme. That way even the users without the possibility of running your code might be able to identify what is wrong. Commented Jun 26, 2014 at 18:13

1 Answer 1

1

You can set the no data value using the band's SetNoDataValue method:

outband = outRaster.GetRasterBand(1)
outband.SetNoDataValue(0)
outband.WriteArray(array)

Areas matching the no data value should then be displayed as transparent

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! SetNoDataValue seemed to do the trick (I'd had it in there before, but must have not had something set right, because it wasn't working. Now it is!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.