Trying to convert a CSV file (3 columns: Lon, Lat, Ref) to .tif GeoTIFF (the Ref values) using gdal. Using Python 3.6, I am getting the error below.

What am I doing wrong?

C:\ProgramData\Anaconda2\Envs\Python3.6\lib\site-packages\osgeo\gdal.py in Grid(destName, srcDS, **kwargs) 966 srcDS= OpenEX(srcDS, OF_VECTOR) 967 968 return GridInternal(destName, srcDS, opts, callback, callback_data) 969 970 def RasterizeOptions(options = [], format = None,

in GridINternal(*args) 3227 def GridInternal(*args): 3228 """GridInternal(char const * dest, Dataset dataset, GDALGridOptions options, GDALProgressFunc callback=0, void * callback_data=None) -> Dataset """ 3229 return_gdal.GridInternal(*args) 32230 class GDALRasterizeOptions(_object): 3231 """Proxy of C++ GDALRasterizeOptions class.""" ValueError: Received a NULL pointer.


import os
import gdal
#this code will loop over a directory and read all .csv files
dir_with_csvs = r"C:\User"

def find_csv_filenames(path_to_dir, suffix=".csv"):
    filenames = os.listdir(path_to_dir)
    return [ filename for filename in filenames if filename.endswith(suffix) ]
csvfiles = find_csv_filenames(dir_with_csvs)
for fn in csvfiles:
    vrt_fn = fn.replace(".csv", ".vrt")
    lyr_name = fn.replace('.csv', '')
    out_tif = fn.replace('.csv', '.tiff')
#create a .vrt file
    with open(vrt_fn, 'w') as fn_vrt:
        fn_vrt.write('\t<OGRVRTLayer name="%s">\n' % lyr_name)
        fn_vrt.write('\t\t<SrcDataSource>%s</SrcDataSource>\n' % fn)
        fn_vrt.write('\t\t<GeometryField encoding="PointFromColumns" x="Lon" y="Lat" z="Ref"/>\n')
#create 2 .tif files
output = gdal.Grid('outcome.tif','name.vrt')

output2 = gdal.Grid('outcome2.tif','name.vrt', algorithm='invdist:power=2.0:smoothing=1.0')

CSV sample: Lon,Lat, Ref


This is my geotiff. Pixels image are just an example and not at same time of mygeotiff file (don't expect a match).

enter image description here

so here is what i did: import subprocess (below import gdal), removed the last 2 lines of code (below #create 2 .tif files), and replaced them with (indendent):

output = 'gdal_grid -a invdist:power=2.0:smoothing=1.0 -zfield "Ref" -of GTiff -ot Float64 -l %s %s %s' % (lyr_name, vrt_fn, out_tif)

The output was not what I expected at all. my aim was to create a colored pixels (centroid is Lon-Lat and color scale is based on "Ref" value). Can I still do that with my geotiff file (below)?enter image description here

  • 1
    Please Edit the question with updates. Non-answer Answers will be deleted by policy. – Vince Dec 22 '18 at 19:17
  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. – PolyGeo Dec 22 '18 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.