zoukankan      html  css  js  c++  java
  • install gdal from source on ubuntu

    一定不要在之前装qgis,那样会安装一堆XXX-dev, python3-XXX, 那就直接是给系统自带的python3.6用了。而且会装编译好的gdal库,影响编译。

    用新立得包管理器,确保没有安装的proj,gdal rasterlite2 等等库。

    总之,开发机还是先编译gdal为敬。

     

    为什么要自己编译gdal?

    想在自己安装的python里用gdal,pipi官网gdal就是这么给的:自己编译。

    https://pypi.org/project/GDAL/

    如果用ubuntu自带的python3.6,是不用自己编译的。比如安装qgis的时候就会安装一堆lib

    但是用自己编译的高版本python3.8,就必须得自己编译gdal

    如果只是简单用geopandas 或者shapley 根本不需要这么麻烦,直接pip install 就可以了.

    windows下安装gdal这这样介绍的

    https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal

    GDAL: the Geospatial Data Abstraction Library is a translator library for raster geospatial data formats.
    This distribution includes a complete GDAL installation. Do not use together with OSGeo4W or gdalwin32.
    Built with KML, HDF5, NetCDF, SpatiaLite, PostGIS, GEOS, PROJ etc.

    基本够用了,

    非要在linux下编译安装的原因是:   用win下编译好的python 的gdal库, 在读取osm.pdf数据文件 在Getlayer时,只能读"points", 其他的lines, multpolygons 读出的feature都是-1个

    怀疑是gdal和ogr功能不全,缺乏对OSM的完整支持. 但是要编译,就要从各个依赖库编译起.梳理一下

    1.GIS相关的几个库的关系:

    共性: 都是osgeo 开源地理信息组织负责维护的库.  所以不用担心相互的兼容性, 尽可以放心.

    http://download.osgeo.org/

    只不过现在拆分得相对独立,  独立升级, 发布.

    在编译gdal的时候其实默认也是需要依赖的其他这些的.

    1.1 GEOS:

    注意是Geometry Engine, Open Source

    https://trac.osgeo.org/geos/#GEOS-GeometryEngineOpenSource

    不是Google Earth Overlay Server

    (https://geos.readthedocs.io/en/latest/index.html#)

    GEOS (Geometry Engine - Open Source) is a C++ port of the ​JTS Topology Suite (JTS). It aims to contain the complete functionality of JTS in C++.

    This includes all the ​OpenGIS Simple Features for SQL spatial predicate functions and spatial operators, as well as specific JTS enhanced functions.

    GEOS provides spatial functionality to many other projects and products.

    字面意思是: 几何引擎开源. 

    是JTS的C++版

    包括空间预测函数和空间运算符, 

    被大量其他项目和产品使用

    地位基本相当于科学计算领域的BLAS,LAPACK. 所以,是基础设施的基础设施.  

    因为聚焦于点/线/面 的定义与计算, 所以源码体积很小: 才2M

     1.2 PROJ

    https://github.com/OSGeo/PROJ

    主要负责处理各种大地/投影坐标系变换. 也是事实上的基础设施.   

    以前叫proj.4  现在版本升级到了6, 名字也直接叫proj了

    在编译gdal时依赖 .

    1.3 GDAL

     gdal在gis地位约等于cv领域里opencv的地位,   起到"系统集成"和粘合剂的作用.

    读写各种矢量和栅格文件(依赖各种libXXX-dev的IO库(tiff,  kml, ncf, ....)),   然后用proj变换投影, 用geos(ogr)进行空间分析,几何计算.

    看上去在整个计算模型:

    1read

    2proj

    3compute

    4proj

    5write

    每个功能的具体实现都是来自外部的.

    gdal实现了一些统一的抽象,比如对tiff和kml 都可以getlayer.

    也提供了完整的python接口. (所以说类似opencv)

    虽然gdal也升级, 但基本把具体功能由各个"专业分队"实现,放到系统之外, 这样的切分思路,很值得借鉴. 

    所以要最后编译, 带上 各种 编译选项  --with-geos  python 之类的.

    2编译与安装

    参考

    https://gist.github.com/robinkraft/2a8ee4dd7e9ee9126030

    https://django-doc-test1.readthedocs.io/en/stable-1.5.x/ref/contrib/gis/install/geolibs.html

    为了让geos和gdal都支持python binding 事先需要

    https://stackoverflow.com/questions/49234367/trying-to-install-geos-for-basemap-python-python-bindings-false

    安装:swig

    sudo apt install -y swig

    因为我是用单独安装的 python3.8 而不是 18.04自带的3.6

    所以编译前需要

    确保python 安装了numpy, 否则在python中实际使用gdal时, 会报告 ModuleNotFoundError: No module named 'osgeo._gdal_array' 

    安装前需要确定使用的python, 如果是20.04 自带3.8 就用系统自带的

    用自己安装的python 

    export PYTHON=/usr/local/bin/python3.8

    如果是用系统自带的python

    export PYTHON=/usr/bin/python3.8

    2.1  geos

    在这里下载最新版

    http://download.osgeo.org/geos/

    cd ~
    wget http://download.osgeo.org/geos/geos-3.8.1.tar.bz2
    bunzip2 geos-3.8.1.tar.bz2
    tar xvf  geos-3.8.1.tar
    
    cd geos-3.8.1
    ./configure --enable-shared --enable-python
    make
    sudo make install
    sudo ldconfig

    geos3.8.1以后,官方已经不支持通过configure配置python bind了,官方要求直接用shaply

    https://github.com/libgeos/geos

    因为编译时已经带python了, 所以安装成功后, 在python中可以直接使用ogr

    import ogr
    geom = ogr.CreateGeometryFromWkt("POINT(1 1)")
    geom
    geom.ExportToWkt()

     

     

    2.2 PROJ

    https://github.com/OSGeo/PROJ

    20200702  proj 7.0.1

    依赖

    sudo apt-get install -y sqlite3 libtiff5-dev libcurl4-gnutls-dev

    如果cofigure报错找不到sqlite3 就下载源码

    https://sqlite.org/download.html

    自己编译一下 

    https://www.cnblogs.com/xuanmanstein/p/13227545.html

    注意加入

    #define SQLITE_ENABLE_COLUMN_METADATA 1

    不然编译gdal还是要报错 

    本身编译过程没有太特殊的

    ./configure

    make

    sudo make install

    sudo ldconfig

    完事。编译时间较长。

    2.3 GDAL

    尝试, 如果不行再继续下面安装步骤

    sudo pip3.8 install GDAL 

    https://gdal.org/download.html

    20200702 3.1.1  mint20  配合proj7.0.1 系统自带的python3.8.2 编译通过 

    没有必要刻意装各种driver,  最常用的shp, tiff 都是支持的. 其他的用到什么再自己编译一遍 .

    参考https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal 的说明:

    Built with KML, HDF5, NetCDF, SpatiaLite, PostGIS, GEOS, PROJ etc.

    安装 libkml, libxml2 libhdf5-dev libnetcdf-c++4-dev libspatialite-dev 

    sudo apt install -y libkml-dev libxml2-dev libhdf5-dev libnetcdf-c++4-dev libspatialite-dev

    再一次需要python

    export PYTHON=/usr/local/bin/python3.8

    如果是用系统自带的python

    export PYTHON=/usr/bin/python3.8
    
    cd ~/gdal-3.1.1
    ./configure --with-python   

    3.0.4只需要上面的 --with-python就可以了。 下面这些都不必要,备用

    --with-spatialite  --with-proj=/usr/local --with-rasterlite2


     源码安装 librasterlit2

     https://www.cnblogs.com/xuanmanstein/p/12365924.html 

    然后肉眼检查一下配置:

    GDAL is now configured for x86_64-pc-linux-gnu

    Installation directory: /usr/local
    C compiler: gcc -DHAVE_AVX_AT_COMPILE_TIME -DHAVE_SSSE3_AT_COMPILE_TIME -DHAVE_SSE_AT_COMPILE_TIME -g -O2
    C++ compiler: g++ -DHAVE_AVX_AT_COMPILE_TIME -DHAVE_SSSE3_AT_COMPILE_TIME -DHAVE_SSE_AT_COMPILE_TIME -g -O2
    C++14 support: no

    LIBTOOL support: yes

    LIBZ support: external
    LIBLZMA support: no
    ZSTD support: no
    cryptopp support: no
    crypto/openssl support: yes
    GRASS support: no
    CFITSIO support: no
    PCRaster support: internal
    LIBPNG support: external
    DDS support: no
    GTA support: no
    LIBTIFF support: external (BigTIFF=yes)
    LIBGEOTIFF support: internal
    LIBJPEG support: external
    12 bit JPEG: no
    12 bit JPEG-in-TIFF: no
    LIBGIF support: external
    JPEG-Lossless/CharLS: yes
    OGDI support: no
    HDF4 support: no
    HDF5 support: yes
    Kea support: no
    NetCDF support: yes
    NetCDF has netcdf_mem.h: yes
    Kakadu support: no
    JasPer support: no
    OpenJPEG support: yes
    ECW support: no
    MrSID support: no
    MrSID/MG4 Lidar support: no
    JP2Lura support: no
    MSG support: no
    EPSILON support: no
    WebP support: yes
    cURL support (wms/wcs/...):yes
    PostgreSQL support: yes
    LERC support: yes
    MySQL support: no
    Ingres support: no
    Xerces-C support: no
    Expat support: yes
    libxml2 support: yes
    Google libkml support: yes
    ODBC support: no
    FGDB support: no
    MDB support: no
    PCIDSK support: internal
    OCI support: no
    GEORASTER support: no
    SDE support: no
    Rasdaman support: no
    RDB support: no
    DODS support: no
    SQLite support: yes
    PCRE support: yes
    SpatiaLite support: yes
    RasterLite2 support: yes
    Teigha (DWG and DGNv8): no
    INFORMIX DataBlade support:no
    GEOS support: yes
    SFCGAL support: no
    QHull support: internal
    Poppler support: no
    Podofo support: no
    PDFium support: no
    OpenCL support: no
    Armadillo support: no
    FreeXL support: no
    SOSI support: no
    MongoDB support: no
    MongoCXX v3 support: no
    HDFS support: no
    TileDB support: no
    EXR support: no
    userfaultfd support: yes
    misc. gdal formats: aaigrid adrg aigrid airsar arg blx bmp bsb cals ceos ceos2 coasp cosar ctg dimap dted e00grid elas envisat ers fit gff gsg gxf hf2 idrisi ignfheightasciigrid ilwis ingr iris iso8211 jaxapalsar jdem kmlsuperoverlay l1b leveller map mrf msgn ngsgeoid nitf northwood pds prf r raw rmf rs2 safe saga sdts sentinel2 sgi sigdem srtmhgt terragen til tsx usgsdem xpm xyz zmap rik ozi grib eeda plmosaic rda wcs wms wmts daas rasterlite mbtiles pdf
    disabled gdal formats:
    misc. ogr formats: aeronavfaa arcgen avc bna cad csv dgn dxf edigeo flatgeobuf geoconcept georss gml gmt gpsbabel gpx gtm htf jml mvt ntf openair openfilegdb pgdump rec s57 segukooa segy selafin shape sua svg sxf tiger vdv wasp xplane idrisi pds sdts ods xlsx amigocloud carto cloudant couchdb csw elastic ngw plscenes wfs gpkg vfk osm
    disabled ogr formats:


    SWIG Bindings: python

    PROJ >= 6: yes
    enable GNM building: yes
    enable pthread support: yes
    enable POSIX iconv support:yes
    hide internal symbols: no

     然后

    make
    sudo make install 
    sudo ldconfig

     编译速度不算快。 注意必须有最后1句,不然在python 里 import gdal 会找不到libgdal.o

     20200702 编译中,出现

    File "setup.py", line 155, in fetch_config
    r = p.stdout.readline().decode('ascii').strip()
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 16: ordinal not in range(128)

    初步判断是因为python是3了, 所以直接

    xed ./swig/python/setup.py

    找到第155行,直接修改

                #r = p.stdout.readline().decode('ascii').strip()
                r = p.stdout.readline().decode('utf-8').strip()

    如果在python中提示:

    ERROR 4: Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files.
    Traceback (most recent call last):

      

    xed ~/.profile

    export GDAL_DATA=/usr/local/lib/python3.8/site-packages/fiona/gdal_data

    或者

    export GDAL_DATA="$HOME/.local/lib/python3.8/site-packages/fiona/gdal_data"

    注意, 必须 export  否则 os.environ不会有

  • 相关阅读:
    opencv访问图像像素
    利用chrome浏览器的proxy switchy扩展智能切换代理
    一些linux命令【ubuntu】
    如何从 Ubuntu 10.04 升级到 10.10
    关于“无法获得排它锁 ”的解决办法
    【ubuntu】Grub2配置详解(转)
    BibTeX使用介绍
    【ubuntu】imagemagick用法
    ubuntu环境下安装OpenCV
    安装wordpress
  • 原文地址:https://www.cnblogs.com/xuanmanstein/p/12356020.html
Copyright © 2011-2022 走看看