Python GeoDjango:无法使用要素ID(索引器)从图层对象访问要素

Python GeoDjango:无法使用要素ID(索引器)从图层对象访问要素,python,django,gis,gdal,geodjango,Python,Django,Gis,Gdal,Geodjango,我正在使用此处找到的世界边界数据完成GeoDjango教程:()。我遇到的问题是从图层对象访问单个要素。我尝试过使用列表切片并通过is功能id访问对象,但没有成功。我还尝试添加“from django.contrib.gis.gdal import*”,以防遗漏一些内容,但这也没有帮助。如果有人能就可能出现的问题或我在过程中犯了错误与我分享任何想法,我将不胜感激。请参阅下面的代码以获得进一步解释,谢谢 $ python manage.py shell # Following the GeoDj

我正在使用此处找到的世界边界数据完成GeoDjango教程:()。我遇到的问题是从图层对象访问单个要素。我尝试过使用列表切片并通过is功能id访问对象,但没有成功。我还尝试添加“from django.contrib.gis.gdal import*”,以防遗漏一些内容,但这也没有帮助。如果有人能就可能出现的问题或我在过程中犯了错误与我分享任何想法,我将不胜感激。请参阅下面的代码以获得进一步解释,谢谢

$ python manage.py shell

# Following the GeoDjango tutorial step by step
>>> from pathlib import Path
>>> import my_app
>>> world_shp = Path(my_app.__file__).resolve().parent/'data'/'world.shp'
>>> from django.contrib.gis.gdal import DataSource
>>> ds = DataSource(str(world_shp))
>>> print(ds)
C:\path\to\my_app\data\world.shp (ESRI Shapefile)
>>> print(len(ds))
1
>>> lyr = ds[0]
>>> print(len(lyr))
246

# The Layer object's features exist
>>> for feature in lyr:
...     print(feature.fid)
... 
0
1
2
3
...
245

# Attempt to access Feature object at index[0]
>>> lyr[0]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "...\src\venv\lib\site-packages\django\contrib\gis\gdal\layer.py", line 47, in __getitem__
    return self._make_feature(index)
  File "...\src\venv\lib\site-packages\django\contrib\gis\gdal\layer.py", line 90, in _make_feature
    raise IndexError('Invalid feature id: %s.' % feat_id)
IndexError: Invalid feature id: 0.

# After iterating through features in lyr, a feature object now exists with feature id of 245
>>> print(feature.fid)  
245

# Attempt to access Feature object at index[245]
>>> lyr[245]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "...\src\venv\lib\site-packages\django\contrib\gis\gdal\layer.py", line 47, in __getitem__
    return self._make_feature(index)
  File "...\src\venv\lib\site-packages\django\contrib\gis\gdal\layer.py", line 90, in _make_feature
    raise IndexError('Invalid feature id: %s.' % feat_id)
IndexError: Invalid feature id: 245.
$python manage.py shell
#遵循GeoDjango教程的步骤
>>>从pathlib导入路径
>>>导入我的应用程序
>>>world\u shp=Path(我的应用程序文件解析().parent/'data'/'world.shp'
>>>从django.contrib.gis.gdal导入数据源
>>>ds=数据源(str(世界_shp))
>>>打印(ds)
C:\path\to\my\u app\data\world.shp(ESRI形状文件)
>>>打印(镜头(ds))
1.
>>>lyr=ds[0]
>>>打印(len(lyr))
246
#图层对象的特征存在
>>>对于lyr中的功能:
...     打印(feature.fid)
... 
0
1.
2.
3.
...
245
#尝试访问索引[0]处的功能对象
>>>歌词[0]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“…\src\venv\lib\site packages\django\contrib\gis\gdal\layer.py”,第47行,在\uu getitem中__
返回自我。生成功能(索引)
文件“…\src\venv\lib\site packages\django\contrib\gis\gdal\layer.py”,第90行,在“制作”功能中
提升索引器('无效的功能id:%s.%feat\u id)
索引器:无效的功能id:0。
#在对lyr中的特征进行迭代之后,现在存在一个特征id为245的特征对象
>>>打印(feature.fid)
245
#尝试访问索引[245]处的要素对象
>>>里尔[245]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“…\src\venv\lib\site packages\django\contrib\gis\gdal\layer.py”,第47行,在\uu getitem中__
返回自我。生成功能(索引)
文件“…\src\venv\lib\site packages\django\contrib\gis\gdal\layer.py”,第90行,在“制作”功能中
提升索引器('无效的功能id:%s.%feat\u id)
索引器:无效的功能id:245。