'Render the pivot table data as a dict ready to be exported as
GeoJSON for display on a Map.
@param r: the S3Request instance
@param attr: controller attributes for the request'

def geojson(self, r, **attr):

    resource = self.resource
    response = current.response
    s3 = response.s3
    response.headers['Content-Type'] = s3.content_type.get('geojson', 'application/json')
    if (not resource.count()):
        return json.dumps({})
    get_vars = r.get_vars
    layer_id = r.get_vars.get('layer', None)
    level = get_vars.get('level', 'L0')
    get_config = resource.get_config
    report_options = get_config('report_options', {})
    defaults = report_options.get('defaults', {})
    context = get_config('context')
    if (context and ('location' in context)):
        rows = ('(location)$%s' % level)
    else:
        rows = ('location_id$%s' % level)
    resource.add_filter((FS(rows) != None))
    stylesheet = os.path.join(r.folder, r.XSLT_PATH, 'geojson', 'export.xsl')
    fallback_to_points = True
    output = None
    if fallback_to_points:
        if (resource.count() == 0):
            resource.clear_query()
            resource.build_query(filter=s3.filter, vars=get_vars)
            xmlformat = S3XMLFormat(stylesheet)
            (include, exclude) = xmlformat.get_fields(resource.tablename)
            resource.load(fields=include, skip=exclude, start=0, limit=None, orderby=None, virtual=False, cacheable=True)
            gis = current.gis
            attr_fields = []
            style = gis.get_style(layer_id=layer_id, aggregate=False)
            popup_format = style.popup_format
            if popup_format:
                if ('T(' in popup_format):
                    T = current.T
                    items = regex_translate.findall(popup_format)
                    for item in items:
                        titem = str(T(item[1:(-1)]))
                        popup_format = popup_format.replace(('T(%s)' % item), titem)
                    style.popup_format = popup_format
                parts = popup_format.split('{')
                parts = parts[1:]
                for part in parts:
                    attribute = part.split('}')[0]
                    attr_fields.append(attribute)
                attr_fields = ','.join(attr_fields)
            location_data = gis.get_location_data(resource, attr_fields=attr_fields)
            current.xml.show_ids = True
            output = resource.export_xml(fields=include, mcomponents=None, references=[], stylesheet=stylesheet, as_json=True, location_data=location_data, map_data=dict(style=style))
            if (not output):
                r.error(400, ('XSLT    Transformation    Error:    %s    ' % current.xml.error))
    else:
        while (resource.count() == 0):
            level = int(level[1:])
            if (level == 0):
                return json.dumps({})
            resource.clear_query()
            resource.build_query(filter=s3.filter, vars=get_vars)
            level = ('L%s' % (level - 1))
            if (context and ('location' in context)):
                rows = ('(location)$%s' % level)
            else:
                rows = ('location_id$%s' % level)
            resource.add_filter((FS(rows) != None))
    if (not output):
        cols = None
        layer = get_vars.get('fact', defaults.get('fact', 'count(id)'))
        facts = S3PivotTableFact.parse(layer)[:1]
        pivottable = S3PivotTable(resource, rows, cols, facts)
        style = current.gis.get_style(layer_id=layer_id, aggregate=True)
        popup_format = style.popup_format
        if popup_format:
            if ('T(' in popup_format):
                T = current.T
                items = regex_translate.findall(popup_format)
                for item in items:
                    titem = str(T(item[1:(-1)]))
                    popup_format = popup_format.replace(('T(%s)' % item), titem)
                style.popup_format = popup_format
        (ids, location_data) = pivottable.geojson(fact=facts[0], level=level)
        current.xml.show_ids = True
        gresource = current.s3db.resource('gis_location', id=ids)
        output = gresource.export_xml(fields=[], mcomponents=None, references=[], stylesheet=stylesheet, as_json=True, location_data=location_data, map_data=dict(level=int(level[1:]), style=style))
        if (not output):
            r.error(400, ('XSLT    Transformation    Error:    %s    ' % current.xml.error))
    return output
