TransWikia.com

Adding support to Esri's FileGDB driver when fiona/gdal/osgeo were installed through Anaconda

Geographic Information Systems Asked on June 8, 2021

Is there a way to tweak the fiona/gdal/osgeo packages installed through Anaconda so that they can be used to read/write GDB files using the ESRI FileGDB driver? The main thing I’m actually trying to achieve is writing to GDB a file.

I have found multiple answers online, but all of them involve re-installing OSGeo or GDAL from the python wheel files. These methods do not work for me because I installed fiona, OSGeo and GDAL using Anaconda. So none the solutions I found online (listed below) seem to work.

I have already downloaded the DLL file from ESRI, placed it in my C:...Libsite-packagesosgeo folder and added a new environment variable called GDAL_DRIVER_PATH indicating the appropriate path. However, contrary to the suggestions in the posts listed below, the __init__.py inside the osgeo folder does not have a commented line that reads os.environ['GDAL_DRIVER_PATH'] = os.path.join(os.path.dirname(__file__), 'gdalplugins'). Therefore, I am not sure where to include this line in my own file system.

Similar (unresolved) discussions can be found on the GeoPandas and on the Fiona Github pages.

Also, here is a list of related posts/questions/sites with relevant info but which ultimately did not solve my problem.

How I installed Fiona and other packages:

I’m currently using Windows 10. After installing Anaconda, I created a new environment called myenv and installed the main libraries I was going to use:

conda create -n myenv pandas geopandas fiona spyder numpy scipy

GDAL was automatically installed as one of the associated/required packages for fiona and geopandas.

In my command prompt, when I activate myenv and type gdalinfo --version, I get the following response:

GDAL 3.0.2, released 2019/10/28

When I use the python-based route, I get the same answer.

import osgeo.gdal
print(osgeo.gdal.__version__)
>3.0.2

Additional Notes:

If the answer involves something like "you need to compile/build GDAL from source files", please include a step-by-step on how to do so, including what to do after "building" (for example, where to put the files and how to get Python to use the newly-built version of GDAL within a specific conda environment). I know this is a lot to ask, and I apologize for doing so. It’s just that I have tried looking into this process and it is not very newbie-friendly.

3 Answers

There is also the dirty little trick to do pip install although conda is used in principle. That usually works. With that it could be easier to get fiona compiled. I tested the build instructions for windows from the fiona help page and they actually worked where I used OSGeo4W as the precompiled gdal source . I guess you have to fork fiona, clone it to local with git or download it and within the source you might want search for allowed drivers and at least add FileGDB there if it isnt allowed yet. Otherwise in the gitter channel of geopandas there was some hints about how to compile pyogrio in the right way. But actually in pyogrio code you really have to extend that allowed driver list. Because it actually also prevented me to use openapi, although it worked fine on gdal to open an url in a dataset. Nope isnt there for my gdal install https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#get-list-of-ogr-drivers-alphabetically-a-z And I cannot make sense of this here either: https://trac.osgeo.org/gdal/ticket/4420 from https://gdal.org/drivers/vector/filegdb.html#vector-filegdb tricky stuff

Answered by Sönke Schmachtel on June 8, 2021

You could actually also use gdal to open the dataset and iterate through its features, which is slow but should work. OGR/GDAL cookbook https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#load-data-to-memory <-Does that help you to get started with gdal for example? That is where I started to work with gdal and where I come back to when analysing the sources of fiona and pyogrio. GDAL is slow but should be working, if you know how to get FileGDB working within gdal that is. Much easier though than to build the python libraries from skratch. Chance that you find a non conda repo with built in FileGDB is bigger. Than you can pip install that one into your conda environment? I would start with that and with GDAL. I will crosscheck if FileGDB works with my installed GDAL which is somehow supplied by osgeo

Answered by Sönke Schmachtel on June 8, 2021

Are you sure that you cant do it? I think if you go through our discussion there Brendan confirmed that this driver list where you find FileGDB with only r option is not really in use. Or did you try and it didn't work/ and I understood something wrong or missread the code in pyogrio.

Anyway if you managed to find gdal drivers with FileGDB support you should be definitely able to use GDAL. Or did you use the OpenFileGDB driver with only read support? If you have the FileGDB somehow compiled into GDAL (or is it just an extension, I don't know?) then I would try this here (adapted from read to memory script in cookbook):

That's basically copying layer by layer with the appropriate layer/file or dir names.

from osgeo import ogr

#open an input datasource
indriver=ogr.GetDriverByName('FileGDB')
srcdb = indriver.Open('File1.gdb',0)

#create an output datasource in memory
outdriver=ogr.GetDriverByName('FileGDB')
source=outdriver.CreateDataSource('File2.gdb')

#open the memory datasource with write access
tmp=outdriver.Open('File2.gdb',1)

#copy a layer to memory
pipes_mem=source.CopyLayer(srcdb.GetLayer('layer_you_have'),'layer_you_have',['OVERWRITE=YES'])

Answered by Sönke Schmachtel on June 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP