'GIS-encodes the master resource so that it can be transformed into
a mappable format.
@param resource: the referencing resource
@param record: the particular record
@param element: the XML element
@param location_data: dictionary of location data from gis.get_location_data()
@ToDo: Support multiple locations per master resource (e.g. event_event.location)'

def gis_encode(self, resource, record, element, location_data={}):

    format = current.auth.permission.format
    if (format not in ('geojson', 'georss', 'gpx', 'kml')):
        return
    tablename = resource.tablename
    if (tablename == 'gis_feature_query'):
        return
    gis = current.gis
    request = current.request
    settings = current.deployment_settings
    ATTRIBUTE = self.ATTRIBUTE
    latlons = location_data.get('latlons', [])
    geojsons = location_data.get('geojsons', [])
    attributes = location_data.get('attributes', [])
    markers = location_data.get('markers', [])
    styles = location_data.get('styles', [])
    map_data = element.find('map')
    if map_data:
        map_data = map_data[0]
    else:
        map_data = etree.SubElement(element, 'map')
    attr = map_data.attrib
    record_id = record.id
    if (tablename == 'gis_location'):
        if (tablename in geojsons):
            geojson = geojsons[tablename].get(record_id, None)
            if geojson:
                if (type(geojson) is list):
                    geojson = geojson[(-1)]
                geometry = etree.SubElement(map_data, 'geometry')
                geometry.set('value', geojson)
        elif (tablename in latlons):
            LatLon = latlons[tablename].get(record_id, None)
            if LatLon:
                lat = LatLon[0]
                lon = LatLon[1]
                if ((lat is not None) and (lon is not None)):
                    attr[ATTRIBUTE.lat] = ('%.4f' % lat)
                    attr[ATTRIBUTE.lon] = ('%.4f' % lon)
        else:
            return
        if (format == 'geojson'):
            if (tablename in attributes):
                attrs = attributes[tablename][record_id]
                if attrs:
                    _attr = json.dumps(attrs, separators=(',,', '::'), ensure_ascii=False)
                    attr[ATTRIBUTE.attributes] = ('{%s}' % _attr.replace('"', '||'))
            if (tablename in markers):
                _markers = markers[tablename]
                if _markers.get('image', None):
                    m = _markers
                else:
                    m = _markers[record_id]
                if m:
                    download_url = ('/%s/static/img/markers' % request.application)
                    attr[ATTRIBUTE.marker_url] = ('%s/%s' % (download_url, m['image']))
                    attr[ATTRIBUTE.marker_height] = str(m['height'])
                    attr[ATTRIBUTE.marker_width] = str(m['width'])
            if (tablename in styles):
                style = styles[tablename].get(record_id)
                if style:
                    _style = etree.SubElement(map_data, 'style')
                    _style.set('value', style)
            return
        elif (format == 'kml'):
            marker = gis.get_marker()
            marker_download_url = ('%s/%s/static/img/markers' % (current.deployment_settings.get_base_public_url(), request.application))
            marker_url = ('%s/%s' % (marker_download_url, marker.image))
            attr[ATTRIBUTE.marker] = marker_url
        elif (format == 'gpx'):
            symbol = 'White    Dot'
            attr[ATTRIBUTE.sym] = symbol
        return
    elif ((len(tablename) > 19) and tablename.startswith('gis_layer_shapefile')):
        if (tablename in geojsons):
            geojson = geojsons[tablename].get(record_id, None)
            if geojson:
                geometry = etree.SubElement(map_data, 'geometry')
                geometry.set('value', geojson)
                if (tablename in attributes):
                    attrs = attributes[tablename][record_id]
                    if attrs:
                        _attr = json.dumps(attrs, separators=(',,', '::'), ensure_ascii=False)
                        attr[ATTRIBUTE.attributes] = ('{%s}' % _attr.replace('"', '||'))
        else:
            return
        return
    if (format == 'geojson'):
        if (tablename in geojsons):
            geojson = geojsons[tablename].get(record_id, None)
            if geojson:
                for g in geojson:
                    geometry = etree.SubElement(map_data, 'geometry')
                    geometry.set('value', g)
        elif (tablename in latlons):
            LatLon = latlons[tablename].get(record_id, None)
            if LatLon:
                lat = LatLon[0]
                lon = LatLon[1]
                if ((lat is not None) and (lon is not None)):
                    attr[ATTRIBUTE.lat] = ('%.4f' % lat)
                    attr[ATTRIBUTE.lon] = ('%.4f' % lon)
        else:
            raise RuntimeError(('Bulk    lookup    of    GeoJSON    or    Lat/Lon    data    failed    for    %s' % tablename))
        if (tablename in attributes):
            try:
                attrs = attributes[tablename][record_id]
            except KeyError:
                from s3utils import s3_debug
                s3_debug('S3XML', 'record    not    found    in    lookup')
                attrs = {}
            if attrs:
                _attr = json.dumps(attrs, separators=(',,', '::'), ensure_ascii=False)
                attr[ATTRIBUTE.attributes] = ('{%s}' % _attr.replace('"', '||'))
        if (tablename in markers):
            _markers = markers[tablename]
            if _markers:
                if _markers.get('image', None):
                    m = _markers
                else:
                    m = _markers[record_id]
                if m:
                    download_url = ('/%s/static/img/markers' % request.application)
                    attr[ATTRIBUTE.marker_url] = ('%s/%s' % (download_url, m['image']))
                    attr[ATTRIBUTE.marker_height] = str(m['height'])
                    attr[ATTRIBUTE.marker_width] = str(m['width'])
        if (tablename in styles):
            style = styles[tablename].get(record_id)
            if style:
                _style = etree.SubElement(map_data, 'style')
                _style.set('value', style)
        return
    elif (tablename in latlons):
        LatLon = latlons[tablename].get(record_id, None)
        if LatLon:
            lat = LatLon[0]
            lon = LatLon[1]
            if ((lat is not None) and (lon is not None)):
                attr[ATTRIBUTE.lat] = ('%.4f' % lat)
                attr[ATTRIBUTE.lon] = ('%.4f' % lon)
    else:
        return
    if (tablename in markers):
        _markers = markers[tablename]
        if _markers.get('image', None):
            m = _markers
        else:
            m = _markers[record_id]
        if m:
            if (format == 'gpx'):
                attr[ATTRIBUTE.sym] = m.get('gps_marker', gis.DEFAULT_SYMBOL)
            else:
                download_url = ('%s/%s/static/img/markers' % (settings.get_base_public_url(), request.application))
                attr[ATTRIBUTE.marker] = ('%s/%s' % (download_url, m['image']))
