Skip to content

Commit

Permalink
Fixed #26789 -- Fixed handling of empty geometries in BaseSpatialFiel…
Browse files Browse the repository at this point in the history
…d.get_db_prep_save.
  • Loading branch information
sir-sigurd committed Dec 1, 2016
1 parent 065feca commit b261db5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions django/contrib/gis/db/backends/base/features.py
Expand Up @@ -26,6 +26,8 @@ class BaseSpatialFeatures(object):
supports_real_shape_operations = True
# Can geometry fields be null?
supports_null_geometries = True
# Are empty geometries supported?
supports_empty_geometries = False
# Can the the function be applied on geodetic coordinate systems?
supports_distance_geodetic = True
supports_length_geodetic = True
Expand Down
1 change: 1 addition & 0 deletions django/contrib/gis/db/backends/postgis/features.py
Expand Up @@ -8,3 +8,4 @@ class DatabaseFeatures(BaseSpatialFeatures, Psycopg2DatabaseFeatures):
supports_3d_functions = True
supports_left_right_lookups = True
supports_raster = True
supports_empty_geometries = True
6 changes: 3 additions & 3 deletions django/contrib/gis/db/models/fields.py
Expand Up @@ -177,10 +177,10 @@ def get_db_prep_save(self, value, connection):
"""
Prepare the value for saving in the database.
"""
if not value:
return None
else:
if isinstance(value, Geometry) or value:
return connection.ops.Adapter(self.get_prep_value(value))
else:
return None

def get_raster_prep_value(self, value, is_candidate):
"""
Expand Down
27 changes: 25 additions & 2 deletions tests/gis_tests/geoapp/tests.py
Expand Up @@ -6,8 +6,8 @@
from django.contrib.gis import gdal
from django.contrib.gis.db.models import Extent, MakeLine, Union
from django.contrib.gis.geos import (
GeometryCollection, GEOSGeometry, LinearRing, LineString, Point, Polygon,
fromstr,
GeometryCollection, GEOSGeometry, LinearRing, LineString, MultiLineString,
MultiPoint, MultiPolygon, Point, Polygon, fromstr,
)
from django.core.management import call_command
from django.db import connection
Expand Down Expand Up @@ -215,6 +215,29 @@ def test_dumpdata_loaddata_cycle(self):
call_command('loaddata', tmp.name, verbosity=0)
self.assertListEqual(original_data, list(City.objects.all().order_by('name')))

@skipUnlessDBFeature("supports_empty_geometries")
def test_empty_geometries(self):
geometry_classes = [
Point,
LineString,
LinearRing,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon,
GeometryCollection,
]
for klass in geometry_classes:
g = klass(srid=4326)
feature = Feature.objects.create(name="Empty %s" % klass.__name__, geom=g)
feature.refresh_from_db()
if klass is LinearRing:
# LinearRing is not representable in WKB, so
# GEOSGeomtry.wkb uses LineString instead.
g = LineString(srid=4326)
self.assertEqual(feature.geom, g)
self.assertEqual(feature.geom.srid, g.srid)


@skipUnlessDBFeature("gis_enabled")
class GeoLookupTest(TestCase):
Expand Down

0 comments on commit b261db5

Please sign in to comment.