Home  >  Article  >  Backend Development  >  How to install gdal in python

How to install gdal in python

(*-*)浩
(*-*)浩Original
2019-06-24 15:23:038263browse

GDAL (Geospatial Data Abstraction Library) is an open source raster spatial data conversion library under the X/MIT license. It utilizes an abstract data model to express the various file formats supported. It also has a range of command line tools for data conversion and processing.

How to install gdal in python

Method 1: Download the whl file corresponding to the python version at the URL https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal. In the command line pip install whl file full path installation (windows mode). (Recommended learning: Python video tutorial)

Method 2:

Command line conda/pip search gdal View version , select the appropriate version (mine is 2.2.4), if not, use method one.

Command line conda/pip install gdal=version number, be careful to add the version number, otherwise an old version may be installed (available for windows/linux).

The gdal package is used to process raster data, and ogr is used to process vector data.
The following program is a simple application of gdal for raster processing.

from osgeo import gdal
import numpy as np
np.set_printoptions(threshold=np.inf)#使print大量数据不用符号...代替而显示所有
dataset = gdal.Open("E:/RS_data/caijian1214/caijian.tif")
print(dataset.GetDescription())#数据描述
print(dataset.RasterCount)#波段数
cols=dataset.RasterXSize#图像长度
rows=(dataset.RasterYSize)#图像宽度
xoffset=cols/2
yoffset=rows/2
band = dataset.GetRasterBand(3)#取第三波段
r=band.ReadAsArray(xoffset,yoffset,1000,1000)#从数据的中心位置位置开始,取1000行1000列数据
band = dataset.GetRasterBand(2)
g=band.ReadAsArray(xoffset,yoffset,1000,1000)
band = dataset.GetRasterBand(1)
b=band.ReadAsArray(xoffset,yoffset,1000,1000)
import cv2
import matplotlib.pyplot as plt
img2=cv2.merge([r,g,b])
plt.imshow(img2)
plt.xticks([]),plt.yticks([]) # 不显示坐标轴
plt.show()

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to install gdal in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn