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)
On Friday, January 12, 2018 at 5:37:10 PM UTC-5, Justin Johnson wrote:
>
>
>
> 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
>
> <https://docs.djangoproject.com/en/2.0/ref/contrib/gis/gdal/#django.contrib.gis.gdal.DataSource>
>
> using the *django.contrib.gis.gdal *module
> 3. I am able to retrieve, from this DataSource object, a Layer
> object
> <https://docs.djangoproject.com/en/2.0/ref/contrib/gis/gdal/#layer>
> 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
>
>
> <https://lh3.googleusercontent.com/-brhlB-hy304/Wlk0oyv_XwI/AAAAAAAAf4c/mjZO4-9mEq4NjCj06XXm60HB90SwE9YSwCLcBGAs/s1600/Django_Slice_Error.jpg>
>
>
>
> 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.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/82b69997-caed-4330-aef7-4d5c26bc6656%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.