0

I'm new to Python.

I have two dataset folders one with subfolders containing shapefiles and the other with subfolders containing raster. I'm trying to complete a loop that takes the polygons of the first subfolder that have in common the same attribute e.g. 'class' and uses them to make a clip on the first raster subfolder, repeating the loop for the second folder ec.., saving all the results in a new folder with the subfolders of its new cut raster. I tried using gdal but the output only returns the list of files ending with '.tiff'.

How can I fix it?

import os, fnmatch
import geopandas as gpd
import gdal

Workspace = 'F://...//rasters'
S2_path = os.path.join (Workspace)

for subdir, dirs, files in os.walk(S2_path):
    for file in files:
        filepath = subdir + os.sep + file
        if filepath.endswith(".tif"):
            print (filepath)
            
OutputFolder = 'F://...//output/'

for polygon in polygons:
    gdf=gpd.read_file('F://...//shapefile.shp')
    df1 = gdf[['class','geometry']]  
    options = gdal.WarpOptions(cutlineDSName=df1,cropToCutline=True)
    outRaster = gdal.Warp(srcDSOrSrcDSTab=filepath,
                        destNameOrDestDS=OutputFolder + '_crop',
                        options=options)
    outRaster= None

1 Answer 1

1

Not sure what's the error of the code, but print(filepath) won't solve the problem. Try to modify this way:

for subdir, dirs, files in os.walk(S2_path):
for file in files:
    filepath = subdir + os.sep + file
    if filepath.endswith(".tif"):
        print (filepath)

Instead:

filepaths = []
for subdir, dirs, files in os.walk(S2_path):
for file in files:
    filepath = subdir + os.sep + file
    if filepath.endswith(".tif"):
        filepaths.append(filepath)

poly_path = "d:/here_is_my_polygon.shp"
i = 0
raster_path in filepaths:
   output_path = "d:/here_is_my_polygon_raster" + str(i) + ".tif"
   result = gdal.Warp(output_path, raster_path, cutlineDSName=poly_path, cropToCutline=True, dstNodata = 0)
   i = i + 1
   ...

So basically cumulate rasters to a list, and iterate over it. And forget the 'geopandas' with gdal, you can simply. Just a notice, the raster and vector have to be in the same CRS (reference system, coordinate system). I guess you can play with this pattern.

2
  • Thank you for your answer, your code uses only one shapefile, I would like to iterate the first shapefile on all the files in the first folder, the second shapefile for the files in the second folder, etc... Selecting the starting shp based on a unique numeric attribute. How could I do that?
    – vins_26
    Jun 2, 2020 at 9:36
  • For this you only need to implement some control operations like extra for loops, lists, if-s. Append the file paths to some lists, then you can do them what you want by using the pattern provided in my answer.
    – pnz1337
    Jun 2, 2020 at 14:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.