Ticket #13788: 13788_geos_transform.2.diff

File 13788_geos_transform.2.diff, 10.0 KB (added by jbronn, 13 years ago)

Updated

  • django/contrib/gis/geos/geometry.py

    diff -r 35aeedea954d django/contrib/gis/geos/geometry.py
    a b  
    44"""
    55# Python, ctypes and types dependencies.
    66import re
     7import warnings
    78from ctypes import addressof, byref, c_double, c_size_t
    89
    910# super-class for mutable list behavior
     
    498499        instead.
    499500        """
    500501        srid = self.srid
    501         if gdal.HAS_GDAL and srid:
    502             # Creating an OGR Geometry, which is then transformed.
    503             g = gdal.OGRGeometry(self.wkb, srid)
    504             g.transform(ct)
    505             # Getting a new GEOS pointer
    506             ptr = wkb_r().read(g.wkb)
     502
     503        if ct == srid:
     504            # short-circuit where source & dest SRIDs match
    507505            if clone:
    508                 # User wants a cloned transformed geometry returned.
    509                 return GEOSGeometry(ptr, srid=g.srid)
    510             if ptr:
    511                 # Reassigning pointer, and performing post-initialization setup
    512                 # again due to the reassignment.
    513                 capi.destroy_geom(self.ptr)
    514                 self.ptr = ptr
    515                 self._post_init(g.srid)
     506                return self.clone()
    516507            else:
    517                 raise GEOSException('Transformed WKB was invalid.')
     508                return
     509
     510        if (srid is None) or (srid < 0):
     511            warnings.warn("Calling transform() with no SRID set does no transformation!",
     512                          stacklevel=2)
     513            warnings.warn("Calling transform() with no SRID will raise GEOSException in v1.5",
     514                          FutureWarning, stacklevel=2)
     515            return
     516
     517        if not gdal.HAS_GDAL:
     518            raise GEOSException("GDAL library is not available to transform() geometry.")
     519
     520        # Creating an OGR Geometry, which is then transformed.
     521        g = gdal.OGRGeometry(self.wkb, srid)
     522        g.transform(ct)
     523        # Getting a new GEOS pointer
     524        ptr = wkb_r().read(g.wkb)
     525        if clone:
     526            # User wants a cloned transformed geometry returned.
     527            return GEOSGeometry(ptr, srid=g.srid)
     528        if ptr:
     529            # Reassigning pointer, and performing post-initialization setup
     530            # again due to the reassignment.
     531            capi.destroy_geom(self.ptr)
     532            self.ptr = ptr
     533            self._post_init(g.srid)
     534        else:
     535            raise GEOSException('Transformed WKB was invalid.')
    518536
    519537    #### Topology Routines ####
    520538    def _topology(self, gptr):
  • django/contrib/gis/geos/tests/test_geos.py

    diff -r 35aeedea954d django/contrib/gis/geos/tests/test_geos.py
    a b  
    852852            self.assertAlmostEqual(trans.x, p.x, prec)
    853853            self.assertAlmostEqual(trans.y, p.y, prec)
    854854
     855    def test23_transform_noop(self):
     856        """ Testing `transform` method (SRID match) """
     857        # transform() should no-op if source & dest SRIDs match,
     858        # regardless of whether GDAL is available.
     859        if gdal.HAS_GDAL:
     860            g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
     861            gt = g.tuple
     862            g.transform(4326)
     863            self.assertEqual(g.tuple, gt)
     864            self.assertEqual(g.srid, 4326)
     865
     866            g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
     867            g1 = g.transform(4326, clone=True)
     868            self.assertEqual(g1.tuple, g.tuple)
     869            self.assertEqual(g1.srid, 4326)
     870            self.assert_(g1 is not g, "Clone didn't happen")
     871
     872        old_has_gdal = gdal.HAS_GDAL
     873        try:
     874            gdal.HAS_GDAL = False
     875
     876            g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
     877            gt = g.tuple
     878            g.transform(4326)
     879            self.assertEqual(g.tuple, gt)
     880            self.assertEqual(g.srid, 4326)
     881
     882            g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
     883            g1 = g.transform(4326, clone=True)
     884            self.assertEqual(g1.tuple, g.tuple)
     885            self.assertEqual(g1.srid, 4326)
     886            self.assert_(g1 is not g, "Clone didn't happen")
     887        finally:
     888            gdal.HAS_GDAL = old_has_gdal
     889
     890    def test23_transform_nosrid(self):
     891        """ Testing `transform` method (no SRID) """
     892        # raise a warning if SRID <0/None
     893        import warnings
     894
     895        print "\nBEGIN - expecting Warnings; safe to ignore.\n"
     896
     897        # test for do-nothing behaviour
     898        g = GEOSGeometry('POINT (-104.609 38.255)', srid=None)
     899        g.transform(2774)
     900        self.assertEqual(g.tuple, (-104.609, 38.255))
     901        self.assertEqual(g.srid, None)
     902
     903        g = GEOSGeometry('POINT (-104.609 38.255)', srid=None)
     904        g1 = g.transform(2774, clone=True)
     905        self.assert_(g1 is None)
     906
     907        g = GEOSGeometry('POINT (-104.609 38.255)', srid=-1)
     908        g.transform(2774)
     909        self.assertEqual(g.tuple, (-104.609, 38.255))
     910        self.assertEqual(g.srid, -1)
     911
     912        g = GEOSGeometry('POINT (-104.609 38.255)', srid=-1)
     913        g1 = g.transform(2774, clone=True)
     914        self.assert_(g1 is None)
     915
     916        # test warning is raised
     917        try:
     918            warnings.simplefilter('error', FutureWarning)
     919
     920            g = GEOSGeometry('POINT (-104.609 38.255)', srid=None)
     921            self.assertRaises(FutureWarning, g.transform, 2774)
     922
     923            g = GEOSGeometry('POINT (-104.609 38.255)', srid=None)
     924            self.assertRaises(FutureWarning, g.transform, 2774, clone=True)
     925
     926            g = GEOSGeometry('POINT (-104.609 38.255)', srid=-1)
     927            self.assertRaises(FutureWarning, g.transform, 2774)
     928
     929            g = GEOSGeometry('POINT (-104.609 38.255)', srid=-1)
     930            self.assertRaises(FutureWarning, g.transform, 2774, clone=True)
     931        finally:
     932            warnings.simplefilter('default', category=FutureWarning)
     933
     934        print "\nEND - expecting Warnings; safe to ignore.\n"
     935
     936    def test23_transform_nogdal(self):
     937        """ Testing `transform` method (GDAL not available) """
     938        old_has_gdal = gdal.HAS_GDAL
     939        try:
     940            gdal.HAS_GDAL = False
     941
     942            g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
     943            self.assertRaises(GEOSException, g.transform, 2774)
     944
     945            g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
     946            self.assertRaises(GEOSException, g.transform, 2774, clone=True)
     947        finally:
     948            gdal.HAS_GDAL = old_has_gdal
     949
    855950    def test24_extent(self):
    856951        "Testing `extent` method."
    857952        # The xmin, ymin, xmax, ymax of the MultiPoint should be returned.
  • docs/internals/deprecation.txt

    diff -r 35aeedea954d docs/internals/deprecation.txt
    a b  
    136136          template variable, not an implied string. The new-style
    137137          behavior is provided in the ``future`` template tag library.
    138138
     139        * :meth:`~django.contrib.gis.geos.GEOSGeometry.transform` will raise
     140          a :class:`~django.contrib.gis.geos.GEOSException` when called
     141          on a geometry with no SRID value.
     142
    139143    * 2.0
    140144        * ``django.views.defaults.shortcut()``. This function has been moved
    141145          to ``django.contrib.contenttypes.views.shortcut()`` as part of the
  • docs/ref/contrib/gis/geos.txt

    diff -r 35aeedea954d docs/ref/contrib/gis/geos.txt
    a b  
    523523
    524524    Requires GDAL.
    525525
    526 .. method:: transform(ct, clone=False)
     526.. method:: GEOSGeometry.transform(ct, clone=False)
     527
     528.. versionchanged:: 1.3
    527529
    528530Transforms the geometry according to the given coordinate transformation paramter
    529531(``ct``), which may be an integer SRID, spatial reference WKT string,
     
    537539
    538540    Requires GDAL.
    539541
     542.. note::
     543
     544   Prior to 1.3, this method would silently no-op if GDAL was not available.
     545   Now, a :class:`~django.contrib.gis.geos.GEOSException` is raised as
     546   application code relying on this behavior is in error. In addition,
     547   use of this method when the SRID is ``None`` or less than 0 now generates
     548   a warning because a :class:`~django.contrib.gis.geos.GEOSException` will
     549   be raised instead in version 1.5.
     550
     551
    540552``Point``
    541553---------
    542554
  • docs/ref/contrib/gis/install.txt

    diff -r 35aeedea954d docs/ref/contrib/gis/install.txt
    a b  
    9898.. admonition::  Install GDAL
    9999
    100100    While :ref:`gdalbuild` is technically not required, it is *recommended*.
    101     Some features of GeoDjango (including the :ref:`ref-layermapping` and the geographic
    102     admin) depend on its functionality.
     101    Important features of GeoDjango (including the :ref:`ref-layermapping`,
     102    geometry reprojection, and the geographic admin) depend on its
     103    functionality.
    103104
    104105.. note::
    105106
     
    273274
    274275First download the latest GDAL release version and untar the archive::
    275276
    276     $ wget http://download.osgeo.org/gdal/gdal-1.7.2.tar.gz
    277     $ tar xzf gdal-1.7.2.tar.gz
    278     $ cd gdal-1.7.2
     277    $ wget http://download.osgeo.org/gdal/gdal-1.7.3.tar.gz
     278    $ tar xzf gdal-1.7.3.tar.gz
     279    $ cd gdal-1.7.3
    279280
    280281Configure, make and install::
    281282
  • docs/releases/1.3-alpha-2.txt

    diff -r 35aeedea954d docs/releases/1.3-alpha-2.txt
    a b  
    9595    **if the value is not None**, and falls back to the previously used
    9696    :setting:`MEDIA_URL` setting otherwise.
    9797
     98GeoDjango
     99---------
     100
     101  * Previously, calling :meth:`~django.contrib.gis.geos.GEOSGeometry.transform`
     102    would silently do nothing when GDAL wasn't available.  Now,
     103    a :class:`~django.contrib.gis.geos.GEOSException` is properly raised
     104    to indicate possible faulty application code.  A warning is now raised
     105    if :meth:`~django.contrib.gis.geos.GEOSGeometry.transform` is called when
     106    the SRID of the geometry is less than 0 or ``None``.
     107
     108
    98109The Django 1.3 roadmap
    99110======================
    100111
Back to Top