From b37be072a27f3d4aaf22342a17afd0c24518ff8b Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Fri, 27 Nov 2020 18:58:28 +0100 Subject: [PATCH] Fixed #32230 -- Made DataSource support pathlib.Path. --- django/contrib/gis/gdal/datasource.py | 3 ++- docs/ref/contrib/gis/gdal.txt | 4 ++++ docs/ref/contrib/gis/tutorial.txt | 2 +- docs/releases/3.2.txt | 3 +++ tests/gis_tests/gdal_tests/test_ds.py | 6 ++++++ 5 files changed, 16 insertions(+), 2 deletions(-) diff --git a/django/contrib/gis/gdal/datasource.py b/django/contrib/gis/gdal/datasource.py index 602b244d31..6b7112b30e 100644 --- a/django/contrib/gis/gdal/datasource.py +++ b/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: diff --git a/docs/ref/contrib/gis/gdal.txt b/docs/ref/contrib/gis/gdal.txt index aa7e2a7eb8..35abec1168 100644 --- a/docs/ref/contrib/gis/gdal.txt +++ b/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`` diff --git a/docs/ref/contrib/gis/tutorial.txt b/docs/ref/contrib/gis/tutorial.txt index 2f80a18e47..c585dd9248 100644 --- a/docs/ref/contrib/gis/tutorial.txt +++ b/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) diff --git a/docs/releases/3.2.txt b/docs/releases/3.2.txt index 1209b357e2..206609422d 100644 --- a/docs/releases/3.2.txt +++ b/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` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/gis_tests/gdal_tests/test_ds.py b/tests/gis_tests/gdal_tests/test_ds.py index d462bec971..f2a6ddf101 100644 --- a/tests/gis_tests/gdal_tests/test_ds.py +++ b/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: