浏览代码

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

Hasan Ramezani 4 年之前
父节点
当前提交
b37be072a2

+ 2 - 1
django/contrib/gis/gdal/datasource.py

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

+ 4 - 0
docs/ref/contrib/gis/gdal.txt

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

+ 1 - 1
docs/ref/contrib/gis/tutorial.txt

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

+ 3 - 0
docs/releases/3.2.txt

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

+ 6 - 0
tests/gis_tests/gdal_tests/test_ds.py

@@ -1,6 +1,7 @@
 import os
 import re
 from datetime import datetime
+from pathlib import Path
 
 from django.contrib.gis.gdal import (
     DataSource, Envelope, GDALException, OGRGeometry,
@@ -120,6 +121,11 @@ class DataSourceTest(SimpleTestCase):
             with self.assertRaisesMessage(IndexError, 'Invalid OGR layer name given: 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):
         "Testing invalid SHP files for the Data Source."
         for source in bad_ds: