0

There are a few problems when I run the following code.

  1. Image comes out flipped vertically, even though lat lon is input correctly
  2. I have to run the code twice for it to run properly (so wierrd)
  3. 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)
asked Feb 22, 2021 at 2:55

1 Answer 1

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.

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
answered Aug 25, 2021 at 22:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.