'Export data as a Shapefile
@param data_source: the source of the data that is to be encoded
as a shapefile. This may be:
resource: the resource
item:     a list of pre-fetched values
the headings are in the first row
the data types are in the second row
@param attr: dictionary of parameters:
* title:          The export filename
* list_fields:    Fields to include in list views'

def encode(self, data_source, **attr):

    title = attr.get('title')
    if isinstance(data_source, (list, tuple)):
        headers = data_source[0]
        items = data_source[2:]
    else:
        current.s3db.gis_location.wkt.represent = None
        list_fields = attr.get('list_fields')
        if (not list_fields):
            list_fields = data_source.list_fields()
        if (data_source.tablename == 'gis_location'):
            wkt_field = 'wkt'
        else:
            wkt_field = 'location_id$wkt'
        if (wkt_field not in list_fields):
            list_fields.append(wkt_field)
        (_title, types, lfields, headers, items) = self.extractResource(data_source, list_fields)
        if (not title):
            title = _title
    output = []
    oappend = output.append
    headers['gis_location.wkt'] = 'WKT'
    fields = []
    fappend = fields.append
    header = []
    happend = header.append
    for selector in lfields:
        h = s3_unicode(headers[selector].replace('    ', '_'))
        happend(h)
        if (selector != 'gis_location.wkt'):
            fappend(h)
    oappend(('"%s"' % '","'.join(header)))
    fields = ','.join(fields)
    for item in items:
        row = []
        rappend = row.append
        for selector in lfields:
            represent = s3_strip_markup(s3_unicode(item[selector]))
            rappend(represent)
        oappend(('"%s"' % '","'.join(row)))
    import tempfile
    web2py_path = os.getcwd()
    if os.path.exists(os.path.join(web2py_path, 'temp')):
        TEMP = os.path.join(web2py_path, 'temp')
    else:
        TEMP = tempfile.gettempdir()
    (os_handle_temp, temp_filepath) = tempfile.mkstemp(dir=TEMP, suffix='.csv')
    with open(temp_filepath, 'w') as f:
        for line in output:
            f.write(('%s\n' % line.encode('utf-8')))
    temp_filename = temp_filepath.rsplit(os.path.sep, 1)[1]
    vrt = ('<OGRVRTDataSource>\n                <OGRVRTLayer    name="%s">\n                                <SrcDataSource>%s</SrcDataSource>\n                                <GeometryType>wkbGeometryCollection</GeometryType>\n                                <TargetSRS>EPSG:4326</TargetSRS>\n                                <GeometryField    encoding="WKT"    field="WKT"/>\n                </OGRVRTLayer>\n</OGRVRTDataSource>' % (temp_filename.rsplit('.', 1)[0], temp_filename))
    (os_handle_vrt, vrt_filename) = tempfile.mkstemp(dir=TEMP, suffix='.vrt')
    with open(vrt_filename, 'w') as f:
        f.write(vrt)
    os.chdir(TEMP)
    cmd = (((((('ogr2ogr    -a_srs    "EPSG:4326"    -f    "ESRI    Shapefile"    ' + title) + '_point.shp    ') + vrt_filename) + '    -select    ') + fields) + '    -skipfailures    -nlt    POINT    -where    "WKT    LIKE    \'%POINT%\'"')
    os.system(cmd)
    cmd = (((((('ogr2ogr    -a_srs    "EPSG:4326"    -f    "ESRI    Shapefile"    ' + title) + '_line.shp    ') + vrt_filename) + '    -select    ') + fields) + '    -skipfailures    -nlt    MULTILINESTRING    -where    "WKT    LIKE    \'%LINESTRING%\'"')
    os.system(cmd)
    cmd = (((((('ogr2ogr    -a_srs    "EPSG:4326"    -f    "ESRI    Shapefile"    ' + title) + '_polygon.shp    ') + vrt_filename) + '    -select    ') + fields) + '    -skipfailures    -nlt    MULTIPOLYGON    -where    "WKT    LIKE    \'%POLYGON%\'"')
    os.system(cmd)
    os.close(os_handle_temp)
    os.unlink(temp_filepath)
    os.close(os_handle_vrt)
    os.unlink(vrt_filename)
    import zipfile
    request = current.request
    filename = ('%s_%s.zip' % (request.env.server_name, title))
    fzip = zipfile.ZipFile(filename, 'w')
    for item in ('point', 'line', 'polygon'):
        for exten in ('shp', 'shx', 'prj', 'dbf'):
            tfilename = ('%s_%s.%s' % (title, item, exten))
            fzip.write(tfilename)
            os.unlink(tfilename)
    fzip.close()
    os.chdir(web2py_path)
    disposition = ('attachment;    filename="%s"' % filename)
    response = current.response
    response.headers['Content-Type'] = contenttype('.zip')
    response.headers['Content-disposition'] = disposition
    stream = open(os.path.join(TEMP, filename), 'rb')
    return response.stream(stream, chunk_size=DEFAULT_CHUNK_SIZE, request=request)
