/
_a_source.py
145 lines (116 loc) · 4 KB
/
_a_source.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
import sys
from osgeo import osr
from buzzard import _tools
class ASource:
"""Base abstract class defining the common behavior of all sources opened in the Dataset.
Features Defined
----------------
- Has a `stored` spatial reference
- Has a `virtual` spatial reference that is influenced by the Dataset's opening mode
- Can be closed
"""
def __init__(self, ds, back):
self._ds = ds
self._back = back
@property
def wkt_stored(self):
"""The spatial reference that can be found in the metadata of a source, in wkt format.
string or None
"""
return self._back.wkt_stored
@property
def proj4_stored(self):
"""The spatial reference that can be found in the metadata of a source, in proj4 format.
string or None
"""
return self._back.proj4_stored
@property
def wkt_virtual(self):
"""The spatial reference considered to be written in the metadata of a source, in wkt
format.
string or None
"""
return self._back.wkt_virtual
@property
def proj4_virtual(self):
"""The spatial reference considered to be written in the metadata of a source, in proj4
format.
string or None
"""
return self._back.proj4_virtual
def get_keys(self):
"""Get the list of keys under which this source is registered to in the Dataset"""
return list(self._ds._keys_of_source[self])
@property
def close(self):
"""Close a source with a call or a context management.
The `close` attribute returns an object that can be both called and used in a with statement
Examples
--------
>>> ds.dem.close()
>>> with ds.dem.close:
# code...
>>> with ds.acreate_raster('result.tif', fp, float, 1).close as result:
# code...
>>> with ds.acreate_vector('results.shp', 'linestring').close as roofs:
# code...
"""
def _close():
self._back.close()
self._ds._unregister(self)
del self._ds
del self._back
return _CloseRoutine(self, _close)
def __del__(self):
if hasattr(self, '_ds'):
self.close()
# Deprecation
wkt_origin = _tools.deprecation_pool.wrap_property(
'wkt_virtual',
'0.4.4'
)
proj4_origin = _tools.deprecation_pool.wrap_property(
'proj4_virtual',
'0.4.4'
)
class ABackSource:
"""Implementation of ASource's specifications"""
def __init__(self, back_ds, wkt_stored, rect, **kwargs):
wkt_virtual = back_ds.virtual_of_stored_given_mode(
wkt_stored, back_ds.wkt_work, back_ds.wkt_fallback, back_ds.wkt_forced,
)
if wkt_virtual is not None:
sr_virtual = osr.SpatialReference(wkt_virtual)
else:
sr_virtual = None
to_work, to_virtual = back_ds.get_transforms(sr_virtual, rect)
self.back_ds = back_ds
self.wkt_stored = wkt_stored
self.wkt_virtual = wkt_virtual
self.to_work = to_work
self.to_virtual = to_virtual
super().__init__(**kwargs)
def close(self):
"""Virtual method:
- May be overriden
- Should always be called
"""
del self.back_ds
@property
def proj4_virtual(self):
if self.wkt_virtual is None:
return None # pragma: no cover
return osr.SpatialReference(self.wkt_virtual).ExportToProj4()
@property
def proj4_stored(self):
if self.wkt_stored is None:
return None # pragma: no cover
return osr.SpatialReference(self.wkt_stored).ExportToProj4()
_CloseRoutine = type('_CloseRoutine', (_tools.CallOrContext,), {
'__doc__': ASource.close.__doc__,
})
if sys.version_info < (3, 6):
# https://www.python.org/dev/peps/pep-0487/
for k, v in ASource.__dict__.items():
if hasattr(v, '__set_name__'):
v.__set_name__(ASource, k)