11

I am trying to open a shapefile with Jupyter notebook, but I can't get it to open. The shapefile is located in the same folder as my workbook, and Python can't identify it even if I use the full path.

import shapefile as shp
import matplotlib.pyplot as plt

sf = shp.Reader(r'C:\Users\Public\Documents\1-11-99n.shp')

ShapefileException: Unable to open C:\Users\Public\Documents\1-11-99n.dbf or C:\Users\Public\Documents\1-11-99n.shp.

Opening it with fiona and geopandas does not work either.

import fiona
shape = fiona.open("1-11-99n.shp")

and

import geopandas as gp
shp = gp.GeoDataFrame.from_file('1-11-99n.shp')
print (shp)

both end up with

CPLE_OpenFailedError: b'Unable to open 1-11-99n.shx or 1-11-99n.SHX.Try --config SHAPE_RESTORE_SHX true to restore or create it'

I know this might be a trivial issue, but I am new to using shapefiles and spatial Python libraries so I really don't know how to work around this issue.

  • replace the minus with an underscore and try again.. shapefile names are supposed to contain letters, numbers and underscores only. It would also be a good idea to start the name with a letter. If the problem persists then the shapefile is probably corrupt or the .SHX is missing.. do you have just the .SHP file or do you have a full set of files (.SHP, .SHX, .DBF, .PRJ etc..)? – Michael Stimson Nov 21 '17 at 2:18
  • Probably your shapefile is corrupt. I save one arbitrary shapefile as 1-11-99n.shp and I didn't have any issue with whatever of your mini scripts with shapely, fiona or geopandas. – xunilk Nov 21 '17 at 2:32
  • 3
    Yeah the problem was that I did not include the other shx, dbf, etc. files that I needed. I have never used shp files before so I did not know that it required the complement of files. The filename worked fine without having to be changed. Thanks! – Leonard Shaw Nov 21 '17 at 2:34
  • 1
    @LeonardShaw Please consider providing an answer to your question so we can consider this question resolved. – Aaron Nov 21 '17 at 3:10
  • To reiterate, it is a violation of the shapefile specification to have a leading numeric or hyphen in the file name. It is best practice to avoid naming which will cause failures in some shapefile reader implementations. – Vince Nov 21 '17 at 11:26
15

There's a trap for first time users of shapefiles. The actual shapefile (.shp) is useless without the companion files: .dbf, .shx, .prj etc..

This is outlined in the shapefile technical specification but that's a bit advanced, the Esri article might be more suitable for a quick explanation.

It is critical that all the files belonging to a shapefile are treated as a single unit. If the shapefile is edited at any stage, even just attribute changes, the FIDs (feature identifiers) change, which means the order in the .DBF file is changed.. should you edit a shapefiles' attributes and only copy the .DBF file, overwriting an existing .DBF file the shapes will point to the wrong rows. Always copy ALL the files together.

When sending a shapefile by email, Dropbox, Google Drive etc. I find it expedient to zip all the files into a single archive so that when it is unzipped all associated files appear together.

  • 1
    To be more precise: .shp, .dbf and .shx are the absolute minimum (according to the standard). QGIS/OGR will allow you to open files with a missing .dbf, but you will not be able to delete or edit those features. You can, however, use ogr2ogr to create the missing dbf-File (with an empty attribute table) – LuWi Nov 21 '17 at 10:28
  • 1
    @LuWi even the .SHX file can be rebuilt, it's the spatial index, there are a few tools and options that can rebuild the spatial index from the existing shapes. The precise number of related files varies but it's always best to keep everything together. – Michael Stimson Nov 21 '17 at 20:47

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.