Skip to content

Commit

Permalink
Refs #32230 -- Made LayerMapping support pathlib.Path.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixxm committed Nov 28, 2020
1 parent b37be07 commit 30f96b3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion django/contrib/gis/utils/layermapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
import sys
from decimal import Decimal, InvalidOperation as DecimalInvalidOperation
from pathlib import Path

from django.contrib.gis.db.models import GeometryField
from django.contrib.gis.gdal import (
Expand Down Expand Up @@ -93,7 +94,7 @@ def __init__(self, model, data, mapping, layer=0,
argument usage.
"""
# Getting the DataSource and the associated Layer.
if isinstance(data, str):
if isinstance(data, (str, Path)):
self.ds = DataSource(data, encoding=encoding)
else:
self.ds = data
Expand Down
4 changes: 4 additions & 0 deletions docs/ref/contrib/gis/layermapping.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ Keyword Arguments
Default is ``'default'``.
===================== =====================================================

.. versionchanged:: 3.2

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

``save()`` Keyword Arguments
----------------------------

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 @@ -117,6 +117,9 @@ Minor features
* The :class:`~django.contrib.gis.gdal.DataSource` class now supports
:class:`pathlib.Path`.

* The :class:`~django.contrib.gis.utils.LayerMapping` class now supports
:class:`pathlib.Path`.

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

Expand Down
21 changes: 13 additions & 8 deletions tests/gis_tests/layermap/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import datetime
import os
import unittest
from copy import copy
from decimal import Decimal
Expand All @@ -18,13 +17,14 @@
Interstate, Invalid, State, city_mapping, co_mapping, cofeat_mapping,
has_nulls_mapping, inter_mapping,
)
from pathlib import Path

shp_path = os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir, 'data'))
city_shp = os.path.join(shp_path, 'cities', 'cities.shp')
co_shp = os.path.join(shp_path, 'counties', 'counties.shp')
inter_shp = os.path.join(shp_path, 'interstates', 'interstates.shp')
invalid_shp = os.path.join(shp_path, 'invalid', 'emptypoints.shp')
has_nulls_geojson = os.path.join(shp_path, 'has_nulls', 'has_nulls.geojson')
shp_path = Path(__file__).resolve().parent.parent / 'data'
city_shp = shp_path / 'cities' / 'cities.shp'
co_shp = shp_path / 'counties' / 'counties.shp'
inter_shp = shp_path / 'interstates' / 'interstates.shp'
invalid_shp = shp_path / 'invalid' / 'emptypoints.shp'
has_nulls_geojson = shp_path / 'has_nulls' / 'has_nulls.geojson'

# Dictionaries to hold what's expected in the county shapefile.
NAMES = ['Bexar', 'Galveston', 'Harris', 'Honolulu', 'Pueblo']
Expand Down Expand Up @@ -83,6 +83,11 @@ def test_simple_layermap(self):
self.assertAlmostEqual(pnt1.x, pnt2.x, 5)
self.assertAlmostEqual(pnt1.y, pnt2.y, 5)

def test_data_source_str(self):
lm = LayerMapping(City, str(city_shp), city_mapping)
lm.save()
self.assertEqual(City.objects.count(), 3)

def test_layermap_strict(self):
"Testing the `strict` keyword, and import of a LineString shapefile."
# When the `strict` keyword is set an error encountered will force
Expand Down Expand Up @@ -307,7 +312,7 @@ def test_textfield(self):

def test_encoded_name(self):
""" Test a layer containing utf-8-encoded name """
city_shp = os.path.join(shp_path, 'ch-city', 'ch-city.shp')
city_shp = shp_path / 'ch-city' / 'ch-city.shp'
lm = LayerMapping(City, city_shp, city_mapping)
lm.save(silent=True, strict=True)
self.assertEqual(City.objects.count(), 1)
Expand Down

0 comments on commit 30f96b3

Please sign in to comment.