Skip to content

Commit 31d7571

Browse files
authored
ENH: add "driver" property to read_info result (#224)
1 parent 75e8f13 commit 31d7571

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 0.6.0 (???)
4+
5+
### Improvements
6+
7+
- ENH: add "driver" property to read_info result (#224)
8+
39
## 0.5.1 (2023-01-26)
410

511
### Bug fixes

docs/source/introduction.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ first layer.
7676
>>> from pyogrio import read_info
7777
>>> read_info('ne_10m_admin_0_countries.shp')
7878

79-
# Outputs a dictionary with `crs`, `encoding`, `fields`, `geometry_type`, and `features`
79+
# Outputs a dictionary with `crs`, `driver`, `encoding`, `fields`, `geometry_type`, and
80+
# `features`
8081
{
8182
'crs': 'EPSG:4326',
8283
'encoding': 'UTF-8',
8384
'fields': array(['featurecla', 'scalerank', 'LABELRANK', ...], dtype=object),
8485
'dtypes': array(['int64', 'object', 'object', 'object', 'float64'], dtype=object),
8586
'geometry_type': 'Polygon',
86-
'features': 255
87+
'features': 255,
88+
'driver': 'ESRI Shapefile',
8789
}
8890
```
8991

pyogrio/_io.pyx

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,35 +264,49 @@ cdef str get_crs(OGRLayerH ogr_layer):
264264
return wkt
265265

266266

267-
cdef detect_encoding(OGRDataSourceH ogr_dataset, OGRLayerH ogr_layer):
268-
"""Attempt to detect the encoding of the layer.
269-
If it supports UTF-8, use that.
270-
If it is a shapefile, it must otherwise be ISO-8859-1.
271-
267+
cdef get_driver(OGRDataSourceH ogr_dataset):
268+
"""Get the driver for a dataset.
269+
272270
Parameters
273271
----------
274272
ogr_dataset : pointer to open OGR dataset
275-
ogr_layer : pointer to open OGR layer
276-
277273
Returns
278274
-------
279275
str or None
280276
"""
281277
cdef void *ogr_driver
282278

283-
if OGR_L_TestCapability(ogr_layer, OLCStringsAsUTF8):
284-
return 'UTF-8'
285-
286279
try:
287280
ogr_driver = exc_wrap_pointer(GDALGetDatasetDriver(ogr_dataset))
288281

289282
except NullPointerError:
290-
raise DataLayerError(f"Could not detect encoding of layer") from None
283+
raise DataLayerError(f"Could not detect driver of dataset") from None
291284

292285
except CPLE_BaseError as exc:
293286
raise DataLayerError(str(exc))
294287

295288
driver = OGR_Dr_GetName(ogr_driver).decode("UTF-8")
289+
return driver
290+
291+
292+
cdef detect_encoding(OGRDataSourceH ogr_dataset, OGRLayerH ogr_layer):
293+
"""Attempt to detect the encoding of the layer.
294+
If it supports UTF-8, use that.
295+
If it is a shapefile, it must otherwise be ISO-8859-1.
296+
297+
Parameters
298+
----------
299+
ogr_dataset : pointer to open OGR dataset
300+
ogr_layer : pointer to open OGR layer
301+
302+
Returns
303+
-------
304+
str or None
305+
"""
306+
if OGR_L_TestCapability(ogr_layer, OLCStringsAsUTF8):
307+
return 'UTF-8'
308+
309+
driver = get_driver(ogr_dataset)
296310
if driver == 'ESRI Shapefile':
297311
return 'ISO-8859-1'
298312

@@ -973,7 +987,7 @@ def ogr_read(
973987
'crs': crs,
974988
'encoding': encoding,
975989
'fields': fields[:,2], # return only names
976-
'geometry_type': geometry_type
990+
'geometry_type': geometry_type,
977991
}
978992

979993
finally:
@@ -1205,6 +1219,7 @@ def ogr_read_info(str path, object layer=None, object encoding=None, **kwargs):
12051219
'dtypes': fields[:,3],
12061220
'geometry_type': get_geometry_type(ogr_layer),
12071221
'features': OGR_L_GetFeatureCount(ogr_layer, 1),
1222+
'driver': get_driver(ogr_dataset),
12081223
"capabilities": {
12091224
"random_read": OGR_L_TestCapability(ogr_layer, OLCRandomRead),
12101225
"fast_set_next_by_index": OGR_L_TestCapability(ogr_layer, OLCFastSetNextByIndex),

pyogrio/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ def read_info(path_or_buffer, /, layer=None, encoding=None):
172172
"dtypes": <ndarray of field dtypes>,
173173
"encoding": "<encoding>",
174174
"geometry": "<geometry type>",
175-
"features": <feature count>
175+
"features": <feature count>,
176+
"driver": "<driver>",
176177
}
177178
"""
178179
path, buffer = get_vsi_path(path_or_buffer)

pyogrio/raw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def read(
118118
"crs": "<crs>",
119119
"fields": <ndarray of field names>,
120120
"encoding": "<encoding>",
121-
"geometry": "<geometry type>"
121+
"geometry_type": "<geometry type>"
122122
}
123123
"""
124124
path, buffer = get_vsi_path(path_or_buffer)

pyogrio/tests/test_core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ def test_read_info(naturalearth_lowres):
227227
assert meta["fields"].shape == (5,)
228228
assert meta["dtypes"].tolist() == ["int64", "object", "object", "object", "float64"]
229229
assert meta["features"] == 177
230+
assert meta["driver"] == "ESRI Shapefile"
230231

231232

232233
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)