'Download a KML file:
- unzip it if-required
- follow NetworkLinks recursively if-required
Save the file to the /uploads folder
Designed to be called asynchronously using:
current.s3task.async("download_kml", [record_id, filename])
@param record_id: id of the record in db.gis_layer_kml
@param filename: name to save the file as
@param session_id_name: name of the session
@param session_id: id of the session
@ToDo: Pass error messages to Result & have JavaScript listen for these'

def download_kml(self, record_id, filename, session_id_name, session_id):

    table = current.s3db.gis_layer_kml
    record = current.db((table.id == record_id)).select(table.url, limitby=(0, 1)).first()
    url = record.url
    filepath = os.path.join(global_settings.applications_parent, current.request.folder, 'uploads', 'gis_cache', filename)
    warning = self.fetch_kml(url, filepath, session_id_name, session_id)
    if (('URLError' in warning) or ('HTTPError' in warning)):
        if os.access(filepath, os.R_OK):
            statinfo = os.stat(filepath)
            if statinfo.st_size:
                pass
            else:
                return
        else:
            return
    else:
        if ('ParseError' in warning):
            pass
        if (('GroundOverlay' in warning) or ('ScreenOverlay' in warning)):
            pass
