我尝试在 Docker 环境中运行的 Jupyter Notebook 中使用 GDAL,GDAL 库工作正常,但当使用 SetConfigOption 设置配置时,它不起作用。
我在 Docker 文件中使用 Use jupyter/minimal-notebook 映像,并且没有使用 pip 和 conda 在映像上安装 GDAL 作为依赖项,但它们似乎都没有响应 SetConfigOption,当我在Windows 中的 Jupyter Notebook 似乎一切正常,没有任何问题。为什么会出现这种情况?
(当我打印 SetConfigOption 时,它表示配置已设置,但实际上它不会影响任何内容。)
这是我的 Jupyter Notebook 文件:
# Activating the GDAL Driver
# Set GDAL Config Parameters
# importing the libraries "GDAL" and "OGR"
# importing the library "os" with helps with some operating system specific i/o tasks (working with paths, directories, files, ..)
try:
from osgeo import gdal, ogr
import os
except:
print ('ERROR: cannot find GDAL/OGR modules')
gdal.UseExceptions()
gdal.SetConfigOption("CPL_LOG_ERRORS","ON")
gdal.SetConfigOption("VALIDATE", "YES")
gdal.SetConfigOption("FAIL_IF_VALIDATION_ERROR","YES")
gdal.SetConfigOption('WRITE_GFS','NO')
print(gdal.GetConfigOption("WRITE_GFS"))
driverName="GMLAS"
gml_driver = ogr.GetDriverByName(driverName)
if gml_driver is None:
raise Exception('GDAL driver >"+driverName+"< not available')
else: print("GDAL driver successfully loaded: >"+gml_driver.GetName()+"<")
gdal.VectorTranslate(
'test.geojson',
'Simple_XtraServerGetFeature (3).xml',
options='-f "GeoJSON"'
)
print(gdal.GetConfigOption("WRITE_GFS"))
这是我的 Docker 文件:
# Use the jupyter/minimal-notebook image as the parent image
FROM jupyter/minimal-notebook:latest
# Install required dependencies
RUN conda install -c conda-forge gdal
RUN pip install --no-cache-dir \
jupyter \
requests>=2.21.0
# Set the working directory to /src
WORKDIR /src
# Make port 8888 available to the world outside this container
EXPOSE 8888
# Define environment variable
ENV NAME World
# # Copy the current directory contents into the container at /app
# COPY . /src
# Mount the host machine directory into the container
VOLUME /src
# Run Jupyter Notebook when the container launches
CMD ["jupyter", "notebook", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root"]
问题可能是您需要显式关闭 gdal.VectorTranslate() 返回的数据集:
ds = gdal.VectorTranslate(....)
del ds # ogr ds.Close() with GDAL >= 3.8.0