I would like to be able to open and view a GDB file (GIS file for raster data) in Python.
The data is official US government GIS data, the file can be downloaded publicly here: https://www.fs.usda.gov/detail/r5/communityforests/?cid=fseprd1009698
there is also an arcgis app that hosts the data so that it can be viewed: https://usfs.maps.arcgis.com/apps/webappviewer/index.html?id=d1bb42e8e14c46528bbedcede6b03e62
When clicking the download button a zip file is downloaded that has a folder with many GIS files within it.
I am looking into two options:
Is there a way to directly read a GDB folder or zip file in Python?
Is there a way to convert the GDB file to another format like GeoTIFF and then load that file into Python?
2 Answers 2
I downloaded the Oahu dataset and had a try with GDAL v3.8.4 installed with OSGeo4W.
GDAL can read the raster layer but it is awfully slow to read the whole dataset. It may be better to convert the data into GeoTIFF and add overviews. However, it seems that the mask band that GDAL creates makes even the GeoTIFF version very slow to view with QGIS at small scale. That happens because GDAL does not create automatically overviews for the .msk band. The fix is to create overviews for .msk manually. GDAL 3.9 will do it automatically.
gdal_translate -of gtiff -co tiled=yes -co compress=deflate Oahu_Canopy_2021.gdb oahu_canopy_2021.tif
gdaladdo oahu_canopy_2021.tif
gdaladdo oahu_canopy_2021.tif.msk
The FileGDB version does not have overviews and it is usable for browsing only at scales 1:20000 and larger.
What I tested with gdal_translate applies also to GDAL Python bindinds. It is possible to read data directly from the FileGDB but it may make sense to convert data into GeoTIFF first.
-
@MustardTiger The rendering can be improved with overviews
gdaladdo -ro --config COMPRESS_OVERVIEW DEFLATE --config NBITS 1 oahu_canopy_2021.tif 2 4 8 16
. Notes:--config NBITS 1
is required as the raster is 1 bit; default resampling isnearest
but can be changed using-r [resampling_method]
e.g.-r average
, seegdaladdo
documentation.user2856– user28562024年04月03日 21:27:47 +00:00Commented Apr 3, 2024 at 21:27 -
At least my GDAL version keeps NBITS as 1 without specially demanding it. Gdaladdo was not enough for making rendering fast because rendering was still slow due to the full resolution .msk file. Therefore the need to run gdaladdo also for the .msk file. In the next GDAL version that will happen automatically github.com/OSGeo/gdal/pull/9603. Compressing the overviews is a good suggestion.user30184– user301842024年04月04日 05:57:02 +00:00Commented Apr 4, 2024 at 5:57
-
"my GDAL version keeps NBITS as 1 without specially demanding it", yes I'm also running v3.8.4, but my
NBITs 1
comment was referring togdaladdo
which errored if I didn't specify--config NBITS 1
notgdal_translate
which automatically output a 1 bit tiff from the FGDB raster.user2856– user28562024年04月04日 22:40:31 +00:00Commented Apr 4, 2024 at 22:40 -
gdaladdo -ro --config COMPRESS_OVERVIEW DEFLATE oahu_canopy_2021.tif
works for me. What error did you get?user30184– user301842024年04月05日 06:11:28 +00:00Commented Apr 5, 2024 at 6:11
A file geodatabase can store both vector and raster data, see What is a file geodatabase?.
Vector data can be read using geopandas
import geopandas as gpd
import fiona
file_geodatabase = r"C:\GIS\GIStest\test.gdb" #A file geodatabase with multiple vector layers in it
#Print them
for feature_class in fiona.listlayers(file_geodatabase):
print(feature_class)
# test
# randompoints
# roads_with_gap_GenerateNearT
# roads_with_gap_G_TableSelect
# nature
df = gpd.read_file(filename=file_geodatabase, layer="randompoints") #Read one layer
print(df.head(2))
# ID Value Date geometry
# 0 A 1 2023年05月31日 00:00:00+00:00 POINT (714582.638 6728435.051)
# 1 A 5 2023年06月01日 00:00:00+00:00 POINT (1149955.918 6240796.036)
df.to_file(r"C:\GIS\GIStest\random_points_converted.gpkg") #Save as geopackage
If you have raster layers you should be able to use GDAL
-
I ran your code and got the following error: not recognized as a supported file format.Mustard Tiger– Mustard Tiger2024年04月03日 05:59:42 +00:00Commented Apr 3, 2024 at 5:59
-
Is it a raster or vector layer you are trying to open?Bera– Bera2024年04月03日 06:44:08 +00:00Commented Apr 3, 2024 at 6:44
-
1@MustardTiger You need a recent version of GDAL. Raster support was added to the FileGDB driver in GDAL 3.7.user2856– user28562024年04月03日 07:14:43 +00:00Commented Apr 3, 2024 at 7:14
Explore related questions
See similar questions with these tags.