Skip to content

Commit cb7f124

Browse files
authored
More flexible with shapefile filetypes, in line with the docs
Fixes #35. The docs for the reader say that no exception is thrown if any of the filetypes are missing until an operation is called that depends on that file, and that the .shx file is optional. This was only the case when giving file-like objects for each filetype, not when specifying the filename. And missing .shx files raised errors. This commit allows the same flexibility when specifying filenames, with the added benefit of reading standalone dbf files.
1 parent 0cc0d58 commit cb7f124

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

shapefile.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,23 +259,24 @@ def __init__(self, *args, **kwargs):
259259
def load(self, shapefile=None):
260260
"""Opens a shapefile from a filename or file-like
261261
object. Normally this method would be called by the
262-
constructor with the file object or file name as an
263-
argument."""
262+
constructor with the file name as an argument."""
264263
if shapefile:
265264
(shapeName, ext) = os.path.splitext(shapefile)
266265
self.shapeName = shapeName
267266
try:
268267
self.shp = open("%s.shp" % shapeName, "rb")
269268
except IOError:
270-
raise ShapefileException("Unable to open %s.shp" % shapeName)
269+
pass
271270
try:
272271
self.shx = open("%s.shx" % shapeName, "rb")
273272
except IOError:
274-
raise ShapefileException("Unable to open %s.shx" % shapeName)
273+
pass
275274
try:
276275
self.dbf = open("%s.dbf" % shapeName, "rb")
277276
except IOError:
278-
raise ShapefileException("Unable to open %s.dbf" % shapeName)
277+
pass
278+
if not (self.shp or self.dbf):
279+
raise ShapefileException("Unable to open %s.dbf or %s.shp." % (shapeName, shapeName) )
279280
if self.shp:
280281
self.__shpHeader()
281282
if self.dbf:

0 commit comments

Comments
 (0)