GeoDjango GDAL Slice Error in layer.py

29 views
Skip to first unread message

Justin Johnson

unread,
Jan 12, 2018, 5:37:10 PM1/12/18
to Django users


I'm completely new to GeoDjango.  I'm working through the tutorial for Django version 2.0, and I am encountering an error when I follow the steps in the GeoDjango section.  

Specifically, when I attempt to slice a Layer object to extract Features, it gives me an error which looks like a possible bug.   I'll go through the steps here.

  1. https://docs.djangoproject.com/en/2.0/ref/contrib/gis/tutorial/#importing-spatial-data
    1. in this section of the tutorial, we import spatial data and use the GDAL library to work with an imported shapefile of world borders
    2. Next, through the Django shell, I create a DataSource object using the django.contrib.gis.gdal module
    3. I am able to retrieve, from this DataSource object, a Layer object containing Features for each polygon
    4. I can work through the examples showing the properties of that Layer object, and iterate through the Features in the Layer, so I know that's working
  2. The problem is when I try to Slice a Layer, to extract a subset of Features: 
    1. The example says I should be able to run  >>> lyr[0:2] and receive two Feature objects
    2. However, what I get is an error message saying there is an IndexError, and it mentions a TypeError

This is an image of the Traceback




And here is the traceback in text:



In [12]: lyr[0:2]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-12-a781c315deac> in <module>()
----> 1 lyr[0:2]

c:\projects\python\env\ENV3\lib\site-packages\django\contrib\gis\gdal\layer.py in __getitem__(self, index)
     49             # A slice was given
     50             start, stop, stride = index.indices(self.num_feat)
---> 51             return [self._make_feature(fid) for fid in range(start, stop, stride)]
     52         else:
     53             raise TypeError('Integers and slices may only be used when indexing OGR Layers.')

c:\projects\python\env\ENV3\lib\site-packages\django\contrib\gis\gdal\layer.py in <listcomp>(.0)
     49             # A slice was given
     50             start, stop, stride = index.indices(self.num_feat)
---> 51             return [self._make_feature(fid) for fid in range(start, stop, stride)]
     52         else:
     53             raise TypeError('Integers and slices may only be used when indexing OGR Layers.')

c:\projects\python\env\ENV3\lib\site-packages\django\contrib\gis\gdal\layer.py in _make_feature(self, feat_id)
     88                     return feat
     89         # Should have returned a Feature, raise an IndexError.
---> 90         raise IndexError('Invalid feature id: %s.' % feat_id)
     91
     92     # #### Layer properties ####

IndexError: Invalid feature id: 0.


Again, I'm totally new to GeoDjango, so this could all be a user error.  I have restarted the tutorial 3 times and it always fails at the slice example.  The comment in the Traceback mentioning an IndexError has me wondering.

Thanks in advance.


Jason

unread,
Jan 13, 2018, 8:23:41 AM1/13/18
to Django users
Are you able to access any features in the layer?

eg, the snippet above the one that gives you the error

    >>> for feat in lyr:
...    print(feat.get('NAME'), feat.geom.num_points)
Reply all
Reply to author
Forward
0 new messages