DriverError: unsupported driver: 'LIBKML' When using GDAL to trim a GEOTiff File around a KMZ

115 Views Asked by At

I am working with the below code that will take a Geotiff File and trim it around the boundary of a KMZ file. I am using Anaconda Navigator and working in macOS.

import geopandas as gpd
import rasterio
from shapely.geometry import box
from rasterio.mask import mask


def trim_geotiff(geotiff_path, kmz_path, output_path):
    # Open the GeoTIFF
    with rasterio.open(geotiff_path, driver='GTiff') as src:
        # Read the KMZ file using geopandas
        kmz_gdf = gpd.read_file(kmz_path)
        
        # Merge all geometries in the KMZ file
        kmz_geometry = kmz_gdf.geometry.unary_union

        # Create a bounding box geometry
        bbox = box(*kmz_geometry.bounds)

        # Crop the GeoTIFF using the bounding box
        out_image, out_transform = mask(src, [bbox], crop=True)

        # Update metadata for the new GeoTIFF
        out_meta = src.meta
        out_meta.update({
            "driver": "GTiff",
            "height": out_image.shape[1],
            "width": out_image.shape[2],
            "transform": out_transform
        })

        # Write the trimmed GeoTIFF to the specified output path
        with rasterio.open(output_path, "w", **out_meta) as dest:
            dest.write(out_image)


geotiff_path = "/Users/Path/To/GEOTIFF/Geotiff.tif"
kmz_path = "/Users/Path/To/KMZ/kmz_file.kmz"
output_path = "/Users/Output/OutputFile_001.tif" 

trim_geotiff(geotiff_path, kmz_path, output_path)

running this code produces the DriverError: unsupported driver: 'LIBKML' error.

After doing some research it seems that the issue could be with the LIBKML dependency being absent from the GDAL Library. If I am using Anaconda do I need to manually toggle this dependency? Or is it auto installed when I install GDAL using conda install -c conda-forge gdal

0

There are 0 best solutions below