I have a shape file (shp)
which is missing its dbf
. I tried to load this into Postgis using shp2pgsql
and got the error.
could not open shapename.dbf
I opened it in QGIS, without issue, and when I clicked Open attribute table
, I could see a column based on the shp's gid, but could not use the field editor to add a new field. Instead, I used, Save as
and saved as a new shp file.
This new shp file, once added to the map, did allow me to add fields and I can now successfully load it into Postgis using shp2pgsql
.
Question: while the above works, it is a bit of a faff, and I am wondering if I have missed some obvious trick, either in shp2pgsql, QGis, ogr2ogr
or some other application not mentioned.
Answer
First of all, a shapefile without .dbf part is invalid by the ESRI Shapefile specification http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
An ESRI shapefile consists of a main file, an index file, and a dBASE table. The main file is a direct access, variable-record-length file in which each record describes a shape with a list of its vertices. In the index file, each record contains the offset of the corresponding main file record from the beginning of the main file. The dBASE table contains feature attributes with one record per feature. The one-to-one relationship between geometry and attributes is based on record number. Attribute records in the dBASE file must be in the same order as records in the main file
However, because the .dbf part holds only attribute data it is possible to read the geometries from the main file (.shp) and the index file (.shx) and GDAL and QGIS can really do that. Actually, even .shx part is not necessary for getting the geometries and OpenJUMP GIS http://openjump.org/ can fetch the geometries from the plain .shp file. However, GDAL and QGIS need both .shp and .shx parts at least by now but there is an open GDAL ticket about making GDAL more lenient http://trac.osgeo.org/gdal/ticket/5035. Because it is not so uncommon that .dbf or .shx parts are missing, or .dbf part is corrupted because user has tried to edit it with a program like OpenOffice Calc, it is good that at least the geometries can be saved.
If a shapefile without .dbf part is opened into QGIS or OpenJUMP its schema can not be altered because that would mean editing a non-existing .dbf file. As a workaround this shapefile can be saved as a new shapefile which will then be complete with .dbf part. New, compliant shapefile can also be created from command line with GDAL as
ogr2ogr with_dbf.shp without_dbf.shp
No comments:
Post a Comment