Exemple #1
0
 def __getitem__(self, index):
     "Allows use of the index [] operator to get a layer at the index."
     if isinstance(index, six.string_types):
         l = capi.get_layer_by_name(self.ptr, force_bytes(index))
         if not l: raise OGRIndexError('invalid OGR Layer name given: "%s"' % index)
     elif isinstance(index, int):
         if index < 0 or index >= self.layer_count:
             raise OGRIndexError('index out of range')
         l = capi.get_layer(self._ptr, index)
     else:
         raise TypeError('Invalid index type: %s' % type(index))
     return Layer(l, self)
Exemple #2
0
 def __getitem__(self, index):
     "Gets the Geometry at the specified index."
     if index < 0 or index >= self.geom_count:
         raise OGRIndexError('index out of range: %s' % index)
     else:
         return OGRGeometry(
             capi.clone_geom(capi.get_geom_ref(self.ptr, index)), self.srs)
Exemple #3
0
 def index(self, field_name):
     "Returns the index of the given field name."
     i = capi.get_field_index(self.ptr, force_bytes(field_name))
     if i < 0:
         raise OGRIndexError('invalid OFT field name given: "%s"' %
                             field_name)
     return i
Exemple #4
0
 def index(self, field_name):
     "Returns the index of the given field name."
     i = get_field_index(self._ptr, field_name)
     if i < 0:
         raise OGRIndexError('invalid OFT field name given: "%s"' %
                             field_name)
     return i
Exemple #5
0
 def __getitem__(self, index):
     "Returns the Point at the given index."
     if index >= 0 and index < self.point_count:
         x, y, z = c_double(), c_double(), c_double()
         capi.get_point(self.ptr, index, byref(x), byref(y), byref(z))
         dim = self.coord_dim
         if dim == 1:
             return (x.value, )
         elif dim == 2:
             return (x.value, y.value)
         elif dim == 3:
             return (x.value, y.value, z.value)
     else:
         raise OGRIndexError('index out of range: %s' % str(index))
Exemple #6
0
 def __getitem__(self, index):
     """
     Gets the Field object at the specified index, which may be either
     an integer or the Field's string label.  Note that the Field object
     is not the field's _value_ -- use the `get` method instead to
     retrieve the value (e.g. an integer) instead of a Field instance.
     """
     if isinstance(index, six.string_types):
         i = self.index(index)
     else:
         if index < 0 or index > self.num_fields:
             raise OGRIndexError('index out of range')
         i = index
     return Field(self, i)
Exemple #7
0
 def __getitem__(self, index):
     "Gets the Feature at the specified index."
     if isinstance(index, (int, long)):
         # An integer index was given -- we cannot do a check based on the
         # number of features because the beginning and ending feature IDs
         # are not guaranteed to be 0 and len(layer)-1, respectively.
         if index < 0: raise OGRIndexError('Negative indices are not allowed on OGR Layers.')
         return self._make_feature(index)
     elif isinstance(index, slice):
         # A slice was given
         start, stop, stride = index.indices(self.num_feat)
         return [self._make_feature(fid) for fid in xrange(start, stop, stride)]
     else:
         raise TypeError('Integers and slices may only be used when indexing OGR Layers.')
Exemple #8
0
 def __getitem__(self, index):
     "Gets the Feature at the specified index."
     if not isinstance(index, (slice, int)):
         raise TypeError
     end = self.num_feat
     if isinstance(index, int):
         # An integer index was given
         if index < 0:
             index = end - index
         if index < 0 or index >= self.num_feat:
             raise OGRIndexError('index out of range')
         return self._make_feature(index)
     else:
         # A slice was given
         start, stop, stride = index.indices(end)
         return [
             self._make_feature(offset)
             for offset in range(start, stop, stride)
         ]
 def _make_feature(self, feat_id):
     """
     Helper routine for __getitem__ that constructs a Feature from the given
     Feature ID.  If the OGR Layer does not support random-access reading,
     then each feature of the layer will be incremented through until the
     a Feature is found matching the given feature ID.
     """
     if self._random_read:
         # If the Layer supports random reading, return.
         with suppress(GDALException):
             return Feature(capi.get_feature(self.ptr, feat_id), self)
     else:
         # Random access isn't supported, have to increment through
         # each feature until the given feature ID is encountered.
         for feat in self:
             if feat.fid == feat_id:
                 return feat
     # Should have returned a Feature, raise an OGRIndexError.
     raise OGRIndexError('Invalid feature id: %s.' % feat_id)
=======
        "Return the Point at the given index."
        if 0 <= index < self.point_count:
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
            x, y, z = c_double(), c_double(), c_double()
            capi.get_point(self.ptr, index, byref(x), byref(y), byref(z))
            dim = self.coord_dim
            if dim == 1:
                return (x.value,)
            elif dim == 2:
                return (x.value, y.value)
            elif dim == 3:
                return (x.value, y.value, z.value)
        else:
<<<<<<< HEAD
            raise OGRIndexError('index out of range: %s' % str(index))

    def __iter__(self):
        "Iterates over each point in the LineString."
        for i in range(self.point_count):
            yield self[i]

    def __len__(self):
        "The length returns the number of points in the LineString."
=======
            raise IndexError('Index out of range when accessing points of a line string: %s.' % index)

    def __len__(self):
        "Return the number of points in the LineString."
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
        return self.point_count
Exemple #11
0
        """
<<<<<<< HEAD
        Gets the Field object at the specified index, which may be either
=======
        Get the Field object at the specified index, which may be either
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
        an integer or the Field's string label.  Note that the Field object
        is not the field's _value_ -- use the `get` method instead to
        retrieve the value (e.g. an integer) instead of a Field instance.
        """
<<<<<<< HEAD
        if isinstance(index, six.string_types):
            i = self.index(index)
        else:
            if index < 0 or index > self.num_fields:
                raise OGRIndexError('index out of range')
            i = index
        return Field(self, i)

    def __iter__(self):
        "Iterates over each field in the Feature."
        for i in range(self.num_fields):
            yield self[i]

    def __len__(self):
        "Returns the count of fields in this feature."
=======
        if isinstance(index, str):
            i = self.index(index)
        elif 0 <= index < self.num_fields:
            i = index
Exemple #12
0
        self._random_read = self.test_capability(b'RandomRead')

    def __getitem__(self, index):
<<<<<<< HEAD
        "Gets the Feature at the specified index."
        if isinstance(index, six.integer_types):
=======
        "Get the Feature at the specified index."
        if isinstance(index, int):
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
            # An integer index was given -- we cannot do a check based on the
            # number of features because the beginning and ending feature IDs
            # are not guaranteed to be 0 and len(layer)-1, respectively.
            if index < 0:
<<<<<<< HEAD
                raise OGRIndexError('Negative indices are not allowed on OGR Layers.')
=======
                raise IndexError('Negative indices are not allowed on OGR Layers.')
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
            return self._make_feature(index)
        elif isinstance(index, slice):
            # A slice was given
            start, stop, stride = index.indices(self.num_feat)
            return [self._make_feature(fid) for fid in range(start, stop, stride)]
        else:
            raise TypeError('Integers and slices may only be used when indexing OGR Layers.')

    def __iter__(self):
<<<<<<< HEAD
        "Iterates over each Feature in the Layer."
=======