Geodjango 使用 LayerMapping 和 OGR 导入 Shapefile
Geodjango Import Shapefile with LayerMapping and OGR
我正在尝试将 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()
它 returns 我 :
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
,这就是将您的路径转换为正确的 OS 路径(使用 \
用于 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
但这是可选的。
我正在尝试将 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()
它 returns 我 :
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
,这就是将您的路径转换为正确的 OS 路径(使用 \
用于 Windows 或 /
用于 Linux 等):
Normalize a pathname by collapsing redundant separators and up-level references so that
A//B
,A/B/
,A/./B
andA/foo/../B
all becomeA/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
但这是可选的。