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
1 Answer 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.
-
1By convention, it's also good practice for class names to start with a capital letter, not a lower case letter. Not compulsory, though.Son of a Beach– Son of a Beach2021年11月23日 03:30:24 +00:00Commented Nov 23, 2021 at 3:30
class.method()
or call them in the__init__()
method.