python3安装FileGDB读写驱动
环境
系统:win10x64
python:3.6.5x64
安装步骤
安装gdal: GDAL‑2.4.1‑cp36‑cp36m‑win_amd64.whl
pip install GDAL-2.4.1-cp36-cp36m-win_amd64.whl
安装FileGDBAPI.dll:下载地址
拷贝FileGDBAPI.dll
至C:\Program Files\Python36\Lib\site-packages\osgeo
目录,安装目录与python安装目录一致
测试,已成功加载FileGDB:
C:\WINDOWS\system32>ogrinfo --formats
Supported Formats:
FileGDB -vector- (rw+): ESRI FileGDB
PCIDSK -raster,vector- (rw+v): PCIDSK Database File
netCDF -raster,vector- (rw+s): Network Common Data Format
JP2OpenJPEG -raster,vector- (rwv): JPEG-2000 driver based on OpenJPEG library
...
ipython测试,可以看到gdb_driver为空,目前为止还是不能使用
>ipython
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from osgeo import ogr
In [2]: gdb_driver=ogr.GetDriverByName("FileGDB")
In [3]: gdb_driver
In [4]:
修改osgeo/__init__.py文件:注释15行,取消注释17行
# __init__ for osgeo package.
# unofficial Windows binaries: set GDAL environment variables if necessary
import os
try:
_here = os.path.dirname(__file__)
if _here not in os.environ['PATH']:
os.environ['PATH'] = _here + ';' + os.environ['PATH']
if 'GDAL_DATA' not in os.environ:
os.environ['GDAL_DATA'] = os.path.join(_here, 'data', 'gdal')
if 'PROJ_LIB' not in os.environ:
os.environ['PROJ_LIB'] = os.path.join(_here, 'data', 'proj')
if 'GDAL_DRIVER_PATH' not in os.environ:
#pass
# uncomment the next line to enable plugins
os.environ['GDAL_DRIVER_PATH'] = os.path.join(_here, 'gdalplugins')
except Exception:
pass
del os
再次测试,成功!
>ipython
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from osgeo import ogr
In [2]: gdb_driver=ogr.GetDriverByName("FileGDB")
In [3]: gdb_driver
Out[3]: <osgeo.ogr.Driver; proxy of <Swig Object of type 'OGRDriverShadow *' at 0x00000277407BBC60> >
In [4]:
原文链接: https://blog.youkuaiyun.com/this_is_id/article/details/103272505
测试代码
# -*- coding: utf-8 -*-
import osgeo
from osgeo import ogr, osr, gdal
import sys
gdb_driver = ogr.GetDriverByName("FileGDB")
print(gdb_driver)
gdb_driver = ogr.GetDriverByName("ESRI Shapefile")
print(gdb_driver)
gdb_driver = ogr.GetDriverByName("Geojson")
print(gdb_driver)
None
<osgeo.ogr.Driver; proxy of <Swig Object of type 'OGRDriverShadow *' at 0x0000017AFB5D6B40> >
<osgeo.ogr.Driver; proxy of <Swig Object of type 'OGRDriverShadow *' at 0x0000017AFC0C0F90> >
添加头后:
# -*- coding: utf-8 -*-
import osgeo
from osgeo import ogr, osr, gdal
import sys
# 激活filegdb,打包后识别filegdb中文数据目录不识别、pycharm中识别filegdb和中文数据目录
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO")
gdal.AllRegister()
# 激活中文路径,打包后识别中文数据目录不识别、pycharm中识别中文数据目录
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
# # 中文路径
# # gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
# # 中文字段
# gdal.SetConfigOption("SHAPE_ENCODING", "")
# mapinfo_driver = ogr.GetDriverByName('MapInfo File')
# # shp_driver = ogr.GetDriverByName('ESRI Shapefile')
# mem_driver = ogr.GetDriverByName('Memory')
# gdb_driver = ogr.GetDriverByName("OpenFileGDB")
gdb_driver = ogr.GetDriverByName("FileGDB")
print(gdb_driver)
gdb_driver = ogr.GetDriverByName("ESRI Shapefile")
print(gdb_driver)
gdb_driver = ogr.GetDriverByName("Geojson")
print(gdb_driver)
<osgeo.ogr.Driver; proxy of <Swig Object of type 'OGRDriverShadow *' at 0x0000024DD1EC6B40> >
<osgeo.ogr.Driver; proxy of <Swig Object of type 'OGRDriverShadow *' at 0x0000024DD29B0F90> >
<osgeo.ogr.Driver; proxy of <Swig Object of type 'OGRDriverShadow *' at 0x0000024DD1EC6B40> >
pyinstaller 打包
不加-F:
# 激活filegdb,打包后识别filegdb但中文数据目录不识别、pycharm中识别filegdb和中文数据目录
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO")
gdal.AllRegister()
# 激活中文路径,打包后识别中文数据目录、pycharm中识别中文数据目录
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
pyinstaller testgdb.py ---拷贝osgeo中的东西到osgeo
pyinstaller --add-data='osgeo;osgeo' testgdb.py
加-F:
pyinstaller -F testgdb.py ---找不到osgeo
pyinstaller -F --add-data='osgeo;osgeo' testgdb.py 识别filegdb和中文