Code

Ticket #9745: snaptogrid_corrected.diff

File snaptogrid_corrected.diff, 3.0 KB (added by jbronn, 5 years ago)

SVN diff of user-provided patch

Line 
1Index: django/contrib/gis/db/models/manager.py
2===================================================================
3--- django/contrib/gis/db/models/manager.py     (revision 9548)
4+++ django/contrib/gis/db/models/manager.py     (working copy)
5@@ -63,6 +63,9 @@
6     def scale(self, *args, **kwargs):
7         return self.get_query_set().scale(*args, **kwargs)
8 
9+    def snaptogrid(self, *args, **kwargs):
10+        return self.get_query_set().snaptogrid(*args, **kwargs)
11+
12     def svg(self, *args, **kwargs):
13         return self.get_query_set().svg(*args, **kwargs)
14 
15Index: django/contrib/gis/db/models/query.py
16===================================================================
17--- django/contrib/gis/db/models/query.py       (revision 9548)
18+++ django/contrib/gis/db/models/query.py       (working copy)
19@@ -217,6 +217,24 @@
20              }
21         return self._spatial_attribute('scale', s, **kwargs)
22 
23+    def snaptogrid(self, size=0.01, **kwargs):
24+        """
25+        Snap all points of the input geometry to the grid defined by
26+        cell size.
27+        """
28+        if SpatialBackend.version[0]<1:
29+            raise "Postgis version doesn't support SnapToGrid"
30+        if not isinstance(size, (float, int)):
31+            raise TypeError('Size of the grid must be float or int.')       
32+
33+        field_name = kwargs.get('field_name', None)
34+        tmp, geo_field = self._spatial_setup('snaptogrid', field_name=field_name)
35+        field_col = self._geocol_select(geo_field, field_name)
36+        geo_col = self.query.custom_select.get(geo_field, field_col)
37+        custom_sel = '%s(%s, %s)' % (SpatialBackend.snaptogrid, geo_col, size)
38+        self.query.custom_select[geo_field] = custom_sel
39+        return self._clone()
40+
41     def svg(self, **kwargs):
42         """
43         Returns SVG representation of the geographic field in a `svg`
44Index: django/contrib/gis/db/backend/postgis/__init__.py
45===================================================================
46--- django/contrib/gis/db/backend/postgis/__init__.py   (revision 9548)
47+++ django/contrib/gis/db/backend/postgis/__init__.py   (working copy)
48@@ -30,6 +30,7 @@
49                                     point_on_surface=POINT_ON_SURFACE,
50                                     scale=SCALE,
51                                     select=GEOM_SELECT,
52+                                    snaptogrid=SNAP_TO_GRID,
53                                     svg=ASSVG,
54                                     sym_difference=SYM_DIFFERENCE,
55                                     transform=TRANSFORM,
56Index: django/contrib/gis/db/backend/postgis/query.py
57===================================================================
58--- django/contrib/gis/db/backend/postgis/query.py      (revision 9548)
59+++ django/contrib/gis/db/backend/postgis/query.py      (working copy)
60@@ -60,6 +60,7 @@
61     PERIMETER = get_func('Perimeter')
62     POINT_ON_SURFACE = get_func('PointOnSurface')
63     SCALE = get_func('Scale')
64+    SNAP_TO_GRID = get_func('SnapToGrid')
65     SYM_DIFFERENCE = get_func('SymDifference')
66     TRANSFORM = get_func('Transform')
67     TRANSLATE = get_func('Translate')