0

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,

c:\ProgramData\Anaconda2\envs\Python3.6\lib\site-packages\osgeo\gdal.py
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.

Code:

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

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('<OGRVRTDataSource>\n')
        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<GeometryType>wkbPoint</GeometryType>\n')
        fn_vrt.write('\t\t<GeometryField encoding="PointFromColumns" x="Lon" y="Lat" z="Ref"/>\n')
        fn_vrt.write('\t</OGRVRTLayer>\n')
        fn_vrt.write('</OGRVRTDataSource>\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

-113.3869,52.8681,30.5
-113.3718,52.8771,0.5
-113.5384,52.8951,6.5
-113.6292,52.9131,0.5
-113.6141,52.9131,0.5
-113.5989,52.9311,15.0
-113.5838,52.9311,0.5
-113.7049,52.9401,0.5
-113.5535,52.9401,1.5
-113.6898,52.9491,0.5
-113.4021,52.9581,22.0
-113.5384,52.9761,0.5
-113.5535,52.9851,0.5
-113.5384,52.9851,0.5
-115.7797,53.0031,0.5

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)
subprocess.call(output)

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.