Python Forum
Thread Rating:
  • 3 Vote(s) - 2.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NoneType Error
#1
Hi, I'me trying to run a code but I'm getting an error that is 'NoneType' object has no attribute 'SetGeoTransform. I'm not sure what this means or how to fix it. My code is:

#Import OS
import os
import sys
os.environ['PATH'] = "C:/OSGeo4W64/bin" + ';' + os.environ['PATH']
sys.path.append('C:/OSGeo4W64/apps/Python27/Lib/site-packages/')
from osgeo import gdal
import numpy as np

#Define Filepath
filename = "C:\Users\masam066\Desktop\Files for python exercises\sfu.tif"

#Use GDAl to open() funtion to open the database
#Use if statement to check if the object ds exists and return error code '1' if it doesn't
ds = gdal.Open(filename)
if ds is None:
   sys.exit(1)

#Get info about the dataset
xsize = ds.RasterXSize
ysize = ds.RasterYSize
nbands = ds.RasterCount
projection = ds.GetProjection()
geotransform = ds.GetGeoTransform()

#Read the bands
bands = []
for i in range (nbands):
   bands.append(ds.GetRasterBand(i+1))
band1 = bands[0]
min = band1.GetMinimum()
band1.ComputeRasterMinMax(1)

#Read Data To Arrays
arrays = []
for i in range(nbands):
   arrays.append(bands[i].ReadAsArray(0, 0, xsize, ysize))

#Calculate pixel brightness
brightness = (arrays[0] + arrays[1] + arrays[2]) / 3


#Test whether band values are integes or floats
if(np.issubdtype(arrays[0].dtype, np.integer) is True):
   integerValues = True
else:
   integerValues = False

#Floating point division
brightness = (arrays[0] + arrays[1] + arrays[2]) / 3.0

#Writing results to raster files
filename = "C:\Users\masam066\Desktop\brightness.tif"
driver = gdal.GetDriverByName('GTiff')
dataset = driver.Create(filename, xsize, ysize, 1, gdal.GDT_Float32)
dataset.SetGeoTransform(geotransform)
dataset.SetProjection(projection)
band = dataset.GetRasterBand(1)
band.WriteArray(brightness)
dataset = None
It is the blue lines where PyScripter finds the error.

Moderator Larz60+: Added Python tags. Please do this in the future (see help, BBCODE)
Reply
#2
Put print() after initializing dataset to see if it is set. Do the same after ds and geotransform initialisation.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
So we added this like you suggested.

#Writing results to raster files
filename = "C:\Users\masam066\Desktop\brightness.tif"
driver = gdal.GetDriverByName('GTiff')
dataset = driver.Create(filename, xsize, ysize, 1, gdal.GDT_Float32)
print()
dataset.SetGeoTransform(geotransform)
print()
dataset.SetProjection(projection)
band = dataset.GetRasterBand(1)
band.WriteArray(brightness)
dataset = None
but recieved the same error
Reply
#4
We need to see the output. What are the print statements printing? What exactly is the error you are getting?

Most likely, driver.Create() is returning None, not a dataset, and that's why you're getting the error. But we can't be sure of that without seeing the output and error message.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
The print statements are not producing anything. And the error reads AttributeError: 'NoneType' object has no attribute 'SetGeoTransform'
Reply
#6
I was meaning to print print(dataset), print(ds) and print(geotransform), not just to call print()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
NoneType is the type of the special None object. When you get "'NoneType' object has no attribute 'XXX'" it means you are trying to use the attribute XXX (or a method, since in Python, methods  behave like callable attributes) of the None object, usually because one of your variables contains None and not what you think.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ERROR NoneType object has no attribute content denizkb 1 2,613 Nov-21-2019, 01:18 PM
Last Post: denizkb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020