如何讓 GDAL 與 Google GCLOUD 配合使用

如何讓 GDAL 與 Google GCLOUD 配合使用

我在嘗試將應用程式部署到 gcloud 應用程式引擎時收到以下錯誤。整個問題是由於嘗試將 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_GetFieldAsInt64

我遵循了我在網上可以找到的所有指示。但似乎沒有任何作用。這是我的 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 ERROR ERROR: build step 0 "gcr.io/cloud-builders/ docker」失敗:步驟以非零狀態退出:127

有人在這裡幫助我。我需要做什麼才能使其正常工作?

答案1

GDAL 與 GAE 的依賴可能會非常痛苦。另外,我注意到為 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 二進位檔案等。在容器中以及較新的 GDAL/GIS 依賴項,因為 GAE 的容器是基於 Ubuntu 16.04 LTS (xenial),它附帶了一些相當過時的軟體套件。請注意,您不需要設定編譯器標誌,因為您沒有在容器中編譯任何內容。

相關內容