1

I am simply trying to delete a TIF that I created with GDAL. I have tried, simply, to use shutil which errors with:

NotADirectoryError: [WinError 267] The directory name is invalid: 'T:/p003M01p0001/extent_files/t215_E.tif'

My first attempt was to use:

filename = out_loc + '/' + temp_tif
driver = ogr.GetDriverByName("GTiff")
if os.path.exists(filename):
    driver.DeleteDataSource(filename)

but this just resulted in a None driver for some reason (AttributeError: 'NoneType' object has no attribute 'DeleteDataSource'). This proves os recognizes the file exists (so why does shutil not work?). I have confirmed the existence of the GTiff driver using:

driver_list = []
for i in range(gdal.GetDriverCount()):
    driver = gdal.GetDriver(i)
    driver_list.append(driver.GetDescription())
    print(gdal.GetDriver(i).ShortName)

I am a bit perplexed that ogr couldn't delete it, but even more perplexed on why shutil doesn't recognize that it exists. There must be something I am doing wrong creating the tiff.

The overall purpose of the following code is to get the boundary of valid TIF values.

Current code:

from osgeo import gdal, osr
from osgeo.gdalconst import GA_Update
import ogr
from core.gdal_functions import GDALFunc

input_tif = 'T:/p003M01p0001/t215.tif'
out_loc = 'T:/p003M01p0001/extent_files'
copy_tif = out_loc + '/t215_E.tif'
temp_tif = out_loc + '/shaded.tif'
shp = out_loc + '/t215_Ex.shp'

# get copy of tif
src_ds = gdal.Open(input_tif, gdal.GA_ReadOnly)
srs = osr.SpatialReference()
srs.ImportFromWkt(src_ds.GetProjectionRef())
dst_ds = src_ds.GetDriver().CreateCopy(copy_tif, src_ds, 0)
dst_ds = None
src_ds = None

# Edit data to remove 0 to turn it into nodata
nodata = 0
ras = gdal.Open(copy_tif, GA_Update)
for i in range(1, ras.RasterCount + 1):
    ras.GetRasterBand(i).SetNoDataValue(nodata)
ras = None

# create temp tif of where actual values exist
drv = ogr.GetDriverByName('ESRI Shapefile')
outfile = drv.CreateDataSource(shp)

outlayer = outfile.CreateLayer('polygonized raster', srs=srs)
newField = ogr.FieldDefn('DN', ogr.OFTReal)
outlayer.CreateField(newField)
command = 'python C:/Users/david/AppData/Local/Programs/Python/Python38/Lib/site-packages/osgeo/scripts/gdal_calc.py ' \
          '-A {infile} --A_band=1 --outfile {outfile} --calc="(A>0)*1" --quiet'.format(infile=copy_tif, outfile=temp_tif)

process = subprocess.Popen(command, shell=True)
while process.poll() is None:
    time.sleep(0.5)

#create polygon extent
tif = gdal.Open(temp_tif)
band = tif.GetRasterBand(1)
gdal.Polygonize(band, None, outlayer, 0)
outfile = None
tif = None


shutil.rmtree(copy_tif)
5
  • 1
    The error is The directory name is invalid. Rmtree wants to remove a directory but you are giving a file name as input. – user30184 Jan 7 at 19:09
  • I am brain dead.... thanks.... I still have no idea why the ogr method didn't work – D_C Jan 7 at 20:37
  • Maybe if is because of ogr.GetDriverByName("GTiff"). GTiff is not a vector driver. – user30184 Jan 8 at 7:51
  • ?I am trying to delete a tif? So not vector. – D_C Jan 9 at 4:45
  • Functions beginning with ogr. are for vectors and functions beginning with gdal. are for rasters. – user30184 Jan 9 at 10:50

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.