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?
JavaScript
x
90
90
1
import sys
2
from botocore.exceptions import ClientError
3
import pandas as pd
4
import numpy as np
5
import rasterio
6
from datetime import datetime
7
from osgeo import gdal
8
9
10
class gdal_toolbox:
11
12
## CONSTANTS ##
13
14
## API handling Globals ##
15
gdal_types = [ 'GDT_Unknown','GDT_Byte','GDT_UInt16','GDT_Int16',
16
'GDT_UInt32','GDT_Int32','GDT_Float32','GDT_Float64',
17
'GDT_CInt16','GDT_CInt32','GDT_CFloat32','GDT_CFloat64',
18
'GDT_TypeCount' ]
19
jobDict = {}
20
xyz_dict = {}
21
layerJson = {}
22
msk = {}
23
24
def __init__( self, kwargs ):
25
26
self.jobDict = kwargs
27
28
if self.jobDict['no_data'] is None:
29
self.jobDict['no_data'] = -11000
30
else:
31
self.jobDict['no_data'] = int(self.jobDict['no_data'])
32
33
if self.jobDict['gridAlgorithm'] is None:
34
self.jobDict['gridAlgorithm'] = 'nearest:radius1=2.25:radius2=2.25:nodata=' + str(self.jobDict['no_data'])
35
36
37
def normalizeToCsv( self ):
38
MAX_POINTS = 64000000
39
40
try:
41
42
# Read in ungridded data
43
self.df = pd.read_csv('C:/Users/......xyz', sep='s+|,|:|t',header=None, engine='python')
44
45
cnt = self.df.shape[0]
46
if(cnt > MAX_POINTS):
47
raise ValueError('Maximum number of points (' + str(cnt) + ' > ' + str(MAX_POINTS) + ') in datasource exceeded')
48
49
50
# convert to named x,y,z columns
51
print(str(datetime.now()) + ' normalizeToCsv: to_csv (start)')
52
self.ds = self.df.to_csv(self.csv_buf,sep=',',header=['x','y','z'],index=None)
53
self.csv_buf.seek(0)
54
print(str(datetime.now()) + ' normalizeToCsv: to_csv (end)')
55
56
dfsize = sys.getsizeof(self.df)
57
print('df (1) size : ' + str(dfsize))
58
#return df
59
60
except Exception as e:
61
self.logException(e)
62
raise
63
64
65
def csvToTiff(self):
66
67
try:
68
x = self.xyz_dict['xAxis'] / self.xyz_dict['xCellSize']
69
y = self.xyz_dict['yAxis'] / self.xyz_dict['yCellSize']
70
71
no_data = str(self.jobDict['no_data'])
72
73
if self.jobDict['srs'] is not None:
74
srs = self.jobDict['srs']
75
elif self.jobDict['wkt'] is not None:
76
srs = rasterio.crs.CRS.from_wkt(self.jobDict['wkt'])
77
78
option = gdal.GridOptions(format = 'GTIFF', outputType = gdal.GDT_Float32, width = x, height = y,
79
outputBounds = [self.xyz_dict['minX'], self.xyz_dict['minY'], self.xyz_dict['maxX'], self.xyz_dict['maxY']],
80
outputSRS = srs, algorithm=self.jobDict['gridAlgorithm'])
81
82
self.ds_tif = gdal.Grid('C:/Users/Public/......tif', self.ds, options = option)
83
84
except Exception as e:
85
self.logException(e)
86
raise
87
88
89
90
Advertisement
Answer
Use
JavaScript
1
4
1
if __name__ == "__main__":
2
app = gdal_toolbox(kwargs)
3
app.run()
4
or
JavaScript
1
3
1
if __name__ == "__main__":
2
gdal_toolbox(kwargs).run()
3
Use thease codes at the end of your script.
And the solution is :
JavaScript
1
25
25
1
class gdal_toolbox:
2
3
## CONSTANTS ##
4
5
def __init__( self, kwargs ):
6
print(kwargs)
7
8
9
10
def normalizeToCsv( self ):
11
MAX_POINTS = 64000000
12
print(MAX_POINTS)
13
14
15
16
def csvToTiff(self):
17
18
print("Its the secoend function")
19
20
21
if __name__ == "__main__":
22
23
kwargs="Its the keyword arguments"
24
gdal_toolbox(kwargs)
25