一定不要在之前裝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 開源地理信息組織負責維護的庫. 所以不用擔心相互的兼容性, 盡可以放心.
只不過現在拆分得相對獨立, 獨立升級, 發布.
在編譯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
主要負責處理各種大地/投影坐標系變換. 也是事實上的基礎設施.
以前叫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 事先需要
安裝: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.9.1.tar.bz2 bunzip2 geos-3.9.1.tar.bz2 tar xvf geos-3.9.1.tar cd geos-3.9.1
./configure --enable-shared
make
sudo make install
sudo ldconfig
geos3.8.1以后, 官方已經不支持通過configure配置python bind了,官方要求直接用shaply
https://github.com/libgeos/geos
以下在3.9.1不再適用
因為編譯時已經帶python了, 所以安裝成功后, 在python中可以直接使用ogr
import ogr geom = ogr.CreateGeometryFromWkt("POINT(1 1)") geom geom.ExportToWkt()
2.2 PROJ
20210330 proj 8.0.0
依賴
sudo apt-get install -y sqlite3 libtiff5-dev libcurl4-gnutls-dev
本身編譯過程沒有太特殊的
./configure
make -j 4
sudo make install
sudo ldconfig
完事。編譯時間較長。
如果cofigure報錯找不到sqlite3 就下載源碼
https://sqlite.org/download.html
自己編譯一下
https://www.cnblogs.com/xuanmanstein/p/13227545.html
注意加入
#define SQLITE_ENABLE_COLUMN_METADATA 1不然編譯gdal還是要報錯
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 librasterlite2-dev
再一次需要python
export PYTHON=/usr/local/bin/python3.8
如果是用系統自帶的python
export PYTHON=/usr/bin/python3.8
cd ~/gdal-3.2.2 ./configure --with-python --with-spatialite
3.0.4只需要上面的 --with-python就可以了。 下面這些都不必要,備用
--with-spatialite --with-proj=/usr/local--with-rasterlite2
源碼安裝 librasterlit2
然后肉眼檢查一下配置:
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: noLIBTOOL 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: pythonPROJ >= 6: yes
enable GNM building: yes
enable pthread support: yes
enable POSIX iconv support:yes
hide internal symbols: no
然后
make -j 4 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不會有