Fixed #32230 -- Made DataSource support pathlib.Path.

This commit is contained in:
Hasan Ramezani 2020-11-27 18:58:28 +01:00 committed by Mariusz Felisiak
parent 3828879eee
commit b37be072a2
5 changed files with 16 additions and 2 deletions

View File

@ -34,6 +34,7 @@
val = field.value val = field.value
""" """
from ctypes import byref from ctypes import byref
from pathlib import Path
from django.contrib.gis.gdal.base import GDALBase from django.contrib.gis.gdal.base import GDALBase
from django.contrib.gis.gdal.driver import Driver from django.contrib.gis.gdal.driver import Driver
@ -62,7 +63,7 @@ class DataSource(GDALBase):
Driver.ensure_registered() Driver.ensure_registered()
if isinstance(ds_input, str): if isinstance(ds_input, (str, Path)):
# The data source driver is a void pointer. # The data source driver is a void pointer.
ds_driver = Driver.ptr_type() ds_driver = Driver.ptr_type()
try: try:

View File

@ -92,6 +92,10 @@ each feature in that layer.
Returns the name of the data source. Returns the name of the data source.
.. versionchanged:: 3.2
Support for :class:`pathlib.Path` ``ds_input`` was added.
__ https://gdal.org/drivers/vector/ __ https://gdal.org/drivers/vector/
``Layer`` ``Layer``

View File

@ -331,7 +331,7 @@ Now, open the world borders shapefile using GeoDjango's
:class:`~django.contrib.gis.gdal.DataSource` interface:: :class:`~django.contrib.gis.gdal.DataSource` interface::
>>> from django.contrib.gis.gdal import DataSource >>> from django.contrib.gis.gdal import DataSource
>>> ds = DataSource(str(world_shp)) >>> ds = DataSource(world_shp)
>>> print(ds) >>> print(ds)
/ ... /geodjango/world/data/TM_WORLD_BORDERS-0.3.shp (ESRI Shapefile) / ... /geodjango/world/data/TM_WORLD_BORDERS-0.3.shp (ESRI Shapefile)

View File

@ -114,6 +114,9 @@ Minor features
* The :meth:`.GDALRaster.transform` method now supports * The :meth:`.GDALRaster.transform` method now supports
:class:`~django.contrib.gis.gdal.SpatialReference`. :class:`~django.contrib.gis.gdal.SpatialReference`.
* The :class:`~django.contrib.gis.gdal.DataSource` class now supports
:class:`pathlib.Path`.
:mod:`django.contrib.messages` :mod:`django.contrib.messages`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -1,6 +1,7 @@
import os import os
import re import re
from datetime import datetime from datetime import datetime
from pathlib import Path
from django.contrib.gis.gdal import ( from django.contrib.gis.gdal import (
DataSource, Envelope, GDALException, OGRGeometry, DataSource, Envelope, GDALException, OGRGeometry,
@ -120,6 +121,11 @@ class DataSourceTest(SimpleTestCase):
with self.assertRaisesMessage(IndexError, 'Invalid OGR layer name given: invalid.'): with self.assertRaisesMessage(IndexError, 'Invalid OGR layer name given: invalid.'):
ds.__getitem__('invalid') ds.__getitem__('invalid')
def test_ds_input_pathlib(self):
test_shp = Path(get_ds_file('test_point', 'shp'))
ds = DataSource(test_shp)
self.assertEqual(len(ds), 1)
def test02_invalid_shp(self): def test02_invalid_shp(self):
"Testing invalid SHP files for the Data Source." "Testing invalid SHP files for the Data Source."
for source in bad_ds: for source in bad_ds: