Use GDAL with Python with Docker

Overview

I was a little addicted to using geotiff, so I made a memorandum.

In conclusion, it was because Pyhon had the wrong version support for the "GDAL" library and "libgdal-dev". ** The stable version "libgdal-dev" managed in the apt standard repository is v2.4.0 (as of November 19, 2020). As a result, v3.2.0 will be downloaded if you simply do "pip install GDAL" with pip, but "libgdal-dev" corresponding to v3.2.0 requires v3.2.0 or higher . Therefore, when installing, it is necessary to specify the version by setting " pip install GDAL == 2.4.4 **". The docker file etc. based on this area are described below.

Prerequisites

Here, we are thinking about the following.

--Use docker to use the environment. ――I don't like anaconda very much --Python is used because the learning cost is low --docker-compose is described only for operation test --Set the volume so that you can change the code --Since I am not particular about it, the base image is "python: 3.8" (probably the OS is Debian)

Construction

/
|-/app
| |-/image #Insert tif file
| |-test.py #Test code
| '-Dockerfile
'-docker-compose.yaml 

Dockerfile/docker-compose.yaml

Dockerfile


FROM python:3.8

ENV APP_HOME /app
WORKDIR $APP_HOME

RUN apt-get update && apt-get install -y tzdata \
    libgdal-dev

RUN pip install --upgrade pip
RUN pip install gdal==2.4.4

CMD ["python", "/app/test.py"]

docker-compose.yaml


version: "2"
services:
  test:
    container_name: "test"
    build:
      context: ./app
    volumes:
      - ./app:/app

test

The test code is as follows.

test.py


from osgeo import gdal, gdalconst

if __name__ == "__main__":
    print("gdal version", gdal.VersionInfo())
    file_name = '/app/image/1_index_ndvi.tif'
    src = gdal.Open(file_name,
                    gdalconst.GA_ReadOnly)  #loading tif(read only)
    print(type(src))  # "osgeo.gdal.Dataset"

Execution uses docker-compose, so it is as follows.

docker-compose up

in conclusion

This is quite common. If you want to use the latest version of the library, you may need to build the environment with ubuntu because there is no package in debian (in ubuntu, you can often use the test version by registering the repository). Basically, as long as you can read the data to some extent, it is faster to process the numerical values yourself, so I think that the stable version is sufficient.

Recommended Posts

Use GDAL with Python with Docker
Use Puphpeteer with Docker
Use ngrok with Docker
[Docker] Use whenever with Docker + Rails
Why use orchestration tools with Docker
Use cuda11.0 with pytorch using Docker
[Rails] How to use rails console with docker
Use Symbolic Link with Docker multi-stage builds
Use ZStandard with .NET Core + Docker (Alpine)
Use Amazon ECR Credential Helper with Docker Desktop
How to use docker compose with NVIDIA Jetson
How to use nginx-ingress-controller with Docker for Mac
Use ProGuard with Gradle
python notes on docker
Launch MariaDB with Docker
Rails deploy with Docker
Run Pico with docker
Explode Docker with WSL2
Why we use Docker
Use XVim2 with Xcode 12.0.1
Use CentOS with LXD
Operate Emby with Docker
Try WildFly with Docker
Run Payara with Docker
Use webmock with Rspec
[Docker] Connection with MySQL
Php settings with Docker
Getting Started with Docker
Use WebJars with Gradle
Use jlink with gradle
Disposable PHP with Docker
Install Composer with Docker
[Note] Build a Python3 environment with Docker in EC2
Periodically execute Python Script with Docker Compose + Daemon (cron)
Pytorch execution environment with Docker
Use Lambda Layers with Java
Use Thymeleaf with Azure Functions
Deploy with EC2 / Docker / Laravel
Run TAO Core with Docker
Docker management with VS Code
[PHP8] Install and use PECL YAML function (YAML parser) with Docker
Use docker's in-container shell with cygwin [docker exec -it bash]
Use pfx certificate with Okhttp3
Set up GitLab with docker
Use Bulk API with RestHighLevelClient
Use SDKMAN! With Git Bash
Prepare Python3 environment with CentOS7
Run Rails whenever with docker
Get started with DynamoDB with docker
Docker autostart settings with wsl2
[Docker] Rails 5.2 environment construction with docker
Use multiple databases with Rails 6.0
Spring Boot starting with Docker
Build docker environment with WSL
Version control CocoaPods with Docker
How to use mysql with M1 mac Docker preview version
Use Spring JDBC with Spring Boot
Web application built with docker (1)
I tried BIND with Docker
Use Ruby with Google Colab
Use SpatiaLite with Java / JDBC