我正在尝试将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使用/)。


  通过折叠冗余分隔符和上级引用来标准化路径名,以使A//BA/B/A/./BA/foo/../B都变为A/B。此字符串操作可能会更改包含符号链接的路径的含义。在Windows上,它将正斜杠转换为反斜杠。要规范大小写,请使用normcase()。


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

关于python - Geodjango导入具有LayerMapping和OGR的Shapefile,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58833307/

10-16 12:04