[Python] Automate QGIS v.kernel.rast across multiple nested folders with Python

Discussão em 'Python' iniciado por Stack, Julho 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using QGIS 3.40.8 and need to automate kernel density calculations across a nested folder structure. I don't know Python - the code below was created by an LLM based on my QGIS log output from running v.kernel.rast manually in the GUI.

    Current working code (single folder):

    import processing
    import os
    from qgis.core import QgsRasterLayer

    # === Inputs ===
    point_layer = 'main_folder/manchester/2018/01/poi.shp'
    reference_raster = 'main_folder/manchester/2018/01/lc.tif'
    output_dir = 'main_folder/manchester/2018/01/'

    # === Bandwidths to test ===
    bandwidths = [50, 100, 150, 200]

    # === Extract parameters from reference raster ===
    print("Extracting parameters from reference raster...")
    ref_layer = QgsRasterLayer(reference_raster, "reference")

    if not ref_layer.isValid():
    print(f"ERROR: Could not load reference raster: {reference_raster}")
    exit()

    # Get extent
    extent = ref_layer.extent()
    region_extent = f"{extent.xMinimum()},{extent.xMaximum()},{extent.yMinimum()},{extent.yMaximum()} [EPSG:{ref_layer.crs().postgisSrid()}]"

    # Get pixel size
    pixel_size = ref_layer.rasterUnitsPerPixelX()

    print(f"Extracted region extent: {region_extent}")
    print(f"Extracted pixel size: {pixel_size}")

    # === Kernel density loop ===
    for radius in bandwidths:
    output_path = os.path.join(output_dir, f'kernel_bw_{radius}.tif')
    print(f"Processing bandwidth: {radius}...")
    processing.run("grass7:v.kernel.rast", {
    'input': point_layer,
    'radius': radius,
    'kernel': 5, # Gaussian
    'multiplier': 1,
    'output': output_path,
    'GRASS_REGION_PARAMETER': region_extent,
    'GRASS_REGION_CELLSIZE_PARAMETER': pixel_size,
    'GRASS_RASTER_FORMAT_OPT': 'TFW=YES,COMPRESS=LZW',
    'GRASS_RASTER_FORMAT_META': ''
    })

    print("All kernel rasters created.")


    Folder structure:

    main_folder/
    ├── city (e.g., rome)/
    │ ├── year (e.g., 2018)/
    │ │ ├── month (e.g., 11)/
    │ │ │ ├── poi.shp
    │ │ │ └── lc.tif
    │ │ └── 04/
    │ │ ├── poi.shp
    │ │ └── lc.tif
    │ └── 2019/
    │ └── 11/
    │ ├── poi.shp
    │ └── lc.tif
    └── london/
    └── 2021/
    └── 03/
    ├── poi.shp
    └── lc.tif


    What I need:

    • Loop through all monthly folders following the pattern: main_folder/city/year/month/
    • Skip folders that don't contain poi.shp
    • Run kernel density analysis for each valid monthly folder
    • Save output rasters in the same monthly folder where poi.shp is located
    • Files are consistently named: poi.shp (points) and lc.tif (reference raster)

    How can I modify this code to automatically iterate through the entire nested folder structure?

    Continue reading...

Compartilhe esta Página