Tôi biết tôi có thể sử dụng io.StringIO hoặc io.BytesIO để trả về một tay cầm tệp mở có thể được ghi và đọc từ đó.
Tuy nhiên, tôi đang tìm kiếm một cách để làm cho một vùng trong bộ nhớ trông giống như một tệp đĩa được đặt tên chưa được mở.
Lý do tôi muốn điều này là do có những thói quen lấy tên của một tệp đĩa làm đối số và các thói quen này sau đó mở tệp và thao tác nó. Trong một số trường hợp, tôi muốn đầu vào hoặc đầu ra cho các thói quen này là bộ đệm bộ nhớ, không phải là tệp đĩa và đối với các thói quen đó, tôi không thể vượt qua xử lý tệp đã mở.
Ví dụ: một thói quen như vậy là Image.save() từ PIL, mong đợi một tên đường dẫn là đối số của nó, không phải là một tay cầm tệp đã mở. Khi sử dụng thói quen đó, tôi muốn dữ liệu hình ảnh được lưu trực tiếp vào bộ đệm bộ nhớ mà không có bất kỳ tệp trung gian nào được thực hiện. Ngoài ra còn có nhiều thói quen khác lấy tên đường dẫn làm đối số mà tôi muốn hành vi này.
Có cách nào để thực hiện điều này trong Python không?
Các phần khác của tài liệu này đã giải thích cách Rasterio có thể truy cập dữ liệu được lưu trữ trong các tệp hiện có trên đĩa được viết bởi các chương trình khác hoặc ghi các tệp được sử dụng bởi các chương trình GIS khác. Tên tệp là các đầu vào và tệp điển hình trên đĩa là các đầu ra điển hình.
with rasterio.open('example.tif') as dataset: data_array = dataset.read()
Có các tùy chọn khác nhau cho các chương trình Python có các luồng byte, ví dụ: từ ổ cắm mạng, làm đầu vào hoặc đầu ra của chúng thay vì tên tệp. Một là việc sử dụng một tệp tạm thời trên đĩa.
import tempfile with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(data) with rasterio.open(tmpfile.name) as dataset: data_array = dataset.read()
Một cách khác là Rasterio sườn ____10, một sự trừu tượng hóa cho các đối tượng trong hệ thống tập tin trong bộ nhớ của GDAL.
MemoryFile: Bytesio đáp ứng được đặt tên
Lớp import tempfile with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(data) with rasterio.open(tmpfile.name) as dataset: data_array = dataset.read() 0 hoạt động hơi giống import tempfile with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(data) with rasterio.open(tmpfile.name) as dataset: data_array = dataset.read() 2 và import tempfile with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(data) with rasterio.open(tmpfile.name) as dataset: data_array = dataset.read() 3. Một tệp GeoTiff trong một chuỗi các byte import tempfile with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(data) with rasterio.open(tmpfile.name) as dataset: data_array = dataset.read() 4 có thể được mở trong bộ nhớ như được hiển thị bên dưới.
from rasterio.io import MemoryFile with MemoryFile(data) as memfile: with memfile.open() as dataset: data_array = dataset.read()
Mã này có thể nhanh hơn nhiều lần so với mã sử dụng import tempfile with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(data) with rasterio.open(tmpfile.name) as dataset: data_array = dataset.read() 3 với giá gấp đôi giá trong bộ nhớ.
Viết bộ nhớ
Gia tăng ghi vào một import tempfile with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(data) with rasterio.open(tmpfile.name) as dataset: data_array = dataset.read() 0 trống cũng có thể.
with MemoryFile() as memfile: while True: data = f.read(8192) # ``f`` is an input stream. if not data: break memfile.write(data) with memfile.open() as dataset: data_array = dataset.read()
Hai chế độ này không tương thích: A import tempfile with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(data) with rasterio.open(tmpfile.name) as dataset: data_array = dataset.read() 0 được khởi tạo với chuỗi byte không thể được mở rộng.
Một import tempfile with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(data) with rasterio.open(tmpfile.name) as dataset: data_array = dataset.read() 0 trống cũng có thể được viết để sử dụng các phương thức API dữ liệu.
with MemoryFile() as memfile: with memfile.open(driver='GTiff', count=3, ...) as dataset: dataset.write(data_array)
Đọc MemoryFiles
Giống như import tempfile with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(data) with rasterio.open(tmpfile.name) as dataset: data_array = dataset.read() 2, import tempfile with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(data) with rasterio.open(tmpfile.name) as dataset: data_array = dataset.read() 0 thực hiện giao thức tệp Python và cung cấp các phương thức from rasterio.io import MemoryFile with MemoryFile(data) as memfile: with memfile.open() as dataset: data_array = dataset.read() 1, from rasterio.io import MemoryFile with MemoryFile(data) as memfile: with memfile.open() as dataset: data_array = dataset.read() 2 và from rasterio.io import MemoryFile with MemoryFile(data) as memfile: with memfile.open() as dataset: data_array = dataset.read() 3. Do đó, các trường hợp phù hợp như các đối số cho các phương thức như yêu cầu.post ().
with MemoryFile() as memfile: with memfile.open(driver='GTiff', count=3, ...) as dataset: dataset.write(data_array) requests.post('//example.com/upload', data=memfile)