There are a few problems when I run the following code.
- Image comes out flipped vertically, even though lat lon is input correctly
- I have to run the code twice for it to run properly (so wierrd)
- I also get the following errors:
ERROR 4: `result.tif' not recognized as a supported file format.
ERROR 4: Unable to open result.tif to obtain file list.
ERROR 1: Deleting result.tif failed:
Permission denied
I use the following data in the points.csv:
lat lon value
39.545 -75.78 0.001034494
39.555 -75.78 0.001033509
39.565 -75.78 0.001032392
39.575 -75.78 0.001001829
39.585 -75.78 0.00094428
39.595 -75.78 0.000940016
39.605 -75.78 0.000897172
39.615 -75.78 0.000896742
39.625 -75.78 0.000896021
And here is the code...
# create the csv and vrt file
vrt_file = """<OGRVRTDataSource>
<OGRVRTLayer name="points">
<SrcDataSource>points.csv</SrcDataSource>
<GeometryType>wkbPoint</GeometryType>
<GeometryField encoding="PointFromColumns" x="lon" y="lat" z="value"/>
</OGRVRTLayer>
</OGRVRTDataSource>"""
with open("points.vrt", "w") as f:
f.write(vrt_file.strip())
# perform gridding and RGB rastering
# for some reason this has to run twice????
gdal.AllRegister()
gdal.Grid('result.tif',
'points.vrt',
format='GTiff',
outputType=gdal.GDT_Float32,
algorithm='linear:radius=0.5',
zfield='value')
# plot, comes out flipped on accident
im = gdal.Open('result.tif').ReadAsArray()
im = plt.imshow(im)
1 Answer 1
You can solve this issue by assigning gdal.Grid()
to a variable, like output = gdal.Grid()
and then immediately assign that variable to 'None', so after you run your model, output = None
.
It's apparently a 'gotcha' when using gdal.Grid()
in Python. This prevents the error of having to run the model twice and not have that other error where it says that your result is not a TIFF file.