-
Notifications
You must be signed in to change notification settings - Fork 523
/
io.py
283 lines (219 loc) · 8.85 KB
/
io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""Classes capable of reading and writing datasets
Instances of these classes are called dataset objects.
"""
import logging
import rasterio._loading
with rasterio._loading.add_gdal_dll_directories():
from rasterio._base import (
get_dataset_driver, driver_can_create, driver_can_create_copy)
from rasterio._io import (
DatasetReaderBase, DatasetWriterBase, BufferedDatasetWriterBase,
MemoryFileBase)
from rasterio.windows import WindowMethodsMixin
from rasterio.env import ensure_env
from rasterio.transform import TransformMethodsMixin
from rasterio._path import _UnparsedPath
try:
from rasterio._filepath import FilePathBase
except ImportError:
FilePathBase = object
log = logging.getLogger(__name__)
class DatasetReader(DatasetReaderBase, WindowMethodsMixin,
TransformMethodsMixin):
"""An unbuffered data and metadata reader"""
def __repr__(self):
return "<{} DatasetReader name='{}' mode='{}'>".format(
self.closed and 'closed' or 'open', self.name, self.mode)
class DatasetWriter(DatasetWriterBase, WindowMethodsMixin,
TransformMethodsMixin):
"""An unbuffered data and metadata writer. Its methods write data
directly to disk.
"""
def __repr__(self):
return "<{} DatasetWriter name='{}' mode='{}'>".format(
self.closed and 'closed' or 'open', self.name, self.mode)
class BufferedDatasetWriter(BufferedDatasetWriterBase, WindowMethodsMixin,
TransformMethodsMixin):
"""Maintains data and metadata in a buffer, writing to disk or
network only when `close()` is called.
This allows incremental updates to datasets using formats that don't
otherwise support updates, such as JPEG.
"""
def __repr__(self):
return "<{} BufferedDatasetWriter name='{}' mode='{}'>".format(
self.closed and 'closed' or 'open', self.name, self.mode)
class MemoryFile(MemoryFileBase):
"""A BytesIO-like object, backed by an in-memory file.
This allows formatted files to be read and written without I/O.
A MemoryFile created with initial bytes becomes immutable. A
MemoryFile created without initial bytes may be written to using
either file-like or dataset interfaces.
Examples
--------
A GeoTIFF can be loaded in memory and accessed using the GeoTIFF
format driver
>>> with open('tests/data/RGB.byte.tif', 'rb') as f, MemoryFile(f) as memfile:
... with memfile.open() as src:
... pprint.pprint(src.profile)
...
{'count': 3,
'crs': CRS({'init': 'epsg:32618'}),
'driver': 'GTiff',
'dtype': 'uint8',
'height': 718,
'interleave': 'pixel',
'nodata': 0.0,
'tiled': False,
'transform': Affine(300.0379266750948, 0.0, 101985.0,
0.0, -300.041782729805, 2826915.0),
'width': 791}
"""
def __init__(self, file_or_bytes=None, dirname=None, filename=None, ext='.tif'):
"""Create a new file in memory
Parameters
----------
file_or_bytes : file-like object or bytes, optional
File or bytes holding initial data.
filename : str, optional
An optional filename. A unique one will otherwise be generated.
ext : str, optional
An optional extension.
Returns
-------
MemoryFile
"""
super().__init__(
file_or_bytes=file_or_bytes, dirname=dirname, filename=filename, ext=ext
)
@ensure_env
def open(self, driver=None, width=None, height=None, count=None, crs=None,
transform=None, dtype=None, nodata=None, sharing=False, **kwargs):
"""Open the file and return a Rasterio dataset object.
If data has already been written, the file is opened in 'r'
mode. Otherwise, the file is opened in 'w' mode.
Parameters
----------
Note well that there is no `path` parameter: a `MemoryFile`
contains a single dataset and there is no need to specify a
path.
Other parameters are optional and have the same semantics as the
parameters of `rasterio.open()`.
"""
mempath = _UnparsedPath(self.name)
if self.closed:
raise OSError("I/O operation on closed file.")
if len(self) > 0:
log.debug("VSI path: {}".format(mempath.path))
return DatasetReader(mempath, driver=driver, sharing=sharing, **kwargs)
else:
writer = get_writer_for_driver(driver)
return writer(mempath, 'w+', driver=driver, width=width,
height=height, count=count, crs=crs,
transform=transform, dtype=dtype,
nodata=nodata, sharing=sharing, **kwargs)
def __enter__(self):
return self
def __exit__(self, *args):
self.close()
class _FilePath(FilePathBase):
"""A BytesIO-like object, backed by an in-memory file.
This allows formatted files to be read and written without I/O.
A MemoryFile created with initial bytes becomes immutable. A
MemoryFile created without initial bytes may be written to using
either file-like or dataset interfaces.
Examples
--------
A GeoTIFF can be loaded in memory and accessed using the GeoTIFF
format driver
>>> with open('tests/data/RGB.byte.tif', 'rb') as f, FilePath(f) as vsi_file:
... with vsi_file.open() as src:
... pprint.pprint(src.profile)
...
{'count': 3,
'crs': CRS({'init': 'epsg:32618'}),
'driver': 'GTiff',
'dtype': 'uint8',
'height': 718,
'interleave': 'pixel',
'nodata': 0.0,
'tiled': False,
'transform': Affine(300.0379266750948, 0.0, 101985.0,
0.0, -300.041782729805, 2826915.0),
'width': 791}
"""
def __init__(self, filelike_obj, dirname=None, filename=None):
"""Create a new wrapper around the provided file-like object.
Parameters
----------
filelike_obj : file-like object
Open file-like object. Currently only reading is supported.
filename : str, optional
An optional filename. A unique one will otherwise be generated.
Returns
-------
PythonVSIFile
"""
super().__init__(
filelike_obj, dirname=dirname, filename=filename
)
@ensure_env
def open(self, driver=None, sharing=False, **kwargs):
"""Open the file and return a Rasterio dataset object.
The provided file-like object is assumed to be readable.
Writing is currently not supported.
Parameters are optional and have the same semantics as the
parameters of `rasterio.open()`.
"""
mempath = _UnparsedPath(self.name)
if self.closed:
raise IOError("I/O operation on closed file.")
# Assume we were given a non-empty file-like object
log.debug("VSI path: {}".format(mempath.path))
return DatasetReader(mempath, driver=driver, sharing=sharing, **kwargs)
def __enter__(self):
return self
def __exit__(self, *args):
self.close()
if FilePathBase is not object:
# only make this object available if the cython extension was compiled
FilePath = _FilePath
class ZipMemoryFile(MemoryFile):
"""A read-only BytesIO-like object backed by an in-memory zip file.
This allows a zip file containing formatted files to be read
without I/O.
"""
def __init__(self, file_or_bytes=None):
super().__init__(file_or_bytes, ext="zip")
@ensure_env
def open(self, path, driver=None, sharing=False, **kwargs):
"""Open a dataset within the zipped stream.
Parameters
----------
path : str
Path to a dataset in the zip file, relative to the root of the
archive.
Other parameters are optional and have the same semantics as the
parameters of `rasterio.open()`.
Returns
-------
A Rasterio dataset object
"""
zippath = _UnparsedPath('/vsizip{0}/{1}'.format(self.name, path.lstrip('/')))
if self.closed:
raise OSError("I/O operation on closed file.")
return DatasetReader(zippath, driver=driver, sharing=sharing, **kwargs)
def get_writer_for_driver(driver):
"""Return the writer class appropriate for the specified driver."""
if not driver:
raise ValueError("'driver' is required to read/write dataset.")
cls = None
if driver_can_create(driver):
cls = DatasetWriter
elif driver_can_create_copy(driver): # pragma: no branch
cls = BufferedDatasetWriter
return cls
def get_writer_for_path(path, driver=None):
"""Return the writer class appropriate for the existing dataset."""
if not driver:
driver = get_dataset_driver(path)
return get_writer_for_driver(driver)