Skip to content

Commit d516797

Browse files
committed
Allow reading individual file extensions via filepaths
1 parent af1dee9 commit d516797

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ OR
244244
>>> sf = shapefile.Reader("shapefiles/blockgroups.dbf")
245245

246246
OR any of the other 5+ formats which are potentially part of a shapefile. The
247-
library does not care about file extensions.
247+
library does not care about file extensions. You can also specify that you only
248+
want to read some of the file extensions through the use of keyword arguments:
249+
250+
251+
>>> sf = shapefile.Reader(dbf="shapefiles/blockgroups.dbf")
248252

249253
#### Reading Shapefiles from Zip Files
250254

shapefile.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ def __init__(self, *args, **kwargs):
10431043
self.load(path)
10441044
return
10451045

1046-
# Otherwise, load from separate shp/shx/dbf args (must be file-like)
1046+
# Otherwise, load from separate shp/shx/dbf args (must be path or file-like)
10471047
if "shp" in kwargs.keys():
10481048
if hasattr(kwargs["shp"], "read"):
10491049
self.shp = kwargs["shp"]
@@ -1053,7 +1053,8 @@ def __init__(self, *args, **kwargs):
10531053
except (NameError, io.UnsupportedOperation):
10541054
self.shp = io.BytesIO(self.shp.read())
10551055
else:
1056-
raise ShapefileException('The shp arg must be file-like.')
1056+
(baseName, ext) = os.path.splitext(kwargs["shp"])
1057+
self.load_shp(baseName)
10571058

10581059
if "shx" in kwargs.keys():
10591060
if hasattr(kwargs["shx"], "read"):
@@ -1064,7 +1065,8 @@ def __init__(self, *args, **kwargs):
10641065
except (NameError, io.UnsupportedOperation):
10651066
self.shx = io.BytesIO(self.shx.read())
10661067
else:
1067-
raise ShapefileException('The shx arg must be file-like.')
1068+
(baseName, ext) = os.path.splitext(kwargs["shx"])
1069+
self.load_shx(baseName)
10681070

10691071
if "dbf" in kwargs.keys():
10701072
if hasattr(kwargs["dbf"], "read"):
@@ -1075,7 +1077,8 @@ def __init__(self, *args, **kwargs):
10751077
except (NameError, io.UnsupportedOperation):
10761078
self.dbf = io.BytesIO(self.dbf.read())
10771079
else:
1078-
raise ShapefileException('The dbf arg must be file-like.')
1080+
(baseName, ext) = os.path.splitext(kwargs["dbf"])
1081+
self.load_dbf(baseName)
10791082

10801083
# Load the files
10811084
if self.shp or self.dbf:

test_shapefile.py

+65-1
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,56 @@ def test_reader_pathlike():
416416
assert len(sf) == 663
417417

418418

419+
def test_reader_dbf_only():
420+
"""
421+
Assert that specifying just the
422+
dbf argument to the shapefile reader
423+
reads just the dbf file.
424+
"""
425+
with shapefile.Reader(dbf="shapefiles/blockgroups.dbf") as sf:
426+
assert len(sf) == 663
427+
record = sf.record(3)
428+
assert record[1:3] == ['060750601001', 4715]
429+
430+
431+
def test_reader_shp_shx_only():
432+
"""
433+
Assert that specifying just the
434+
shp and shx argument to the shapefile reader
435+
reads just the shp and shx file.
436+
"""
437+
with shapefile.Reader(shp="shapefiles/blockgroups.shp", shx="shapefiles/blockgroups.shx") as sf:
438+
assert len(sf) == 663
439+
shape = sf.shape(3)
440+
assert len(shape.points) is 173
441+
442+
443+
def test_reader_shp_dbf_only():
444+
"""
445+
Assert that specifying just the
446+
shp and shx argument to the shapefile reader
447+
reads just the shp and dbf file.
448+
"""
449+
with shapefile.Reader(shp="shapefiles/blockgroups.shp", dbf="shapefiles/blockgroups.dbf") as sf:
450+
assert len(sf) == 663
451+
shape = sf.shape(3)
452+
assert len(shape.points) is 173
453+
record = sf.record(3)
454+
assert record[1:3] == ['060750601001', 4715]
455+
456+
457+
def test_reader_shp_only():
458+
"""
459+
Assert that specifying just the
460+
shp argument to the shapefile reader
461+
reads just the shp file (shx optional).
462+
"""
463+
with shapefile.Reader(shp="shapefiles/blockgroups.shp") as sf:
464+
assert len(sf) == 663
465+
shape = sf.shape(3)
466+
assert len(shape.points) is 173
467+
468+
419469
def test_reader_filelike_dbf_only():
420470
"""
421471
Assert that specifying just the
@@ -440,7 +490,21 @@ def test_reader_filelike_shp_shx_only():
440490
assert len(shape.points) is 173
441491

442492

443-
def test_reader_filelike_shx_optional():
493+
def test_reader_filelike_shp_dbf_only():
494+
"""
495+
Assert that specifying just the
496+
shp and shx argument to the shapefile reader
497+
reads just the shp and dbf file.
498+
"""
499+
with shapefile.Reader(shp=open("shapefiles/blockgroups.shp", "rb"), dbf=open("shapefiles/blockgroups.dbf", "rb")) as sf:
500+
assert len(sf) == 663
501+
shape = sf.shape(3)
502+
assert len(shape.points) is 173
503+
record = sf.record(3)
504+
assert record[1:3] == ['060750601001', 4715]
505+
506+
507+
def test_reader_filelike_shp_only():
444508
"""
445509
Assert that specifying just the
446510
shp argument to the shapefile reader

0 commit comments

Comments
 (0)