0

I have created the below code to grid an XYZ file to GeoTIFF using Python.

However, when I run the code it seems to be skipping my functions within the class. I tried print statements within them and they were ignored.

The file is ran with no errors either. Where I am going wrong?

import sys
from botocore.exceptions import ClientError
import pandas as pd
import numpy as np
import rasterio
from datetime import datetime
from osgeo import gdal
class gdal_toolbox:
 ## CONSTANTS ##
 ## API handling Globals ##
 gdal_types = [ 'GDT_Unknown','GDT_Byte','GDT_UInt16','GDT_Int16',\
 'GDT_UInt32','GDT_Int32','GDT_Float32','GDT_Float64',\
 'GDT_CInt16','GDT_CInt32','GDT_CFloat32','GDT_CFloat64',\
 'GDT_TypeCount' ]
 jobDict = {}
 xyz_dict = {}
 layerJson = {}
 msk = {}
 def __init__( self, kwargs ):
 if self.jobDict['no_data'] is None:
 self.jobDict['no_data'] = -11000
 else:
 self.jobDict['no_data'] = int(self.jobDict['no_data'])
 
 if self.jobDict['gridAlgorithm'] is None:
 self.jobDict['gridAlgorithm'] = 'nearest:radius1=2.25:radius2=2.25:nodata=' + str(self.jobDict['no_data'])
 def normalizeToCsv( self, xyzS3Obj ):
 MAX_POINTS = 64000000
 try:
 
 # Read in ungridded data
 self.df = pd.read_csv('C:/Users/Public/FLX_2020_10_AgitationTrial_OSGB_Average_1m_20201023_clip.xyz', sep='\s+|,|:|\t',header=None, engine='python')
 cnt = self.df.shape[0]
 if(cnt > MAX_POINTS):
 raise ValueError('Maximum number of points (' + str(cnt) + ' > ' + str(MAX_POINTS) + ') in datasource exceeded')
 # convert to named x,y,z columns
 print(str(datetime.now()) + ' normalizeToCsv: to_csv (start)')
 self.df.to_csv(self.csv_buf,sep=',',header=['x','y','z'],index=None)
 self.csv_buf.seek(0)
 print(str(datetime.now()) + ' normalizeToCsv: to_csv (end)')
 dfsize = sys.getsizeof(self.df)
 print('df (1) size : ' + str(dfsize))
 #return df
 
 except Exception as e:
 self.logException(e)
 raise 
 
 def csvToTiff(self):
 try:
 x = self.xyz_dict['xAxis'] / self.xyz_dict['xCellSize']
 y = self.xyz_dict['yAxis'] / self.xyz_dict['yCellSize']
 
 no_data = str(self.jobDict['no_data'])
 if self.jobDict['srs'] is not None:
 srs = self.jobDict['srs']
 elif self.jobDict['wkt'] is not None:
 srs = rasterio.crs.CRS.from_wkt(self.jobDict['wkt'])
 option = gdal.GridOptions(format = 'GTIFF', outputType = gdal.GDT_Float32, width = x, height = y, \
 outputBounds = [self.xyz_dict['minX'], self.xyz_dict['minY'], self.xyz_dict['maxX'], self.xyz_dict['maxY']], \
 outputSRS = srs, algorithm=self.jobDict['gridAlgorithm'])
 
 self.ds_tif = gdal.Grid('C:/Users/Public/flx_grid_gdal.tif', self.ds, options = option)
 
 except Exception as e:
 self.logException(e)
 raise 
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Nov 22, 2021 at 23:02
2
  • 3
    Did you create an instance of your class and call your functions? When you define a function it is not run automatically. You have to call a function, passing any required arguments. And when functions are defined inside a class, you must create an instance of the class and call it's methods using dot notation e.g. class.method() or call them in the __init__() method. Commented Nov 22, 2021 at 23:19
  • 2
    Python is not skipping the functions (class methods), you have not created an instance of you class or called any of the methods. Commented Nov 23, 2021 at 2:28

1 Answer 1

1

As others have written in comments, if what you've posted is your entire script, then the script should be expected to do nothing. It defines a class, but never instaniates an object of that class (and never goes the next step of calling any of the functions on an instantiated object of that class).

At the very least, you may need to add something like the following at the end of your script without any indentation (ie, not within the class):

gtb = gdal_toolbox()
gtb.normalizeToCsv()
gtb.csvToTiff()

NB: It looks as though your normalizeToCsv() function expects an argument (xyzS3Obj), but the function does nothing with the argument (parameter), so something is wrong there. Either don't include the xyzS3Obj parameter, or do something with it. I my suggested code above, I excluded the parameter, but it will not run like that, because the class function expects a parameter.

answered Nov 23, 2021 at 3:25
1
  • 1
    By convention, it's also good practice for class names to start with a capital letter, not a lower case letter. Not compulsory, though. Commented Nov 23, 2021 at 3:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.