GeoDjango GDAL Slice Error in layer.py

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

GeoDjango GDAL Slice Error in layer.py

Justin-23


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.


--
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 [hidden email].
To post to this group, send email to [hidden email].
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/a8fb48a0-8bcd-41bb-a3ce-737c324d96cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: GeoDjango GDAL Slice Error in layer.py

Jason-6
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. <a href="https://docs.djangoproject.com/en/2.0/ref/contrib/gis/tutorial/#importing-spatial-data" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F2.0%2Fref%2Fcontrib%2Fgis%2Ftutorial%2F%23importing-spatial-data\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH1TCG_XMSe4zEFFPHohvD-qfqIpg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F2.0%2Fref%2Fcontrib%2Fgis%2Ftutorial%2F%23importing-spatial-data\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH1TCG_XMSe4zEFFPHohvD-qfqIpg&#39;;return true;">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 <a href="https://docs.djangoproject.com/en/2.0/ref/contrib/gis/gdal/#django.contrib.gis.gdal.DataSource" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F2.0%2Fref%2Fcontrib%2Fgis%2Fgdal%2F%23django.contrib.gis.gdal.DataSource\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG0SgeIT0l8gm-mvED3Xq4cK91hBg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F2.0%2Fref%2Fcontrib%2Fgis%2Fgdal%2F%23django.contrib.gis.gdal.DataSource\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG0SgeIT0l8gm-mvED3Xq4cK91hBg&#39;;return true;">DataSource object using the django.contrib.gis.gdal module
    3. I am able to retrieve, from this DataSource object, a <a href="https://docs.djangoproject.com/en/2.0/ref/contrib/gis/gdal/#layer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F2.0%2Fref%2Fcontrib%2Fgis%2Fgdal%2F%23layer\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHE-HmX_5UskE3CViTiDKcogGg19w&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F2.0%2Fref%2Fcontrib%2Fgis%2Fgdal%2F%23layer\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHE-HmX_5UskE3CViTiDKcogGg19w&#39;;return true;">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

<a href="https://lh3.googleusercontent.com/-brhlB-hy304/Wlk0oyv_XwI/AAAAAAAAf4c/mjZO4-9mEq4NjCj06XXm60HB90SwE9YSwCLcBGAs/s1600/Django_Slice_Error.jpg" style="margin-left:1em;margin-right:1em" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://lh3.googleusercontent.com/-brhlB-hy304/Wlk0oyv_XwI/AAAAAAAAf4c/mjZO4-9mEq4NjCj06XXm60HB90SwE9YSwCLcBGAs/s1600/Django_Slice_Error.jpg&#39;;return true;" onclick="this.href=&#39;https://lh3.googleusercontent.com/-brhlB-hy304/Wlk0oyv_XwI/AAAAAAAAf4c/mjZO4-9mEq4NjCj06XXm60HB90SwE9YSwCLcBGAs/s1600/Django_Slice_Error.jpg&#39;;return true;">




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 [hidden email].
To post to this group, send email to [hidden email].
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.