Skip to content
Advertisement

Python skipping functions when ran in VS Code

I have written the following python code to read in XYZ data as CSV and then grid to a GTiff format.

When I run the code I am getting no errors.

However, after trying to debug, I added some print statements and noticed that the functions aren’t actually being called.

How can I run this script so that it all completes?

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 ):

        self.jobDict = 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 ):
            MAX_POINTS = 64000000

            try:
                
                # Read in ungridded data
                self.df = pd.read_csv('C:/Users/......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.ds = 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/......tif', self.ds, options = option)
                
            except Exception as e:
                self.logException(e)
                raise 


            

Advertisement

Answer

Use

if __name__ == "__main__":
    app = gdal_toolbox(kwargs)
    app.run()

or

 if __name__ == "__main__":
    gdal_toolbox(kwargs).run()

Use thease codes at the end of your script.

And the solution is :

class gdal_toolbox:

    ## CONSTANTS ##

    def __init__( self, kwargs ):
        print(kwargs)



    def normalizeToCsv( self ):
            MAX_POINTS = 64000000
            print(MAX_POINTS)

        

    def csvToTiff(self):

          print("Its the secoend function")
      
      
if __name__ == "__main__":
 
    kwargs="Its the keyword arguments"
    gdal_toolbox(kwargs)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement