2

I am trying to create a WGS84 raster from a matrix like so:

from osgeo import gdal
import numpy as np

test = np.random.random((64, 128))
nx = test.shape[0]
ny = test.shape[1]

xmin, ymin, xmax, ymax = [-180, -90, 180, 90]

xres = (xmax - xmin) / float(nx)
yres = (ymax - ymin) / float(ny)
geotransform = (xmin, xres, 0, ymax, 0, -yres)


dst_ds = gdal.GetDriverByName('GTiff').Create('Z:/spotter/output.tif', ny, nx, 1, gdal.GDT_Float32)


dst_ds.SetGeoTransform(geotransform)    
srs = osr.SpatialReference()            
srs.ImportFromEPSG(4326)                # WGS84 lat/long

but srs.ImportFromEPSG(4326) returns 6 and isn't being set properly.

g = gdal.Open('Z:/spotter/output.tif')
g.GetProjection() 

returns empty brackets, ''

  • Does gdalinfo recognize it? – user30184 Nov 15 '17 at 17:38
  • opening the out put up as g = gdal.Open('Z:/spotter/output.tif') and g.GetProjection() returns empty brackets – Stefano Potter Nov 15 '17 at 17:40
  • You might check the SetProjection call by immediately calling GetProjection on it to see if it worked properly. – mkennedy Nov 15 '17 at 17:44
  • it appears srs.ImportFromEPSG(4326) returns 6, and no it is net being set properly in the first place – Stefano Potter Nov 15 '17 at 17:46
  • I guess you need also from osgeo import osr – user30184 Nov 15 '17 at 18:06

Your Answer

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

Browse other questions tagged or ask your own question.