@@ -1019,38 +1019,93 @@ def test_write_shp_only(tmpdir):
1019
1019
shp argument to the shapefile writer
1020
1020
creates just a shp file.
1021
1021
"""
1022
- filename = tmpdir .join ("test.shp" ).strpath
1023
- with shapefile .Writer (shp = filename ) as writer :
1024
- pass
1022
+ filename = tmpdir .join ("test" ).strpath
1023
+ with shapefile .Writer (shp = open (filename + '.shp' ,'wb' )) as writer :
1024
+ writer .point (1 , 1 )
1025
+ assert writer .shp and not writer .shx and not writer .dbf
1026
+ assert writer .shpNum == 1
1027
+ assert len (writer ) == 1
1028
+ assert writer .shp .closed == True
1025
1029
1026
1030
# assert test.shp exists
1027
- assert os .path .exists (filename )
1031
+ assert os .path .exists (filename + '.shp' )
1032
+
1033
+ # test that can read shapes
1034
+ with shapefile .Reader (shp = open (filename + '.shp' ,'rb' )) as reader :
1035
+ assert reader .shp and not reader .shx and not reader .dbf
1036
+ assert (reader .numRecords , reader .numShapes ) == (None , None ) # numShapes is unknown in the absence of shx file
1037
+ assert len (reader .shapes ()) == 1
1028
1038
1029
1039
# assert test.shx does not exist
1030
- assert not os .path .exists (tmpdir . join ( "test. shx" ). strpath )
1040
+ assert not os .path .exists (filename + '. shx' )
1031
1041
1032
1042
# assert test.dbf does not exist
1033
- assert not os .path .exists (tmpdir . join ( "test. dbf" ). strpath )
1043
+ assert not os .path .exists (filename + '. dbf' )
1034
1044
1035
1045
1036
- def test_write_shx_only (tmpdir ):
1046
+ def test_write_shp_shx_only (tmpdir ):
1037
1047
"""
1038
- Assert that specifying just the
1048
+ Assert that specifying just the shp and
1039
1049
shx argument to the shapefile writer
1040
- creates just a shx file.
1050
+ creates just a shp and shx file.
1041
1051
"""
1042
- filename = tmpdir .join ("test.shx" ).strpath
1043
- with shapefile .Writer (shx = filename ) as writer :
1044
- pass
1052
+ filename = tmpdir .join ("test" ).strpath
1053
+ with shapefile .Writer (shp = open (filename + '.shp' ,'wb' ), shx = open (filename + '.shx' ,'wb' )) as writer :
1054
+ writer .point (1 , 1 )
1055
+ assert writer .shp and writer .shx and not writer .dbf
1056
+ assert writer .shpNum == 1
1057
+ assert len (writer ) == 1
1058
+ assert writer .shp .closed == writer .shx .closed == True
1059
+
1060
+ # assert test.shp exists
1061
+ assert os .path .exists (filename + '.shp' )
1045
1062
1046
1063
# assert test.shx exists
1047
- assert os .path .exists (filename )
1064
+ assert os .path .exists (filename + '.shx' )
1048
1065
1049
- # assert test.shp does not exist
1050
- assert not os .path .exists (tmpdir .join ("test.shp" ).strpath )
1066
+ # test that can read shapes and offsets
1067
+ with shapefile .Reader (shp = open (filename + '.shp' ,'rb' ), shx = open (filename + '.shx' ,'rb' )) as reader :
1068
+ assert reader .shp and reader .shx and not reader .dbf
1069
+ assert (reader .numRecords , reader .numShapes ) == (None , 1 )
1070
+ reader .shape (0 ) # trigger reading of shx offsets
1071
+ assert len (reader ._offsets ) == 1
1072
+ assert len (reader .shapes ()) == 1
1051
1073
1052
1074
# assert test.dbf does not exist
1053
- assert not os .path .exists (tmpdir .join ("test.dbf" ).strpath )
1075
+ assert not os .path .exists (filename + '.dbf' )
1076
+
1077
+
1078
+ def test_write_shp_dbf_only (tmpdir ):
1079
+ """
1080
+ Assert that specifying just the
1081
+ shp and dbf argument to the shapefile writer
1082
+ creates just a shp and dbf file.
1083
+ """
1084
+ filename = tmpdir .join ("test" ).strpath
1085
+ with shapefile .Writer (shp = open (filename + '.shp' ,'wb' ), dbf = open (filename + '.dbf' ,'wb' )) as writer :
1086
+ writer .field ('field1' , 'C' ) # required to create a valid dbf file
1087
+ writer .record ('value' )
1088
+ writer .point (1 , 1 )
1089
+ assert writer .shp and not writer .shx and writer .dbf
1090
+ assert writer .shpNum == writer .recNum == 1
1091
+ assert len (writer ) == 1
1092
+ assert writer .shp .closed == writer .dbf .closed == True
1093
+
1094
+ # assert test.shp exists
1095
+ assert os .path .exists (filename + '.shp' )
1096
+
1097
+ # assert test.dbf exists
1098
+ assert os .path .exists (filename + '.dbf' )
1099
+
1100
+ # test that can read records and shapes
1101
+ with shapefile .Reader (shp = open (filename + '.shp' ,'rb' ), dbf = open (filename + '.dbf' ,'rb' )) as reader :
1102
+ assert reader .shp and not reader .shx and reader .dbf
1103
+ assert (reader .numRecords , reader .numShapes ) == (1 , None ) # numShapes is unknown in the absence of shx file
1104
+ assert len (reader .records ()) == 1
1105
+ assert len (reader .shapes ()) == 1
1106
+
1107
+ # assert test.shx does not exist
1108
+ assert not os .path .exists (filename + '.shx' )
1054
1109
1055
1110
1056
1111
def test_write_dbf_only (tmpdir ):
@@ -1059,18 +1114,29 @@ def test_write_dbf_only(tmpdir):
1059
1114
dbf argument to the shapefile writer
1060
1115
creates just a dbf file.
1061
1116
"""
1062
- filename = tmpdir .join ("test.dbf " ).strpath
1063
- with shapefile .Writer (dbf = filename ) as writer :
1117
+ filename = tmpdir .join ("test" ).strpath
1118
+ with shapefile .Writer (dbf = open ( filename + '.dbf' , 'wb' ) ) as writer :
1064
1119
writer .field ('field1' , 'C' ) # required to create a valid dbf file
1120
+ writer .record ('value' )
1121
+ assert not writer .shp and not writer .shx and writer .dbf
1122
+ assert writer .recNum == 1
1123
+ assert len (writer ) == 1
1124
+ assert writer .dbf .closed == True
1065
1125
1066
1126
# assert test.dbf exists
1067
- assert os .path .exists (filename )
1127
+ assert os .path .exists (filename + '.dbf' )
1128
+
1129
+ # test that can read records
1130
+ with shapefile .Reader (dbf = open (filename + '.dbf' ,'rb' )) as reader :
1131
+ assert not writer .shp and not writer .shx and writer .dbf
1132
+ assert (reader .numRecords , reader .numShapes ) == (1 , None )
1133
+ assert len (reader .records ()) == 1
1068
1134
1069
1135
# assert test.shp does not exist
1070
- assert not os .path .exists (tmpdir . join ( "test. shp" ). strpath )
1136
+ assert not os .path .exists (filename + '. shp' )
1071
1137
1072
1138
# assert test.shx does not exist
1073
- assert not os .path .exists (tmpdir . join ( "test. shx" ). strpath )
1139
+ assert not os .path .exists (filename + '. shx' )
1074
1140
1075
1141
1076
1142
def test_write_default_shp_shx_dbf (tmpdir ):
@@ -1133,10 +1199,10 @@ def test_write_record(tmpdir):
1133
1199
with shapefile .Writer (filename ) as writer :
1134
1200
writer .autoBalance = True
1135
1201
1136
- writer .field ('one' , 'C' ) # many under length limit
1137
- writer .field ('two' , 'C' ) # 1 under length limit
1138
- writer .field ('three' , 'C' ) # at length limit
1139
- writer .field ('four' , 'C' ) # 1 over length limit
1202
+ writer .field ('one' , 'C' )
1203
+ writer .field ('two' , 'C' )
1204
+ writer .field ('three' , 'C' )
1205
+ writer .field ('four' , 'C' )
1140
1206
1141
1207
values = ['one' ,'two' ,'three' ,'four' ]
1142
1208
writer .record (* values )
@@ -1160,10 +1226,10 @@ def test_write_partial_record(tmpdir):
1160
1226
with shapefile .Writer (filename ) as writer :
1161
1227
writer .autoBalance = True
1162
1228
1163
- writer .field ('one' , 'C' ) # many under length limit
1164
- writer .field ('two' , 'C' ) # 1 under length limit
1165
- writer .field ('three' , 'C' ) # at length limit
1166
- writer .field ('four' , 'C' ) # 1 over length limit
1229
+ writer .field ('one' , 'C' )
1230
+ writer .field ('two' , 'C' )
1231
+ writer .field ('three' , 'C' )
1232
+ writer .field ('four' , 'C' )
1167
1233
1168
1234
values = ['one' ,'two' ]
1169
1235
writer .record (* values )
@@ -1220,4 +1286,11 @@ def test_write_empty_shapefile(tmpdir, shape_type):
1220
1286
w .field ('field1' , 'C' ) # required to create a valid dbf file
1221
1287
1222
1288
with shapefile .Reader (filename ) as r :
1289
+ # test correct shape type
1223
1290
assert r .shapeType == shape_type
1291
+ # test length 0
1292
+ assert len (r ) == r .numRecords == r .numShapes == 0
1293
+ # test records are empty
1294
+ assert len (r .records ()) == 0
1295
+ # test shapes are empty
1296
+ assert len (r .shapes ()) == 0
0 commit comments