MCPcopy
hub / github.com/django/django / Layer

Class Layer

django/contrib/gis/gdal/layer.py:21–234  ·  view source on GitHub ↗

A class that wraps an OGR Layer, needs to be instantiated from a DataSource object.

Source from the content-addressed store, hash-verified

19#
20# The OGR_L_* routines are relevant here.
21class Layer(GDALBase):
22 """
23 A class that wraps an OGR Layer, needs to be instantiated from a DataSource
24 object.
25 """
26
27 def __init__(self, layer_ptr, ds):
28 """
29 Initialize on an OGR C pointer to the Layer and the `DataSource` object
30 that owns this layer. The `DataSource` object is required so that a
31 reference to it is kept with this Layer. This prevents garbage
32 collection of the `DataSource` while this Layer is still active.
33 """
34 if not layer_ptr:
35 raise GDALException("Cannot create Layer, invalid pointer given")
36 self.ptr = layer_ptr
37 self._ds = ds
38 self._ldefn = capi.get_layer_defn(self._ptr)
39 # Does the Layer support random reading?
40 self._random_read = self.test_capability(b"RandomRead")
41
42 def __getitem__(self, index):
43 "Get the Feature at the specified index."
44 if isinstance(index, int):
45 # An integer index was given -- we cannot do a check based on the
46 # number of features because the beginning and ending feature IDs
47 # are not guaranteed to be 0 and len(layer)-1, respectively.
48 if index < 0:
49 raise IndexError("Negative indices are not allowed on OGR Layers.")
50 return self._make_feature(index)
51 elif isinstance(index, slice):
52 # A slice was given
53 start, stop, stride = index.indices(self.num_feat)
54 return [self._make_feature(fid) for fid in range(start, stop, stride)]
55 else:
56 raise TypeError(
57 "Integers and slices may only be used when indexing OGR Layers."
58 )
59
60 def __iter__(self):
61 "Iterate over each Feature in the Layer."
62 # ResetReading() must be called before iteration is to begin.
63 capi.reset_reading(self._ptr)
64 for i in range(self.num_feat):
65 yield Feature(capi.get_next_feature(self._ptr), self)
66
67 def __len__(self):
68 "The length is the number of features."
69 return self.num_feat
70
71 def __str__(self):
72 "The string name of the layer."
73 return self.name
74
75 def _make_feature(self, feat_id):
76 """
77 Helper routine for __getitem__ that constructs a Feature from the given
78 Feature ID. If the OGR Layer does not support random-access reading,

Callers 1

__getitem__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected