'Extract the hierarchy labels
@param levels: the exposed hierarchy levels
@param country: the country (gis_location record ID) for which
to read the hierarchy labels
@return: tuple (labels, compact) where labels is for
internal use with _lx_selectors, and compact
the version ready for JSON output
@ToDo: Country-specific Translations of Labels'

def _labels(self, levels, country=None):

    T = current.T
    table = current.s3db.gis_hierarchy
    fields = [table[level] for level in levels if (level != 'L0')]
    query = (table.uuid == 'SITE_DEFAULT')
    if country:
        fields.append(table.uuid)
        query |= (table.location_id == country)
        limit = 2
    else:
        limit = 1
    rows = current.db(query).select(limitby=(0, limit), *fields)
    labels = {}
    compact = {}
    if ('L0' in levels):
        labels['L0'] = current.messages.COUNTRY
    if country:
        for row in rows:
            if (row.uuid == 'SITE_DEFAULT'):
                d = compact['d'] = {}
                for level in levels:
                    if (level == 'L0'):
                        continue
                    d[int(level[1:])] = row[level]
            else:
                d = compact[country] = {}
                for level in levels:
                    if (level == 'L0'):
                        continue
                    labels[level] = d[int(level[1:])] = s3_unicode(T(row[level]))
    else:
        row = rows.first()
        d = compact['d'] = {}
        for level in levels:
            if (level == 'L0'):
                continue
            d[int(level[1:])] = s3_unicode(T(row[level]))
    return (labels, compact)
