如何让 GDAL 与 Google GCLOUD 协同工作

如何让 GDAL 与 Google GCLOUD 协同工作

我在尝试将我的应用部署到 gcloud app engine 时收到以下错误。整个问题源于尝试将 GDAL 库添加到我的应用。

文件“/opt/python3.7/lib/python3.7/ctypes/init.py”,第 377 行,在 getattr func = self.getitem(name) 文件“/opt/python3.7/lib/python3.7/ctypes/init.py”,第 382 行,在 getitem func = self._FuncPtr((name_or_ordinal, self)) AttributeError:/usr/lib/libgdal.so.1:未定义符号:OGR_F_GetFieldAsInteger64

我按照网上能找到的所有说明操作。但似乎什么都不起作用。这是我的 app.yml 文件:

runtime: custom

entrypoint: gunicorn -b :8080 app.wsgi
env: flex 

# any environment variables you want to pass to your application.
# accessible through os.environ['VARIABLE_NAME']
env_variables:
 ... 
  
beta_settings:
  cloud_sql_instances: site:asia-northeast2:site-db

handlers:
- url: /.*
  script: auto
  secure: always
manual_scaling: 
  instances: 1

runtime_config:
  python_version: 3 

Dockerfile 如下:

FROM gcr.io/google-appengine/python
#FROM python:3.7
#FROM python:3.8.0-slim-buster

EXPOSE 8080
ENV PYTHONUNBUFFERED 1

# Install GDAL dependencies
#RUN apt-get update && apt-get install --yes libgdal-dev
RUN apt-get update && apt-get install --reinstall -y \
  #libopenjp2-7 \
  #libopenjp2-7-dev \
  #libgdal-dev \
  binutils \
  gdal-bin \
  python-gdal \
  python3-gdal 


# Update C env vars so compiler can find gdal
#ENV CPLUS_INCLUDE_PATH=/usr/include/gdal
#ENV C_INCLUDE_PATH=/usr/include/gdal
RUN export CPLUS_INCLUDE_PATH=/usr/include/gdal
RUN export C_INCLUDE_PATH=/usr/include/gdal

# Create a virtualenv for dependencies. This isolates these packages from
# system-level packages.
# Use -p python3 or -p python3.7 to select python version. Default is version 2.
RUN virtualenv /env -p python3

# Setting these environment variables are the same as running
# source /env/bin/activate.
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

# Copy the application's requirements.txt and run pip to install all
# dependencies into the virtualenv.
ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
# Add the application source code.
ADD . /app

# Run a WSGI server to serve the application. gunicorn must be declared as
# a dependency in requirements.txt.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app --timeout 0 --preload

更新: 正如网上大多数建议使用 Python3.8 图像一样。

FROM python:3.8

当我尝试这样做时,出现以下错误:

/bin/sh: 1: virtualenv: 未找到命令 '/bin/sh -c virtualenv /env -p python3' 返回非零代码:127 错误错误:构建步骤 0“gcr.io/cloud-builders/docker”失败:步骤以非零状态退出:127

有人能帮帮我吗?我需要做什么才能让它正常工作?

答案1

GAE 中的 GDAL 依赖关系可能非常麻烦。另外,我注意到为 flex 环境发布的 GAE 映像不包含 python3.8,而标准环境则应该包含 3.8(flex 一直到 python3.7,您可以在 中看到它们/opt/python3.*)因此,您应该自己安装 3.8 以及一些必要的 GIS 依赖关系。

尝试一下这个 Dockerfile:

FROM gcr.io/google-appengine/python
ENV PYTHONUNBUFFERED 1
ENV DEBIAN_FRONTEND noninteractive

RUN apt -y update && apt -y upgrade\
    && apt-get install -y software-properties-common \
    && add-apt-repository -y ppa:ubuntugis/ppa \
    && add-apt-repository -y ppa:deadsnakes/ppa \
    && apt -y update && apt -y upgrade\
    && apt-get -y install python3.8 python3.8-distutils python3.8-venv \
       gdal-bin libgdal-dev python3-gdal  \ 
    && apt-get autoremove -y \
    && apt-get autoclean -y \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN virtualenv /env -p python3.8
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

ADD requirements.txt /app/requirements.txt
RUN python3 -m pip install -r /app/requirements.txt 
ADD . /app/
WORKDIR /app
CMD gunicorn -b :$PORT app.wsgi

这使用 Deadsnakes PPA 在容器中安装 python3.8 二进制文件和 co. 以及较新的 GDAL/GIS 依赖项,因为 GAE 的容器基于 Ubuntu 16.04 LTS (xenial),它附带一些相当过时的软件包。请注意,您不需要设置编译器标志,因为您没有在容器中编译任何内容。

相关内容