gpt4 book ai didi

python - 使用 LayerMapping 和 OGR 导入 Geodjango 形状文件

转载 作者:行者123 更新时间:2023-12-01 06:55:21 24 4
gpt4 key购买 nike

我正在尝试将 shapefile 加载到 Django

我的文件夹

vnm_polbn_adm3_2014_pdc.dbf
vnm_polbn_adm3_2014_pdc.prj
vnm_polbn_adm3_2014_pdc.qpj
vnm_polbn_adm3_2014_pdc.shp
vnm_polbn_adm3_2014_pdc.shx

当我运行这个时:

simon@DESKTOP-V82N14G:/mnt/c/WINDOWS/system32/poc_dashboard/pocdash$  ogrinfo -ro -so  testdjango/data/vnm_polbn_adm3_2014_pdc.shp
INFO: Open of `testdjango/data/vnm_polbn_adm3_2014_pdc.shp'
using driver `ESRI Shapefile' successful.
1: vnm_polbn_adm3_2014_pdc (Polygon)

当我使用 python shell 运行我的代码时:

python manage.py shell
from testdjango import load_layer
load_layer.run()

它返回我:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/mnt/c/WINDOWS/system32/poc_dashboard/pocdash/testdjango/load_layer.py", line 27, in run
lm = LayerMapping(Adm3_names, adm3_name_shp, adm3_name_mapping, transform= False, encoding='iso-8859-1')
File "/mnt/c/WINDOWS/system32/poc_dashboard/poc/lib/python3.6/site-packages/django/contrib/gis/utils/layermapping.py", line 92, in __init__
self.ds = DataSource(data, encoding=encoding)
File "/mnt/c/WINDOWS/system32/poc_dashboard/poc/lib/python3.6/site-packages/django/contrib/gis/gdal/datasource.py", line 74, in __init__
raise GDALException('Could not open the datasource at "%s"' % ds_input)
django.contrib.gis.gdal.error.GDALException: Could not open the datasource at "/mnt/c/WINDOWS/system32/poc_dashboard/pocdash/testdjango/testdjango/data/vnm_polbn_adm3_2014_pdc.shp"

是我的代码有问题还是我的 shapefile 有问题?我附上了文件here

import os
from django.contrib.gis.utils import LayerMapping
from django.contrib.gis.db import models
from .models import Adm3_names

adm3_name_mapping = {
'gid': 'gid',
'adm1_code': 'adm1_code',
'adm1_name': 'adm1_name',
'adm2_code': 'adm2_code',
'adm2_name': 'adm2_name',
'adm3_code': 'adm3_code',
'adm3_name': 'adm3_name',
'adm3_statu': 'adm3_statu',
'pop': 'pop',
'pop_0to14': 'pop_0to14',
'pop_65plus': 'pop_65plus',
'hh': 'hh',
'shape_leng': 'shape_leng',
'shape_area': 'shape_area',
'geom': 'MULTIPOLYGON',
}

adm3_name_shp = os.path. abspath(os.path.join(os.path.dirname(__file__),'testdjango/data/vnm_polbn_adm3_2014_pdc.shp'))

def run(verbose=True):
lm = LayerMapping(Adm3_names, adm3_name_shp, adm3_name_mapping, transform= False, encoding='iso-8859-1')
lm.save(strict=True,verbose=verbose)

https://docs.djangoproject.com/en/2.2/ref/contrib/gis/layermapping/

最佳答案

正如我们从评论中发现的,问题在于创建了 adm3_name_shp,它向路径添加了一个额外的 testdjango 文件夹:

更改此:

adm3_name_shp = os.path.abspath(os.path.join(os.path.dirname(__file__),'testdjango/data/vnm_polbn_adm3_2014_pdc.shp'))

对此:

adm3_name_shp = os.path.abspath(os.path.join(os.path.dirname(__file__),'data/vnm_polbn_adm3_2014_pdc.shp'))

您应该考虑的另一个提示是 os.path.abspath 调用 os.path.normpath在后台,这就是将您的路径转换为正确的操作系统路径的原因(对于 Windows 使用 \ 或对于 Linux 等使用 /):

Normalize a pathname by collapsing redundant separators and up-level references so that A//B, A/B/, A/./B and A/foo/../B all become A/B. This string manipulation may change the meaning of a path that contains symbolic links. On Windows, it converts forward slashes to backward slashes. To normalize case, use normcase().

因此,您可以将 abspath 替换为 normpath,但这是可选的。

关于python - 使用 LayerMapping 和 OGR 导入 Geodjango 形状文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58833307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com