Ticket #12733: extent-patch.diff

File extent-patch.diff, 2.7 KB (added by jann.kleen@…, 6 years ago)
  • django/contrib/gis/db/backends/mysql/operations.py

     
    1111    select = 'AsText(%s)'
    1212    from_wkb = 'GeomFromWKB'
    1313    from_text = 'GeomFromText'
     14    valid_aggregates = dict([(k, None) for k in ('Extent',)])
    1415
     16    extent_sql = '''CONCAT_WS(',', MAX(X(%(field)s)), MAX(Y(%(field)s)), MIN(X(%(field)s)), MIN(Y(%(field)s)))'''
     17    extent = 'Extent'
     18
    1519    Adapter = WKTAdapter
    1620
    1721    geometry_functions = {
     
    3135
    3236    gis_terms = dict([(term, None) for term in geometry_functions.keys() + ['isnull']])
    3337
     38    def convert_extent (self,box):
     39        """
     40        Returns a 4-tuple extent for the `Extent` aggregate, created by MIN/MAX
     41        queries on the dataset
     42        """
     43        xmax, ymax, xmin, ymin =  map(float, box.split(','))
     44        return (xmin, ymin, xmax, ymax)
     45
     46    def check_aggregate_support(self, aggregate):
     47        """
     48        Checks if the given aggregate name is supported (that is, if it's
     49        in `self.valid_aggregates`).
     50        """
     51        agg_name = aggregate.__class__.__name__
     52        return agg_name in self.valid_aggregates
     53
    3454    def geo_db_type(self, f):
    3555        return f.geom_type
    3656
     
    4666            placeholder = '%s(%%s)' % self.from_text
    4767        return placeholder
    4868
     69
     70    def spatial_aggregate_sql(self, agg):
     71        """
     72        Returns the spatial aggregate SQL template and function for the
     73        given Aggregate instance.
     74        """
     75        agg_name = agg.__class__.__name__
     76        if not self.check_aggregate_support(agg):
     77            raise NotImplementedError('%s spatial aggregate is not implmented for this backend.' % agg_name)
     78        agg_name = agg_name.lower()
     79        sql_template = getattr(self, agg_name + '_sql')
     80        sql_function = getattr(self, agg_name)
     81        return sql_template, sql_function
     82
    4983    def spatial_lookup_sql(self, lvalue, lookup_type, value, field, qn):
    5084        alias, col, db_type = lvalue
    5185
  • django/contrib/gis/tests/relatedapp/tests.py

     
    5656            qs = list(City.objects.filter(name=name).transform(srid, field_name='location__point'))
    5757            check_pnt(GEOSGeometry(wkt, srid), qs[0].location.point)
    5858
    59     @no_mysql
    6059    @no_spatialite
    6160    def test04a_related_extent_aggregate(self):
    6261        "Testing the `extent` GeoQuerySet aggregates on related geographic models."
Back to Top