21
21
pytestmark = pytest .mark .skipif (not has_geopandas , reason = "GeoPandas not available" )
22
22
23
23
24
- def test_read_dataframe (naturalearth_lowres ):
25
- df = read_dataframe (naturalearth_lowres )
24
+ def test_read_dataframe (naturalearth_lowres_all_ext ):
25
+ df = read_dataframe (naturalearth_lowres_all_ext )
26
26
27
27
assert isinstance (df , gp .GeoDataFrame )
28
28
@@ -45,8 +45,19 @@ def test_read_dataframe_vsi(naturalearth_lowres_vsi):
45
45
assert len (df ) == 177
46
46
47
47
48
- def test_read_no_geometry (naturalearth_lowres ):
49
- df = read_dataframe (naturalearth_lowres , read_geometry = False )
48
+ @pytest .mark .parametrize (
49
+ "naturalearth_lowres, expected_ext" ,
50
+ [(".gpkg" , ".gpkg" ), (".shp" , ".shp" )],
51
+ indirect = ["naturalearth_lowres" ])
52
+ def test_fixture_naturalearth_lowres (naturalearth_lowres , expected_ext ):
53
+ # Test the fixture with "indirect" parameter
54
+ assert naturalearth_lowres .suffix == expected_ext
55
+ df = read_dataframe (naturalearth_lowres )
56
+ assert len (df ) == 177
57
+
58
+
59
+ def test_read_no_geometry (naturalearth_lowres_all_ext ):
60
+ df = read_dataframe (naturalearth_lowres_all_ext , read_geometry = False )
50
61
assert isinstance (df , pd .DataFrame )
51
62
assert not isinstance (df , gp .GeoDataFrame )
52
63
@@ -82,9 +93,9 @@ def test_read_layer(test_fgdb_vsi):
82
93
assert "RIVER_MILE" in df .columns
83
94
84
95
85
- def test_read_layer_invalid (naturalearth_lowres ):
96
+ def test_read_layer_invalid (naturalearth_lowres_all_ext ):
86
97
with pytest .raises (DataLayerError , match = "Layer 'wrong' could not be opened" ):
87
- read_dataframe (naturalearth_lowres , layer = "wrong" )
98
+ read_dataframe (naturalearth_lowres_all_ext , layer = "wrong" )
88
99
89
100
90
101
@pytest .mark .filterwarnings ("ignore: Measured" )
@@ -98,77 +109,87 @@ def test_read_null_values(test_fgdb_vsi):
98
109
99
110
# make sure that Null values are preserved
100
111
assert df .SEGMENT_NAME .isnull ().max () == True
101
- assert df .loc [df .SEGMENT_NAME .isnull ()].SEGMENT_NAME .iloc [0 ] == None
112
+ assert df .loc [df .SEGMENT_NAME .isnull ()].SEGMENT_NAME .iloc [0 ] is None
102
113
103
114
104
- def test_read_fid_as_index (naturalearth_lowres ):
115
+ def test_read_fid_as_index (naturalearth_lowres_all_ext ):
105
116
kwargs = {"skip_features" : 2 , "max_features" : 2 }
106
117
107
118
# default is to not set FIDs as index
108
- df = read_dataframe (naturalearth_lowres , ** kwargs )
119
+ df = read_dataframe (naturalearth_lowres_all_ext , ** kwargs )
109
120
assert_index_equal (df .index , pd .RangeIndex (0 , 2 ))
110
121
111
- df = read_dataframe (naturalearth_lowres , fid_as_index = False , ** kwargs )
122
+ df = read_dataframe (naturalearth_lowres_all_ext , fid_as_index = False , ** kwargs )
112
123
assert_index_equal (df .index , pd .RangeIndex (0 , 2 ))
113
124
114
- df = read_dataframe (naturalearth_lowres , fid_as_index = True , ** kwargs )
115
- assert_index_equal (df .index , pd .Index ([2 , 3 ], name = "fid" ))
125
+ df = read_dataframe (naturalearth_lowres_all_ext , fid_as_index = True , ** kwargs )
126
+ if naturalearth_lowres_all_ext .suffix in ['.gpkg' ]:
127
+ # File format where fid starts at 1
128
+ assert_index_equal (df .index , pd .Index ([3 , 4 ], name = "fid" ))
129
+ else :
130
+ # File format where fid starts at 0
131
+ assert_index_equal (df .index , pd .Index ([2 , 3 ], name = "fid" ))
116
132
117
133
118
134
@pytest .mark .filterwarnings ("ignore: Layer" )
119
- def test_read_where (naturalearth_lowres ):
135
+ def test_read_where (naturalearth_lowres_all_ext ):
120
136
# empty filter should return full set of records
121
- df = read_dataframe (naturalearth_lowres , where = "" )
137
+ df = read_dataframe (naturalearth_lowres_all_ext , where = "" )
122
138
assert len (df ) == 177
123
139
124
140
# should return singular item
125
- df = read_dataframe (naturalearth_lowres , where = "iso_a3 = 'CAN'" )
141
+ df = read_dataframe (naturalearth_lowres_all_ext , where = "iso_a3 = 'CAN'" )
126
142
assert len (df ) == 1
127
143
assert df .iloc [0 ].iso_a3 == "CAN"
128
144
129
- df = read_dataframe (naturalearth_lowres , where = "iso_a3 IN ('CAN', 'USA', 'MEX')" )
145
+ df = read_dataframe (naturalearth_lowres_all_ext , where = "iso_a3 IN ('CAN', 'USA', 'MEX')" )
130
146
assert len (df ) == 3
131
147
assert len (set (df .iso_a3 .unique ()).difference (["CAN" , "USA" , "MEX" ])) == 0
132
148
133
149
# should return items within range
134
150
df = read_dataframe (
135
- naturalearth_lowres , where = "POP_EST >= 10000000 AND POP_EST < 100000000"
151
+ naturalearth_lowres_all_ext , where = "POP_EST >= 10000000 AND POP_EST < 100000000"
136
152
)
137
153
assert len (df ) == 75
138
154
assert df .pop_est .min () >= 10000000
139
155
assert df .pop_est .max () < 100000000
140
156
141
157
# should match no items
142
- df = read_dataframe (naturalearth_lowres , where = "ISO_A3 = 'INVALID'" )
158
+ df = read_dataframe (naturalearth_lowres_all_ext , where = "ISO_A3 = 'INVALID'" )
143
159
assert len (df ) == 0
144
160
145
161
146
- def test_read_where_invalid (naturalearth_lowres ):
147
- with pytest .raises (ValueError , match = "Invalid SQL" ):
148
- read_dataframe (naturalearth_lowres , where = "invalid" )
162
+ def test_read_where_invalid (naturalearth_lowres_all_ext ):
163
+ if naturalearth_lowres_all_ext .suffix in [".gpkg" ]:
164
+ # Geopackage doesn't raise, but returns empty df?
165
+ gdf = read_dataframe (naturalearth_lowres_all_ext , where = "invalid" )
166
+ assert len (gdf ) == 0
167
+ else :
168
+ with pytest .raises (ValueError , match = "Invalid SQL" ):
169
+ read_dataframe (naturalearth_lowres_all_ext , where = "invalid" )
149
170
150
171
151
172
@pytest .mark .parametrize ("bbox" , [(1 ,), (1 , 2 ), (1 , 2 , 3 )])
152
- def test_read_bbox_invalid (naturalearth_lowres , bbox ):
173
+ def test_read_bbox_invalid (naturalearth_lowres_all_ext , bbox ):
153
174
with pytest .raises (ValueError , match = "Invalid bbox" ):
154
- read_dataframe (naturalearth_lowres , bbox = bbox )
175
+ read_dataframe (naturalearth_lowres_all_ext , bbox = bbox )
155
176
156
177
157
- def test_read_bbox (naturalearth_lowres ):
178
+ def test_read_bbox (naturalearth_lowres_all_ext ):
158
179
# should return no features
159
180
with pytest .warns (UserWarning , match = "does not have any features to read" ):
160
- df = read_dataframe (naturalearth_lowres , bbox = (0 , 0 , 0.00001 , 0.00001 ))
181
+ df = read_dataframe (naturalearth_lowres_all_ext , bbox = (0 , 0 , 0.00001 , 0.00001 ))
161
182
assert len (df ) == 0
162
183
163
- df = read_dataframe (naturalearth_lowres , bbox = (- 140 , 20 , - 100 , 40 ))
184
+ df = read_dataframe (naturalearth_lowres_all_ext , bbox = (- 140 , 20 , - 100 , 40 ))
164
185
assert len (df ) == 2
165
186
assert np .array_equal (df .iso_a3 , ["USA" , "MEX" ])
166
187
167
188
168
- def test_read_fids (naturalearth_lowres ):
189
+ def test_read_fids (naturalearth_lowres_all_ext ):
169
190
# ensure keyword is properly passed through
170
- fids = np .array ([0 , 10 , 5 ], dtype = np .int64 )
171
- df = read_dataframe (naturalearth_lowres , fids = fids , fid_as_index = True )
191
+ fids = np .array ([1 , 10 , 5 ], dtype = np .int64 )
192
+ df = read_dataframe (naturalearth_lowres_all_ext , fids = fids , fid_as_index = True )
172
193
assert len (df ) == 3
173
194
assert np .array_equal (fids , df .index .values )
174
195
@@ -238,27 +259,27 @@ def test_write_empty_dataframe(tmpdir, driver, ext):
238
259
assert_geodataframe_equal (df , expected )
239
260
240
261
241
- def test_write_dataframe_gdalparams (tmpdir , naturalearth_lowres ):
262
+ def test_write_dataframe_gdalparams (tmp_path , naturalearth_lowres ):
242
263
original_df = read_dataframe (naturalearth_lowres )
243
-
244
- test_noindex_filename = os . path . join ( str ( tmpdir ), f "test_gdalparams_noindex.shp")
264
+
265
+ test_noindex_filename = tmp_path / "test_gdalparams_noindex.shp"
245
266
write_dataframe (original_df , test_noindex_filename , SPATIAL_INDEX = "NO" )
246
- assert os . path . exists (test_noindex_filename ) is True
247
- test_noindex_index_filename = os . path . join ( str ( tmpdir ), f "test_gdalparams_noindex.qix")
248
- assert os . path . exists (test_noindex_index_filename ) is False
249
-
250
- test_withindex_filename = os . path . join ( str ( tmpdir ), f "test_gdalparams_withindex.shp")
267
+ assert test_noindex_filename . exists () is True
268
+ test_noindex_index_filename = tmp_path / "test_gdalparams_noindex.qix"
269
+ assert test_noindex_index_filename . exists () is False
270
+
271
+ test_withindex_filename = tmp_path / "test_gdalparams_withindex.shp"
251
272
write_dataframe (original_df , test_withindex_filename , SPATIAL_INDEX = "YES" )
252
- assert os . path . exists (test_withindex_filename ) is True
253
- test_withindex_index_filename = os . path . join ( str ( tmpdir ), f "test_gdalparams_withindex.qix")
254
- assert os . path . exists (test_withindex_index_filename ) is True
273
+ assert test_withindex_filename . exists () is True
274
+ test_withindex_index_filename = tmp_path / "test_gdalparams_withindex.qix"
275
+ assert test_withindex_index_filename . exists () is True
255
276
256
277
257
278
@pytest .mark .filterwarnings (
258
279
"ignore: You will likely lose important projection information"
259
280
)
260
- def test_custom_crs_io (tmpdir , naturalearth_lowres ):
261
- df = read_dataframe (naturalearth_lowres )
281
+ def test_custom_crs_io (tmpdir , naturalearth_lowres_all_ext ):
282
+ df = read_dataframe (naturalearth_lowres_all_ext )
262
283
# project Belgium to a custom Albers Equal Area projection
263
284
expected = df .loc [df .name == "Belgium" ].to_crs (
264
285
"+proj=aea +lat_1=49.5 +lat_2=51.5 +lon_0=4.3"
@@ -275,4 +296,3 @@ def test_custom_crs_io(tmpdir, naturalearth_lowres):
275
296
assert crs ["lat_2" ] == 51.5
276
297
assert crs ["lon_0" ] == 4.3
277
298
assert df .crs .equals (expected .crs )
278
-
0 commit comments