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.
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)
/
|-/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
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
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