Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #25865 -- Made OSMGeoAdmin to require GDAL only if transformation is really needed. #5770

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions django/contrib/gis/admin/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def formfield_for_dbfield(self, db_field, **kwargs):
3D editing).
"""
if isinstance(db_field, models.GeometryField) and db_field.dim < 3:
if not HAS_GDAL and db_field.srid != self.map_srid:
raise ImproperlyConfigured(
"Map SRID is %s and SRID of `%s` is %s. "
"GDAL should be installed to perform transformation." % (self.map_srid, db_field, db_field.srid)
)
kwargs.pop('request', None)
# Setting the widget with the newly defined widget.
kwargs['widget'] = self.get_map_widget(db_field)
Expand Down Expand Up @@ -135,8 +140,3 @@ class OSMGeoAdmin(GeoModelAdmin):
max_resolution = '156543.0339'
point_zoom = num_zoom - 6
units = 'm'

def __init__(self, *args):
if not HAS_GDAL:
raise ImproperlyConfigured("OSMGeoAdmin is not usable without GDAL libs installed")
super(OSMGeoAdmin, self).__init__(*args)
12 changes: 12 additions & 0 deletions django/contrib/gis/geos/prototypes/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class WKBWriter_st(Structure):
'GEOSWKTWriter_setOutputDimension', argtypes=[WKT_WRITE_PTR, c_int]
)

wkt_writer_set_trim = GEOSFuncFactory('GEOSWKTWriter_setTrim', argtypes=[WKT_WRITE_PTR, c_char])
wkt_writer_set_precision = GEOSFuncFactory('GEOSWKTWriter_setRoundingPrecision', argtypes=[WKT_WRITE_PTR, c_int])

# WKBReader routines
wkb_reader_create = GEOSFuncFactory('GEOSWKBReader_create', restype=WKB_READ_PTR)
wkb_reader_destroy = GEOSFuncFactory('GEOSWKBReader_destroy', argtypes=[WKB_READ_PTR])
Expand Down Expand Up @@ -176,6 +179,15 @@ def outdim(self, new_dim):
raise ValueError('WKT output dimension must be 2 or 3')
wkt_writer_set_outdim(self.ptr, new_dim)

def set_trim(self, trim):
wkt_writer_set_trim(self.ptr, b'\x01' if trim else b'\x00')

def set_precision(self, precision):
if isinstance(precision, int) and precision >= 0 or precision is None:
wkt_writer_set_precision(self.ptr, -1 if precision is None else precision)
else:
raise ValueError('WKT output rounding precision must be non-negative integer or None.')


class WKBWriter(IOBase):
_constructor = wkb_writer_create
Expand Down
20 changes: 17 additions & 3 deletions tests/gis_tests/geoadmin/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.contrib.gis.gdal import HAS_GDAL
from django.utils.encoding import python_2_unicode_compatible

from ..admin import admin
Expand All @@ -19,6 +18,21 @@ class Meta:
def __str__(self):
return self.name


@python_2_unicode_compatible
class CityMercator(models.Model):
name = models.CharField(max_length=30)
point = models.PointField(srid=3857)

objects = models.GeoManager()

class Meta:
app_label = 'geoadmin'
required_db_features = ['gis_enabled']

def __str__(self):
return self.name

site = admin.AdminSite(name='admin_gis')
if HAS_GDAL:
site.register(City, admin.OSMGeoAdmin)
site.register(City, admin.OSMGeoAdmin)
site.register(CityMercator, admin.OSMGeoAdmin)
20 changes: 18 additions & 2 deletions tests/gis_tests/geoadmin/tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from __future__ import unicode_literals

from unittest import skipUnless

from django.contrib.gis import admin
from django.contrib.gis.gdal import HAS_GDAL
from django.contrib.gis.geos import Point
from django.test import TestCase, override_settings, skipUnlessDBFeature
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase, mock, override_settings, skipUnlessDBFeature

from .admin import UnmodifiableAdmin
from .models import City, site
from .models import City, CityMercator, site


@skipUnlessDBFeature("gis_enabled")
Expand Down Expand Up @@ -52,6 +56,18 @@ def test_olmap_WMS_rendering(self):
""""http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic', format: 'image/jpeg'});""",
result)

@mock.patch('django.contrib.gis.admin.options.HAS_GDAL', False)
def test_no_gdal_admin_model_diffent_srid(self):
with self.assertRaises(ImproperlyConfigured):
geoadmin = site._registry[City]
geoadmin.get_changelist_form(None)()

@mock.patch('django.contrib.gis.admin.options.HAS_GDAL', False)
def test_no_gdal_admin_model_same_srid(self):
geoadmin = site._registry[CityMercator]
geoadmin.get_changelist_form(None)()

@skipUnless(HAS_GDAL, "GDAL is required.")
def test_olwidget_has_changed(self):
"""
Check that changes are accurately noticed by OpenLayersWidget.
Expand Down