Skip to content

Commit

Permalink
Fixed #32230 -- Added support for pathlib.Path to the django.contrib.…
Browse files Browse the repository at this point in the history
…gis.gdal.DataSource.
  • Loading branch information
hramezani committed Nov 27, 2020
1 parent e46ca51 commit d032b05
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion django/contrib/gis/gdal/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,7 +63,7 @@ def __init__(self, ds_input, ds_driver=False, write=False, encoding='utf-8'):

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:
Expand Down
4 changes: 4 additions & 0 deletions docs/ref/contrib/gis/gdal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ each feature in that layer.
``DataSource`` instance gives the OGR name of the underlying data source
that it is using.

.. versionchanged:: 3.2

Support for :class:`pathlib.Path` was added.

The optional ``encoding`` parameter allows you to specify a non-standard
encoding of the strings in the source. This is typically useful when you
obtain ``DjangoUnicodeDecodeError`` exceptions while reading field values.
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/contrib/gis/tutorial.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions docs/releases/3.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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` now supports
:class:`pathlib.Path`.

:mod:`django.contrib.messages`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
6 changes: 6 additions & 0 deletions tests/gis_tests/gdal_tests/test_ds.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -120,6 +121,11 @@ def test01_valid_shp(self):
with self.assertRaisesMessage(IndexError, 'Invalid OGR layer name given: invalid.'):
ds.__getitem__('invalid')

def test_pathlib_path_input(self):
test_shp = Path(ds_list[0].ds)
ds = DataSource(test_shp)
self.assertEqual(1, len(ds))

def test02_invalid_shp(self):
"Testing invalid SHP files for the Data Source."
for source in bad_ds:
Expand Down

0 comments on commit d032b05

Please sign in to comment.