Read a table from an ESRI file geodatabase (.gdb) using R
This can now be read by sf
, with
vessel <- sf::st_read(dsn = "Zone9_2014_01.gdb", layer = "Zone9_2014_01_Vessel")
It returns a warning (no feature geometries present) but also a data.frame with the table. See the thread that started here: https://stat.ethz.ch/pipermail/r-sig-geo/2018-February/026344.html
I use GDAL 2.0.2 that is "shipped" with FDGB support and without a third party a FGDB driver to investigate that stuff. The test environment is Debian Jessie 64-bit.
In short, it seems, that the "layer" Zone9_2014_01_Vessel
contains pure attribute data and the layer Zone9_2014_01_Broadcast
contains position data. You can use a workaround within R via a system call and the conversation of the GDB to a shapefile container (last script at the end of the answer).
Here are the investigation steps:
$ mkdir ~/dev.d/gis-se.d/gdb
$ cd ~/dev.d/gis-se.d/gdb
$ wget https://coast.noaa.gov/htdata/CMSP/AISDataHandler/2014/01/Zone9_2014_01.zip
$ unzip Zone9_2014_01.zip
$ ogrinfo Zone9_2014_01.gdb Zone9_2014_01_Vessel | head -20
Had to open data source read-only.
INFO: Open of `Zone9_2014_01.gdb'
using driver `OpenFileGDB' successful.
Layer name: Zone9_2014_01_Vessel
Geometry: None <---------------------------- HERE
Feature Count: 1282
Layer SRS WKT:
(unknown)
FID Column = OID
MMSI: Integer (0.0)
IMO: Integer (0.0)
CallSign: String (255.0)
Name: String (255.0)
VesselType: Integer (0.0)
Length: Integer (0.0)
Width: Integer (0.0)
DimensionComponents: String (255.0)
OGRFeature(Zone9_2014_01_Vessel):1
MMSI (Integer) = 367603345
As you see the field Geometry
is set to None
. You can convert the data to a shape file using ogr2ogr
and get also only a dbase attribute file:
$ ogr2ogr -f 'ESRI SHAPEFILE' test Zone9_2014_01.gdb Zone9_2014_01_Vessel
$ ls test
Zone9_2014_01_Vessel.dbf
Geometries (positions) can be found in the layer Zone9_2014_01_Broadcast
.
$ ogr2ogr -f 'ESRI SHAPEFILE' test Zone9_2014_01.gdb
$ ls test
Zone9_2014_01_Broadcast.dbf
Zone9_2014_01_Broadcast.shp
Zone9_2014_01_Broadcast.prj
Zone9_2014_01_Broadcast.shx
Zone9_2014_01_Vessel.dbf
Zone9_2014_01_Voyage.dbf
Vessel and Voyage containing no position data according to the AIS messages protocol.
Here the complete workaround in R using a system call for the GDB to shapefile conversation and the package foreign
to read the dbf's:
# Load module to get readOGR
require('rgdal');
# Load module to get read.dbf
require('foreign');
# goto the directory with the GDB stuff
setwd('~/dev.d/gis-se.d/gdb')
# Conversation to a shapefile container
system("ogr2ogr -f 'ESRI SHAPEFILE' test Zone9_2014_01.gdb")
# read the vessels
vessel <- read.dbf('test/Zone9_2014_01_Vessel.dbf');
# read hte voyage data
voyage <- read.dbf('test/Zone9_2014_01_Voyage.dbf');
# read the geometries in broad cast
broadcast <- readOGR('test/Zone9_2014_01_Broadcast.shp','Zone9_2014_01_Broadcast')
OGR data source with driver: ESRI Shapefile
Source: "test/Zone9_2014_01_Broadcast.shp", layer: "Zone9_2014_01_Broadcast"
with 1639274 features
It has 10 fields
# is vessel OK?
head(vessel)
MMSI IMO CallSign Name VesselType Length Width DimensionC
1 367603345 NA <NA> <NA> 50 20 6 7,13,3,3
2 563000574 NA <NA> <NA> 70 276 40 188,88,20,20
3 367449580 NA <NA> <NA> 31 28 10 9,19,5,5
4 367302503 NA <NA> <NA> 31 20 8 8,12,4,4
5 304003909 NA <NA> <NA> 71 222 32 174,48,21,11
6 210080027 NA <NA> <NA> 71 294 32 222,72,22,10
# is voyage OK?
head(voyage)
VoyageID Destinatio Cargo Draught ETA StartTime EndTime MMSI
1 12 KAKE 50 20 <NA> 2014-01-01 <NA> 367603345
2 23 YOKOHAMA 70 125 2014-01-11 2014-01-01 2014-01-30 563000574
3 38 KETCHIKAN AK 31 40 2014-11-12 2014-01-01 <NA> 367449580
4 52 CLARENCE STRAIT LOGS 31 30 2014-09-12 2014-01-01 <NA> 367302503
5 62 JP TYO 71 90 2014-01-13 2014-01-01 2014-01-31 304003909
6 47 VOSTOCHNYY 71 106 2014-01-13 2014-01-01 <NA> 210080027
There is a recently released integration between the R and ArcGIS from Esri, called R ArcGIS Tools. It provides integration between R and ArcGIS making it possible to interchangeably access R tools and ArcGIS resources. You should be able to access geodatabase feature classes/tables with this integration.
Sample R tools are available here and sample tools illustrating R usage in geoprocessing scripts are here.