5

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.

  • Shapefiles, by the specification, must have .shp, .shx, and .dbf. That QGIS can open an invalid shapefile is a nice extra. Adding a new field means that a .dbf file must be edited but in this case there is no .dbf to edit. Theoretically QGIS could keep the new schema in memory and create a new .dbf to suit the schema once shapefile is saved. However, this does not feel like a feature that is needed every day. By the way, if you happen to loose also .shx file, OpenJUMP can read geometries from the plain .shp. Like QGIS, it does not allow to edit the schema before a new shapefile is created. – user30184 Jan 21 '15 at 11:14
  • Yes, I was surprised that GQIS opened it, but it did. I was sent this shapefile, so there wasn't much esle I could do. Thanks for the OpenJUMP pointer. – John Powell Jan 21 '15 at 11:20
  • Usually with any real data you will need the attributes as well but I know that if you ask someone to send a shapefile you can get exactly .shp. – user30184 Jan 21 '15 at 11:43
  • Actually, I only needed the geometries, so this was fine. While my problem is solved, I asked because I am sure this must happen a fair amount, and I wondered if there was a cleaner solution that save as new shp. – John Powell Jan 21 '15 at 11:45
  • Well, making a new copy with GDAL: ogr2ogr with_dbf.shp without_dbf.shpis cleaner for me than doing the same with QGIS. – user30184 Jan 21 '15 at 11:56
11

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
  • Perfect, if I have to do this again, ogr2ogr is just the job. – John Powell Jan 21 '15 at 13:18
1

for corrupted shapefile, I also used shapechecker in the past. It reconstruct the dbf when missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.