-
-
Notifications
You must be signed in to change notification settings - Fork 27
ENH: support reading from in-memory buffers #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0a10bc4
60b8d28
d81a70c
dadea0c
083a32a
9b3bdf3
fa9808b
6d1aeff
719399d
1502ab1
b8f7985
bc1b4f1
792210b
3e055f3
74f12a9
41f70d4
bd68a50
dadcc40
985a1ab
332680e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
with GDALEnv(): | ||
from pyogrio._io import ogr_read, ogr_read_info, ogr_list_layers, ogr_write | ||
from pyogrio._ogr import buffer_to_virtual_file, remove_virtual_file | ||
|
||
|
||
DRIVERS = { | ||
|
@@ -17,7 +18,8 @@ | |
|
||
|
||
def read( | ||
path, | ||
path_or_buffer, | ||
/, | ||
brendan-ward marked this conversation as resolved.
Show resolved
Hide resolved
|
||
layer=None, | ||
encoding=None, | ||
columns=None, | ||
|
@@ -37,8 +39,8 @@ def read( | |
|
||
Parameters | ||
---------- | ||
path : pathlib.Path or str | ||
A dataset path or URI. | ||
path_or_buffer : pathlib.Path or str, or bytes buffer | ||
A dataset path or URI, or raw buffer. | ||
layer : int or str, optional (default: first layer) | ||
If an integer is provided, it corresponds to the index of the layer | ||
with the data source. If a string is provided, it must match the name | ||
|
@@ -98,26 +100,46 @@ def read( | |
"geometry": "<geometry type>" | ||
} | ||
""" | ||
path = vsi_path(str(path)) | ||
if hasattr(path_or_buffer, "read"): | ||
path_or_buffer = path_or_buffer.read() | ||
|
||
from_buffer = False | ||
if isinstance(path_or_buffer, bytes): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I am checking for bytes vs strings to determine whether it's a path or in-memory bytes. I don't know if that is robust enough? Or do we want a separate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think file-like objects that have a |
||
from_buffer = True | ||
ext = "" | ||
is_zipped = path_or_buffer[:4].startswith(b'PK\x03\x04') | ||
if is_zipped: | ||
ext = ".zip" | ||
brendan-ward marked this conversation as resolved.
Show resolved
Hide resolved
|
||
path = buffer_to_virtual_file(path_or_buffer, ext=ext) | ||
if is_zipped: | ||
path = "/vsizip/" + path | ||
brendan-ward marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
path = vsi_path(str(path_or_buffer)) | ||
|
||
if not "://" in path: | ||
if not "/vsi" in path.lower() and not os.path.exists(path): | ||
raise ValueError(f"'{path}' does not exist") | ||
|
||
return ogr_read( | ||
path, | ||
layer=layer, | ||
encoding=encoding, | ||
columns=columns, | ||
read_geometry=read_geometry, | ||
force_2d=force_2d, | ||
skip_features=skip_features, | ||
max_features=max_features or 0, | ||
where=where, | ||
bbox=bbox, | ||
fids=fids, | ||
return_fids=return_fids, | ||
) | ||
try: | ||
result = ogr_read( | ||
path, | ||
layer=layer, | ||
encoding=encoding, | ||
columns=columns, | ||
read_geometry=read_geometry, | ||
force_2d=force_2d, | ||
skip_features=skip_features, | ||
max_features=max_features or 0, | ||
where=where, | ||
bbox=bbox, | ||
fids=fids, | ||
return_fids=return_fids, | ||
) | ||
finally: | ||
if from_buffer: | ||
remove_virtual_file(path) | ||
|
||
return result | ||
|
||
|
||
def write( | ||
|
Uh oh!
There was an error while loading. Please reload this page.